Skip to content

Commit

Permalink
add get scan event
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Apr 4, 2024
1 parent 0baa7d2 commit ba77762
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 20 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Add jfrog-client-js as a dependency to your package.json file:
- [Sending Log Message Event](#sending-log-mesage-event)
- [Sending Start Scan Event](#sending-start-scan-event)
- [Sending End Scan Event](#sending-end-scan-event)
- [Getting Scan Event Details](#getting-scan-event-details)

### Setting up JFrog client

Expand Down Expand Up @@ -412,3 +413,21 @@ jfrogClient
console.error(error);
});
```

##### Getting Scan Event Details

```javascript
const multiScanId = XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX // UUID
jfrogClient
.xsc()
.event()
.getScanEvent(multiScanId)
.then((result) => {
...
})
.catch((error) => {
...
});
```

Please note that you need to replace 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' with the actual multi scan ID that you've generated with `startScan`.
5 changes: 4 additions & 1 deletion model/Xsc/Event/ScanEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ScanEventStatus } from './index';

export interface ScanEvent {
export interface ScanEvent extends ScanEventEndData {
multi_scan_id: string;
}

export interface ScanEventEndData {
event_status?: ScanEventStatus;
total_findings?: number;
total_ignored_findings?: number;
Expand Down
4 changes: 4 additions & 0 deletions model/Xsc/Event/ScanEventResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ScanEventEndData } from './ScanEvent';
import { StartScanRequest } from './StartScanRequest';

export interface ScanEventResponse extends StartScanRequest, ScanEventEndData {}
1 change: 1 addition & 0 deletions model/Xsc/Event/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { ScanEvent } from './ScanEvent';
export { StartScanRequest } from './StartScanRequest';
export { ScanEventResponse } from './ScanEventResponse';
export { XscLog } from './XscLog';

export enum ScanEventStatus {
Expand Down
2 changes: 1 addition & 1 deletion src/Xray/XrayClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class XrayClient {
}

public scan(): XrayScanClient {
return new XrayScanClient(this.httpClient, this.logger);
return new XrayScanClient(this.httpClient, false, this.logger);
}

public entitlements(): XrayEntitlementsClient {
Expand Down
31 changes: 18 additions & 13 deletions src/Xray/XrayScanClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ export class XrayScanClient {
private static readonly SLEEP_INTERVAL_MILLISECONDS: number = 5000;
private static readonly MAX_ATTEMPTS: number = 60;

constructor(private readonly httpClient: HttpClient, private readonly logger: ILogger) {}
constructor(
private readonly httpClient: HttpClient,
private readonly xsc: boolean,
private readonly logger: ILogger
) {}

public async graph(
request: IGraphRequestModel,
progress: XrayScanProgress,
checkCanceled: () => void,
projectKey: string | undefined,
watches: string[] | undefined,
multiScanId: string | undefined,
technologies: string[] | undefined,
multiScanId?: string,
technologies?: string[],
sleepIntervalMilliseconds: number = XrayScanClient.SLEEP_INTERVAL_MILLISECONDS
): Promise<IGraphResponse> {
try {
Expand All @@ -39,7 +43,6 @@ export class XrayScanClient {
progress,
checkCanceled,
(!projectKey || projectKey.length === 0) && (!watches || watches.length === 0),
multiScanId !== undefined && multiScanId.length > 0,
sleepIntervalMilliseconds
);
} finally {
Expand Down Expand Up @@ -88,21 +91,25 @@ export class XrayScanClient {
}

/**
* Get URL for "POST api/v1/scan/graph".
* If no project key provided - api/v1/scan/graph
* If project key was provided - api/v1/scan/graph?project=<projectKey>
* If watches provided - api/v1/scan/graph?watch=<watch-1>&watch=<watch-2>
* Get URL for "POST scan/graph" (Xray: api/v1/scan/graph, XSC: api/v1/sca/scan/graph).
* If no project key provided - /scan/graph
* If project key was provided - /scan/graph?project=<projectKey>
* If watches provided - /scan/graph?watch=<watch-1>&watch=<watch-2>
* If multiScanId provided - /scan/graph?multi_scan_id=<multiScanId>
* If technologies provided - /scan/graph?tech=<tech-1>&tech=<tech-2>
* @param projectKey - Project key or undefined
* @param watches - List of Watches or undefined
* @returns URL for "POST api/v1/scan/graph"
* @param multiScanId - Multi scan ID or undefined
* @param technologies - List of technologies or undefined
* @returns URL for "POST /scan/graph"
*/
private getUrl(
projectKey: string | undefined,
watches?: string[],
multiScanId?: string,
technologies?: string[]
): string {
let url: string = XrayScanClient.scanGraphEndpoint;
let url: string = this.xsc ? XrayScanClient.xscScanGraphEndpoint : XrayScanClient.scanGraphEndpoint;
let params: string[] = [];

if (projectKey && projectKey.length > 0) {
Expand All @@ -112,7 +119,6 @@ export class XrayScanClient {
}
if (multiScanId) {
params.push(`multi_scan_id=${multiScanId}`);
url = XrayScanClient.xscScanGraphEndpoint;
}
if (technologies && technologies.length > 0) {
params.push(`tech=${technologies.join('&tech=')}`);
Expand Down Expand Up @@ -140,10 +146,9 @@ export class XrayScanClient {
progress: XrayScanProgress,
checkCanceled: () => void,
includeVulnerabilities: boolean,
xsc: boolean,
sleepIntervalMilliseconds: number
): Promise<IGraphResponse> {
const scanGraphUrl: string = xsc
const scanGraphUrl: string = this.xsc
? XrayScanClient.xscScanGraphEndpoint
: XrayScanClient.scanGraphEndpoint +
'/' +
Expand Down
2 changes: 1 addition & 1 deletion src/Xsc/XscClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class XscClient {
}

public scan(): XrayScanClient {
return new XrayScanClient(this.httpClient, this.logger);
return new XrayScanClient(this.httpClient, true, this.logger);
}

public event(): XscEventClient {
Expand Down
13 changes: 13 additions & 0 deletions src/Xsc/XscEventClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
StartScanRequest,
ScanEvent,
} from '../../model/';
import { ScanEventResponse } from './Event/ScanEventResponse';

export class XscEventClient {
static readonly eventEndpoint: string = 'api/v1/event';
Expand Down Expand Up @@ -86,4 +87,16 @@ export class XscEventClient {
return false;
}
}

public async getScanEvent(multiScanId: string): Promise<ScanEventResponse> {
this.logger.debug(`Sending GET event/${multiScanId} request...`);
const requestParams: IRequestParams = {
url: XscEventClient.eventEndpoint + '/' + multiScanId,
method: 'GET',
validateStatus: (status: number) => status === 200,
};
return await (
await this.httpClient.doAuthRequest(requestParams)
).data;
}
}
22 changes: 20 additions & 2 deletions test/tests/Xray/XrayScanClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ describe('Scan graph tests', () => {
await client
.xray()
.scan()
.graph({ component_id: 'engine' } as IGraphRequestModel, progress, () => undefined, '', [], 10);
.graph(
{ component_id: 'engine' } as IGraphRequestModel,
progress,
() => undefined,
'',
[],
undefined,
undefined,
10
);
expect(scope.isDone()).toBeTruthy();
expect(progress.lastPercentage).toBe(100);
});
Expand All @@ -139,7 +148,16 @@ describe('Scan graph tests', () => {
await client
.xray()
.scan()
.graph({ component_id: 'engine' } as IGraphRequestModel, progress, () => undefined, '', [], 10);
.graph(
{ component_id: 'engine' } as IGraphRequestModel,
progress,
() => undefined,
'',
[],
undefined,
undefined,
10
);
}).rejects.toThrow(`Xray get scan graph exceeded the timeout.`);
expect(progress.lastPercentage).toBe(100);
});
Expand Down
28 changes: 26 additions & 2 deletions test/tests/Xsc/XscEventClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import nock from 'nock';
import * as os from 'os';
import { JfrogClient } from '../../../src/JfrogClient';
import { TestUtils } from '../../TestUtils';
import { IJfrogClientConfig, ScanEvent, ScanEventStatus, XscLog } from '../../../model';
import {
IJfrogClientConfig,
ScanEvent,
ScanEventStatus,
ScanEventType,
StartScanRequest,
ScanEventResponse,
XscLog,
} from '../../../model';

describe('Xsc Events tests', () => {
describe.only('Xsc Events tests', () => {
const clientConfig: IJfrogClientConfig = TestUtils.getJfrogClientConfig();
const jfrogClient: JfrogClient = new JfrogClient(clientConfig);

Expand All @@ -29,6 +37,13 @@ describe('Xsc Events tests', () => {
if (await shouldSkipTest()) {
return;
}
let testEvent: StartScanRequest = {
product: 'jfrog-client-js tests',
os_platform: os.platform(),
os_architecture: os.arch(),
event_type: ScanEventType.SourceCode,
event_status: ScanEventStatus.Started,
};
// Start scan
const res: ScanEvent = await jfrogClient
.xsc()
Expand All @@ -37,9 +52,18 @@ describe('Xsc Events tests', () => {
expect(res).toBeTruthy();
expect(isValidUUID(res.multi_scan_id)).toBeTruthy();

// Get scan information
let response: ScanEventResponse = await jfrogClient.xsc().event().getScanEvent(res.multi_scan_id);
expect(response).toBeTruthy();
expect(response).toStrictEqual(testEvent);

// End scan
res.event_status = ScanEventStatus.Completed;
expect(await jfrogClient.xsc().event().endScan(res)).toBeTruthy();

response = await jfrogClient.xsc().event().getScanEvent(res.multi_scan_id);
expect(response).toBeTruthy();
expect(response).toStrictEqual({ ...testEvent, event_status: ScanEventStatus.Completed });
});

async function shouldSkipTest(): Promise<boolean> {
Expand Down

0 comments on commit ba77762

Please sign in to comment.