def github(self): gh = github3.GitHub() user = self.config('github.user') if not user: user = raw_input("GitHub user: "******"""Callback for github3.py's 2FA support.""" return raw_input("Two-Factor Authentication Code: ").strip() password = getpass.getpass("GitHub password: "******"GitSpindle on %s" % socket.gethostname(), "http://seveas.github.com/git-spindle") except github3.GitHubError: type, exc, tb = sys.exc_info() if hasattr(exc, 'response'): response = exc.response if response.status_code == 422: for error in response.json()['errors']: if error['resource'] == 'OauthAccess' and error[ 'code'] == 'already_exists': err("An OAuth token for this host already exists, please delete it on https://github.com/settings/applications" ) raise type.with_traceback(tb) if auth is None: err("Authentication failed") token = auth.token self.config('github.token', token) self.config('github.auth_id', auth.id) print( "A GitHub authentication token is now cached in ~/.gitspindle - do not share this file" ) print( "To revoke access, visit https://github.com/settings/applications" ) if not user or not token: err("No user or token specified") gh.login(username=user, token=token) try: self.me = gh.user() except github3.GitHubError: # Token obsolete self.gitm('config', '--file', self.config_file, '--unset', 'github.token') return self.github() return gh
def __init__(self, manifest: dict, session: requests.Session): self.app_name = manifest['name'] self.app_id = slugify(self.app_name) self.manifest = manifest self.session = session self._user = github3.GitHub() self._app = github3.GitHub() self.state = GitHubTestState(self.app_id)
def __init__(self, owner, repo, user, password=None, token=None, **kwargs): super(GithubWrapper, self).__init__(**kwargs) if (token is None) and (password is None): self.session = github3.GitHub(user) elif token is None: self.session = github3.GitHub(user, password) elif password is None: self.session = github3.GitHub(user, token=token) else: self.session = github3.GitHub(user, password, token=token) self.repo = self.session.repository(owner, repo)
def get_github_client(token): """Obtain a github3 client using an API token for authentication. If the ``BETAMAX_LIBRARY_DIR`` and ``BETAMAX_CASSETTE`` environment variables are defined, the ``requests.Session`` used by the client will be hooked up to betamax and pre-recorded HTTP requests will be used instead of incurring actual requests. When betamax is active, the auth token is not relevant. """ gh = github3.GitHub() betamax_library_dir = os.environ.get('BETAMAX_LIBRARY_DIR') betamax_cassette = os.environ.get('BETAMAX_CASSETTE') if betamax_library_dir and betamax_cassette: # Delay import because only needed for testing. import betamax with betamax.Betamax.configure() as config: config.cassette_library_dir = betamax_library_dir # We don't want requests hitting the network at all. config.default_cassette_options['record_mode'] = 'none' recorder = betamax.Betamax(gh._session) recorder.use_cassette(betamax_cassette) recorder.start() gh.login(token=token) return gh
def get_repos(*args): g = github3.GitHub(*args) org = g.organization(ORG_NAME) repos = list(org.repositories()) return [r for r in repos if (r.name.startswith('project-') and not r.name == 'project-template')]
def load_github_or_local_yaml(filename, dirname, github_user=None, github_repo=None, github_api_key=None): """ load_yaml from a local or remote source if github credentials are supplied, they'll be used and we'll read from the directory or file within the repo otherwise, the local file will be used """ if github_user and github_repo: gh = github3.GitHub(token=github_api_key) repo = gh.repository(github_user, github_repo) if dirname: data = {} for fname, contents in repo.directory_contents(dirname): contents.refresh() data.update(yaml.safe_load(contents.decoded)) elif filename: contents = repo.file_contents(filename).decoded data = yaml.safe_load(contents) elif dirname: data = {} for filename in glob.glob(dirname + "/*"): with open(filename) as f: data.append(yaml.safe_load(f)) else: with open(filename) as f: data = yaml.safe_load(f) return data
def get_repo(owner, name, token, cache_path='~/.gh2/cache'): cache_path = os.path.expanduser(cache_path) gh = github3.GitHub(token=token) gh.session = cachecontrol.CacheControl( gh.session, cache=cachecontrol.caches.FileCache(cache_path)) return gh.repository(owner, name)
def _gh_repository_api(repo_name, github_token): if not repo_name or not github_token: return None owner, repository = repo_name.split("/") gh = github3.GitHub() gh.login(token=github_token) return gh.repository(owner, repository)
def find(self, version=None): tags = [] log.info('looking for tags of {}/{}'.format(self.owner, self.project)) repos = github3.GitHub().repository(self.owner, self.project) for tag in repos.iter_tags(): version_tag = tag.name # so far all tags appear to prepend "v" to version if version_tag[0] == 'v': actual_version = version_tag[1:] else: actual_version = version_tag try: semver = semantic_version.Version(actual_version) except ValueError: # bad semver, no cookie continue if version in (version_tag, actual_version): return tag.zipball_url if semver.prerelease or semver.build: continue tags.append((semver, tag)) if not tags: log.error('could not find a stable release') return None tags.sort() return tags[-1][1].zipball_url
def register_webhook(self): if not getattr(settings, 'AASEMBLE_BUILDSVC_USE_WEBHOOKS', True): return True if self.webhook_registered: return True try: owner, repo = self.github_owner_repo() except NotAValidGithubRepository: return False try: for token in SocialToken.objects.filter(account__in=self.series.repository.user.socialaccount_set.filter(provider='github')): gh = github3.GitHub(token=token.token) repo = gh.repository(owner, repo) if repo.create_hook(name='web', config={'url': settings.GITHUB_WEBHOOK_URL, 'content_type': 'json'}): self.webhook_registered = True self.save() return True except github3.GitHubError as exc: msgs = [e['message'] for e in exc.errors] if 'Hook already exists on this repository' in msgs: self.webhook_registered = True self.save() else: raise return False
def get_auth_token(cls, login, password): import platform auth = github3.GitHub().authorize(login, password, scopes=[ 'repo', 'delete_repo', 'gist' ], note='git-repo token used on {}'.format(platform.node()), note_url='https://github.com/guyzmo/git-repo') return auth.token
def main(repository, file_name): gh = github3.GitHub(token=GITHUB_TOKEN) owner, name = repository.split('/', 1) repo = gh.repository(owner, name) repoId = repo.id data = [[ 'ID', 'Link', 'Title', 'Status', 'Milestone', 'Assignee', 'CreatedOn', 'Estimate', 'Labels' ]] with progressbar.ProgressBar(max_value=progressbar.UnknownLength) as bar: for items in repo.issues(): row = [] issuenumber = items.number row.append(str(issuenumber)) row.append(str(items.html_url)) row.append(str(items.title)) row.append(str(items.state)) row.append(str(items.milestone)) row.append(str(items.assignee.login)) row.append(str(items.created_at.strftime('%m/%d/%Y'))) try: estimate = getZenhub_estimate(repoId, issuenumber)['estimate']['value'] except: estimate = 0 row.append(estimate) row.append(collate_labels(items.original_labels)) data.append(row) bar.update() write_to_csv(file_name, data)
def write_notifications(self, user, output): """Write all notifications to a file. Args: user: Name of the user to get notifications for output: The file to write notifications to. Fetches all notifications, including ones marked read, and writes them to the supplied file. """ token = os.getenv(TOKEN_NAME) if not token: raise ValueError( ("Environment variable {0} needs to be set to a GitHub " "token.").format(token)) client = github3.GitHub(username=user, token=token) notifications = client.notifications(all=True) # https://developer.github.com/v3/activity/notifications/ # # How do we identify closed pull requests? i = 0 with open(output, mode="w") as hf: for n in notifications: i += 1 hf.write(n.as_json()) hf.write("\n") logging.info("Wrote %s notifications to %s", i, output)
def __init__( self, githubToken: str, pull_request_dicts: List[dict], repo_owner: str, repo_name: str, jira_token: str = "", jira_username: str = "", jira_url: str = "", ): """ :param githubToken: GitHub oauth githubToken """ self.githubToken = githubToken self.pull_request_dicts = pull_request_dicts self.repo_owner = repo_owner self.repo_name = repo_name self.jira_token = jira_token self.jira_url = jira_url self.gh = github3.GitHub(token=self.githubToken) # documentation claims you need to use username/password combo but username/token works as well if jira_token: self.jira = jira.JIRA(jira_url, basic_auth=(jira_username, self.jira_token))
def __init__(self, owner, repo, pr=None, branch=None, token=None, url=None, commit=None, ignore_paths=None): """ GitHubInterface lets us post messages to GitHub. owner and repo are the repository owner/organization and repo name respectively. pr is the ID number of the pull request. branch is the branch name. either pr OR branch must be populated. token is your GitHub API token. url is the base URL of your GitHub instance, such as https://github.com commit is the commit hash we're running against ignore_paths are paths to ignore comments from """ self.github = None self.ignore_paths = set(ignore_paths or []) if not url or url == 'https://github.com': self.github = github3.GitHub(token=token) else: self.github = github3.GitHubEnterprise(url, token=token) self.owner = owner self.repo = repo print('Branch: {0}'.format(branch)) if branch and not pr: github_repo = self.github.repository(self.owner, self.repo) for pull_request in github_repo.iter_pulls(): if pull_request.to_json()['head']['ref'] == branch: pr = pull_request.to_json()['number'] break # TODO: support non-PR runs try: pr = int(pr) except (ValueError, TypeError): print('{0} is not a valid pull request ID'.format(pr)) self.github = None return print('PR ID: {0}'.format(pr)) self.pr = pr self.pull_request = self.github.pull_request(owner, repo, pr) self.commits = self.pr_commits(self.pull_request) self.last_sha = commit or git.current_sha() print('Last SHA: {0}'.format(self.last_sha)) self.first_sha = self.commits[0].sha self.parent_sha = git.parent_sha(self.first_sha) self.diff = git.diff(self.parent_sha, self.last_sha) self.patch = unidiff.PatchSet(self.diff.split('\n')) self.review_comments = list(self.pull_request.review_comments()) self.last_update = time.time()
def __init__(self, **kwargs): """Constructor accepts the same parameters as github3.GitHub object. more details here: https://github3py.readthedocs.io/en/master/api-reference/github.html#github3.github.GitHub """ # using username and password self.api = github3.GitHub(**kwargs)
def _create_client(self, user): token = os.getenv(TOKEN_NAME) if not token: raise ValueError(("Environment variable {0} needs to be set to a GitHub " "token.").format(token)) client = github3.GitHub(username=user, token=token) return client
def get_repos(): ''' returns a github iterator object with a list of repos that are from the user specified ''' gh = github3.GitHub(token=TOKEN) repos = gh.iter_user_repos(USER) return repos
def __init__(self, username=None, password=None): if username and password: self.client = github3.login( username=username, password=password, two_factor_callback=two_factor_callback) else: self.client = github3.GitHub()
def setUp(self): self.g = github3.GitHub() self.session = self.g._session if os.environ.get('GH_AUTH'): self.g.login(token=os.environ['GH_AUTH']) self.args = () self.conf = {'allow_redirects': True} self.mock = patch.object(requests.sessions.Session, 'request') self.request = self.mock.start()
def github3_installation_auth(repo: Repo, app_id: int, private_key: bytes): """ """ gh3 = github3.GitHub() gh3.login_as_app(private_key, app_id) installation = gh3.app_installation_for_repository(repo.owner, repo.name) gh3.login_as_app_installation(private_key, app_id, installation.id) return gh3
def get_github_client(self): load_dotenv() GITHUB_PRIVATE_KEY = os.getenv('GITHUB_PRIVATE_KEY') GITHUB_APP_IDENTIFIER = os.getenv('GITHUB_APP_IDENTIFIER') client = github3.GitHub() client.login_as_app_installation(GITHUB_PRIVATE_KEY.encode(), GITHUB_APP_IDENTIFIER, self.installation_id) return client
def setUp(self): self.github = github3.GitHub() self.session = self.github._session self.configure_session(self.session) self.recorder = Betamax(self.session) with self.recorder.use_cassette('github-pr-initialization'): self.ghpr = GitHubPR(AUTH_TOKEN, 'servo/servo', '', github=self.github)
def __init__(self, owner, repo_name): """ Returns a GitHub object, possibly authed as a user. """ token = os.environ.get('GITHUB_TOKEN', '') if len(token): self.gh = github3.login(token=token) else: self.gh = github3.GitHub() self.repo = self.gh.repository(owner, repo_name)
def load_token(): tokenfile = join(dirs.user_data_dir, '%s.token' % quote_plus('https://api.github.com')) token = id = '' with open(tokenfile, 'r') as fd: token = fd.readline().strip() id = fd.readline().strip() gh = github3.GitHub() gh.login(token=token) return gh
def __init__(self, owner, project, context, pem, app_id, install_id, sha, details_url=None, external_id=None): self.owner = owner self.project = project self.context = context self.sha = sha self.details_url = details_url self.external_id = external_id self.github = github3.GitHub() self.github.login_as_app_installation(pem, app_id, install_id) self.check_run = None
def main(): parser = argparse.ArgumentParser() parser.add_argument('--token') subparsers = parser.add_subparsers() what_affects_parser(subparsers) args = parser.parse_args() if args.token is not None: gh = github3.login(token=args.token) else: gh = github3.GitHub() args.func(gh, args)
def _make_gist(contents, description='', filename='data.geojson'): """ Create and return an anonymous gist with a single file and specified contents """ ghapi = github3.GitHub() files = {filename: {'content': contents}} gist = ghapi.create_gist(description, files) return gist
def get_github_repository(reponame, login=env.get('GITHUB_LOGIN', None), password=env.get('GITHUB_PASSWORD', None), key=env.get('GITHUB_KEY', None)): if login: g = github3.login(login, password) elif key: g = github3.login(token=key) else: g = github3.GitHub() return g.repository("elasticsearch", reponame)
def as_github_repo(self, github_token=None): """Converts it to a repository object which wraps the GitHub API""" if self._github_repo is None: if not _have_github3: raise ImportError('Must install github3.py') github_token = github_token or self.github_token username, reponame = self._parse_github_user_repo() session = github3.session.GitHubSession(default_connect_timeout=10, default_read_timeout=30) github = github3.GitHub(session=session) github.login(token=github_token) self._github_repo = github.repository(username, reponame) return self._github_repo