Skip to content

Commit

Permalink
New script to update Github teams access to plugin repos
Browse files Browse the repository at this point in the history
The 'Core team' and 'translatewiki.net' teams in the mantisbt-plugins
org should have push access to all repositories in the organization.

Due to the large number of repositories within the org, this becomes
painful to check manually. This script leverages the Github API to
check for any missing access, and grant it.
  • Loading branch information
dregad committed May 6, 2021
1 parent 7ab8cab commit da62975
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config_defaults.yml
Expand Up @@ -22,3 +22,9 @@ github:
# Generate at https://github.com/settings/tokens
# The token must have at least `public_repo` scope
token: null

# Global teams in mantisbt-plugins org that should have access to
# all repositories
teams:
- translatewiki.net
- Core team
79 changes: 79 additions & 0 deletions github_plugin_teams.py
@@ -0,0 +1,79 @@
#!/usr/bin/python -u
"""
Uses Github API to grant write access to all of mantisbt-plugins organization
to the teams specified in config file
"""

import sys

# PyGithub library https://github.com/PyGithub/PyGithub
from github import Github, GithubException

# Configuration variables
import config
from config import cfg


def retrieve_teams(org):
"""
Returns a list of Team objects for the names defined in github_teams
that have fewer repositories than the specified organization
"""
teams = []
for team in org.get_teams():
if (
team.name in cfg.github['teams'] and
team.repos_count < org.public_repos
):
teams.append(team)
return teams


def main():
print "Connecting to Github as '{0}'".format(cfg.github['user'])
g = Github(cfg.github['user'], cfg.github['password'])

# Organization
org = g.get_organization(config.ORG_PLUGINS)
print "Retrieving organization '{0}' ({1})".format(org.login, org.name)

# Teams
print "Retrieving teams"
try:
teams = retrieve_teams(org)
except GithubException as err:
if err.status == 401:
print "This script requires authentication with a privileged " \
"account to access and update the organization's team."
print "Please update the configuration as appropriate"
else:
print "Unknown error", (err)
sys.exit(1)
if not teams:
print "The teams", ', '.join(cfg.github['teams']),
print "already have write access to all repositories"
sys.exit()

# Check that the team grants access to each of the org's repo
# Add write access if not
print "Processing {0} plugins for {1} teams".format(
org.public_repos,
len(teams)
)
count = 0
for repo in org.get_repos():
for team in teams:
if not team.has_in_repos(repo):
count += 1
print "Grant team '{0}' write access to plugin '{1}'".format(
team.name,
repo.name
)
team.set_repo_permission(repo, 'push')

print "{0} plugins processed".format(count)
print "Done"


if __name__ == "__main__":
main()

0 comments on commit da62975

Please sign in to comment.