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

Check for user write access (also, more speed and less code!) #222

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

infinisil
Copy link

@infinisil infinisil commented Apr 25, 2024

Description

This PR improves the owners check:

  • It now checks that users have write access (this notably was already done for teams, though undocumented)
    • Updated the README.md documentation to mention that
  • Speeds up the users check by not querying for all organisation members (gets just a bit slow if you have 3k members!), instead just querying the users with access to the repository
  • Simplify the team query code by just querying the teams with access to the repository

See the commits for more details.

Other than the locally runnable tests, I've also tested this minimally with an actual GitHub org repo

Related issue(s)


This work is sponsored by Antithesis

This switches from [`Teams.ListTeams`](https://pkg.go.dev/github.com/google/go-github/v41/github#TeamsService.ListTeams)
and potentially many calls of [`Teams.IsTeamRepoBySlug`](https://pkg.go.dev/github.com/google/go-github/v41/github#TeamsService.IsTeamRepoBySlug)
to just a single [`Repositories.ListTeams`](https://pkg.go.dev/github.com/google/go-github/v41/github#RepositoriesService.ListTeams),
which returns all the teams permissions to a specific repository.

This does have the minor drawback of not being able to distinguish
between teams not existing and teams not being in the organisation,
but this does also have the benefit of potentially speeding up the
result when there's many teams in the organisation.
For organizations with many members without write access (e.g. https://github.com/orgs/NixOS/people),
this speeds up the user check by switching from [`Organizations.ListMembers`](https://pkg.go.dev/github.com/google/go-github/v41/github#OrganizationsService.ListMembers)
to [`Repositories.ListCollaborators`](https://pkg.go.dev/github.com/google/go-github/v41/github#RepositoriesService.ListCollaborators).

Furthermore, using `ListCollaborators` we can easily check that the users
actually have write access to the repo, the same way as is done for
teams since the parent commit.
@infinisil infinisil changed the title Simpler faster permission check Check for user write access (also, more speed and less code!) Apr 25, 2024
infinisil added a commit to NixOS/org that referenced this pull request Apr 26, 2024
We shouldn't use personal access tokens, instead we created a GitHub App
with read-only access to just this repository.

While codeowners-validator supports GitHub App authentication,
the same cannot be said for the hacky script I wrote because there was no support
for checking write access: mszostok/codeowners-validator#157

Instead of trying to hack the script more to make it work with GitHub App authentication,
I decided to implement it into codeowners-validator itself: mszostok/codeowners-validator#222

Because it's not merged/released yet, we need to build it ourselves,
so I added some Nix to do that reproducibly.
@infinisil
Copy link
Author

infinisil commented Apr 26, 2024

Just discovered that when using this with a GitHub App, it needs the "Repository/Administration/read-only" permission instead of the "Organization/Members/read-only" permission, see NixOS/org#9. So I guess this is a breaking change. I only updated the documentation for now, but I imagine more might be needed because of this.

Edit: Needs to be updated to clarify Repository/Administration instead of Organization/Administration

infinisil added a commit to NixOS/org that referenced this pull request Apr 26, 2024
We shouldn't use personal access tokens, instead we created a GitHub App
with read-only access to just this repository.

While codeowners-validator supports GitHub App authentication,
the same cannot be said for the hacky script I wrote because there was no support
for checking write access: mszostok/codeowners-validator#157

Instead of trying to hack the script more to make it work with GitHub App authentication,
I decided to implement it into codeowners-validator itself: mszostok/codeowners-validator#222

Because it's not merged/released yet, we need to build it ourselves,
so I added some Nix to do that reproducibly.
@infinisil infinisil force-pushed the simpler-faster-permission-check branch from 74b5eca to 840eeb8 Compare April 26, 2024 17:50
infinisil added a commit to NixOS/org that referenced this pull request Apr 26, 2024
We shouldn't use personal access tokens, instead we created a GitHub App
with read-only access to just this repository.

While codeowners-validator supports GitHub App authentication,
the same cannot be said for the hacky script I wrote because there was no support
for checking write access: mszostok/codeowners-validator#157

Instead of trying to hack the script more to make it work with GitHub App authentication,
I decided to implement it into codeowners-validator itself: mszostok/codeowners-validator#222

Because it's not merged/released yet, we need to build it ourselves,
so I added some Nix to do that reproducibly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant