forked from wyozi/contextual-qa-checklist-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
123 lines (109 loc) · 3.49 KB
/
index.ts
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
const core = require("@actions/core");
import * as github from "@actions/github";
const YAML = require("yaml");
const minimatch = require("minimatch");
const { readFileSync } = require("fs");
const header = core.getInput("comment-header");
const footer = core.getInput("comment-footer")
const minimatchOptions = {
dot: core.getInput('include-hidden-files') === 'true'
};
function getChecklistPaths(): Record<string, string[]> {
const inputFile = core.getInput("input-file");
const parsedFile = YAML.parse(readFileSync(inputFile, { encoding: "utf8" }));
return parsedFile;
}
function formatItemsForPath(applicableChecklist): string {
const showPaths = core.getInput("show-paths") === 'true';
let text = ""
for (const temp of applicableChecklist) {
if (showPaths){
text +=
`__Files were changed in the following path(s):__\n` +
`${temp.changedPath.map((path) => `- \`${path}\``).join("\n")}\n`+
`\n${temp.description}\n` +
`${temp.items.map((item) => `- [ ] ${item}`).join("\n")}\n\n`;
} else {
text +=
`${temp.description}\n` +
`${temp.items.map((item) => `- [ ] ${item}`).join("\n")}\n\n`;
}
}
return text;
}
function getMatchingPaths(checklistPaths, modifiedPaths) {
let applicableChecklistPaths = [];
for (const [key, value] of Object.entries(checklistPaths)){
let isApplicable = false
let changedPath = []
for (const path in (value as any).paths){
for (const modifiedPath of modifiedPaths) {
if (minimatch(modifiedPath, (value as any).paths[path], minimatchOptions)) {
if (!changedPath.includes((value as any).paths[path]))
changedPath.push((value as any).paths[path])
isApplicable = true
}
}
}
if (isApplicable) {
(value as any).changedPath = changedPath
applicableChecklistPaths.push(value)
}
}
return applicableChecklistPaths;
}
async function run() {
const context = github.context;
const { owner, repo } = context.repo;
const number = (context.payload.issue ?? context.payload.pull_request ?? context.payload).number;
const ghToken = core.getInput("gh-token");
const client = github.getOctokit(ghToken);
const checklistPaths = getChecklistPaths();
const modifiedPaths: string[] = (
await client.rest.pulls.listFiles({
owner: owner,
repo: repo,
pull_number: number
})
).data.map(file => file.filename);
const applicableChecklistPaths = getMatchingPaths(checklistPaths, modifiedPaths);
const existingComment = (
await client.rest.issues.listComments({
owner: owner,
repo: repo,
issue_number: number
})
).data.find(comment => comment.body.includes(footer));
if (applicableChecklistPaths.length > 0) {
const body = [
`${header}\n\n`,
formatItemsForPath(applicableChecklistPaths),
`\n${footer}`,
].join("");
if (existingComment) {
await client.rest.issues.updateComment({
owner: owner,
repo: repo,
comment_id: existingComment.id,
body
});
} else {
await client.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: number,
body
});
}
} else {
if (existingComment) {
await client.rest.issues.deleteComment({
owner: owner,
repo: repo,
comment_id: existingComment.id
});
}
console.log("No paths were modified that match checklist paths");
}
}
run().catch(err => core.setFailed(err.message));