-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (123 loc) · 4.53 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
import minimist from 'minimist'
import {publish, poll, getMarkdownComment} from 'lightlytics-publisher-core'
import Table from 'cli-table3'
import colors from '@colors/colors'
const pollTimeoutDefault = 10 // minutes
const pollIntervalDefault = 5000
try {
const args = minimist(process.argv.slice(2))
if (args['help'] || args['h']) {
throw "Expected usage: node index.js --dir=\"./working-dir\" --hostname=\"./api-Url\" --plan=\"./working-dir/plan.json\" --graph=\"./working-dir/graph.dot\" --token=\"collection-token\""
}
const requiredArgs = ['dir', 'hostname', 'graph', 'token']
if (Object.keys(args).length <= requiredArgs.length) {
throw `Expected at least ${requiredArgs.length} cli arguments. usage: node index.js --dir=\"./working-dir\" --hostname="./api-Url\" --plan=\"./working-dir/plan.json\" --graph=\"./working-dir/graph.dot\" --token=\"collection-token\"`
}
requiredArgs.forEach(requiredArg => {
if (!args[requiredArg]) {
throw `Missing arg - \"${requiredArg}\" usage: node index.js --${requiredArg}=value. try --help if needed`
}
})
const tfWorkingDir = args['dir']
const apiUrl = args['hostname']
const tfPlan = args['plan']
const tfcToken = args['tfc-token']
const tfcRunId = args['tfc-run-id']
const tfGraph = args['graph']
const collectionToken = args['token']
const metadata = {source: {format: 'Terraform'}}
const optionalMetadataArgs = ['name', 'type', 'format', 'branch', 'base_branch', 'commit_hash', 'pr_id', 'repository', 'user_name']
optionalMetadataArgs.map(arg => {
if (args[arg]) {
metadata['source'][arg] = args[arg]
}
})
const {eventId, customerId} = await publish({
apiUrl,
tfWorkingDir,
tfPlan,
tfcToken,
tfcRunId,
tfGraph,
collectionToken,
metadata
})
if (args['poll']) {
await poll({
apiUrl,
collectionToken,
customerId,
eventId,
pollTimeout: args['pollTimeout'] ? Number(args['pollTimeout']) : pollTimeoutDefault,
pollInterval: args['pollInterval'] ? Number(args['pollInterval']) : pollIntervalDefault,
onStatusUpdate: (status, violations) => {
if (status.conclusion) {
const details_url = `https://${apiUrl}/w/${customerId}/simulations/${eventId}`
args['markdown'] ?
console.log(getMarkdownComment(status, violations, details_url)) :
printCLI(status, violations, details_url)
if (status.conclusion !== 'success') {
process.exit(1)
}
}
}
})
} else {
logFormattedSimulation(`https://${apiUrl}/w/${customerId}/simulations/${eventId}`)
}
} catch (error) {
console.error(error)
}
function logFormattedSimulation(link) {
const pullRequestMessage = `An execution simulation has been generated by **Lightlytics**, to view this run impact analysis, Visit:
${link}
> _This comment was added automatically by a git workflow to help DevOps teams predict what will be the impact of the proposed change after completing this PR_`
console.log(pullRequestMessage)
}
function printCLI(status, violations = [], details_url) {
const violationsTable = new Table({
head: ['Severity', 'Category', 'Violation', 'Count']
})
const formatViolation = violation =>
[`${severityToColorCLI(violation.severity)}${violation.fail_simulation ? '*' : ''}`, violation.category, violation.name, violation.count]
let forceFailCount = 0
for (const violation of violations) {
if (violation.fail_simulation) forceFailCount++
violationsTable.push(formatViolation(violation))
}
console.log(`An execution simulation has been generated by Lightlytics:\n${details_url}`)
console.log(`Simulation Status: ${statusToColorCLI(status.label)}\n`)
console.log(`Total violations count: ${violations.length}`)
console.log(`Forced failed violations: ${forceFailCount}`)
if (violations) {
console.log('Violation Summary:')
console.log(violationsTable.toString())
if (forceFailCount) {
console.log('* Violation that is forcing the simulation to fail')
}
}
}
function severityToColorCLI(severity) {
switch (severity) {
case 4:
return colors.red('Critical')
case 3:
return colors.red('High')
case 2:
return colors.yellow('Medium')
case 1:
return colors.gray('Low')
}
return severity
}
function statusToColorCLI(statusLabel) {
switch (statusLabel) {
case 'Errored':
return colors.red(statusLabel)
case 'Failed':
return colors.red(statusLabel)
case 'Passed':
return colors.green(statusLabel)
}
return statusLabel
}