-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgithub-auto-label.js
More file actions
94 lines (84 loc) · 2.9 KB
/
github-auto-label.js
File metadata and controls
94 lines (84 loc) · 2.9 KB
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
var express = require('express')
var _ = require('lodash')
var request = require('request')
var GithubWebHook = require('express-github-webhook')
var bodyParser = require('body-parser')
var chalk = require('chalk')
var debug = require('debug-log')('github-auto-label')
var repos = process.env.GITHUB_REPOS
if (repos.indexOf(',') > -1) {
repos = repos.split(',')
} else {
repos = [repos]
}
var labels = {
pr: process.env.GITHUB_PR_LABELS,
issue: process.env.GITHUB_ISSUE_LABELS
}
if (!labels.pr || labels.pr === '') {
labels.pr = []
} else if (labels.pr.indexOf(',') > -1) {
labels.pr = labels.pr.split(',')
} else {
labels.pr = [labels.pr]
}
if (!labels.issue || labels.issue === '') {
labels.issue = []
} else if (labels.issue.indexOf(',') > -1) {
labels.issue = labels.issue.split(',')
} else {
labels.issue = [labels.issue]
}
var accessToken = process.env.GITHUB_TOKEN
// Personal Access Token: https://github.com/settings/tokens/new
var webhookSettings = {
path: process.env.WEBHOOK_PATH || '/',
secret: process.env.GITHUB_SECRET || 'thisIsASuperSecretSecret'
}
var webhookHandler = GithubWebHook(webhookSettings)
var app = express()
app.set('port', process.env.PORT || 5555)
app.use(bodyParser.json())
app.use(webhookHandler)
webhookHandler.on('pull_request', function (repo, data) {
if (repos.indexOf(repo) < 0 || data.action !== 'opened' || labels.pr.length === 0) return
debug('[%s] Incoming webhook. adding labels %s to %s#%s', chalk.yellow('github-auto-label'), JSON.stringify(labels.pr), repo, data.pull_request.number)
var opts = {
method:'POST',
uri: data.pull_request.issue_url + '/labels',
headers: {
'User-Agent': 'Github-Auto-Label',
'Authorization': 'token '+accessToken,
'Content-Type': 'application/json'
},
form: JSON.stringify(labels.pr)
}
request(opts, function(err, results, body){
if (err) console.error(err)
debug('[%s] API response %s', chalk.magenta('github'), JSON.stringify(body, null, ' '))
})
})
webhookHandler.on('issues', function (repo, data) {
if (repos.indexOf(repo) < 0 || data.action !== 'opened' || labels.issue.length === 0) return
debug('[%s] Incoming webhook. adding labels %s to %s#%s', chalk.yellow('github-auto-label'), JSON.stringify(labels.issue), repo, data.issue.number)
var opts = {
method:'POST',
uri: data.issue.url + '/labels',
headers: {
'User-Agent': 'Github-Auto-Label',
'Authorization': 'token '+accessToken,
'Content-Type': 'application/json'
},
form: JSON.stringify(labels.issue)
}
request(opts, function(err, results, body){
if (err) console.error(err)
debug('[%s] API response %s', chalk.magenta('github'), JSON.stringify(body, null, ' '))
})
})
webhookHandler.on('error', function (err, req, res) {
console.error('an error occurred', err)
})
app.listen(app.get('port'), function () {
console.log('Auto Labeler listening on port ' + app.get('port'))
})