-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (111 loc) · 3.49 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
const core = require('@actions/core');
const github = require('@actions/github');
const main = async () => {
try {
/**
* We need to fetch all the inputs that were provided to our action
* and store them in variables for us to use.
**/
const owner = core.getInput('owner', { required: true });
const repo = core.getInput('repo', { required: true });
const pr_number = core.getInput('pr_number', { required: true });
const token = core.getInput('token', { required: true });
/**
* Now we need to create an instance of Octokit which will use to call
* GitHub's REST API endpoints.
* We will pass the token as an argument to the constructor. This token
* will be used to authenticate our requests.
* You can find all the information about how to use Octokit here:
* https://octokit.github.io/rest.js/v18
**/
const octokit = new github.getOctokit(token);
/**
* We need to fetch the list of files that were changes in the Pull Request
* and store them in a variable.
* We use octokit.paginate() to automatically loop over all the pages of the
* results.
* Reference: https://octokit.github.io/rest.js/v18#pulls-list-files
*/
const { data: changedFiles } = await octokit.rest.pulls.listFiles({
owner,
repo,
pull_number: pr_number,
});
/**
* Contains the sum of all the additions, deletions, and changes
* in all the files in the Pull Request.
**/
let diffData = {
additions: 0,
deletions: 0,
changes: 0
};
// Reference for how to use Array.reduce():
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
diffData = changedFiles.reduce((acc, file) => {
acc.additions += file.additions;
acc.deletions += file.deletions;
acc.changes += file.changes;
return acc;
}, diffData);
/**
* Loop over all the files changed in the PR and add labels according
* to files types.
**/
for (const file of changedFiles) {
/**
* Add labels according to file types.
*/
const fileExtension = file.filename.split('.').pop();
switch(fileExtension) {
case 'md':
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pr_number,
labels: ['markdown'],
});
case 'js':
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pr_number,
labels: ['javascript'],
});
case 'yml':
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pr_number,
labels: ['yaml'],
});
case 'yaml':
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pr_number,
labels: ['yaml'],
});
}
}
/**
* Create a comment on the PR with the information we compiled from the
* list of changed files.
*/
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pr_number,
body: `
Pull Request #${pr_number} has been updated with: \n
- ${diffData.changes} changes \n
- ${diffData.additions} additions \n
- ${diffData.deletions} deletions \n
`
});
} catch (error) {
core.setFailed(error.message);
}
}
// Call the main function to run the action
main();