-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
111 lines (91 loc) · 3.01 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
const fs = require("fs");
const core = require("@actions/core");
const github = require("@actions/github");
const mdjson = require("mdjson");
const ISSUE_TEMPLATE_DIR = ".github/ISSUE_TEMPLATE";
// Grab the closing message from params or fallback to a default message
const getIssueCloseMessage = () => {
const message =
core.getInput("issue-close-message") ||
"@${issue.user.login}: hello! :wave:\n\nThis issue is being automatically closed because it does not follow the issue template.";
const { payload } = github.context;
return Function(
...Object.keys(payload),
`return \`${message}\``
)(...Object.values(payload));
};
(async () => {
const client = new github.GitHub(
core.getInput("github-token", { required: true })
);
const { payload } = github.context;
const issueBodyMarkdown = payload.issue.body;
// Get all the markdown titles from the issue body
const issueBodyTitles = Object.keys(mdjson(issueBodyMarkdown));
// Get a list of the templates
const issueTemplates = fs.readdirSync(ISSUE_TEMPLATE_DIR);
// Compare template titles with issue body
const doesIssueMatchAnyTemplate = issueTemplates.some(template => {
const templateMarkdown = fs.readFileSync(
`${ISSUE_TEMPLATE_DIR}/${template}`,
"utf-8"
);
const templateTitles = Object.keys(mdjson(templateMarkdown));
return templateTitles.every(title => issueBodyTitles.includes(title));
});
const { issue } = github.context;
const closedIssueLabel = core.getInput("closed-issues-label");
if (doesIssueMatchAnyTemplate || payload.action !== "opened") {
// Only reopen the issue if there's a `closed-issues-label` so it knows that
// it was previously closed because of the wrong template
if (payload.issue.state === "closed" && closedIssueLabel) {
const labels = (
await client.issues.listLabelsOnIssue({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number
})
).data.map(({ name }) => name);
if (!labels.includes(closedIssueLabel)) {
return;
}
await client.issues.removeLabel({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
name: closedIssueLabel
});
await client.issues.update({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
state: "open"
});
return;
}
return;
}
// If an closed issue label was provided, add it to the issue
if (closedIssueLabel) {
await client.issues.addLabels({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
labels: [closedIssueLabel]
});
}
// Add the issue closing comment
await client.issues.createComment({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
body: getIssueCloseMessage()
});
// Close the issue
await client.issues.update({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
state: "closed"
});
})();