-
Notifications
You must be signed in to change notification settings - Fork 33
/
qodana.ts
126 lines (118 loc) · 3.24 KB
/
qodana.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
// noinspection JSUnusedGlobalSymbols
export const VERSION = '0.7.5'
export const EXECUTABLE = 'qodana'
export const FAIL_THRESHOLD_OUTPUT =
'The number of problems exceeds the failThreshold'
export const QODANA_SARIF_NAME = 'qodana.sarif.json'
// eslint-disable-next-line no-shadow -- shadowing is intentional here (ESLint bug)
export enum QodanaExitCode {
Success = 0,
FailThreshold = 255
}
/**
* Check if Qodana Docker image execution is successful.
* The codes are documented here: https://www.jetbrains.com/help/qodana/qodana-sarif-output.html#Invocations
* @param exitCode
*/
export function isExecutionSuccessful(exitCode: number): boolean {
return Object.values(QodanaExitCode).includes(exitCode)
}
/**
* The context of the current run – described in action.yaml.
*/
export interface Inputs {
args: string[]
resultsDir: string
cacheDir: string
additionalCacheHash: string
uploadResult: boolean
artifactName: string
useCaches: boolean
githubToken: string
useAnnotations: boolean
}
/**
* Gets Qodana CLI download URL from the GitHub Releases API.
*/
export function getQodanaCliUrl(): string {
const base = `https://github.com/JetBrains/qodana-cli/releases/download/v${VERSION}`
const arch = process.arch === 'x64' ? 'x86_64' : 'arm64'
const archive = process.platform === 'win32' ? 'zip' : 'tar.gz'
switch (process.platform) {
case 'darwin':
return `${base}/qodana_darwin_${arch}.${archive}`
case 'linux':
return `${base}/qodana_linux_${arch}.${archive}`
case 'win32':
return `${base}/qodana_windows_${arch}.${archive}`
default:
throw new Error(`Unsupported platform: ${process.platform}`)
}
}
/**
* Finds the wanted argument value in the given args, if there is one.
* @param argShort the short argument name.
* @param argLong the long argument name.
* @param args command arguments.
* @returns the arg value to use.
*/
export function extractArg(
argShort: string,
argLong: string,
args: string[]
): string {
let arg = ''
for (let i = 0; i < args.length; i++) {
if (args[i] === argShort || args[i] === argLong) {
arg = args[i + 1]
break
}
}
return arg
}
/**
* Builds the `qodana pull` command arguments.
* @returns The `qodana scan` command arguments.
* @param args additional CLI arguments.
*/
export function getQodanaPullArgs(args: string[]): string[] {
const pullArgs = ['pull']
const linter = extractArg('-l', '--linter', args)
if (linter) {
pullArgs.push('-l', linter)
}
const project = extractArg('-i', '--project-dir', args)
if (project) {
pullArgs.push('-i', project)
}
return pullArgs
}
/**
* Builds the `qodana scan` command arguments.
* @param args additional CLI arguments.
* @param resultsDir the directory to store the results.
* @param cacheDir the directory to store the cache.
* @param env QODANA_ENV value.
* @returns The `qodana scan` command arguments.
*/
export function getQodanaScanArgs(
args: string[],
resultsDir: string,
cacheDir: string,
env = `cli`
): string[] {
const cliArgs: string[] = [
'scan',
'--skip-pull',
'-e',
`QODANA_ENV=${env}:${VERSION}`,
'--cache-dir',
cacheDir,
'--results-dir',
resultsDir
]
if (args) {
cliArgs.push(...args)
}
return cliArgs
}