-
Notifications
You must be signed in to change notification settings - Fork 24
/
webhook-update.js
62 lines (57 loc) · 2.47 KB
/
webhook-update.js
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
/* update existing hooks */
const doAsync = require("doasync");
var GH = require("./gh");
var config = require("./config.json");
var hookURL = config.hookURL;
if (require.main === module) {
var Store = require("./store");
config.logToConsole = false;
const store = new Store(config);
(async () => {
const repos = await doAsync(store).repos();
for (repo of repos) {
const owner = repo.owner;
const shortname = repo.name;
const { token } = await doAsync(store).getToken(owner);
const gh = new GH({ accessToken: token });
try {
const { data: hooks } = await gh.octo.request("GET /repos/:owner/:name/hooks",
{
owner: owner,
name: shortname
});
const hook = (hooks || hooks.length) ? hooks.find(function(h) {
return h
&& h.config
&& (h.config.url === hookURL || h.config.url === hookURL.replace('/repo-manager', '/hatchery/repo-manager'));
}) : null;
if (hook) {
const secret = await doAsync(store).getSecret(`${owner}/${shortname}`);
try {
await gh.octo.request("PATCH /repos/:owner/:name/hooks/:hook",
{
owner: owner,
name: shortname,
hook: hook.id,
data: {
config: {
url: config.hookURL || (config.url + "api/hook"),
content_type: "json",
secret: secret.secret
}
, events: ["pull_request", "issue_comment", "repository"]
}
});
console.log(`Hook updated for ${owner}/${shortname}`);
} catch (e) {
console.error(`Error updating webhook for ${owner}/${shortname}: ${e.message}`);
}
} else {
console.error(`Hook not found for ${owner}/${shortname}`);
}
} catch (e) {
console.error(`Error fetching webhooks for ${owner}/${shortname}: ${e.message}`);
}
}
})();
}