1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# Copyright (C) 2018 Breezy Developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import absolute_import
from .propose import (
Hoster,
MergeProposal,
MergeProposalBuilder,
MergeProposalExists,
UnsupportedHoster,
)
from ... import (
branch as _mod_branch,
controldir,
errors,
hooks,
urlutils,
version_string as breezy_version,
)
from ...config import AuthenticationConfig, GlobalStack
from ...git.urls import git_url_to_bzr_url
from ...i18n import gettext
from ...trace import note
from ...lazy_import import lazy_import
lazy_import(globals(), """
from github import Github
""")
class NotGitHubUrl(errors.BzrError):
_fmt = "Not a GitHub URL: %(url)s"
def __init__(self, url):
errors.BzrError.__init__(self)
self.url = url
def connect_github():
user_agent = user_agent="Breezy/%s" % breezy_version
auth = AuthenticationConfig()
credentials = auth.get_credentials('https', 'github.com')
if credentials is not None:
return Github(credentials['user'], credentials['password'],
user_agent=user_agent)
# TODO(jelmer): Support using an access token
#return Github("token", user_agent=user_agent)
return Github(user_agent=user_agent)
def parse_github_url(branch):
url = urlutils.split_segment_parameters(branch.user_url)[0]
(scheme, user, password, host, port, path) = urlutils.parse_url(
url)
if host != 'github.com':
raise NotGitHubUrl(url)
(owner, repo_name) = path.strip('/').split('/')
if repo_name.endswith('.git'):
repo_name = repo_name[:-4]
return owner, repo_name, branch.name
class GitHub(Hoster):
supports_merge_proposal_labels = True
def __init__(self):
self.gh = connect_github()
def publish_derived(self, local_branch, base_branch, name, project=None,
owner=None, revision_id=None, overwrite=False):
import github
base_owner, base_project, base_branch_name = parse_github_url(base_branch)
base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
if owner is None:
owner = self.gh.get_user().login
if project is None:
project = base_repo.name
try:
remote_repo = self.gh.get_repo('%s/%s' % (owner, project))
remote_repo.id
except github.UnknownObjectException:
base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
if owner == self.gh.get_user().login:
owner_obj = self.gh.get_user()
else:
owner_obj = self.gh.get_organization(owner)
remote_repo = owner_obj.create_fork(base_repo)
note(gettext('Forking new repository %s from %s') %
(remote_repo.html_url, base_repo.html_url))
else:
note(gettext('Reusing existing repository %s') % remote_repo.html_url)
remote_dir = controldir.ControlDir.open(git_url_to_bzr_url(remote_repo.ssh_url))
push_result = remote_dir.push_branch(local_branch, revision_id=revision_id,
overwrite=overwrite, name=name)
return push_result.target_branch, urlutils.join_segment_parameters(
remote_repo.html_url, {"branch": name.encode('utf-8')})
def get_push_url(self, branch):
owner, project, branch_name = parse_github_url(branch)
repo = self.gh.get_repo('%s/%s' % (owner, project))
return urlutils.join_segment_parameters(
git_url_to_bzr_url(repo.ssh_url), {"branch": branch_name.encode('utf-8')})
def get_derived_branch(self, base_branch, name, project=None, owner=None):
import github
base_owner, base_project, base_branch_name = parse_github_url(base_branch)
base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
if owner is None:
owner = self.gh.get_user().login
if project is None:
project = base_repo.name
try:
remote_repo = self.gh.get_repo('%s/%s' % (owner, project))
full_url = urlutils.join_segment_parameters(
git_url_to_bzr_url(remote_repo.ssh_url), {"branch": name.encode('utf-8')})
return _mod_branch.Branch.open(full_url)
except github.UnknownObjectException:
raise errors.NotBranchError('https://github.com/%s/%s' % (owner, project))
def get_proposer(self, source_branch, target_branch):
return GitHubMergeProposalBuilder(self.gh, source_branch, target_branch)
@classmethod
def probe(cls, branch):
try:
parse_github_url(branch)
except NotGitHubUrl:
raise UnsupportedHoster(branch)
return cls()
class GitHubMergeProposalBuilder(MergeProposalBuilder):
def __init__(self, gh, source_branch, target_branch):
self.gh = gh
self.source_branch = source_branch
self.target_branch = target_branch
(self.target_owner, self.target_repo_name, self.target_branch_name) = (
parse_github_url(self.target_branch))
(self.source_owner, self.source_repo_name, self.source_branch_name) = (
parse_github_url(self.source_branch))
def get_infotext(self):
"""Determine the initial comment for the merge proposal."""
info = []
info.append("Merge %s into %s:%s\n" % (
self.source_branch_name, self.target_owner,
self.target_branch_name))
info.append("Source: %s\n" % self.source_branch.user_url)
info.append("Target: %s\n" % self.target_branch.user_url)
return ''.join(info)
def get_initial_body(self):
"""Get a body for the proposal for the user to modify.
:return: a str or None.
"""
return None
def create_proposal(self, description, reviewers=None, labels=None):
"""Perform the submission."""
import github
# TODO(jelmer): Probe for right repo name
if self.target_repo_name.endswith('.git'):
self.target_repo_name = self.target_repo_name[:-4]
target_repo = self.gh.get_repo("%s/%s" % (self.target_owner, self.target_repo_name))
# TODO(jelmer): Allow setting title explicitly?
title = description.splitlines()[0]
# TOOD(jelmer): Set maintainers_can_modify?
try:
pull_request = target_repo.create_pull(
title=title, body=description,
head="%s:%s" % (self.source_owner, self.source_branch_name),
base=self.target_branch_name)
except github.GithubException as e:
if e.status == 422:
raise MergeProposalExists(self.source_branch.user_url)
raise
if reviewers:
for reviewer in reviewers:
pull_request.assignees.append(
self.gh.get_user(reviewer))
if labels:
for label in labels:
pull_request.issue.labels.append(label)
return MergeProposal(pull_request.html_url)
|