-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.ts
141 lines (126 loc) · 3.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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.paths;
}
function formatItemsForPath(previousComment, [path, items]) {
const showPaths = core.getInput("show-paths") === "true";
const mergeComment = core.getInput("merge-comment") === "true";
if (!!previousComment && mergeComment) {
const existingCheckedItems = previousComment
.split("\n")
.filter((line) => line !== "" && line.startsWith("- [x]"))
.map((line) => line.substring(5).trim());
const preservedItems = items.filter((item) => {
return !!existingCheckedItems.find((existingItem) =>
existingItem.includes(item)
);
});
const newItems = items.filter((item) => {
return !existingCheckedItems.find((existingItem) =>
existingItem.includes(item)
);
});
return showPaths
? [
`__Files matching \`${path}\`:__\n`,
...preservedItems.map((item) => `- [x] ${item}\n`),
...newItems.map((item) => `- [ ] ${item}\n`),
"\n",
].join("")
: [
...preservedItems.map((item) => `- [x] ${item}\n`),
...newItems.map((item) => `- [ ] ${item}\n`),
].join("");
}
return showPaths
? [
`__Files matching \`${path}\`:__\n`,
...items.map((item) => `- [ ] ${item}\n`),
"\n",
].join("")
: [...items.map((item) => `- [ ] ${item}\n`)].join("");
}
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 = Object.entries(checklistPaths).filter(
([key, _]) => {
for (const modifiedPath of modifiedPaths) {
if (minimatch(modifiedPath, key, minimatchOptions)) {
return true;
}
}
return false;
}
);
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`,
...applicableChecklistPaths.map(([path, items]) =>
formatItemsForPath(
!!existingComment ? existingComment.body : undefined,
[path, items]
)
),
`\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));