diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 0000000000000000000000000000000000000000..581910663c493e29361c8e9b0f364720ada62496 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,48 @@ +name: preview + +on: + # Run the workflow when any branch is updated, since we can't seem to filter + # on the preview branches and main only. + - push + + # Whenever a pull request is labeled or unlabeled. + - pull_request + +permissions: + # Necessary to be able to push to the repo + contents: write + +jobs: + preview: + name: preview + # Set the type of machine to run on + # https://github.com/actions/virtual-environments + runs-on: ubuntu-latest + + if: ${{ github.event.label.name == 'preview' || (github.event.action == 'push' && contains(github.event.pull_request.labels.*.name, 'preview')) }} + + steps: + + # Checks out a copy of your repository on the virtual machine + - uses: actions/checkout@v2 + with: + ref: ${{ github.sha }} + + - uses: actions/setup-python@v2 + with: + python-version: "3.11" + + - name: Setup git + run: | + git config --global user.name 'GitHub Action' + git config --global user.email 'GitHub Action' + + git remote -v + git branch --list --remotes + + - name: Create preview branch + run: | + set -e + source init_env + lisa-make-preview + git push origin preview --force diff --git a/setup.py b/setup.py index bde44be5fc67601652694f2e72bf7d4fdae51c4d..76cde7cd4bb90c7fc856212b10b44ca5bf43e730 100755 --- a/setup.py +++ b/setup.py @@ -69,6 +69,7 @@ extras_require={ "pytest", "build", "twine", + "github3.py", ], "wa": [ diff --git a/tools/lisa-make-preview b/tools/lisa-make-preview new file mode 100755 index 0000000000000000000000000000000000000000..e81a4a91d305b011f4391ee9a50d5d2b74283d94 --- /dev/null +++ b/tools/lisa-make-preview @@ -0,0 +1,107 @@ +#! /usr/bin/env python3 +# +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2023, Arm Limited and contributors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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 subprocess +from itertools import starmap, chain +from tempfile import NamedTemporaryFile +import json +from collections import ChainMap + +from github3 import GitHub + +def main(): + owner = 'ARM-Software' + repo = 'lisa' + + gh = GitHub() + issues = gh.issues_on( + username=owner, + repository=repo, + state='open', + labels=['preview'], + ) + + prs = [ + ( + issue, + gh.pull_request( + owner=owner, + repository=repo, + number=issue.number + ) + ) + for issue in issues + if issue.pull_request_urls + ] + + def make_topic(issue, pr): + remote = f'remote_{pr.head.ref}' + return ( + { + remote: { + 'url': pr.head.repository.clone_url + } + }, + { + 'name': pr.head.ref, + 'remote': remote, + 'base': pr.base.ref, + 'tip': pr.head.ref, + } + ) + + topics = sorted(starmap(make_topic, prs)) + if topics: + remotes, topics = zip(*topics) + remotes = dict(ChainMap(*chain( + [{ + 'github': { + 'url': f'https://github.com/{owner}/{repo}.git' + } + }], + remotes + ))) + + conf = { + 'rebase-conf': { + 'rr-cache': './rr-cache', + 'remotes': remotes, + 'base': { + 'remote': 'github', + 'ref': 'main', + }, + 'topics': sorted(topics) + } + } + conf = json.dumps(conf, indent=4) + print(conf) + + with NamedTemporaryFile(mode='w+', suffix='.manifest.json') as f: + f.write(conf) + f.flush() + + manifest = f.name + + cmd = ['batch-rebase', 'create', '.', '--manifest', manifest, '--create-branch', 'preview'] + print(cmd) + subprocess.check_call(cmd) + else: + print('No topics found') + +main()