2020-03-10 11:45:39 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2020 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://llvm.org/LICENSE.txt
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
from typing import Optional
|
|
|
|
import git
|
|
|
|
import logging
|
2020-03-10 16:11:41 +01:00
|
|
|
from phab_wrapper import PhabWrapper, Revision
|
|
|
|
import subprocess
|
2020-03-10 17:57:05 +01:00
|
|
|
import github
|
|
|
|
import json
|
|
|
|
import sys
|
2020-03-10 11:45:39 +01:00
|
|
|
|
|
|
|
# TODO: move to config file
|
|
|
|
LLVM_GITHUB_URL = 'ssh://git@github.com/llvm/llvm-project'
|
2020-03-10 17:57:05 +01:00
|
|
|
MY_GITHUB_URL = 'ssh://git@github.com/christiankuehnel/llvm-project'
|
|
|
|
MY_REPO = 'christiankuehnel/llvm-project'
|
2020-03-10 11:45:39 +01:00
|
|
|
_LOGGER = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class Phab2Github:
|
|
|
|
|
|
|
|
def __init__(self, workdir: str):
|
|
|
|
self.workdir = workdir
|
|
|
|
self.llvm_dir = os.path.join(self.workdir, 'llvm-project')
|
|
|
|
self.repo = None # type: Optional[git.Repo]
|
2020-03-10 16:11:41 +01:00
|
|
|
self.phab_wrapper = PhabWrapper()
|
2020-03-10 17:57:05 +01:00
|
|
|
self.github = self._create_github()
|
|
|
|
self.github_repo = self.github.get_repo(MY_REPO) # type: github.Repository
|
2020-03-10 11:45:39 +01:00
|
|
|
|
|
|
|
def sync(self):
|
|
|
|
_LOGGER.info('Starting sync...')
|
|
|
|
self._refresh_master()
|
2020-03-10 16:11:41 +01:00
|
|
|
revisions = self.phab_wrapper.get_revisions()
|
|
|
|
for revision in revisions:
|
2020-03-10 17:57:05 +01:00
|
|
|
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)
|
2020-03-10 11:45:39 +01:00
|
|
|
|
|
|
|
def _refresh_master(self):
|
|
|
|
if not os.path.exists(self.workdir):
|
|
|
|
os.mkdir(self.workdir)
|
|
|
|
if os.path.exists(self.llvm_dir):
|
|
|
|
# TODO: in case of errors: delete and clone
|
|
|
|
_LOGGER.info('pulling origin...')
|
|
|
|
self.repo = git.Repo(self.llvm_dir)
|
2020-03-10 17:57:05 +01:00
|
|
|
self.repo.git.fetch()
|
|
|
|
self.repo.git.checkout('master')
|
|
|
|
self.repo.git.pull('upstream', 'master')
|
|
|
|
self.repo.git.push('origin', 'master')
|
2020-03-10 11:45:39 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.info('cloning repository...')
|
2020-03-10 17:57:05 +01:00
|
|
|
git.Repo.clone_from(MY_GITHUB_URL, self.llvm_dir)
|
2020-03-10 16:11:41 +01:00
|
|
|
self.repo = git.Repo(self.llvm_dir)
|
2020-03-10 17:57:05 +01:00
|
|
|
self.repo.create_remote('upstream', url=LLVM_GITHUB_URL)
|
|
|
|
self.repo.remotes.upstream.fetch()
|
2020-03-10 11:45:39 +01:00
|
|
|
_LOGGER.info('refresh of master branch completed')
|
|
|
|
|
2020-03-10 16:11:41 +01:00
|
|
|
def create_branch(self, revision: Revision):
|
2020-03-10 17:57:05 +01:00
|
|
|
name = revision.branch_name
|
2020-03-10 16:11:41 +01:00
|
|
|
if name in self.repo.heads:
|
2020-03-10 17:57:05 +01:00
|
|
|
self.repo.git.checkout('master')
|
|
|
|
self.repo.git.branch('-D', name)
|
2020-03-10 16:11:41 +01:00
|
|
|
base_hash = revision.latest_diff.base_hash
|
|
|
|
if base_hash is None:
|
2020-03-10 17:57:05 +01:00
|
|
|
base_hash = 'upstream/master'
|
2020-03-10 16:11:41 +01:00
|
|
|
_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
|
2020-03-10 17:57:05 +01:00
|
|
|
_LOGGER.warning('commit hash {} not found in upstream repository. '
|
|
|
|
'Trying master instead...'.format(name, base_hash))
|
|
|
|
base_hash = 'upstream/master'
|
2020-03-10 16:11:41 +01:00
|
|
|
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):
|
2020-03-10 17:57:05 +01:00
|
|
|
proc = subprocess.run('git apply --ignore-whitespace --whitespace=fix -', input=raw_patch, shell=True, text=True, cwd=self.repo.working_dir,
|
2020-03-10 16:11:41 +01:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
if proc.returncode != 0:
|
|
|
|
raise Exception('Applying patch failed:\n{}'.format(proc.stdout + proc.stderr))
|
2020-03-10 17:57:05 +01:00
|
|
|
self.repo.git.add('-A')
|
2020-03-10 16:11:41 +01:00
|
|
|
self.repo.index.commit(message='applying diff Phabricator Revision {}\n\ndiff: {}'.format(diff.revision, diff.id))
|
|
|
|
|
2020-03-10 17:57:05 +01:00
|
|
|
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)
|
|
|
|
|
2020-03-10 11:45:39 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-03-10 16:11:41 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2020-03-10 11:45:39 +01:00
|
|
|
rootdir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
tmpdir = os.path.join(rootdir, 'tmp')
|
|
|
|
p2g = Phab2Github(tmpdir)
|
|
|
|
p2g.sync()
|