Skip to content

Commit

Permalink
Merge pull request #366 from snyk-tech-services/feat/CompareProjects
Browse files Browse the repository at this point in the history
feat: compare and update branches
  • Loading branch information
mathild3r authored Oct 13, 2022
2 parents 312a53a + c343971 commit 7daee99
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/project/compare-branches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import debug = require("debug");
import { requestsManager } from "snyk-request-manager";
import { updateProject } from "../api/project";

export async function compareAndUpdateBranches(
requestManager: requestsManager,
snykDefaultBranch: string,
githubBranch: string,
projectId: string,
orgId: string,
): Promise<{projectUpdated:boolean}> {

let projectUpdated = false
try {

if (snykDefaultBranch != githubBranch) {
debug(`default branch update needed for project ${projectId}\n`);

await updateProject(requestManager, orgId, projectId, {branch: githubBranch});
projectUpdated = true
}

return { projectUpdated: projectUpdated }
} catch (e) {
throw new Error(
`Failed to update project ${projectId}. ERROR: ${e.message}`,
);
}
}
58 changes: 58 additions & 0 deletions test/lib/projects.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { requestsManager } from 'snyk-request-manager';
import { updateProject } from '../../src/lib';
import * as compareProject from '../../src/lib/project/compare-branches';

jest.unmock('snyk-request-manager');
jest.requireActual('snyk-request-manager');
Expand Down Expand Up @@ -56,3 +57,60 @@ describe('UpdateProject', () => {

})

describe('compareAndUpdateBranches', () => {
const requestManager = new requestsManager({
userAgentPrefix: 'snyk-api-import:tests',
});
afterAll(async () => {
jest.resetAllMocks()
}, 1000);

it('Update project branch', async () => {

jest.spyOn(requestManager, 'request').mockResolvedValue({ data: {
name: "test",
id: "af137b96-6966-46c1-826b-2e79ac49bbd9",
created: "2018-10-29T09:50:54.014Z",
origin: "github",
type: "maven",
readOnly: false,
testFrequency: "daily",
totalDependencies: 42,
issueCountsBySeverity: {},
imageId: "sha256:caf27325b298a6730837023a8a342699c8b7b388b8d",
imageTag: "latest",
imageBaseImage: "alpine:3",
imagePlatform: "linux/arm64",
imageCluster: "Production",
hostname: null,
remoteRepoUrl: "https://github.com/snyk/test.git",
lastTestedDate: "2019-02-05T08:54:07.704Z",
browseUrl: "https://app.snyk.io/org/4a18d42f-0706-4ad0-b127-24078731fbed/project/af137b96-6966-46c1-826b-2e79ac49bbd9",
importingUser: {},
isMonitored: false,
branch: "newDefaultBranch",
targetReference: null,
tags: [],
attributes: {},
remediation: {}
}, status: 200 })

const res = await compareProject.compareAndUpdateBranches(requestManager, "main", "newDefaultBranch","af137b96-6966-46c1-826b-2e79ac49bbxx", "af137b96-6966-46c1-826b-2e79ac49bbd9",);
expect(res.projectUpdated).toBeTruthy()

}, 5000);

it('Return ProjectNeededUpdate false if branches are the same', async () => {

const res = await compareProject.compareAndUpdateBranches(requestManager, "main", "main","af137b96-6966-46c1-826b-2e79ac49bbxx", "af137b96-6966-46c1-826b-2e79ac49bbd9");
expect(res.projectUpdated).toBeFalsy()

}, 5000);

it('throw if the api requests fails', async () => {

jest.spyOn(requestManager, 'request').mockResolvedValue({statusCode : 500, data: {}})
expect(async () => {await compareProject.compareAndUpdateBranches(requestManager, "main", "newBranch","af137b96-6966-46c1-826b-2e79ac49bbxx", "af137b96-6966-46c1-826b-2e79ac49bbd9")}).rejects.toThrowError('Expected a 200 response, instead received: {"data":{},"status":500}')
}, 5000);

})

0 comments on commit 7daee99

Please sign in to comment.