Merge lp:~jelmer/brz/credentials-error into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/credentials-error
Merge into: lp:brz
Diff against target: 122 lines (+38/-0)
2 files modified
breezy/plugins/propose/github.py (+24/-0)
breezy/plugins/propose/propose.py (+14/-0)
To merge this branch: bzr merge lp:~jelmer/brz/credentials-error
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+362950@code.launchpad.net

Description of the change

Raise a clearer error when not logged into e.g. GitHub.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Not thrilled about this decorator usage, but do see it's convenient. Probably need to refactor the innards to avoid needing an outer wrapper like this.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/plugins/propose/github.py'
2--- breezy/plugins/propose/github.py 2019-02-02 17:36:19 +0000
3+++ breezy/plugins/propose/github.py 2019-02-10 18:54:22 +0000
4@@ -22,6 +22,7 @@
5
6 from .propose import (
7 Hoster,
8+ HosterLoginRequired,
9 MergeProposal,
10 MergeProposalBuilder,
11 MergeProposalExists,
12@@ -74,6 +75,11 @@
13 self.url = url
14
15
16+class GitHubLoginRequired(HosterLoginRequired):
17+
18+ _fmt = "Action requires GitHub login."
19+
20+
21 def connect_github():
22 """Connect to GitHub.
23 """
24@@ -145,6 +151,18 @@
25 git_url_to_bzr_url(url), {"branch": branch_name})
26
27
28+def convert_github_error(fn):
29+ def convert(self, *args, **kwargs):
30+ import github
31+ try:
32+ return fn(self, *args, **kwargs)
33+ except github.GithubException as e:
34+ if e.args[0] == 401:
35+ raise GitHubLoginRequired(self)
36+ raise
37+ return convert
38+
39+
40 class GitHub(Hoster):
41
42 name = 'github'
43@@ -163,6 +181,7 @@
44 def __init__(self):
45 self.gh = connect_github()
46
47+ @convert_github_error
48 def publish_derived(self, local_branch, base_branch, name, project=None,
49 owner=None, revision_id=None, overwrite=False,
50 allow_lossy=True):
51@@ -201,11 +220,13 @@
52 return push_result.target_branch, github_url_to_bzr_url(
53 remote_repo.html_url, name)
54
55+ @convert_github_error
56 def get_push_url(self, branch):
57 owner, project, branch_name = parse_github_url(branch)
58 repo = self.gh.get_repo('%s/%s' % (owner, project))
59 return github_url_to_bzr_url(repo.ssh_url, branch_name)
60
61+ @convert_github_error
62 def get_derived_branch(self, base_branch, name, project=None, owner=None):
63 import github
64 base_owner, base_project, base_branch_name = parse_github_url(base_branch)
65@@ -221,9 +242,11 @@
66 except github.UnknownObjectException:
67 raise errors.NotBranchError('https://github.com/%s/%s' % (owner, project))
68
69+ @convert_github_error
70 def get_proposer(self, source_branch, target_branch):
71 return GitHubMergeProposalBuilder(self.gh, source_branch, target_branch)
72
73+ @convert_github_error
74 def iter_proposals(self, source_branch, target_branch, status='open'):
75 (source_owner, source_repo_name, source_branch_name) = (
76 parse_github_url(source_branch))
77@@ -269,6 +292,7 @@
78 def iter_instances(cls):
79 yield cls()
80
81+ @convert_github_error
82 def iter_my_proposals(self, status='open'):
83 query = ['is:pr']
84 if status == 'open':
85
86=== modified file 'breezy/plugins/propose/propose.py'
87--- breezy/plugins/propose/propose.py 2019-02-01 00:18:37 +0000
88+++ breezy/plugins/propose/propose.py 2019-02-10 18:54:22 +0000
89@@ -83,6 +83,16 @@
90 self.hoster = hoster
91
92
93+class HosterLoginRequired(errors.BzrError):
94+ """Action requires hoster login credentials."""
95+
96+ _fmt = "Action requires credentials for hosting site %(hoster)r."""
97+
98+ def __init__(self, hoster):
99+ errors.BzrError.__init__(self)
100+ self.hoster = hoster
101+
102+
103 class MergeProposal(object):
104 """A merge proposal.
105
106@@ -175,6 +185,8 @@
107 :param base_branch: branch to derive the new branch from
108 :param new_branch: branch to publish
109 :return: resulting branch, public URL
110+ :raise HosterLoginRequired: Action requires a hoster login, but none is
111+ known.
112 """
113 raise NotImplementedError(self.publish)
114
115@@ -225,6 +237,8 @@
116 :param status: Only yield proposals with this status
117 (one of: 'open', 'closed', 'merged', 'all')
118 :return: Iterator over MergeProposal objects
119+ :raise HosterLoginRequired: Action requires a hoster login, but none is
120+ known.
121 """
122 raise NotImplementedError(self.iter_my_proposals)
123

Subscribers

People subscribed via source and target branches