1
0
Fork 0

creating pull requests works

This commit is contained in:
Christian Kühnel 2020-03-10 17:57:05 +01:00
parent 4aed7decc3
commit 1d171c0252
3 changed files with 58 additions and 13 deletions

View file

@ -19,9 +19,14 @@ import git
import logging
from phab_wrapper import PhabWrapper, Revision
import subprocess
import github
import json
import sys
# TODO: move to config file
LLVM_GITHUB_URL = 'ssh://git@github.com/llvm/llvm-project'
MY_GITHUB_URL = 'ssh://git@github.com/christiankuehnel/llvm-project'
MY_REPO = 'christiankuehnel/llvm-project'
_LOGGER = logging.getLogger()
@ -32,15 +37,26 @@ class Phab2Github:
self.llvm_dir = os.path.join(self.workdir, 'llvm-project')
self.repo = None # type: Optional[git.Repo]
self.phab_wrapper = PhabWrapper()
self.github = self._create_github()
self.github_repo = self.github.get_repo(MY_REPO) # type: github.Repository
def sync(self):
_LOGGER.info('Starting sync...')
self._refresh_master()
revisions = self.phab_wrapper.get_revisions()
for revision in revisions:
self.create_branch(revision)
self.apply_patch(revision.latest_diff,
self.phab_wrapper.get_raw_patch(revision.latest_diff))
try:
self.create_branch(revision)
self.apply_patch(revision.latest_diff,
self.phab_wrapper.get_raw_patch(revision.latest_diff))
self.repo.git.push('--force', 'origin', revision.branch_name)
# TODO: check if pull request already exists
self.github_repo.create_pull(title=revision.title,
body=revision.summary,
head=revision.branch_name,
base='master')
except Exception as e:
_LOGGER.exception(e)
def _refresh_master(self):
if not os.path.exists(self.workdir):
@ -49,39 +65,55 @@ class Phab2Github:
# TODO: in case of errors: delete and clone
_LOGGER.info('pulling origin...')
self.repo = git.Repo(self.llvm_dir)
self.repo.remotes.origin.fetch()
self.repo.git.fetch()
self.repo.git.checkout('master')
self.repo.git.pull('upstream', 'master')
self.repo.git.push('origin', 'master')
else:
_LOGGER.info('cloning repository...')
git.Repo.clone_from(LLVM_GITHUB_URL, self.llvm_dir)
git.Repo.clone_from(MY_GITHUB_URL, self.llvm_dir)
self.repo = git.Repo(self.llvm_dir)
self.repo.create_remote('upstream', url=LLVM_GITHUB_URL)
self.repo.remotes.upstream.fetch()
_LOGGER.info('refresh of master branch completed')
def create_branch(self, revision: Revision):
name = 'phab-D{}'.format(revision.id)
name = revision.branch_name
if name in self.repo.heads:
self.repo.head.reference = self.repo.heads['master']
self.repo.head.reset(index=True, working_tree=True)
self.repo.delete_head(name)
self.repo.git.checkout('master')
self.repo.git.branch('-D', name)
base_hash = revision.latest_diff.base_hash
if base_hash is None:
base_hash = 'origin/master'
base_hash = 'upstream/master'
_LOGGER.info('creating branch {} based one {}...'.format(name, base_hash))
try:
new_branch = self.repo.create_head(name, base_hash)
except ValueError:
# commit hash not found, try again with master
base_hash = 'origin/master'
_LOGGER.warning('commit hash {} not found in upstream repository. '
'Trying master instead...'.format(name, base_hash))
base_hash = 'upstream/master'
new_branch = self.repo.create_head(name, base_hash)
self.repo.head.reference = new_branch
self.repo.head.reset(index=True, working_tree=True)
def apply_patch(self, diff: "Diff", raw_patch: str):
proc = subprocess.run('git apply --ignore-whitespace --whitespace=fix -', input=raw_patch, shell=True, text=True,
proc = subprocess.run('git apply --ignore-whitespace --whitespace=fix -', input=raw_patch, shell=True, text=True, cwd=self.repo.working_dir,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if proc.returncode != 0:
raise Exception('Applying patch failed:\n{}'.format(proc.stdout + proc.stderr))
self.repo.git.add('-A')
self.repo.index.commit(message='applying diff Phabricator Revision {}\n\ndiff: {}'.format(diff.revision, diff.id))
def _create_github(self) -> github.Github:
"""Create instance of Github client.
Reads access token from a file.
"""
with open(os.path.expanduser('~/.llvm-premerge-checks/github-token.json')) as json_file:
token = json.load(json_file)['token']
return github.Github(token)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)

View file

@ -45,12 +45,22 @@ class Revision:
def __str__(self):
return 'Revision {}: {} - ({})'.format(self.id, self.status,
','.join([str(d.id) for d in self.diffs]))
@property
def latest_diff(self):
# TODO: make sure self.diffs is sorted
return self.diffs[-1]
@property
def branch_name(self) -> str:
return 'phab-D{}'.format(self.id)
@property
def title(self) -> str:
return self.revision_dict['fields']['title']
@property
def summary(self) -> str:
return self.revision_dict['fields']['summary']
class Diff:
"""A Phabricator diff."""
@ -119,6 +129,8 @@ class PhabWrapper:
revision_response = self.phab.differential.revision.search(
constraints=constraints)
revisions = [Revision(r) for r in revision_response.response['data']]
# TODO: only taking the first 10 to speed things up
revisions = revisions[0:3]
_LOGGER.info('Got {} revisions from the server'.format(len(revisions)))
for revision in revisions:
# TODO: batch-query diffs for all revisions, reduce number of

View file

@ -1,3 +1,4 @@
phabricator==0.7.0
gitpython==3.0.6
retrying==1.3.3
PyGitHub=1.46