forked from noqcks/pull-request-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (150 loc) · 4.43 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const Sentry = require('@sentry/node');
Sentry.init({ dsn: process.env.SENTRY_DSN });
const generated = require('@noqcks/generated');
const minimatch = require("minimatch")
const label = {
XS: 'size/XS',
S: 'size/S',
M: 'size/M',
L: 'size/L',
XL: 'size/XL',
XXL: 'size/XXL'
}
const colors = {
'size/XS': '3CBF00',
'size/S': '42b71b',
'size/M': '1d76db',
'size/L': 'fbca04',
'size/XL': 'FF8C00',
'size/XXL': 'E50009'
}
const sizes = {
S: 10,
M: 100,
L: 500,
Xl: 1000,
Xxl: 2000
}
/**
* sizeLabel will return a string label that can be assigned to a
* GitHub Pull Request. The label is determined by the lines of code
* in the Pull Request.
* @param lineCount The number of lines in the Pull Request.
*/
function sizeLabel (lineCount) {
if (lineCount < sizes.S) {
return label.XS
} else if (lineCount < sizes.M) {
return label.S
} else if (lineCount < sizes.L) {
return label.M
} else if (lineCount < sizes.Xl) {
return label.L
} else if (lineCount < sizes.Xxl) {
return label.XL
}
return label.XXL
}
/**
* getCustomGeneratedFiles will grab a list of file globs that determine
* generated files from the repos .gitattributes.
* @param context The context of the PullRequest.
* @param owner The owner of the repository.
* @param repo The repository where the .gitattributes file is located.
*/
async function getCustomGeneratedFiles (context, owner, repo) {
let files = []
const path = ".gitattributes"
let response;
try {
response = await context.github.repos.getContents({owner, repo, path})
} catch (e) {
return files
}
const buff = new Buffer(response.data.content, 'base64')
const lines = buff.toString('ascii').split("\n")
lines.forEach(function(item) {
if (item.includes("linguist-generated=true")) {
files.push(item.split(" ")[0])
}
})
return files
}
/**
* globMatch compares file name with file blobs to
* see if a file is matched by a file glob expression.
* @param file The file to compare.
* @param globs A list of file globs to match the file.
*/
function globMatch (file, globs) {
for (i=0; i < globs.length; i++) {
if (minimatch(file, globs[i])) {
return true
break;
}
}
return false
}
async function addLabel (context, name, color) {
const params = Object.assign({}, context.issue(), {labels: [name]})
await ensureLabelExists(context, name, color)
await context.github.issues.addLabels(params)
}
async function ensureLabelExists (context, name, color) {
try {
return await context.github.issues.getLabel(context.repo({
name: name
}))
} catch (e) {
return context.github.issues.createLabel(context.repo({
name: name,
color: color
}))
}
}
/**
* This is the main event loop that runs when a revelent Pull Request
* action is triggered.
*/
module.exports = app => {
app.on([
'pull_request.opened',
'pull_request.reopened',
'pull_request.synchronize',
'pull_request.edited'], async context => {
const pullRequest = context.payload.pull_request;
const {owner: {login: owner}, name: repo} = pullRequest.base.repo;
const {number} = pullRequest;
let {additions, deletions} = pullRequest;
// get list of custom generated files as defined in .gitattributes
const customGeneratedFiles = await getCustomGeneratedFiles(context, owner, repo)
// list of files modified in the pull request
const res = await context.github.pullRequests.listFiles({owner, repo, number})
// if files are generated, remove them from the additions/deletions total
res.data.forEach(function(item) {
var g = new generated(item.filename, item.patch)
if (globMatch(item.filename, customGeneratedFiles) || g.isGenerated()) {
additions -= item.additions
deletions -= item.deletions
}
})
// calculate GitHub label
var labelToAdd = sizeLabel(additions + deletions / 10)
// remove existing size/<size> label if it exists and is not labelToAdd
pullRequest.labels.forEach(function(prLabel) {
if(Object.values(label).includes(prLabel.name)) {
if (prLabel.name != labelToAdd) {
context.github.issues.removeLabel(context.issue({
name: prLabel.name
}))
}
}
})
// assign GitHub label
return await addLabel(context, labelToAdd, colors[labelToAdd])
})
// we don't care about marketplace events
app.on('marketplace_purchase', async context => {
return
})
}