Skip to content

Commit 8e0d4d5

Browse files
Merge pull request diffblue#589 from diffblue/cleanup/gitlab-script
Cleanup and complete Gitlab script
2 parents 68281ea + 7bc26ab commit 8e0d4d5

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

gitlab-integration/gitlab-wrapper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
npm start --prefix "$( dirname "${BASH_SOURCE[0]}" )"
3+
npm start --silent --prefix "$( dirname "${BASH_SOURCE[0]}" )"

gitlab-integration/src/analyse-project.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function securityAnalysis() {
2121

2222
const log = new TwoColumnLogger("Diffblue Gitlab Integration");
2323
try {
24-
const projectName = `gitlab-${Date.now()}`;
24+
const projectName = `${process.env.CI_PROJECT_NAMESPACE}-${process.env.CI_PROJECT_NAME}-${process.env.CI_JOB_ID}`;
2525
let localMachine: models.Instance = {
2626
name: "local-executor",
2727
ip: "localhost",
@@ -40,51 +40,47 @@ async function securityAnalysis() {
4040
zoneIndex: 0,
4141
diskImg: { name: "nodiskimgname", link: "nodiskimglink" },
4242
username: "diffblue",
43+
password: "password123",
4344
duration: { start: "nodurationstart" },
4445
};
4546

47+
console.log(`Running against commit with title: ${process.env.CI_COMMIT_TITLE}`);
48+
console.log("Initialising Diffblue platform");
4649
const startTime = Date.now();
47-
4850
localMachine = await platform.createProject(localMachine);
4951
localMachine = await platform.initialiseProject(localMachine);
5052
localMachine = await platform.startAnalysis(localMachine);
53+
console.log("Analysis started");
5154

52-
do {
55+
let complete = 0;
56+
while (true) {
5357
localMachine = await platform.getAnalysisProgress(localMachine);
54-
if (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING") {
55-
const elapsed = (Date.now() - startTime) / 1000;
56-
const nextWait = Math.ceil(elapsed / 10); // Wait for 10% of time elapsed so far
57-
log.debug(`Waiting ${nextWait} seconds before checking again whether Diffblue platform is ready`);
58-
await wait(nextWait);
58+
if (localMachine.analysisProgress !== undefined && localMachine.analysisProgress !== complete) {
59+
complete = localMachine.analysisProgress;
60+
console.log(`Analysis ${complete.toFixed(2)}% complete`);
5961
}
60-
} while (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING");
62+
if (localMachine.analysisStatus !== "RUNNING" && localMachine.analysisStatus !== "WAITING")
63+
break;
64+
const elapsed = (Date.now() - startTime) / 1000;
65+
const nextWait = Math.ceil(elapsed / 10); // Wait for 10% of time elapsed so far
66+
log.debug(`Waiting ${nextWait} seconds before checking again whether Diffblue platform is ready`);
67+
await wait(nextWait);
68+
}
6169

6270
const issues = await platform.getIssues(localMachine);
71+
const issueLogLines = issues.map((issue: any) => `* ${issue.testName}\n Trace: ${getIssueUri(localMachine, issue.id)}`);
72+
console.log(`Identified ${issues.length} issues:\n${issueLogLines.join("\n")}`);
6373

64-
log.debug(`Identified ${issues.length} issues`);
65-
log.debug(JSON.stringify(issues));
66-
67-
const report = issues.map(
68-
function (issue: any) {
69-
70-
// TODO: query the platform's public-facing URL
71-
const testUrl = `http://localhost/${projectName}/${localMachine.analysisNum}/tests/${issue.id}/trace`;
72-
73-
return {
74-
file: `${issue.testClassDir}/${issue.testClass}`,
75-
message: issue.testName,
76-
priority: "High",
77-
tool: "Diffblue-security",
78-
url: testUrl,
79-
cve: `Click here ${testUrl}`,
80-
// cve: testUrl // Mandatory key
81-
};
82-
}
83-
);
84-
85-
const reportFileName = path.join(process.env.INIT_CWD, "gl-sast-report.json");
86-
log.debug(`Writing issue report to ${reportFileName}`);
87-
await fs.writeFile(reportFileName, JSON.stringify(report));
74+
const report = issues.map((issue: any) => ({
75+
file: `${issue.testClassDir}/${issue.testClass}`,
76+
message: issue.testName,
77+
priority: "High",
78+
tool: "Diffblue security analyser",
79+
// TODO: query the platform's public-facing URL
80+
url: getIssueUri(localMachine, issue.id),
81+
cve: "N/A", // Mandatory key when provide URL
82+
}));
83+
await fs.writeFile(path.join(process.env.INIT_CWD, "gl-sast-report.json"), JSON.stringify(report));
8884
} catch (err) {
8985
if (err.message) {
9086
log.debug(err.message);
@@ -97,4 +93,9 @@ async function securityAnalysis() {
9793
}
9894
}
9995

96+
function getIssueUri(localMachine: models.Instance, issueId: any): string {
97+
return `http://localhost/${localMachine.project.name}/${localMachine.analysisNum}/tests/${issueId}/trace`;
98+
}
99+
100+
// Run async entry point
100101
securityAnalysis();

gitlab-integration/src/api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export async function createUser(vm: models.Instance, secsToWait = 30, secsWaiti
134134
let response;
135135
try {
136136
response = await fetch(`http://${vm.ip}/api/users`, {
137-
method: "POST", body: JSON.stringify({ username: vm.username, password: vm.project.name }), headers: { "Content-Type": "application/json" },
137+
method: "POST", body: JSON.stringify({ username: vm.username, password: vm.password }), headers: { "Content-Type": "application/json" },
138138
});
139139
if (response.status !== HttpStatus.OK && response.status !== HttpStatus.BadRequest)
140140
throw new Error(`received ${response.status} (${response.statusText}) response`);
@@ -161,7 +161,7 @@ async function signIn(vm: models.Instance, secsToWait = 30, secsWaiting = 0): Pr
161161
let response;
162162
try {
163163
response = await fetch(`http://${vm.ip}/api/session`, {
164-
method: "POST", body: JSON.stringify({ userName: vm.username, password: "password123" }), headers: { "Content-Type": "application/json" },
164+
method: "POST", body: JSON.stringify({ userName: vm.username, password: vm.password }), headers: { "Content-Type": "application/json" },
165165
});
166166
if (response.status !== HttpStatus.OK && response.status !== HttpStatus.Unauthorized)
167167
throw new Error(`received ${response.status} (${response.statusText}) response`);
@@ -191,7 +191,7 @@ async function signInToken(vm: models.Instance, secsToWait = 30, secsWaiting = 0
191191
let response;
192192
try {
193193
response = await fetch(`http://${vm.ip}/api/signin`, {
194-
method: "POST", body: JSON.stringify({ userName: vm.username, password: vm.project.name }), headers: { "Content-Type": "application/json" },
194+
method: "POST", body: JSON.stringify({ userName: vm.username, password: vm.password }), headers: { "Content-Type": "application/json" },
195195
});
196196
if (response.status !== HttpStatus.OK && response.status !== HttpStatus.Unauthorized)
197197
throw new Error(`received ${response.status} (${response.statusText}) response`);

gitlab-integration/src/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export interface Instance {
9393
project: Project;
9494
ssh?: any;
9595
username: string;
96+
password: string;
9697
}
9798

9899
export interface Project {

0 commit comments

Comments
 (0)