Skip to content

Commit

Permalink
Uses v1 single project API to retrieve browseUrl and imageTag. Remove…
Browse files Browse the repository at this point in the history
…s previous queryOrganization funtion.
  • Loading branch information
jeramysoucy committed Dec 27, 2023
1 parent a3318cb commit c250e28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/github/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 24 additions & 25 deletions lib/snyk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c250e28

Please sign in to comment.