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

Add permissions check to valid_owner #62

Merged
merged 2 commits into from Jan 21, 2021

Conversation

highb
Copy link
Contributor

@highb highb commented Jan 13, 2021

Description

Changes proposed in this pull request:

  • Changes the initial check for the validity/existence of a team to use the Teams List Teams API endpoint
  • Adds a permissions check that uses the Teams IsTeamRepoBySlug endpoint, to check that the owner has write permissions on the repo

I haven't contributed to many Go Projects, so hopefully I'm following good conventions.

Related issue(s)

Fixes #40

Previously, the valid_owner check used the Repositories List Teams API
endpoint to check if a team was valid. Due to an issue in the
GitHub API, that endpoint does not return child teams that would
inherit a parent team's permissions.

This commit changes the initial check for the validity of a team to use
the Teams List Teams API endpoint, which lists all teams in a org to
check if the team exists at all. If that check fails, it will clearly
state that the team does not exist.

As a follow up to the initial valid team check, this commit checks the
Teams IsTeamRepoBySlug endpoint, which returns an object containing the
actual permissions for the team being checked on the repo. By adding
this check, we can verify that a user can actually provide their review
on PRs that made the CODEOWNERS line.
@codecov
Copy link

codecov bot commented Jan 13, 2021

Codecov Report

Merging #62 (d03ee34) into master (cde24ed) will decrease coverage by 3.13%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #62      +/-   ##
==========================================
- Coverage   50.13%   47.00%   -3.14%     
==========================================
  Files          12       12              
  Lines         375      400      +25     
==========================================
  Hits          188      188              
- Misses        183      208      +25     
  Partials        4        4              
Impacted Files Coverage Δ
internal/check/valid_owner.go 24.46% <0.00%> (-5.37%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cde24ed...d03ee34. Read the comment docs.

@highb
Copy link
Contributor Author

highb commented Jan 13, 2021

I'm not surprised to see that I should add more unit tests. I'll work on that when I have a moment.

@jspiro
Copy link
Contributor

jspiro commented Jan 13, 2021

This is just what we need, thanks for fixing this!

@mszostok mszostok self-requested a review January 13, 2021 08:41
@jspiro
Copy link
Contributor

jspiro commented Jan 14, 2021

I don't see an easy way to increase coverage in this file. All of the active check code is uncovered as it hits GitHub APIs – I can't tell if they're mocked nor how, but when I gave it a trivial try by calling Check with @org/team, the github API segfaulted (!).

This might explain the file being 50% coverage target. @mszostok what's the path forward here? This is a critical fix for teams using parent team hierarchies, and the code looks good to me!

@mszostok
Copy link
Owner

mszostok commented Jan 18, 2021

Hi @jspiro today I will take a look. Basically, implemented functionality is covered by the integration tests: https://github.com/mszostok/codeowners-validator/blob/master/docs/development.md#integration-tests but the test coverage is not included in the report (see: https://github.com/mszostok/codeowners-validator/issues). Unfortunately, integration tests cannot be executed on PRs from external contributors because of the problem with tokens. I will run them manually.

Btw. I see that the job failed on code quality check, see: https://travis-ci.com/github/mszostok/codeowners-validator/jobs/471190231 - it is easy to fix :)

@highb
Copy link
Contributor Author

highb commented Jan 19, 2021

@mszostok Thanks for taking a look! If there's anything I can do to help out, please ask.

Copy link
Owner

@mszostok mszostok left a comment

Choose a reason for hiding this comment

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

Once again thanks for your contribution!

I executed integration tests, and it works properly as I also simulated that some teams have only "read" permission, and they were detected properly 👍

I left a few comments which are minor. After resolving them we can merge the pull-request 🚀

If you do not have time for that I can take care of it as they are just small adjustments - if necessary ping me under this pull request 🙂

internal/check/valid_owner.go Outdated Show resolved Hide resolved
internal/check/valid_owner.go Outdated Show resolved Hide resolved
internal/check/valid_owner.go Outdated Show resolved Hide resolved
internal/check/valid_owner.go Outdated Show resolved Hide resolved
internal/check/valid_owner.go Outdated Show resolved Hide resolved
}

// repo contains the permissions for the team slug given
repo, _, err := v.ghClient.Teams.IsTeamRepoBySlug(ctx, v.orgName, team, org, v.orgRepoName)
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for adding that functionality, it was also requested some time ago (#21)

Unfortunately, this approach increases the number of external call to GitHub, probably in the near feature it will be good to switch to GraphQL which will remove problem with over fetched data, and we will be able to merge the ListTeams and IsTeamRepoBySlug into single query, see: #21 (comment)

but this is for future, for now, it lgtm 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

That is quite true. There are also potentially issues with GraphQL, which we've experienced with another project we contribute to, the GitHub Terraform provider – some calls because extremely, extremely slow in larger orgs (branch protection) and it annihilated the performance compared to REST. I doubt it'd be an issue here, just sharing our experience.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll add a todo.

Copy link
Owner

Choose a reason for hiding this comment

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

thx for the heads-up 👍

@highb
Copy link
Contributor Author

highb commented Jan 21, 2021

@mszostok FYI @jspiro is my rockstar colleague who I granted collaborator access on my fork to. 😄 It looks like he's essentially addressed everything and I'm 👍 on the changes.

@jspiro
Copy link
Contributor

jspiro commented Jan 21, 2021

@mszostok As you indicated, Travis is hitting API limits, presumably due to unauthenticated requests having a limit of 60 per hour per IP. I suppose this is a good reason for GQL ;-)

        +    [err] line 8: Cannot initialize organization member list: GET https://api.github.com/orgs/gh-codeowners/members?per_page=100: 403 API rate limit exceeded for 207.254.16.35. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.) [rate reset in 35m37s]
253

Have you considered porting this to GitHub Actions? I suspect that would paper over this issue, at least.

@jspiro
Copy link
Contributor

jspiro commented Jan 21, 2021

If this is acceptable and gets merged, please remember to tag it :-)

@mszostok
Copy link
Owner

mszostok commented Jan 21, 2021

Have you considered porting this to GitHub Actions? I suspect that would paper over this issue, at least.

Yes, this is on my TODO list, as for all other project I already use the GitHub Actions. Maybe during this weekend I will have some time to do it and add also release pipeline cause right now it is pain in the ass as it is manual 😄

Copy link
Owner

@mszostok mszostok left a comment

Choose a reason for hiding this comment

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

@jspiro & @highb Thanks for your contribution! LGTM 🚀

@highb
Copy link
Contributor Author

highb commented Jan 21, 2021

:shipit: Looking forward to rolling this out on all our repos! Thanks for putting the project out there!

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.

Owners check does not support child teams with inherited repo perms
3 participants