From a33d2ce141a8a0b9363780cc406a8fe1558a5d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=BChnel?= Date: Fri, 18 Oct 2019 13:59:58 +0200 Subject: [PATCH] added script to apply arc patch --- Jenkins/Phabricator-pipeline/Jenkinsfile | 5 +- scripts/phabtalk/README.md | 2 + scripts/phabtalk/apply_patch.py | 59 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 scripts/phabtalk/README.md create mode 100755 scripts/phabtalk/apply_patch.py diff --git a/Jenkins/Phabricator-pipeline/Jenkinsfile b/Jenkins/Phabricator-pipeline/Jenkinsfile index 48b8a2b..2d027ef 100644 --- a/Jenkins/Phabricator-pipeline/Jenkinsfile +++ b/Jenkins/Phabricator-pipeline/Jenkinsfile @@ -18,6 +18,9 @@ pipeline { string(name: 'DIFF_ID') string(name: 'PHID') } + environment { + CONDUIT_TOKEN = credentials('phabricator-conduit-token') + } stages { stage("git checkout"){ steps { @@ -31,7 +34,7 @@ pipeline { } stage('arc patch') { steps { - echo 'TODO: arc patch' + sh 'llvm-premerge-checks/scripts/phabtalk/apply_patch.py --host="https://reviews.llvm.org" --conduit-token="${CONDUIT_TOKEN}"' } } stage('CMake') { diff --git a/scripts/phabtalk/README.md b/scripts/phabtalk/README.md new file mode 100644 index 0000000..8005744 --- /dev/null +++ b/scripts/phabtalk/README.md @@ -0,0 +1,2 @@ +This folder contains Python scripts that to Phabricator. +They require a few libraries listed in `requirements.txt`. \ No newline at end of file diff --git a/scripts/phabtalk/apply_patch.py b/scripts/phabtalk/apply_patch.py new file mode 100755 index 0000000..2d58d55 --- /dev/null +++ b/scripts/phabtalk/apply_patch.py @@ -0,0 +1,59 @@ +#!/bin/env python3 +# Copyright 2019 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 argparse +import os +import subprocess +from phabricator import Phabricator + + +def main(): + args = _parse_args() + diff_id = os.environ['DIFF_ID'] + phid = os.environ['PHID'] + + phab = Phabricator(token=args.conduit_token, host=args.host+'/api/') + phab.update_interfaces() + + _git_checkout(_get_parent_hash(diff_id, phab)) + _apply_patch(diff_id, args.conduit_token, args.host) + + +def _get_parent_hash(diff_id: str, phab:Phabricator) -> str: + diff = phab.differential.getdiff(diff_id=diff_id) + return diff['sourceControlBaseRevision'] + +def _git_checkout(git_hash:str): + subprocess.check_call('git reset --hard {}'.format(git_hash), shell=True) + subprocess.check_call('git clean -fdx', shell=True) + +def _apply_patch(diff_id: str, conduit_token: str, host: str): + cmd = 'arc patch --nobranch --no-ansi --diff {} --nocommit '\ + '--conduit-token "{}" --conduit-uri "{}"'.format( + diff_id, conduit_token, host ) + subprocess.call(cmd, shell=True) + + +def _parse_args(): + parser = argparse.ArgumentParser(description='Apply a phabricator patch.') + parser.add_argument('--conduit-token', type=str, dest='conduit_token', default=None) + parser.add_argument('--host', type=str, dest='host', default="None", + help="full URL to API with trailing slash, e.g. https://reviews.llvm.org/api/") + + return parser.parse_args() + + +if __name__ == "__main__": + main() +