Skip to content

Commit

Permalink
feat: Add contributing devs to share results
Browse files Browse the repository at this point in the history
This commit utilises the existing 'getContributors()' function to count the developers of the repo we scan.
It returns the emails and the lastCommitDate for each one of the commits in the last 90 days.
We then send this as part of the body to '/iac-share-cli-results'.

[CFG-1518]
  • Loading branch information
Ilianna Papastefanou committed Mar 21, 2022
1 parent 3be2c7d commit 8ac653a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/snyk-fix/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { CustomError } from './lib/errors/custom-error';
* what should be scanned for issues
*/
export interface GitTarget {
remoteUrl: string;
branch: string;
remoteUrl?: string;
branch?: string;
}
export interface ContainerTarget {
image: string;
Expand Down
8 changes: 1 addition & 7 deletions src/cli/commands/test/iac-local-execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import {
IacFileInDirectory,
Options,
TestOptions,
PolicyOptions, Contributor,
PolicyOptions,
} from '../../../../lib/types';
import {GitTarget} from "../../../../lib/ecosystems/types";

export interface IacFileData extends IacFileInDirectory {
fileContent: string;
Expand Down Expand Up @@ -68,11 +67,6 @@ export interface IacShareResultsFormat {
violatedPolicies: PolicyMetadata[];
}

export interface GitInfo{
gitTarget: GitTarget;
contributors: Contributor[];
}

// This type is the integration point with the CLI test command, please note it is still partial in the experimental version
export type FormattedResult = {
result: {
Expand Down
22 changes: 13 additions & 9 deletions src/lib/iac/cli-share-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import { AuthFailedError } from '../errors/authentication-failed-error';
import { Policy } from '../policy/find-and-load-policy';
import { getInfo } from '../project-metadata/target-builders/git';
import { GitTarget } from '../ecosystems/types';
import {Contributor} from "../types";
import * as analytics from "../analytics";
import {getContributors} from "../monitor/dev-count-analysis";
import * as debug from "debug";
import { Contributor } from '../types';
import * as analytics from '../analytics';
import { getContributors } from '../monitor/dev-count-analysis';
import * as Debug from 'debug';
const debug = Debug('iac-cli-share-results');

export async function shareResults(
results: IacShareResultsFormat[],
policy: Policy | undefined,
): Promise<Record<string, string>> {
const gitTarget = (await getInfo(false)) as GitTarget;
const scanResults = results.map((result) =>
convertIacResultToScanResult(result, policy, gitTarget),
);

let contributors: Contributor[] = [];
if (gitTarget.remoteUrl) {
if (analytics.allowAnalytics()) {
Expand All @@ -27,18 +32,17 @@ export async function shareResults(
}
}
}
const scanResults = results.map((result) => {
convertIacResultToScanResult(result, policy, {gitTarget, contributors});
});

const { res, body } = await makeRequest({
method: 'POST',
url: `${config.API}/iac-cli-share-results`,
json: true,
headers: {
authorization: getAuthHeader(),
},
body: scanResults,
body: {
scanResults,
contributors,
},
});

if (res.statusCode === 401) {
Expand Down
12 changes: 5 additions & 7 deletions src/lib/iac/envelope-formatters.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import {
GitInfo,
IacShareResultsFormat,
PolicyMetadata,
} from '../../cli/commands/test/iac-local-execution/types';
import { GitTarget, ScanResult } from '../ecosystems/types';
import { Policy } from '../policy/find-and-load-policy';

export function convertIacResultToScanResult(
iacResult: IacShareResultsFormat,
policy: Policy | undefined,
gitInfo: GitInfo,
iacResult: IacShareResultsFormat,
policy: Policy | undefined,
gitTarget: GitTarget,
): ScanResult {
return {
identity: {
type: iacResult.projectType,
targetFile: iacResult.targetFile,
},
// TODO: should the contributors be under facts or under a new property?
facts: [],
findings: iacResult.violatedPolicies.map((policy: PolicyMetadata) => {
return {
Expand All @@ -26,9 +24,9 @@ export function convertIacResultToScanResult(
}),
name: iacResult.projectName,
target:
Object.keys(gitInfo.gitTarget).length === 0
Object.keys(gitTarget).length === 0
? { name: iacResult.projectName }
: { ...gitInfo.gitTarget, branch: 'master' },
: { ...gitTarget, branch: 'master' },
policy: policy?.toString() ?? '',
};
}
34 changes: 19 additions & 15 deletions test/jest/acceptance/iac/cli-share-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,27 @@ describe('CLI Share Results', () => {
.filter((request) => request.url?.includes('/iac-cli-share-results'));

expect(testRequests.length).toEqual(1);

expect(testRequests[0]).toMatchObject({
body: [
{
identity: {
type: 'armconfig',
targetFile: './iac/arm/rule_test.json',
},
facts: [],
findings: expect.arrayContaining([]),
name: 'arm',
target: {
expect(testRequests[0].body).toEqual(
expect.objectContaining({
contributors: expect.any(Array),
scanResults: [
{
identity: {
type: 'armconfig',
targetFile: './iac/arm/rule_test.json',
},
facts: [],
findings: expect.any(Array),
policy: '',
name: 'arm',
target: {
branch: 'master',
remoteUrl: 'http://github.com/snyk/cli.git',
},
},
},
],
});
],
}),
);
});
});
});

0 comments on commit 8ac653a

Please sign in to comment.