From 13a8d8a41f4b1777318ea5299c07720fcdc53286 Mon Sep 17 00:00:00 2001 From: "Petr \"Stone\" Hracek" Date: Wed, 4 Sep 2024 09:34:14 +0200 Subject: [PATCH] First draft of automerger Signed-off-by: Petr "Stone" Hracek --- auto-merger/LICENSE.txt | 21 ++++++ auto-merger/README.md | 0 auto-merger/auto_merger/__init__.py | 0 auto-merger/auto_merger/merger.py | 60 ++++++++++++++++ auto-merger/auto_merger/utils.py | 68 +++++++++++++++++++ auto-merger/pytest.ini | 3 + auto-merger/tests/data/pr_missing_ci.json | 44 ++++++++++++ auto-merger/tests/data/pr_missing_review.json | 12 ++++ .../data/pr_missing_review_ci_failed.json | 44 ++++++++++++ auto-merger/tox.ini | 5 ++ 10 files changed, 257 insertions(+) create mode 100644 auto-merger/LICENSE.txt create mode 100644 auto-merger/README.md create mode 100644 auto-merger/auto_merger/__init__.py create mode 100644 auto-merger/auto_merger/merger.py create mode 100644 auto-merger/auto_merger/utils.py create mode 100644 auto-merger/pytest.ini create mode 100644 auto-merger/tests/data/pr_missing_ci.json create mode 100644 auto-merger/tests/data/pr_missing_review.json create mode 100644 auto-merger/tests/data/pr_missing_review_ci_failed.json create mode 100644 auto-merger/tox.ini diff --git a/auto-merger/LICENSE.txt b/auto-merger/LICENSE.txt new file mode 100644 index 0000000..462d1c5 --- /dev/null +++ b/auto-merger/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Red Hat, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/auto-merger/README.md b/auto-merger/README.md new file mode 100644 index 0000000..e69de29 diff --git a/auto-merger/auto_merger/__init__.py b/auto-merger/auto_merger/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auto-merger/auto_merger/merger.py b/auto-merger/auto_merger/merger.py new file mode 100644 index 0000000..f90fc80 --- /dev/null +++ b/auto-merger/auto_merger/merger.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# MIT License +# +# Copyright (c) 2024 Red Hat, Inc. + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +import json +import sys + +from typing import List + + +from auto_merger import utils + + +class AutoMerger: + repo_data: List = [] + + def __init__(self, container_name: str): + self.container_name = container_name + + def get_gh_pr_list(self): + cmd = ["gh pr list -s open --json number,title,labels"] + gh_repo_list = utils.run_command(cmd=cmd, return_output=True) + self.repo_data = json.loads(gh_repo_list) + + def clone_repo(self): + pass + + def merge_pull_request(self): + pass + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("usage: merger.py [container-name]") + print('OUTPUT DIR is ".", if not specified otherwise') + sys.exit(5) + auto_merger = AutoMerger(container_name=sys.argv[1]) + auto_merger.get_gh_pr_list() + auto_merger.merge_pull_request() diff --git a/auto-merger/auto_merger/utils.py b/auto-merger/auto_merger/utils.py new file mode 100644 index 0000000..9e27b83 --- /dev/null +++ b/auto-merger/auto_merger/utils.py @@ -0,0 +1,68 @@ +# MIT License +# +# Copyright (c) 2024 Red Hat, Inc. + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import subprocess +import logging + +logger = logging.getLogger(__name__) + + +def run_command( + cmd, + return_output: bool = True, + ignore_error: bool = False, + shell: bool = True, + debug: bool = False, + **kwargs, +): + """ + Run provided command on host system using the same user as invoked this code. + Raises subprocess.CalledProcessError if it fails. + :param cmd: list or str + :param return_output: bool, return output of the command + :param ignore_error: bool, do not fail in case nonzero return code + :param shell: bool, run command in shell + :param debug: bool, print command in shell, default is suppressed + :return: None or str + """ + if debug: + logger.debug(f"command: {cmd}") + try: + if return_output: + return subprocess.check_output( + cmd, + stderr=subprocess.STDOUT, + universal_newlines=True, + shell=shell, + **kwargs, + ) + else: + return subprocess.check_call(cmd, shell=shell, **kwargs) + except subprocess.CalledProcessError as cpe: + if ignore_error: + if return_output: + return cpe.output + else: + return cpe.returncode + else: + logger.error(f"failed with code {cpe.returncode} and output:\n{cpe.output}") + raise cpe diff --git a/auto-merger/pytest.ini b/auto-merger/pytest.ini new file mode 100644 index 0000000..c5aa348 --- /dev/null +++ b/auto-merger/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +pythonpath = auto_merger +testpaths = tests diff --git a/auto-merger/tests/data/pr_missing_ci.json b/auto-merger/tests/data/pr_missing_ci.json new file mode 100644 index 0000000..c1e1547 --- /dev/null +++ b/auto-merger/tests/data/pr_missing_ci.json @@ -0,0 +1,44 @@ +[ + { + "labels": [ + { + "id": "MDU6TGFiZWwxNzM3MjEwMTc=", + "name": "enhancement", + "description": "", + "color": "84b6eb" + }, + { + "id": "MDU6TGFiZWwzNTI2MjY1NDA=", + "name": "P1", + "description": "", + "color": "d93f0b" + }, + { + "id": "LA_kwDOAc6Y3c7cwogD", + "name": "ready for review", + "description": "The pull request is ready to review", + "color": "0052CC" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABpEG3Zg", + "name": "easyfix", + "description": "", + "color": "0E8A16" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABrYkcpw", + "name": "pr/missing-review", + "description": "", + "color": "ededed" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABrdCuRQ", + "name": "pr/failing-ci", + "description": "", + "color": "ededed" + } + ], + "number": 314, + "title": "Support s2i-core building and testing on Fedora 41" + } +] diff --git a/auto-merger/tests/data/pr_missing_review.json b/auto-merger/tests/data/pr_missing_review.json new file mode 100644 index 0000000..c1e291c --- /dev/null +++ b/auto-merger/tests/data/pr_missing_review.json @@ -0,0 +1,12 @@ +{ + "labels": [ + { + "id": "LA_kwDOA3sMzs8AAAABqTWftQ", + "name": "pr/missing-review", + "description": "", + "color": "ededed" + } + ], + "number": 192, + "title": "[WIP] Make tests work also for rootless containers" +} diff --git a/auto-merger/tests/data/pr_missing_review_ci_failed.json b/auto-merger/tests/data/pr_missing_review_ci_failed.json new file mode 100644 index 0000000..c1e1547 --- /dev/null +++ b/auto-merger/tests/data/pr_missing_review_ci_failed.json @@ -0,0 +1,44 @@ +[ + { + "labels": [ + { + "id": "MDU6TGFiZWwxNzM3MjEwMTc=", + "name": "enhancement", + "description": "", + "color": "84b6eb" + }, + { + "id": "MDU6TGFiZWwzNTI2MjY1NDA=", + "name": "P1", + "description": "", + "color": "d93f0b" + }, + { + "id": "LA_kwDOAc6Y3c7cwogD", + "name": "ready for review", + "description": "The pull request is ready to review", + "color": "0052CC" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABpEG3Zg", + "name": "easyfix", + "description": "", + "color": "0E8A16" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABrYkcpw", + "name": "pr/missing-review", + "description": "", + "color": "ededed" + }, + { + "id": "LA_kwDOAc6Y3c8AAAABrdCuRQ", + "name": "pr/failing-ci", + "description": "", + "color": "ededed" + } + ], + "number": 314, + "title": "Support s2i-core building and testing on Fedora 41" + } +] diff --git a/auto-merger/tox.ini b/auto-merger/tox.ini new file mode 100644 index 0000000..9366ad0 --- /dev/null +++ b/auto-merger/tox.ini @@ -0,0 +1,5 @@ +[testenv] +commands = python3 -m pytest --color=yes -v +deps = + pytest + PyYAML