def github(self): """(:class:`github3.GitHub <github3.github.GitHub>`) The GitHub connection. """ gh = GitHub() gh._session = self.repository._session return gh
def get_github(): from github3 import login from keyring.core import get_keyring key = get_keyring() token = key.get_password('github', 'token') password = key.get_password('github', 'password') username = key.get_password('github', 'login') client_id = key.get_password('github', 'client_id') client_secret = key.get_password('github', 'client_secret') GitHub = login(username=username, password=password, token=token) if client_id and client_secret: GitHub.set_client_id(id=client_id, secret=client_secret) me = GitHub.me() if me.type == 'User': return GitHub return None
def _create_team(github: GitHub, organization_name: str, team_name: str): # passed GitHub object must have org. admin authorization to create a team organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if team: util.verbose("Team {name} already exists".format(name=team_name)) return try: organization.create_team(name=team_name) util.info("Team {name} created".format(name=team_name)) except ForbiddenError as err: util.fail( "{err} Cannot create team {name} in org {org} due to missing privileges" .format(err=err, name=team_name, org=organization_name))
def login_as_installation(action: ActionIn): try: gh = GitHub() gh.login_as_app(GITHUB_PRIVATE_KEY.encode(), GITHUB_APP_IDENTIFIER) install = gh.app_installation_for_repository(action.owner, action.repository) gh.login_as_app_installation(GITHUB_PRIVATE_KEY.encode(), GITHUB_APP_IDENTIFIER, install.id) return gh except NotFoundError: raise HTTPException(404, f"OpeAPI Perf App not installed to {action.repo}")
def _create_github_api_object( github_cfg: 'GithubConfig', webhook_user: bool=False, ): github_url = github_cfg.http_url() github_auth_token = github_cfg.credentials().auth_token() github_verify_ssl = github_cfg.tls_validation() if github_url.strip('/') == 'https://github.com': github = GitHub(token=github_auth_token) else: github = GitHubEnterprise(url=github_url, token=github_auth_token, verify=github_verify_ssl) if not github: util.fail("Could not connect to GitHub-instance {url}".format(url=github_url)) return github
def _add_user_to_team(github: GitHub, organization_name: str, team_name: str, user_name: str): # passed GitHub object must have org. admin authorization to add a user to a team organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if not team: util.fail("Team {name} does not exist".format(name=team_name)) if team.is_member(user_name): util.verbose( "{username} is already assigned to team {teamname}".format( username=user_name, teamname=team_name)) return if team.add_member(username=user_name): util.info("Added {username} to team {teamname}".format( username=user_name, teamname=team_name)) else: util.fail( "Could not add {username} to team {teamname}. Check for missing privileges" .format(username=user_name, teamname=team_name))
def refresh_github_users(project, *, originating_user_id): try: project.refresh_from_db() repo = get_repo_info(None, repo_owner=project.repo_owner, repo_name=project.repo_name) project.github_users = list( sorted( [{ "id": str(collaborator.id), "login": collaborator.login, "avatar_url": collaborator.avatar_url, "permissions": collaborator.permissions, } for collaborator in repo.collaborators()], key=lambda x: x["login"].lower(), )) # Retrieve additional information for each user by querying GitHub gh = GitHub(repo) expanded_users = [] for user in project.github_users: try: full_user = get_cached_user(gh, user["login"]) expanded = {**user, "name": full_user.name} except Exception: logger.exception( f"Failed to expand GitHub user {user['login']}") expanded = user expanded_users.append(expanded) project.github_users = expanded_users except Exception as e: project.finalize_refresh_github_users( error=e, originating_user_id=originating_user_id) tb = traceback.format_exc() logger.error(tb) raise else: project.finalize_refresh_github_users( originating_user_id=originating_user_id)
def _add_all_repos_to_team(github: GitHub, organization_name: str, team_name: str, permission: RepoPermission = RepoPermission.ADMIN): '''Add all repos found in `organization_name` to the given `team_name`''' # passed GitHub object must have org admin authorization to assign team to repo with admin rights organization = github.organization(organization_name) team = _retrieve_team_by_name_or_none(organization, team_name) if not team: util.fail("Team {name} does not exist".format(name=team_name)) for repo in organization.repositories(): if team.has_repository(repo.full_name): util.verbose( "Team {teamnname} already assigned to repo {reponame}".format( teamnname=team_name, reponame=repo.full_name)) continue team.add_repository(repository=repo.full_name, permission=permission.value) util.info("Added team {teamname} to repository {reponame}".format( teamname=team_name, reponame=repo.full_name))
#!/usr/bin/env python import os from github3.github import GitHub from github3.exceptions import NotFoundError USERNAME = os.getenv('GITHUB_USERNAME', '') PASSWORD = os.getenv('GITHUB_PASSWORD', '') gh = GitHub(username=USERNAME, password=PASSWORD) def read_files(): repo_copy_1 = gh.repository('joeseggie', 'repo-copy-1') return repo_copy_1.file_contents('README.md').decoded def commit_update(content: bytes): repo_copy_2 = gh.repository('joeseggie', 'repo-copy-2') try: repo_copy_2.directory_contents('/', return_as=list) repo_copy_2.file_contents('README.md').update('document_update', content, branch='gh-pages') except NotFoundError: repo_copy_2.create_file('README.md', 'Readme upload', content=content, branch='gh-pages')