Skip to content

Commit

Permalink
First draft of automerger
Browse files Browse the repository at this point in the history
Signed-off-by: Petr "Stone" Hracek <[email protected]>
  • Loading branch information
phracek committed Sep 4, 2024
1 parent efbf3e7 commit 13a8d8a
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 0 deletions.
21 changes: 21 additions & 0 deletions auto-merger/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
Empty file added auto-merger/README.md
Empty file.
Empty file.
60 changes: 60 additions & 0 deletions auto-merger/auto_merger/merger.py
Original file line number Diff line number Diff line change
@@ -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()
68 changes: 68 additions & 0 deletions auto-merger/auto_merger/utils.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions auto-merger/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
pythonpath = auto_merger
testpaths = tests
44 changes: 44 additions & 0 deletions auto-merger/tests/data/pr_missing_ci.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
12 changes: 12 additions & 0 deletions auto-merger/tests/data/pr_missing_review.json
Original file line number Diff line number Diff line change
@@ -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"
}
44 changes: 44 additions & 0 deletions auto-merger/tests/data/pr_missing_review_ci_failed.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
5 changes: 5 additions & 0 deletions auto-merger/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[testenv]
commands = python3 -m pytest --color=yes -v
deps =
pytest
PyYAML

0 comments on commit 13a8d8a

Please sign in to comment.