-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
99 lines (85 loc) · 2.72 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
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const deepEqual = require('fast-deep-equal');
module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.edited', check);
robot.on('pull_request.synchronize', check);
robot.on('pull_request.unlabeled', check);
robot.on('pull_request.labeled', check);
async function check(context) {
const pr = context.payload.pull_request;
// set status to pending while checks happen
setStatus(context, {
state: 'pending',
description: `Checking whether to apply or remove dependencies and infra labels`,
});
async function getFileFromRef(filePath, ref, transform) {
const dataBuf = (await context.github.repos.getContent(
context.repo({
path: filePath,
ref: ref,
}),
)).data;
return transform(
Buffer.from(dataBuf.content, dataBuf.encoding).toString('utf8'),
);
}
async function getChangedJsonFile(filePath) {
return {
base: await getFileFromRef(filePath, pr.base.ref, JSON.parse),
head: await getFileFromRef(filePath, pr.head.ref, JSON.parse),
};
}
async function setLabel(check, label) {
try {
if (check) {
await context.github.issues.addLabels(
context.issue({
labels: [label],
}),
);
} else {
await context.github.issues.removeLabel(
context.issue({
name: label,
}),
);
}
} catch (err) {
if (err.code !== 404) {
throw err;
}
}
}
function isDevDependenciesChange(baseJson, headJson) {
return !deepEqual(baseJson.devDependencies, headJson.devDependencies);
}
function isDependenciesChange(baseJson, headJson) {
return !deepEqual(baseJson.dependencies, headJson.dependencies);
}
const changes = await getChangedJsonFile('package.json');
// Set labels for 'infra' and 'dependencies'
setLabel(isDevDependenciesChange(changes.base, changes.head), 'infra');
setLabel(isDependenciesChange(changes.base, changes.head), 'dependencies');
// set status to success
setStatus(context, {
state: 'success',
description: 'Dependencies and infra labels have been set (or unset)',
});
}
};
async function setStatus(context, {state, description}) {
const {github} = context;
return github.repos.createStatus(
context.issue({
state,
description,
sha: context.payload.pull_request.head.sha,
context: 'probot/label-dependency-pr',
}),
);
}