-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpr_labels_automation.js
executable file
·43 lines (37 loc) · 1.25 KB
/
pr_labels_automation.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
import { Octokit } from '@octokit/rest';
import fs from 'fs';
import util from 'util';
const octokit = new Octokit({ auth: process.env.TOKEN });
const owner = process.env.GITHUB_REPOSITORY.split('/')[0];
const repo = process.env.GITHUB_REPOSITORY.split('/')[1];
const prNumber = process.env.GITHUB_EVENT_PATH
? JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')).number
: undefined;
/**
* This script is used to automatically add labels to PRs based on the checkboxes in the PR body.
*/
async function addLabel() {
try {
const { data: pr } = await octokit.pulls.get({ owner, repo, pull_number: prNumber });
const body = pr.body.toLowerCase();
const typesOfChange = ['Bug Fix', 'New Feature', 'Breaking Change', 'Refactor', 'Documentation', 'Other'];
const labelsToAdd = [];
typesOfChange.forEach((type) => {
if (body.includes(`- [x] ${type.toLowerCase()}`)) {
labelsToAdd.push(type);
}
});
if (labelsToAdd.length > 0) {
await octokit.issues.setLabels({
owner,
repo,
issue_number: prNumber,
labels: labelsToAdd.map((label) => label.toLowerCase())
});
}
} catch (error) {
console.log(util.inspect(error, false, null, true /* enable colors */));
console.error(`Error: ${error.message}`);
}
}
addLabel();