Skip to content

Commit

Permalink
feat: add support for update project branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mathild3r committed Oct 11, 2022
1 parent 2fcb103 commit a7bddd8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/lib/api/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as needle from 'needle';
import * as debugLib from 'debug';
import { getApiToken } from '../../get-api-token';
import { getSnykHost } from '../../get-snyk-host';
import { requestsManager } from 'snyk-request-manager';
import { SnykProject } from '../../types';
const debug = debugLib('snyk:api-import');

export async function deleteProjects(
Expand Down Expand Up @@ -56,3 +58,46 @@ export async function deleteProjects(
throw err;
}
}

export async function updateProject(
requestManager: requestsManager,
orgId: string,
projectId: string,
config: { branch: string},
): Promise<SnykProject> {

getApiToken();
getSnykHost();
debug('Update project: ' + projectId);

if (!orgId || !projectId) {
throw new Error(
`Missing required parameters. Please ensure you have set: orgId and projectId.
\nFor more information see: https://snyk.docs.apiary.io/reference/projects/individual-project/update-a-project`,
);
}

const body = {
branch: config.branch,
};

const url = `/org/${orgId.trim()}/project/${projectId.trim()}`
const res = await requestManager.request({
verb: 'put',
url: url,
body: JSON.stringify(body),
useRESTApi: false,
});

const statusCode = res.statusCode || res.status;
if (!statusCode || statusCode !== 200) {
throw new Error(
'Expected a 200 response, instead received: ' +
JSON.stringify({ data: res.data, status: statusCode }),
);
}

const updatedProject: SnykProject = res.data

return updatedProject;
}
3 changes: 3 additions & 0 deletions test/lib/__snapshots__/projects.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UpdateProject Update project branch 1`] = `"newDefaultBranch"`;
58 changes: 58 additions & 0 deletions test/lib/projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { requestsManager } from 'snyk-request-manager';
import { updateProject } from '../../src/lib';

jest.unmock('snyk-request-manager');
jest.requireActual('snyk-request-manager');

describe('UpdateProject', () => {
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 updateProject(requestManager, "af137b96-6966-46c1-826b-2e79ac49bbxx", "af137b96-6966-46c1-826b-2e79ac49bbd9", {branch: "newDefaultBranch"});
expect(res.branch).toMatchSnapshot()

}, 5000);

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

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

}, 5000);

})

34 changes: 34 additions & 0 deletions test/system/projects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { requestsManager } from 'snyk-request-manager';
import { updateProject } from '../../src/lib';

const SNYK_API_TEST = process.env.SNYK_API_TEST as string;
const PROJECT_ID = process.env.TEST_PROJECT_ID as string;
const ORG_ID = process.env.TEST_ORG_ID as string;

jest.unmock('snyk-request-manager');
jest.requireActual('snyk-request-manager');


describe('UpdateProject - e2e', () => {
const OLD_ENV = process.env;
process.env.SNYK_API = SNYK_API_TEST;
process.env.SNYK_TOKEN = process.env.SNYK_TOKEN_TEST;
const requestManager = new requestsManager({
userAgentPrefix: 'snyk-api-import:tests',
});
afterAll(async () => {
process.env = { ...OLD_ENV };
}, 1000);

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

let res = await updateProject(requestManager, ORG_ID, PROJECT_ID, {branch: "newDefaultBranch"});
expect(res.branch).toEqual("newDefaultBranch")

res = await updateProject(requestManager, ORG_ID, PROJECT_ID, {branch: "main"});
expect(res.branch).toEqual("main")

}, 5000);

})

0 comments on commit a7bddd8

Please sign in to comment.