-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
284 lines (253 loc) · 8.55 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs");
const expandDetailsComment = core.getBooleanInput("expand-comment");
const includePlanSummary = core.getBooleanInput("include-plan-job-summary");
const myToken = core.getInput("github-token");
const octokit = github.getOctokit(myToken);
const context = github.context;
const inputFilenames = core.getMultilineInput("json-file");
const commentHeader = core.getMultilineInput("comment-header");
const commentFooter = core.getMultilineInput("comment-footer");
const quietMode = core.getBooleanInput("quiet");
const includeLinkToWorkflow = core.getBooleanInput("include-workflow-link");
const hidePreviousComments = core.getBooleanInput("hide-previous-comments");
const logChangedResources = core.getBooleanInput("log-changed-resources");
const workflowLink = includeLinkToWorkflow
? `
[Workflow: ${context.workflow}](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})
`
: "";
var hasNoChanges = false;
// GraphQL queries and mutations used for hiding previous comments
const minimizeCommentQuery = /* GraphQL */ `
mutation minimizeComment($id: ID!) {
minimizeComment(input: { classifier: OUTDATED, subjectId: $id }) {
clientMutationId
}
}
`;
const commentsQuery = /* GraphQL */ `
query comments($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
comments(last: 100, orderBy: { field: UPDATED_AT, direction: DESC }) {
nodes {
id
body
isMinimized
}
}
}
}
}
`;
const output = () => {
let body = "";
// for each file
for (const file of inputFilenames) {
const resource_changes = JSON.parse(fs.readFileSync(file)).resource_changes;
try {
let changed_resources = resource_changes.filter((resource) => {
return resource.change.actions != ["no-op"];
});
if (logChangedResources) {
console.log("changed_resources", changed_resources);
}
if (Array.isArray(resource_changes) && resource_changes.length > 0) {
const resources_to_create = [],
resources_to_update = [],
resources_to_delete = [],
resources_to_replace = [],
resources_unchanged = [];
// for each resource changes
for (const resource of resource_changes) {
const change = resource.change;
const address = resource.address;
switch (change.actions[0]) {
default:
break;
case "no-op":
resources_unchanged.push(address);
break;
case "create":
resources_to_create.push(address);
break;
case "delete":
if (change.actions.length > 1) {
resources_to_replace.push(address);
} else {
resources_to_delete.push(address);
}
break;
case "update":
resources_to_update.push(address);
break;
}
}
// the body must be indented at the start otherwise
// there will be formatting error when comment is
// showed on GitHub
body += `
${commentHeader}
<details ${expandDetailsComment ? "open" : ""}>
<summary>
<b>Terraform Plan: ${resources_to_create.length} to be created, ${resources_to_delete.length} to be deleted, ${resources_to_update.length} to be updated, ${resources_to_replace.length} to be replaced, ${resources_unchanged.length} unchanged.</b>
</summary>
${details("create", resources_to_create, "+")}
${details("delete", resources_to_delete, "-")}
${details("update", resources_to_update, "!")}
${details("replace", resources_to_replace, "+")}
</details>
${commentFooter.map((a) => (a == "" ? "\n" : a)).join("\n")}
${workflowLink}
`;
if (
resources_to_create +
resources_to_delete +
resources_to_update +
resources_to_replace ==
[]
) {
hasNoChanges = true;
}
} else {
hasNoChanges = true;
console.log(
"No changes found in the plan. setting hasNoChanges to true.",
);
body += `
<p>There were no changes done to the infrastructure.</p>
`;
core.info(
`"The content of ${file} did not result in a valid array or the array is empty... Skipping."`,
);
}
} catch (error) {
core.error(`${file} is not a valid JSON file. error: ${error}`);
}
}
return body;
};
const details = (action, resources, operator) => {
let str = "";
if (resources.length !== 0) {
str = `
#### Resources to ${action}\n
\`\`\`diff\n
`;
for (const el of resources) {
// In the replace block, we show delete (-) and then create (+)
if (action === "replace") {
str += `- ${el}\n`;
}
str += `${operator} ${el}\n`;
}
str += "```\n";
}
return str;
};
const queryComments = (variables) => {
return octokit.graphql(commentsQuery, variables);
};
const minimizeComment = (variables) => {
return octokit.graphql(minimizeCommentQuery, variables);
};
const hideComments = () => {
core.info(`Hiding previous comments.`);
queryComments({
owner: context.repo.owner,
name: context.repo.repo,
number: context.issue.number,
})
.then((response) => {
core.info(
`Successfully retrieved comments for PR #${context.issue.number}.`,
);
const comments = response.repository.pullRequest.comments.nodes;
core.info(`Found ${comments.length} comments in the PR.`);
core.info(`Comment header used for matching is: ${commentHeader}`);
filteredComments = comments.filter(
(comment) =>
comment.body.includes("Terraform Plan:") ||
comment.body.includes(
"There were no changes done to the infrastructure.",
),
);
core.info(
`Filtered down to ${filteredComments.length} comments created by this action.`,
);
filteredComments = filteredComments.filter((comment) =>
comment.body.includes(commentHeader),
);
core.info(
`Filtered down to ${filteredComments.length} comments that need to be minimized.`,
);
const minimizePromises = filteredComments
.filter((comment) => !comment.isMinimized)
.map((comment) => {
return minimizeComment({ id: comment.id }).catch((error) =>
core.error(
`Failed to minimize comment ${comment.id}: ${error.message}`,
),
);
});
return Promise.all(minimizePromises)
.then(() => core.info("All minimize operations completed."))
.catch((error) =>
core.error(`Error during minimize operations: ${error.message}`),
);
})
.catch((error) =>
core.error(`Failed to retrieve comments: ${error.message}`),
);
};
try {
let rawOutput = output();
let createComment = true;
console.log("hidePreviousComments", hidePreviousComments);
console.log(
"hidePreviousComments && context.eventName === pull_request",
hidePreviousComments && context.eventName === "pull_request",
);
if (hidePreviousComments && context.eventName === "pull_request") {
hideComments();
}
console.log("includePlanSummary", includePlanSummary);
if (includePlanSummary) {
core.info("Adding plan output to job summary");
core.summary.addHeading("Terraform Plan Results").addRaw(rawOutput).write();
}
console.log("quietMode", quietMode);
console.log("hasNoChanges", hasNoChanges);
console.log("quietMode && hasNoChanges", quietMode && hasNoChanges);
if (quietMode && hasNoChanges) {
core.info(
"quiet mode is enabled and there are no changes to the infrastructure.",
);
core.info("Skipping comment creation.");
createComment = false;
}
if (context.eventName === "pull_request") {
core.info(
`Found PR # ${context.issue.number} from workflow context - proceeding to comment.`,
);
} else {
core.info("Action doesn't seem to be running in a PR workflow context.");
core.info("Skipping comment creation.");
createComment = false;
}
if (createComment) {
core.info("Adding comment to PR");
core.info(`Comment: ${rawOutput}`);
octokit.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: rawOutput,
});
core.info("Comment added successfully.");
}
} catch (error) {
core.setFailed(error.message);
}