generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (150 loc) · 4.86 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const core = require('@actions/core');
const github = require('@actions/github');
// most @actions toolkit packages have async methods
async function run() {
try {
const config = JSON.parse(core.getInput("config"));
const token = process.env.GITHUB_TOKEN;
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const issue = github.context.payload.issue;
const comment = github.context.payload.comment;
const commentAuthor = comment.user.login
const issueId = issue.node_id
const issueNumber = issue.number
if (!comment.body.trim().startsWith("@bot")) return;
const cmdRegex = /^@bot\s*(.*)$/
const cmd = comment.body.trim().match(cmdRegex)[1].trim();
if (!Array.isArray(config)) throw "Config must be an array"
if (!issue) return core.warning("No issue. Exiting gracefully");
if (!token) return core.setFailed("GITHUB_TOKEN is not available");
const octokit = new github.GitHub(token);
const issueLabels = issue.labels.map((label) => label.name);
console.log("");
console.log(`Removing bot attention grabbing comments`);
const result = await octokit.graphql(`
{
repository(owner: "${owner}", name: "${repo}") {
assignableUsers(first:100) {
edges {
node {
id
login
}
}
}
labels(first: 100){
edges {
node {
id
name
}
}
}
issue(number: ${issueNumber}) {
id
comments(first: 100) {
edges {
node {
id
body
}
}
}
}
}
}
`);
console.log(`Checking authorization...`);
const assignableUsers = result.repository.assignableUsers.edges.map(u => u.node.login)
console.log(`Comment author is ${commentAuthor}`);
if (!assignableUsers.includes(commentAuthor)) {
console.log(`The user ${commentAuthor} is not allowed to trigger this bot.`)
return;
}
const labelLookup = {}
result.repository.labels.edges.forEach((labelEdge) => {
labelLookup[labelEdge.node.name] = labelEdge.node.id;
})
const commentEdges = result.repository.issue.comments.edges;
commentEdges.forEach(async (edge) => {
if (edge.node.body.startsWith("@bot")) {
console.log(`Removing comment ${edge.node.id}`);
await octokit.graphql(`
mutation($id: ID!) {
deleteIssueComment(input: {id: $id}) {
clientMutationId
}
}
`, { id: edge.node.id });
}
})
const entry = config.find((entry) => entry.cmd === cmd);
if (!entry) {
console.log(`No matching command found for ${cmd}.`);
return;
}
console.log("");
console.log(`Processing "${entry.cmd}" for issue ${issueId}...`)
console.log(`Labeling issue with ${entry.labels}`);
const labelIds = entry.labels.map((labelName) => labelLookup[labelName]);
await octokit.graphql(`
mutation($labelableId: ID!, $labelIds: [ID!]! ) {
addLabelsToLabelable(input: { labelableId: $labelableId, labelIds: $labelIds }) {
clientMutationId
}
}
`, { labelableId: issueId, labelIds });
console.log(`Grabbing message from ${entry.message}`);
const { repository } = await octokit.graphql(`
{
repository(owner: "${owner}", name: "${repo}") {
object(expression: "${entry.message}") {
... on Blob {
text
}
}
}
}
`);
if (!repository.object) {
core.warning(`Could not find message at ${entry.message}`)
return;
}
const responseBody = repository.object.text;
console.log(`Found message for ${entry.cmd}...`);
console.log(`Checking if comment already exists...`);
const commentExists = commentEdges.some((edge) => {
return edge.node.body === responseBody;
})
if (commentExists) {
console.log("Comment already exists.");
return;
}
console.log("Comment does not exist.")
console.log(`Adding comment to issue for ${entry.cmd}...`);
await octokit.graphql(`
mutation($subjectId: ID!, $responseBody: String!) {
addComment(input: { subjectId: $subjectId, body: $responseBody}) {
clientMutationId
}
}
`, { subjectId: issueId, responseBody });
console.log('Comment added...')
if (entry.close) {
console.log(`Closing issue...`)
await octokit.graphql(`
mutation($id: ID!) {
updateIssue(input: { id: $id, state: CLOSED}) {
clientMutationId
}
}
`, { id: issueId });
}
console.log('Processing complete.')
console.log("");
}
catch (error) {
core.setFailed(error.message);
}
}
run()