Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 'ensureLabelsAreCreated' by paginating API results #26

Merged

Conversation

jeramysoucy
Copy link
Contributor

We have > 100 labels in the security repo, and we were not paginating the call to retrieve them. Previously, this was working because the labels we wanted to apply to an issue (typically Kibana, snyk, and triage_needed) were within the first 100 results, but as new labels were created, some of the labels are now in the second page of results (specifically "triage_needed"). This caused ensureLabelsAreCreated to throw when attempting to create an existing label.

This PR adds pagination of the listLabelsForRepo API call.

Additionally, this PR replaces the deprecated snyk List all projects v1 API with the newer REST API. The v1 API EOL is December 22, 2023. See the official statement for more information.

@jeramysoucy jeramysoucy requested a review from watson December 13, 2023 18:19
@jeramysoucy jeramysoucy self-assigned this Dec 13, 2023
@jeramysoucy jeramysoucy marked this pull request as ready for review December 13, 2023 18:25
package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "@elastic/snyk-github-issue-creator",
"version": "2.1.1",
"version": "2.1.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@watson Is there anything else I need to do to support rolling out a new version?

Copy link
Contributor

@watson watson Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version number should be updated independently of the changes in a PR. So just revert any changes to this file in this PR. To prepare and publish a new version, do the following after this PR is merged:

# ensure you have the latest main checked out locally
git pull upstream main
# bump the patch version in package.json and create version commit + tag
npm version patch
# ensure the patch commit is pushed upstream
git push upstream main
# ensure the version tag is pushed upstream
git push upstream --tags
# publish the new version to npm
npm publish

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "@elastic/snyk-github-issue-creator",
"version": "2.1.1",
"version": "2.1.2",
Copy link
Contributor

@watson watson Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version number should be updated independently of the changes in a PR. So just revert any changes to this file in this PR. To prepare and publish a new version, do the following after this PR is merged:

# ensure you have the latest main checked out locally
git pull upstream main
# bump the patch version in package.json and create version commit + tag
npm version patch
# ensure the patch commit is pushed upstream
git push upstream main
# ensure the version tag is pushed upstream
git push upstream --tags
# publish the new version to npm
npm publish

@jeramysoucy jeramysoucy requested a review from watson December 14, 2023 14:30
Copy link
Contributor

@watson watson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed the code changes - just the package.json updated

Copy link
Contributor

@watson watson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found a single unused variable after the refactoring. Other than that it looks good 👍

Comment on lines 53 to 59
const response = await octokit.paginate(
await client.issues.listLabelsForRepo({
owner: ghOwner,
repo: ghRepo,
per_page: 100
})
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this way of passing the response as an argument to octokit.paginate in their docs 🤔 https://octokit.github.io/rest.js/v20#pagination

But I assume you tested it?

Copy link
Contributor Author

@jeramysoucy jeramysoucy Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works! However, I have updated the code to be more explicit in this case.

  const responseData = await octokit.paginate(
    await client.issues.listLabelsForRepo({
      owner: ghOwner,
      repo: ghRepo,
      per_page: 100
    }),
    (response) => response.data
  )

lib/snyk.js Outdated
Comment on lines 5 to 6
const baseUrl = 'https://snyk.io/api/v1'
const baseRestUrl = 'https://api.snyk.io'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, the old const isn't used anymore? Why not simply change its value to the new host?

Suggested change
const baseUrl = 'https://snyk.io/api/v1'
const baseRestUrl = 'https://api.snyk.io'
const baseUrl = 'https://api.snyk.io'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all v1 APIs are deprecated. It's still used in the issues and orgs functions. I will rename the const though, since we have 2 base paths now.

@jeramysoucy jeramysoucy merged commit f09fa8c into elastic:main Dec 14, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants