我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用github.UnknownObjectException()。
def test_update_index_does_not_exist(self): """ self.gh().get_user().get_repo().update_file.side_effect = UnknownObjectException(status=404, data="foo") runner = CliRunner() result = runner.invoke(update, ["--name", "testrepo", "--token", "token"]) self.assertEqual(result.exit_code, 0) self.gh.assert_called_with("token") self.gh().get_user().get_repo.assert_called_with(name="testrepo") self.gh().get_user().get_repo().get_labels.assert_called_once_with() self.gh().get_user().get_repo().create_file.assert_called_once_with( branch='gh-pages', content='some foo', message='initial', path='/index.html' ) """
def get_repository(): """Get the GitHub repo specified in settings or the default. If the repo doesn't exist, try to create it. """ try: g = Github(**get_github_credentials()) if app_settings.GITHUB_ORG: user = g.get_organization(app_settings.GITHUB_ORG) else: user = g.get_user() try: return user.get_repo(app_settings.GITHUB_REPO) except UnknownObjectException: logging.info("Creating repository {}".format( app_settings.GITHUB_REPO )) return user.create_repo(app_settings.GITHUB_REPO) except GithubException: logging.exception("Unable to configure Github connection.")
def run_remove_system(name, token, org, system, prompt): """ Removes a system from the repo. """ repo = get_repo(token=token, org=org, name=name) try: label = repo.get_label(name=system.strip()) label.delete() click.secho("Successfully deleted {}".format(system), fg="green") if prompt and click.confirm("Run update to re-generate the page?"): run_update(name=name, token=token, org=org) except UnknownObjectException: click.secho("Unable to remove system {}, it does not exist.".format(system), fg="yellow")
def remove_webhook(self): if not settings.DEBUG: g = get_github(self.user) grepo = g.get_repo(self.full_name) try: hook = grepo.get_hook(self.webhook_id) hook.delete() except UnknownObjectException: pass self.webhook_id = None self.save()
def get_pull(self, number): if number not in _pull_request_state: raise UnknownObjectException(404, 'not found') return MockPullRequest.from_state(id=number)
def get_issue(self, number): if number not in _pull_request_state: raise UnknownObjectException(404, 'not found') return MockIssue()
def get_file_contents(self, path, ref=None): if path not in _repo_state['file_contents']: raise UnknownObjectException(404, 'not found') return MockFileContents(_repo_state['file_contents'].get(path))
def test_404(self): mock_github.create_fake_pull_request(id=1) with self.assertRaises(UnknownObjectException): self.assertTrue(pull_request_ready_to_merge(pr_number=2))
def read_file_lines(self, file_path='OWNERS'): """ Get a list of strings, one per line in the file :param owners_file: A relative path to the file :return: a list of line strings """ self._connect() try: owner_file_contents = self.repo.get_file_contents(file_path) return owner_file_contents.decoded_content.split('\n') except UnknownObjectException: return []
def testUnknownObject(self): self.assertRaises(github.UnknownObjectException, lambda: self.g.get_user().get_repo("Xxx"))
def form_valid(self, form): try: return super().form_valid(form) except UnknownObjectException: message = f'Can\'t find GitHub user {form.cleaned_data["github_username"]}' form.add_error('github_username', message) return self.form_invalid(form) except NoStarredReposException: return HttpResponseRedirect(reverse('no-starred', kwargs={'username': form.cleaned_data['github_username']}))
def run_update(name, token, org): click.echo("Generating..") repo = get_repo(token=token, name=name, org=org) issues = get_issues(repo) # get the SHA of the current HEAD sha = repo.get_git_ref("heads/gh-pages").object.sha # get the template from the repo template_file = repo.get_file_contents( path="/template.html", ref=sha ) systems = get_systems(repo, issues) incidents = get_incidents(repo, issues) panels = get_panels(systems) # render the template config = get_config(repo) template = Template(template_file.decoded_content.decode("utf-8")) content = template.render({ "systems": systems, "incidents": incidents, "panels": panels, "config": config }) # create/update the index.html with the template try: # get the index.html file, we need the sha to update it index = repo.get_file_contents( path="/index.html", ref=sha, ) if is_same_content(content, base64.b64decode(index.content)): click.echo("Local status matches remote status, no need to commit.") return False repo.update_file( path="/index.html", sha=index.sha, message="update index", content=content, branch="gh-pages" ) except UnknownObjectException: # index.html does not exist, create it repo.create_file( path="/index.html", message="initial", content=content, branch="gh-pages", )
def ProcessRepo(request, full_name): user = request.user g = get_github(request.user) grepo = g.get_repo(full_name) if not grepo.full_name: raise Http404('Repo not found') guser = g.get_user(user.username) is_collab = grepo.has_in_collaborators(guser) if not is_collab and grepo.private: raise Http404('You are not a collaborator of this repo') try: repo = Repo.objects.get(full_name=grepo.full_name) repo.disabled = False repo.is_private = grepo.private repo.save() except Repo.DoesNotExist: repo = Repo.objects.create( full_name=grepo.full_name, user=user, default_branch=grepo.default_branch, is_private=grepo.private ) if not repo.webhook_id: try: repo.add_webhook(request) except UnknownObjectException: raise Http404('Github failed to create a hook') # Lint all open branches auth = request.user.get_auth() for branch in grepo.get_branches(): build, created = Build.objects.get_or_create( repo=repo, ref=branch.name, sha=branch.commit.sha ) if created: build.enqueue(auth) url = reverse('repo_detail', kwargs={'full_name': repo.full_name}) return redirect(url)