Skip to content

Commit d43435f

Browse files
authored
Merge pull request #7 from sclorg/split_to_two_commands
Split auto-merge to two commands
2 parents 78baee9 + 40b3bbd commit d43435f

File tree

12 files changed

+534
-119
lines changed

12 files changed

+534
-119
lines changed

.github/workflows/python-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Tox test
1414
strategy:
1515
matrix:
16-
tox_env: [py36, py37, py38, py39, py310, py311, py312]
16+
tox_env: [py38, py39, py310, py311, py312]
1717
# Use GitHub's Linux Docker host
1818
runs-on: ubuntu-latest
1919
steps:

auto-merger

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,29 @@ import sys
3030

3131
import click
3232

33-
from auto_merger.merger import AutoMerger
34-
from auto_merger.utils import setup_logger
3533

34+
from auto_merger.config import GlobalConfig
35+
from auto_merger.utils import setup_logger
36+
from auto_merger.cli.pr_checker import pr_checker
37+
from auto_merger.cli.merger import merger
3638
logger = logging.getLogger(__name__)
3739

3840

39-
@click.command()
41+
@click.group("auto-merger")
4042
@click.option("-d", "--debug", is_flag=True, help="Enable debug logs")
41-
@click.option("--print-results", is_flag=True, help="Prints readable summary")
42-
@click.option("--github-labels", required=True, multiple=True,
43-
help="Specify Git Hub labels to meet criteria")
44-
@click.option("--blocking-labels", multiple=True,
45-
help="Specify Git Hub labels that blocks PR to merge")
46-
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
47-
@click.option("--approvals",
48-
default=2, type=int,
49-
help="Specify number of approvals to automatically merge PR. Default 2")
50-
def auto_merger(debug, print_results, github_labels, blocking_labels, approvals, send_email):
51-
am = AutoMerger(github_labels, blocking_labels, approvals)
43+
@click.pass_context
44+
def auto_merger(ctx, debug):
45+
ctx.obj = GlobalConfig(debug=debug)
5246
if debug:
5347
setup_logger("auto-merger", level=logging.DEBUG)
48+
logger.debug("Logging set to DEBUG")
5449
else:
5550
setup_logger("auto-merger", level=logging.INFO)
56-
ret_value = am.check_all_containers()
57-
if ret_value != 0:
58-
sys.exit(2)
59-
if print_results:
60-
am.print_blocked_pull_request()
61-
am.print_approval_pull_request()
62-
if not am.send_results(send_email):
63-
sys.exit(1)
64-
sys.exit(ret_value)
51+
logger.debug("Logging set to INFO")
52+
53+
auto_merger.add_command(pr_checker)
54+
auto_merger.add_command(merger)
6555

6656

6757
if __name__ == "__main__":
68-
auto_merger()
58+
auto_merger(obj={})

auto_merger/api.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018-2019 Red Hat, Inc.
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
import logging
24+
import sys
25+
26+
from pathlib import Path
27+
from typing import Dict
28+
29+
from auto_merger.pr_checker import PRStatusChecker
30+
from auto_merger.merger import AutoMerger
31+
32+
logger = logging.getLogger(__name__)
33+
34+
35+
def pr_checker(print_results, github_labels, blocking_labels, approvals, send_email) -> int:
36+
"""
37+
Checks NVR from brew build against pulp
38+
"""
39+
pr_status_checker = PRStatusChecker(github_labels, blocking_labels, approvals)
40+
ret_value = pr_status_checker.check_all_containers()
41+
if ret_value != 0:
42+
return ret_value
43+
if print_results:
44+
pr_status_checker.print_blocked_pull_request()
45+
pr_status_checker.print_approval_pull_request()
46+
if not pr_status_checker.send_results(send_email):
47+
return 1
48+
return ret_value
49+
50+
51+
def merger(print_results, merger_labels, approvals, pr_lifetime, send_email) -> int:
52+
auto_merger = AutoMerger(merger_labels, approvals, pr_lifetime)
53+
ret_value = auto_merger.check_all_containers()
54+
if ret_value != 0:
55+
return ret_value
56+
if print_results:
57+
auto_merger.print_pull_request_to_merge()
58+
if not auto_merger.send_results(send_email):
59+
return 1
60+
return ret_value

auto_merger/cli/__init__.py

Whitespace-only changes.

auto_merger/cli/merger.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018-2019 Red Hat, Inc.
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import logging
25+
import click
26+
import sys
27+
28+
from auto_merger.config import pass_global_config
29+
from auto_merger import api
30+
logger = logging.getLogger(__name__)
31+
32+
33+
@click.command("merger")
34+
@click.option("--print-results", is_flag=True, help="Prints readable summary")
35+
@click.option("--merger-labels", required=True, multiple=True,
36+
help="Specify Git Hub labels to meet criteria")
37+
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
38+
@click.option("--approvals",
39+
default=2, type=int,
40+
help="Specify number of approvals to automatically merge PR. Default 2")
41+
@click.option("--pr-lifetime", default=1, type=int, help="Specify a smallest time for which PR should opened")
42+
@pass_global_config
43+
def merger(ctx, print_results, merger_labels, approvals, pr_lifetime, send_email):
44+
logger.debug(ctx.debug)
45+
ret_value = api.merger(print_results, merger_labels, approvals, pr_lifetime, send_email)
46+
sys.exit(ret_value)
47+

auto_merger/cli/pr_checker.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018-2019 Red Hat, Inc.
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import logging
25+
import click
26+
import sys
27+
28+
from auto_merger.config import pass_global_config
29+
from auto_merger import api
30+
31+
logger = logging.getLogger(__name__)
32+
33+
34+
@click.command("pr-checker")
35+
@click.option("--print-results", is_flag=True, help="Prints readable summary")
36+
@click.option("--github-labels", required=True, multiple=True,
37+
help="Specify Git Hub labels to meet criteria")
38+
@click.option("--blocking-labels", multiple=True,
39+
help="Specify Git Hub labels that blocks PR to merge")
40+
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
41+
@click.option("--approvals",
42+
default=2, type=int,
43+
help="Specify number of approvals to automatically merge PR. Default 2")
44+
@pass_global_config
45+
def pr_checker(ctx, print_results, github_labels, blocking_labels, approvals, send_email):
46+
logger.debug(ctx.debug)
47+
ret_value = api.pr_checker(print_results, github_labels, blocking_labels, approvals, send_email)
48+
sys.exit(ret_value)
49+

auto_merger/config.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018-2019 Red Hat, Inc.
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import click
25+
26+
27+
class GlobalConfig(object):
28+
def __init__(self, debug: bool = False):
29+
self.debug = debug
30+
31+
32+
pass_global_config = click.make_pass_decorator(GlobalConfig)

0 commit comments

Comments
 (0)