我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用github3.GitHubError()。
def generate_release_notes(): """Main function to create the release_notes.rst pages """ owner = 'spacetelescope' org = github3.organization(owner) outfile = os.path.join('source', 'release_notes.rst') try: pull_release_notes(org, outfile) except GitHubError as e: print(e) exit(1) #-------------------------------------------------------------------------------
def _setup_github(): if (config.has('github', 'enabled') and config.get('github', 'enabled') == 'false'): return if (config.has('github', 'token') and config.has('github', 'organizations')): return if not click.confirm('Do you wish to enable Github integration?', default=True): config.set('github', 'enabled', 'false') return config.set('github', 'enabled', 'true') for i in range(0, 3): try: _try_setup_github() break except GitHubError: pass else: sys.exit(1)
def create_hook(self, repo_id, repo_name): """Create repository hook.""" config = dict( url=self.webhook_url, content_type='json', secret=current_app.config['GITHUB_SHARED_SECRET'], insecure_ssl='1' if current_app.config['GITHUB_INSECURE_SSL'] else '0', ) ghrepo = self.api.repository_with_id(repo_id) if ghrepo: try: hook = ghrepo.create_hook( 'web', # GitHub identifier for webhook service config, events=['release'], ) except github3.GitHubError as e: # Check if hook is already installed hook_errors = (m for m in e.errors if m['code'] == 'custom' and m['resource'] == 'Hook') if next(hook_errors, None): hooks = (h for h in ghrepo.hooks() if h.config.get('url', '') == config['url']) hook = next(hooks, None) if hook: hook.edit(config=config, events=['release']) finally: if hook: Repository.enable(user_id=self.user_id, github_id=repo_id, name=repo_name, hook=hook.id) return True return False
def _try_setup_github(): try: gh = Github(_ask_credentials=_github_ask_credentials, _ask_2fa=_github_ask_2fa) _setup_github_orgs(gh) except GitHubError as e: if e.code == 401: _error('error: %s' % e.message) raise e _fatal('error: %s' % e.message) except BaseException as e: _fatal('error: %s' % e.message)
def _gen_authorization_token(self, counter=0, creds=None): """ This function creates the authorization token for AerisCloud. If an existing token exists for this computer, it adds a #N counter next to the name. """ if creds: user, pwd = creds['user'], creds['pwd'] else: (user, pwd) = self._ask_credentials() note = 'AerisCloud on %s' % (node()) if counter > 0: note += ' #%d' % counter try: auth = authorize(user, pwd, ['repo', 'read:org'], note=note, two_factor_callback=self._ask_2fa) return auth.token except GitHubError as e: if not e.errors or e.errors[0]['code'] != 'already_exists': raise # token exists, increment counter counter += 1 return self._gen_authorization_token(counter=counter, creds={'user': user, 'pwd': pwd})
def _assign_repo(repo_name, members=[]): """ (PRIVATE method, use the repomanager instead) Creates the repository and adds the members as contributors, idempotently. """ if config.github_read_only_mode: raise RuntimeError("Cannot assign repo because of GitHub read-only mode") github = _get_github_admin() fq_repo_name = "%s/%s" % (config.github_organization, repo_name) organization = github.organization(config.github_organization) try: repo = organization.create_repo(repo_name, private=config.github_private_repos) except github3.GitHubError as e: if e.args and hasattr(e.args[0], "status_code") and e.args[0].status_code == 422: repo = github.repository(config.github_organization, repo_name) assert repo, "Unable to get repository object for GitHub (check API key permissions?)" else: raise collaborators = {user.login for user in repo.iter_collaborators()} for member in members: if member not in collaborators: successfully_added = repo.add_collaborator(member) assert successfully_added, "Unable to add member %s to %s" % (repr(member), repr(fq_repo_name))
def post_messages(self, messages, max_comments): # TODO: support non-PR runs if not self.github: return messages_to_post = 0 messages_posted = 0 paths = dict() # randomize message order to more evenly distribute messages across different files messages = list(messages) random.shuffle(messages) if self.out_of_date(): return messages_to_post start = time.time() for msg in messages: if system.should_stop() or (time.time() - start > 10 and self.out_of_date()): return messages_to_post if not msg.comments: continue msg_position = self.position(msg) if msg_position: messages_to_post += 1 if not self.is_duplicate(msg, msg_position): # skip this message if we already have too many comments on this file # max comments / 5 is an arbitrary number i totally made up. should maybe be configurable. if paths.setdefault(msg.path, 0) > max_comments // 5: continue try: self.pull_request.create_review_comment( self.format_message(msg), self.last_sha, msg.path, msg_position ) except github3.GitHubError: pass paths[msg.path] += 1 messages_posted += 1 if max_comments >= 0 and messages_posted > max_comments: break print('{} messages posted to Github.'.format(messages_to_post)) return messages_to_post