From 7e5775cd086b2292b9e9d9069d4b7b5a8196a871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cjeramysoucy=E2=80=9D?= Date: Wed, 13 Dec 2023 13:07:25 -0500 Subject: [PATCH] Fixes pagination of repo labels. Replaces deprecated snyk v1 projects api with new rest api. --- lib/github/index.js | 2 +- lib/github/labels.js | 16 +++++++----- lib/snyk.js | 61 +++++++++++++++++++++++++++++++------------- package.json | 4 +-- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/lib/github/index.js b/lib/github/index.js index 47d9297..4485f1c 100644 --- a/lib/github/index.js +++ b/lib/github/index.js @@ -42,7 +42,7 @@ module.exports = { let ghUpdatedIssues = [] if (issues.length > 0) { - await ensureLabelsAreCreated(this.client, conf.ghOwner, conf.ghRepo, issues) + await ensureLabelsAreCreated(octokit, this.client, conf.ghOwner, conf.ghRepo, issues) if (conf.batch) { ghNewIssues = [await this.createIssue({ diff --git a/lib/github/labels.js b/lib/github/labels.js index d342a4b..c4bd22f 100644 --- a/lib/github/labels.js +++ b/lib/github/labels.js @@ -48,14 +48,16 @@ const getLabelAttributes = (name) => { return { name, ...(LABELS[name] || DEFAULT_LABEL) } } -const ensureLabelsAreCreated = async (client, ghOwner, ghRepo, issues) => { +const ensureLabelsAreCreated = async (octokit, client, ghOwner, ghRepo, issues) => { const labels = getLabels(issues) - const response = await client.issues.listLabelsForRepo({ - owner: ghOwner, - repo: ghRepo, - per_page: 100 - }) - const currentLabels = response.data.map((x) => x.name) + const response = await octokit.paginate( + await client.issues.listLabelsForRepo({ + owner: ghOwner, + repo: ghRepo, + per_page: 100 + }) + ) + const currentLabels = response.map((x) => x.name) const labelsToCreate = labels.filter((x) => !currentLabels.includes(x)) if (!labelsToCreate.length || conf.dryRun) { return diff --git a/lib/snyk.js b/lib/snyk.js index 4293cc0..acc72b0 100644 --- a/lib/snyk.js +++ b/lib/snyk.js @@ -3,6 +3,7 @@ const request = require('request-promise-native') const baseUrl = 'https://snyk.io/api/v1' +const baseRestUrl = 'https://api.snyk.io' module.exports = class Snyk { constructor ({ token, orgId, minimumSeverity }) { @@ -28,25 +29,32 @@ module.exports = class Snyk { ).orgs } - async projects (orgId, selectedProjects = []) { - const { projects } = await request({ - url: `${baseUrl}/org/${orgId || this._orgId}/projects`, - headers: this._headers, - json: true + async projects(orgId, selectedProjects = []) { + const organizationId = orgId || this._orgId + + const response = await paginateResponseData( + `${baseRestUrl}/rest/orgs/${organizationId}/projects?version=2023-11-27&meta.latest_issue_counts=true&limit=20`, + this._headers + ) + + return response.map((project) => { + const { latest_issue_counts } = project.meta + const { critical, high, medium, low } = latest_issue_counts + const issueCountTotal = critical + high + medium + low + return { + id: project.id, + name: project.attributes.name, + isMonitored: + project.attributes.status === "active", + issueCountTotal + } + }) + .filter(({ id, isMonitored, issueCountTotal }) => { + if (selectedProjects.includes(id)) { + return true + } + return isMonitored }) - return projects - .map((project) => { - const { issueCountsBySeverity } = project - const { critical, high, medium, low } = issueCountsBySeverity - const issueCountTotal = critical + high + medium + low - return { ...project, issueCountTotal } - }) - .filter(({ id, isMonitored, issueCountTotal }) => { - if (selectedProjects.includes(id)) { - return true - } - return isMonitored - }) } async issues (projectId) { @@ -91,3 +99,20 @@ function getSeverities (minimumSeverity) { } return ['critical', 'high', 'medium', 'low'] } + +async function paginateResponseData(url, headers, method = 'get') { + const reponseData = [] + do { + let response = await request({ + method, + url, + headers, + json: true + }) + reponseData.push(...response.data) + if (response.links.next) url = baseRestUrl + response.links.next + else url = undefined + } while (url) + + return reponseData +} diff --git a/package.json b/package.json index cad25a0..100fe6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@elastic/snyk-github-issue-creator", - "version": "2.1.1", + "version": "2.1.2", "description": "A CLI for creating GitHub issues based on vulnerabilities from your Snyk projects", "main": "lib/index.js", "scripts": { @@ -34,4 +34,4 @@ "standard": "^17.0.0", "tape": "^5.0.1" } -} +} \ No newline at end of file