From a7bddd83c130c0229740f01ad1866fc0b590f5d1 Mon Sep 17 00:00:00 2001 From: Mathilde Date: Tue, 11 Oct 2022 17:50:29 +0100 Subject: [PATCH] feat: add support for update project branch --- src/lib/api/project/index.ts | 45 +++++++++++++++ test/lib/__snapshots__/projects.test.ts.snap | 3 + test/lib/projects.test.ts | 58 ++++++++++++++++++++ test/system/projects.test.ts | 34 ++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 test/lib/__snapshots__/projects.test.ts.snap create mode 100644 test/lib/projects.test.ts create mode 100644 test/system/projects.test.ts diff --git a/src/lib/api/project/index.ts b/src/lib/api/project/index.ts index 9546334c..386d3165 100644 --- a/src/lib/api/project/index.ts +++ b/src/lib/api/project/index.ts @@ -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( @@ -56,3 +58,46 @@ export async function deleteProjects( throw err; } } + +export async function updateProject( + requestManager: requestsManager, + orgId: string, + projectId: string, + config: { branch: string}, +): Promise { + + 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; +} \ No newline at end of file diff --git a/test/lib/__snapshots__/projects.test.ts.snap b/test/lib/__snapshots__/projects.test.ts.snap new file mode 100644 index 00000000..84304128 --- /dev/null +++ b/test/lib/__snapshots__/projects.test.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`UpdateProject Update project branch 1`] = `"newDefaultBranch"`; diff --git a/test/lib/projects.test.ts b/test/lib/projects.test.ts new file mode 100644 index 00000000..8c078b61 --- /dev/null +++ b/test/lib/projects.test.ts @@ -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); + +}) + diff --git a/test/system/projects.test.ts b/test/system/projects.test.ts new file mode 100644 index 00000000..fe4efe1e --- /dev/null +++ b/test/system/projects.test.ts @@ -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); + +}) +