diff --git a/project.py b/project.py index 989eb88..f76e0ec 100644 --- a/project.py +++ b/project.py @@ -1393,12 +1393,16 @@ class Project(object): if not pushurl: pushurl = self.manifest.manifestProject.config.GetString('repo.pushurl') if not pushurl: - pushurl = branch.remote.name + # 从user_info中提取namespace进行push_url的set + html_url = self._UserUrl() + namespace = self._GiteeNamespace(html_url) + pushurl = 'git@gitee.com:' + '/' + namespace + # pushurl = branch.remote.name else: pushurl = pushurl.rstrip('/') + '/' + self.name - remote = self.manifest.remotes.get(branch.remote.name) - if remote and remote.autodotgit is not False: - pushurl += ".git" + # remote = self.manifest.remotes.get(branch.remote.name) + # if remote and remote.autodotgit is not False: + # pushurl += ".git" cmd = ['push'] if opt.force: @@ -1472,8 +1476,9 @@ class Project(object): post_url = '/'.join([gitee_url, namespace, self.name, 'forks']) payload = {"access_token": token} r = requests.post(post_url, json=payload) + msg = r.json() # check r.msg - return r.status_code + return r.status_code, msg def _GiteeNamespace(self, url=None): """ @@ -1492,6 +1497,12 @@ class Project(object): # print("remote.url: %s doesn't belong to gitee" % self.remote.url) raise PullRequestError("remote.url: %s doesn't belong to gitee" % self.remote.url) + def _UserUrl(self, token): + gitee_url = 'https://gitee.com/api/v5/user' + payload = {'access_token': token} + r = requests.get(gitee_url, json=payload) + return r.json()['html_url'] + def UploadForReview(self, branch=None, people=([], []), dryrun=False, diff --git a/subcmds/push.py b/subcmds/push.py index 869a1a4..3e621de 100644 --- a/subcmds/push.py +++ b/subcmds/push.py @@ -299,6 +299,19 @@ Gerrit Code Review: http://code.google.com/p/gerrit/ branch.name, str(branch.error)), file=sys.stderr) + status_code, msg = branch.project.ForkProject() + if status_code == 201: + print('[FORK OK] %-15s %s' % ( + branch.project.relpath + '/', + branch.name), + file=sys.stderr) + else: + print(('[FORK FAILED] %-15s %-15s') % ( + branch.project.relpath + '/', + branch.name, + str(msg['message'])), + file=sys.stderr) + if branch.have_pr_errors: if not branch.pull_requested: if len(str(branch.pr_error)) <= 30: diff --git a/subcmds/start.py b/subcmds/start.py index f0dff5b..07a4c01 100644 --- a/subcmds/start.py +++ b/subcmds/start.py @@ -24,6 +24,7 @@ from git_command import git import gitc_utils from progress import Progress from project import SyncBuffer +from git_config import GitConfig class Start(Command): @@ -54,6 +55,7 @@ revision specified in the manifest. if not git.check_ref_format('heads/%s' % nb): self.OptionParser.error("'%s' is not a valid name" % nb) + def Execute(self, opt, args): nb = args[0] err = [] @@ -91,11 +93,29 @@ revision specified in the manifest. if not opt.all: fork_success_count = 0 token = self.manifest.manifestProject.config.GetString('repo.token') + if not token: + token = GitConfig.ForUser().GetString('repo.token') + if not token: + sys.stderr.write('repo.token is None, Please set it, you need `repo config -h`\n') + sys.exit(1) + pushurl = self.manifest.manifestProject.config.GetString('repo.pushurl') + success_msg = None for project in all_projects: - status_code = project.ForkProject(token) + status_code, msg = project.ForkProject(token) if status_code == 201: fork_success_count += 1 - print("fork_success: {}".format(fork_success_count)) + if not success_msg: + success_msg = msg + + if fork_success_count > 0 and pushurl is None: + # 初始化push_url + # 把response中的信息存到push_url中 + ssh_url = success_msg['namespace']['ssh_url'] + pushurl = ssh_url.split('/')[0] + self.manifest.manifestProject.config.SetString('repo.pushurl', pushurl) + + + print("fork_success: %s" %fork_success_count) # 展示fork状态,失败、成功、已存在