-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathadd-web-hook.cs
128 lines (111 loc) · 3.78 KB
/
add-web-hook.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//@req(user, repo, token, callbackUrl, scriptName, envName)
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.auth.AuthScope;
import com.hivext.api.core.utils.JSONUtils;
import java.io.InputStreamReader;
import java.io.BufferedReader;
var client = new HttpClient();
//Parsing repo url
var origRepo = repo;
var domain = "github.com";
if (repo.indexOf(".git") > -1) repo = repo.split(".git")[0];
if (repo.indexOf("/") > -1) {
var arr = repo.split("/");
repo = arr.pop();
if (repo == "") repo = arr.pop();
user = arr.pop();
domain = arr.pop() || domain;
}
var IS_GITHUB = domain.indexOf("github.com") != -1;
//Authentication for GitHub
if (IS_GITHUB) {
var creds = new UsernamePasswordCredentials(user, token);
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, creds);
}
//Get list of hooks
var gitApiUrl = IS_GITHUB ? "https://api." + domain + "/repos/" + user + "/" + repo + "/hooks" : "https://" + domain + "/api/v4/projects/" + user + "%2F" + repo + "/hooks";
var get = new GetMethod(gitApiUrl);
//Authentication for GitLab
if (!IS_GITHUB) get.addRequestHeader("PRIVATE-TOKEN", token);
var resp = exec(get);
if (resp.result != 0) return resp;
var hooks = eval("(" + resp.response + ")");
//Clear previous hooks
for (var i = 0; i < hooks.length; i++) {
var url = IS_GITHUB ? hooks[i].config.url : hooks[i].url;
if (url && url.indexOf(envName + '-' + scriptName) != -1) {
var del = new DeleteMethod(gitApiUrl + "/" + hooks[i].id);
//Authentication for GitLab
if (!IS_GITHUB) del.addRequestHeader("PRIVATE-TOKEN", token);
resp = exec(del);
if (resp.result != 0 && resp.result != 204) return resp;
}
}
var action = getParam('act');
if (action == 'delete' || action == 'clean') {
return {
result: 0
};
}
//Create a new hook
var post = new PostMethod(gitApiUrl);
//Hook request params
var params = IS_GITHUB ? {
"name": "web",
"active": true,
"events": ["push", "pull_request"],
"config": {
"url": callbackUrl,
"content_type": "json"
}
} : {
"push_events": true,
"merge_requests_events": true,
"url": callbackUrl
};
//Authentication for GitLab
if (!IS_GITHUB) post.addRequestHeader("PRIVATE-TOKEN", token);
resp = exec(post, params);
if (resp.result != 0) return resp;
var newHook = eval("(" + resp.response + ")");
return {
result: 0,
hook: newHook
};
function exec(method, params) {
if (params) {
var requestEntity = new StringRequestEntity(JSONUtils.jsonStringify(params), "application/json", "UTF-8");
method.setRequestEntity(requestEntity);
}
var status = client.executeMethod(method),
response = "",
result = 0,
type = null,
error = null;
if (status == 200 || status == 201) {
var br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())),
line;
while ((line = br.readLine()) != null) {
response = response + line;
}
} else {
error = "ERROR: " + method.getStatusLine() + " -> user:" + user + " token:" + token + " repo:" + repo;
if (status == 401) error = "Wrong username or/and token. Please, double check your entries.";
result = status;
type = "error";
response = null;
}
method.releaseConnection();
return {
result: result,
response: response,
type: type,
message: error
};
}