From c250e2868253bcbdf79829670bd3b0459c5b7f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Wed, 27 Dec 2023 14:01:15 -0500 Subject: [PATCH] Uses v1 single project API to retrieve browseUrl and imageTag. Removes previous queryOrganization funtion. --- lib/github/batch.js | 4 ++-- lib/snyk.js | 49 ++++++++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/github/batch.js b/lib/github/batch.js index 492554b..9918356 100644 --- a/lib/github/batch.js +++ b/lib/github/batch.js @@ -35,8 +35,8 @@ module.exports = async (issues) => { const headerText = 'This issue has been created automatically by [snyk-github-issue-creator](https://github.com/elastic/snyk-github-issue-creator).\r\n\r\nSnyk project(s):' const projectText = projects .map( - ({ name, browseUrl }) => - `\r\n * [\`${name}\`](${browseUrl})` + ({ name, browseUrl, imageTag }) => + `\r\n * [\`${name}\`](${browseUrl}) (manifest version ${imageTag})` ) .join('') const sectionText = Object.keys(sevMap) diff --git a/lib/snyk.js b/lib/snyk.js index 9f2adff..06173c1 100644 --- a/lib/snyk.js +++ b/lib/snyk.js @@ -30,48 +30,47 @@ module.exports = class Snyk { ).orgs } - async queryOrgInfo (organizationId) { + async queryProjectDetails (organizationId, projectId) { try { - const response = await request({ + return await request({ method: 'get', - url: `${baseRestUrl}/orgs/${organizationId}?version=2023-11-27`, + url: `${baseV1Url}/org/${organizationId}/project/${projectId}`, // project snapshot via v1 POST org/:orgId/project/:projectId/history headers: this._headers, json: true }) - - if (response.data === undefined || response.data.attributes === undefined || response.data.attributes.name === undefined) { - throw new Error('expected response to include data.attributes.name') - } - - return response.data } catch (err) { - throw new Error(dressError(err, `Failed to query snyk organization with id ${organizationId}`)) + throw new Error(dressError(err, `Failed to query snyk project details. Organization ID: ${organizationId}, Project ID: ${projectId}`)) } } async projects (orgId, selectedProjects = []) { const organizationId = orgId || this._orgId - const organization = await this.queryOrgInfo(organizationId) - const responseData = await paginateRestResponseData( `${baseRestUrl}/orgs/${organizationId}/projects?version=2023-11-27&meta.latest_issue_counts=true&limit=20`, this._headers ) - return responseData.map((project) => { - const { critical, high, medium, low } = project.meta.latest_issue_counts - const issueCountTotal = critical + high + medium + low - - return { - id: project.id, - name: project.attributes.name, - isMonitored: - project.attributes.status === 'active', - issueCountTotal, - browseUrl: `https://app.snyk.io/org/${organization.attributes.name.toLowerCase()}/project/${project.id}` - } - }).filter(({ id, isMonitored }) => { + const projects = await Promise.all( + responseData.map(async (project) => { + const { critical, high, medium, low } = project.meta.latest_issue_counts + const issueCountTotal = critical + high + medium + low + + const projectDetails = await this.queryProjectDetails(organizationId, project.id) + + return { + id: project.id, + name: project.attributes.name, + isMonitored: + project.attributes.status === 'active', + issueCountTotal, + browseUrl: projectDetails.browseUrl, + imageTag: projectDetails.imageTag + } + }) + ) + + return projects.filter(({ id, isMonitored }) => { if (selectedProjects.includes(id)) { return true }