-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.ts
274 lines (267 loc) · 7.9 KB
/
utils.ts
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
import * as artifact from '@actions/artifact'
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as glob from '@actions/glob'
import * as tc from '@actions/tool-cache'
import {
EXECUTABLE,
Inputs,
VERSION,
getQodanaCliUrl,
getQodanaPullArgs,
getQodanaScanArgs
} from '../../common/qodana'
import path from 'path'
function getDeprecatedInputs(args: string[]): string[] {
const deprecatedInputs = {
linter: core.getInput('linter'),
projectDir: core.getInput('project-dir'),
additionalVolumes: core.getMultilineInput('additional-volumes'),
additionalEnvVars: core.getMultilineInput('additional-env-variables'),
inspectedDir: core.getInput('inspected-dir'),
ideaConfigDir: core.getInput('idea-config-dir'),
baselinePath: core.getInput('baseline-path'),
baselineIncludeAbsent: core.getBooleanInput('baseline-include-absent'),
failThreshold: core.getInput('fail-threshold'),
profileName: core.getInput('profile-name'),
profilePath: core.getInput('profile-path'),
gradleSettingsPath: core.getInput('gradle-settings-path'),
changes: core.getBooleanInput('changes'),
script: core.getInput('script')
}
if (deprecatedInputs.linter) {
args.push('-l', deprecatedInputs.linter)
}
if (deprecatedInputs.projectDir) {
args.push('-i', deprecatedInputs.projectDir)
}
if (deprecatedInputs.additionalVolumes) {
for (const volume of deprecatedInputs.additionalVolumes) {
args.push('-v', volume)
}
}
if (deprecatedInputs.additionalEnvVars) {
for (const envVar of deprecatedInputs.additionalEnvVars) {
args.push('-e', envVar)
}
}
if (deprecatedInputs.inspectedDir) {
args.push('-d', deprecatedInputs.inspectedDir)
}
if (deprecatedInputs.ideaConfigDir) {
args.push('-v', `${deprecatedInputs.ideaConfigDir}:/root/.config/idea`)
}
if (deprecatedInputs.baselinePath) {
args.push('-b', deprecatedInputs.baselinePath)
}
if (deprecatedInputs.baselineIncludeAbsent) {
args.push('--baseline-include-absent')
}
if (deprecatedInputs.failThreshold) {
args.push('--fail-threshold', deprecatedInputs.failThreshold)
}
if (deprecatedInputs.profileName) {
args.push('--profile-name', deprecatedInputs.profileName)
}
if (deprecatedInputs.profilePath) {
args.push('--profile-path', deprecatedInputs.profilePath)
}
if (deprecatedInputs.gradleSettingsPath) {
args.push(
'-v',
`${deprecatedInputs.gradleSettingsPath}:/root/.gradle/gradle.properties`
)
}
if (deprecatedInputs.changes) {
args.push('--changes')
}
if (deprecatedInputs.script) {
args.push('--script', deprecatedInputs.script)
}
return args
}
/**
* The context for the action.
* @returns The action inputs.
*/
export function getInputs(): Inputs {
return {
args: getDeprecatedInputs(
core
.getInput('args')
.split(',')
.map(arg => arg.trim())
),
resultsDir: core.getInput('results-dir'),
cacheDir: core.getInput('cache-dir'),
additionalCacheHash: core.getInput('additional-cache-hash'),
uploadResult: core.getBooleanInput('upload-result'),
artifactName: core.getInput('artifact-name'),
useCaches: core.getBooleanInput('use-caches'),
githubToken: core.getInput('github-token'),
useAnnotations: core.getBooleanInput('use-annotations')
}
}
/**
* Runs the qodana command with the given arguments.
* @param args docker command arguments.
* @returns The qodana command execution output.
*/
export async function qodana(args: string[] = []): Promise<number> {
const env = isServer() ? 'github-enterprise' : 'github'
if (args.length === 0) {
const inputs = getInputs()
args = getQodanaScanArgs(
inputs.args,
inputs.resultsDir,
inputs.cacheDir,
env
)
}
return (
await exec.getExecOutput(EXECUTABLE, args, {
ignoreReturnCode: true
})
).exitCode
}
/**
* Prepares the agent for qodana scan: install Qodana CLI and pull the linter.
* @param args qodana arguments
*/
export async function prepareAgent(args: string[]): Promise<void> {
const temp = await tc.downloadTool(getQodanaCliUrl())
let extractRoot
if (process.platform === 'win32') {
extractRoot = await tc.extractZip(temp)
} else {
extractRoot = await tc.extractTar(temp)
}
core.addPath(await tc.cacheDir(extractRoot, EXECUTABLE, VERSION))
const exitCode = await qodana(getQodanaPullArgs(args))
if (exitCode !== 0) {
core.setFailed(`qodana pull failed with exit code ${exitCode}`)
return
}
}
/**
* Uploads the Qodana report files from temp directory to GitHub job artifact.
* @param resultsDir The path to upload report from.
* @param artifactName Artifact upload name.
* @param execute whether to execute promise or not.
*/
export async function uploadReport(
resultsDir: string,
artifactName: string,
execute: boolean
): Promise<void> {
if (!execute) {
return
}
try {
core.info('Uploading report...')
const globber = await glob.create(`${resultsDir}/*`)
const files = await globber.glob()
await artifact
.create()
.uploadArtifact(artifactName, files, path.dirname(resultsDir), {
continueOnError: true
})
} catch (error) {
core.warning(`Failed to upload report – ${(error as Error).message}`)
}
}
/**
* Uploads the cache to GitHub Actions cache from the given path.
* @param cacheDir The path to upload the cache from.
* @param additionalCacheHash Addition to the generated cache hash
* @param execute whether to execute promise or not.
*/
export async function uploadCaches(
cacheDir: string,
additionalCacheHash: string,
execute: boolean
): Promise<void> {
if (!execute) {
return
}
try {
if (isServer()) {
core.warning(
'Cache is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details'
)
return
}
const primaryKey = `qodana-${process.env['GITHUB_REF']}-${additionalCacheHash}`
try {
await cache.saveCache([cacheDir], primaryKey)
core.info(`Cache saved with key ${primaryKey}`)
} catch (error) {
core.warning(
`Failed to save cache with key ${primaryKey} – ${
(error as Error).message
}`
)
}
} catch (error) {
core.warning(`Failed to upload caches – ${(error as Error).message}`)
}
}
/**
* Restores the cache from GitHub Actions cache to the given path.
* @param cacheDir The path to restore the cache to.
* @param additionalCacheHash Addition to the generated cache hash.
* @param execute whether to execute promise or not.
*/
export async function restoreCaches(
cacheDir: string,
additionalCacheHash: string,
execute: boolean
): Promise<void> {
if (!execute) {
return
}
try {
if (isServer()) {
core.warning(
'Cache is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details'
)
return
}
const primaryKey = `qodana-${process.env['GITHUB_REF']}-${additionalCacheHash}`
const restoreKeys = [`qodana-${process.env['GITHUB_REF']}-`, `qodana-`]
try {
const cacheKey = await cache.restoreCache(
[cacheDir],
primaryKey,
restoreKeys
)
if (!cacheKey) {
core.info(
`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(
', '
)}`
)
return
}
core.info(`Cache restored from key: ${cacheKey}`)
} catch (error) {
core.warning(
`Failed to restore cache with key ${primaryKey} – ${
(error as Error).message
}`
)
}
} catch (error) {
core.warning(`Failed to download caches – ${(error as Error).message}`)
}
}
/**
* Check if the action is run on GHE.
*/
export function isServer(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
)
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'
}