-
Notifications
You must be signed in to change notification settings - Fork 2
/
sonarqube-qualitygate-check-cli.js
executable file
·69 lines (60 loc) · 2.57 KB
/
sonarqube-qualitygate-check-cli.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
#!/usr/bin/node
const {promisify} = require('util');
const fs = require("fs");
const path = require("path");
const fsExists = promisify(fs.exists);
const check = require("./sonarqube-qualitygate-check");
(async () => {
try {
const workdir = process.env["WORKDIR"] || process.cwd();
const ceTaskFile = path.resolve(workdir, process.env["CE_TASK_FILE"] || "ce_task.json");
const qualitygateProjectStatusFile = path.resolve(workdir, process.env["QUALITYGATE_PROJECT_STATUS_FILE"] || "qualitygate_project_status.json");
if (! await fsExists(workdir)) {
process.stderr.write(`error: Work directory (${workdir}) does not exist. Did you set WORKDIR correctly?\n`)
process.exit(4);
}
process.stdout.write(`Work directory has been set to: "${workdir}".\n`);
if (! await fsExists(ceTaskFile)) {
process.stderr.write(`error: SonarQube Compute Engine result (${ceTaskFile} does not exist. Did you set WORKDIR and CE_TASK_FILE correctly?\n`);
process.exit(5);
}
if (! await fsExists(qualitygateProjectStatusFile)) {
process.stderr.write(`error: SonarQube QualityGate report (${qualitgateProjectStatusFile} does not exist. Did you set WORKDIR and QUALITYGATE_PROJECT_STATUS_FILE correctly?\n`);
process.exit(21);
}
try {
const ceTask = require(ceTaskFile);
switch (ceTask.task.status) {
case "PENDING":
process.stderr.write(`error: CE Task status is still "PENDING". Can't check quality gate status.\n`)
process.exit(6);
break;
case "IN_PROGRESS":
process.stderr.write(`error: CE Task status is still "IN_PROGRESS". Can't check quality gate status.\n`)
process.exit(7);
break
case "SUCCESS":
break;
default:
process.stderr.write(`error: SonarQube Compute Engine reported unknown status: ${ceTask.task.status}.\n`)
process.exit(99)
break;
}
const qualityGateProjectStatus = require(qualitygateProjectStatusFile);
const result = await check(ceTask, qualityGateProjectStatus);
if (! result.checkSuccessful) {
process.stderr.write(`error: ${result.message}\n`)
process.exit(result.code);
} else {
process.stdout.write(`==> ${result.message}`);
process.exit(result.code);
}
} catch (e) {
process.stderr.write(`An unexpected error occured: ${e.toString()}\n`);
process.exit(199);
}
} catch (e) {
process.stdout.write(`error: An unexpected error occured: ${e.toString()}\n`);
process.exit(1);
}
})();