forked from ahmadnassri/action-terraform-report
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
137 lines (111 loc) · 3.34 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
/* eslint-disable camelcase */
// packages
const core = require('@actions/core')
const github = require('@actions/github')
const unidiff = require('@ahmadnassri/terraform-unidiff')
const { promises: { readFile } } = require('fs')
// parse inputs
const inputs = {
diff: core.getInput('show-diff'),
plan: core.getInput('show-plan'),
text: core.getInput('terraform-text'),
json: core.getInput('terraform-json'),
token: core.getInput('github-token'),
removeStaleReports: core.getInput('remove-stale-reports')
}
// extract relevant variables
const { runId, payload: { pull_request } } = github.context
// build report content
const AUTOMATED_REPORT_TITLE = ":robot: Terraform Report"
const AUTOMATED_REPORT_FOOTER = `This comment was generated by [Terraform Pull Request Report Generator](https://github.com/ahmadnassri/action-terraform-report) - action run [#${runId}](https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${runId}) - commit ${github.context.sha}`
// exit early
if (!inputs.text || !inputs.json || !inputs.token) {
core.error('Missing required inputs')
process.exit(1)
}
// exit
if (!pull_request) {
core.warning('not a pull request. exiting.')
process.exit(0)
}
// exit early
if (pull_request.state !== 'open') {
core.warning('action triggered on a closed pull request')
process.exit(0)
}
// error handler
function errorHandler (err) {
console.error(err)
core.error(`Unhandled error: ${err}`)
process.exit(1)
}
// catch errors and exit
process.on('unhandledRejection', errorHandler)
process.on('uncaughtException', errorHandler)
const octokit = github.getOctokit(inputs.token)
const removeStaleReports = async () => {
// get issue comments
const {data: comments} = await octokit.issues.listComments({
...github.context.repo,
issue_number: pull_request.number,
})
// get action comments
const actionComments = comments.filter(c => c.body.includes(AUTOMATED_REPORT_TITLE) && !c.body.includes(AUTOMATED_REPORT_FOOTER))
// remove existing comments
for (var i = 0; i < actionComments.length; i++) {
try {
await octokit.issues.deleteComment({
...github.context.repo,
issue_number: pull_request.number,
comment_id: actionComments[i]["id"]
})
} catch(e) {
console.warn(`Could not delete comment: ${actionComments[i]["id"]}`)
}
}
}
async function main () {
// load terraform files
const text = await readFile(inputs.text)
const json = await readFile(inputs.json)
// load terraform plan JSON
const data = JSON.parse(json)
// process file
const { summary, patches } = unidiff(data)
const diff = patches.map(patch => `\`\`\`diff\n${patch}\n\`\`\``).join('\n\n')
let body = `
### ${AUTOMATED_REPORT_TITLE}
---
##### Summary: \`${summary.create}\` to add, \`${summary.update}\` to change, \`${summary.delete}\` to destroy
`
if (inputs.plan === 'true') {
body += `
<details><summary>Show Plan</summary>
\`\`\`terraform
${text}
\`\`\`
</details>
`
}
if (inputs.diff === 'true') {
body += `
<details><summary>Show Diff</summary>
${diff}
</details>
`
}
body += `
---
${AUTOMATED_REPORT_FOOTER}
`
if (inputs.removeStaleReports === 'true') {
removeStaleReports()
}
// update PR
await octokit.issues.createComment({
...github.context.repo,
issue_number: pull_request.number,
body
})
}
main()