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

fix(github): handle paginated response #1589

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

boonware
Copy link

Issue

spinnaker/spinnaker#6768

Description

  • Current code does not handle a paginated response from GitHub API. If the user is a member of more the 30 GitHub orgs (the default page size), and the target org is not contained in the first page, then the org membership check will fail.
  • This change handles paginated response by parsing out the link to the next page in the Link header, for example: '<https://github.com/api/v3/users/1234/orgs?page=2>; rel="next", <https://github.com/api/v3/users/1234/orgs?page=3>; rel="last"'.
  • Unit test suite is included. In order to make the code testable, the private modifier was removed from the instance variable restTemplate to allow dependency mocking. A better design would inject this dependency, but this is outside the scope of the current issue.

@boonware
Copy link
Author

@dbyron-sf Could I please get a build approved? Thank you.

@mattgogerly
Copy link
Member

Hi @boonware, could you update your commit message to fix(github) and I can approve the workflow.

@boonware
Copy link
Author

@dbyron-sf Picking this up back up after a while on the back burner. I have updated my commit message as requested.

@boonware boonware changed the title fix: handle paginated response fix(github): handle paginated response Oct 2, 2024
@@ -0,0 +1,148 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

We try to avoid new groovy code. Any chance you have the stamina to rewrite this in java?

Copy link
Author

@boonware boonware Oct 3, 2024

Choose a reason for hiding this comment

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

I wrote the tests in Groovy to be consistent as the existing test files in the package were also in Groovy, and still are (in fact all of the package's code is Groovy). Would this be a blocker on this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to say, but yes. Please no more groovy code.

ResponseEntity<List<Map<String, String>>> response = restTemplate.getForEntity(organizationsUrl, List.class);
HttpHeaders headers = response.getHeaders();
boolean isMember = githubOrganizationMember(organization, response.getBody())
while (!isMember && hasNextPage(headers)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like there's be less duplication with do/while as opposed to while/do.

Copy link
Author

Choose a reason for hiding this comment

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

Groovy does not have do ... while. We can construct something similar, but the syntax is ugly, IMHO, for example: https://stackoverflow.com/a/46474198

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless you're fired up to convert this file to java, it's fine to leave it as is.

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.

3 participants