Skip to content

Commit

Permalink
Fixes pagination of repo labels. Replaces deprecated snyk v1 projects…
Browse files Browse the repository at this point in the history
… api with new rest api.
  • Loading branch information
jeramysoucy committed Dec 13, 2023
1 parent a524487 commit 7e5775c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
16 changes: 9 additions & 7 deletions lib/github/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 43 additions & 18 deletions lib/snyk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -34,4 +34,4 @@
"standard": "^17.0.0",
"tape": "^5.0.1"
}
}
}

0 comments on commit 7e5775c

Please sign in to comment.