forked from ceejbot/jthooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook-cli.js
executable file
·128 lines (110 loc) · 3.05 KB
/
hook-cli.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
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
#!/usr/bin/env node
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "argv" }]*/
var chalk = require('chalk'),
Jthooks = require('./index'),
url = require('url'),
yargs = require('yargs')
.command('add <repo> <hook> <secret>', 'add a hook to the given repo with the shared secret', noop, addHook)
.command('remove <repo> <hook>', 'delete the given webhook; can pass id instead of url', noop, removeHook)
.option('auth', {
alias: 'a',
description: 'auth token (can also be set in GITHUB_AUTH_TOKEN or GITHUB_API_TOKEN)',
global: true,
})
.option('url', {
alias: 'u',
description: 'full URL of github API to use (optional)',
global: true
})
.option('quiet', {
alias: 'q',
description: 'only log errors',
global: true,
})
.describe('id', 'id of existing hook to update (optional)')
.usage('jthooks [add|remove] owner/repo https://example.com/hook shared-sekrit')
.example('jthooks add foo/bar https://example.com/hook sooper-sekrit -a auth-token', 'add a webhook')
.example('jthooks remove foo/bar https://example.com/hook', 'remove a hook by url')
.example('jthooks remove foo/bar 123456', 'remove a hook by id')
.help('help', 'show this help');
var argv = yargs.argv;
function noop() { }
function makeLogger(argv)
{
var logger = function(msg) { if (!argv.quiet) console.log(msg); };
return logger;
}
function create(argv)
{
var authtoken = process.env.GITHUB_AUTH_TOKEN || process.env.GITHUB_API_TOKEN || argv.auth;
if (!authtoken)
{
yargs.showHelp();
process.exit(1);
}
var options = {
auth: {
type: 'oauth',
token: authtoken
}
};
if (argv.url)
{
var api = url.parse();
options.host = api.host;
options.prototol = api.protocol;
options.pathPrefix = api.path;
}
var jthooks = new Jthooks(options);
return jthooks;
}
function addHook(argv)
{
var jthooks = create(argv);
var logger = makeLogger(argv);
logger('Adding webhook to repo ' + chalk.blue(argv.repo) + '...');
var pieces = argv.repo.split('/');
var hookOpts = {
owner: pieces[0],
repo: pieces[1],
secret: argv.secret,
url: argv.hook,
id: argv.id
};
jthooks.setWebhook(hookOpts, function(err, isNew, result)
{
if (err)
{
console.log(chalk.red('Error!'), err.message);
console.log(JSON.stringify(err));
process.exit(1);
}
logger('Hook id ' + chalk.green(result.id) + (isNew ? ' added' : ' updated') + '; test url is');
logger(chalk.green(result.test_url));
});
}
function removeHook(argv)
{
var jthooks = create(argv);
var logger = makeLogger(argv);
logger('Removing webhook from repo ' + chalk.blue(argv.repo) + '...');
var pieces = argv.repo.split('/');
var opts = {
owner: pieces[0],
repo: pieces[1],
};
if (typeof argv.hook === 'number' || !argv.hook.match(/^https?:\/\//))
opts.id = argv.hook;
else
opts.hook = argv.hook;
jthooks.removeWebhook(opts, function(err, result)
{
if (err)
{
console.log(chalk.red('Error!'), err.message);
console.log(JSON.stringify(err));
process.exit(1);
}
logger('Hook id ' + chalk.red(result.id) + ' removed');
});
}