Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split auto-merge to two commands #7

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Tox test
strategy:
matrix:
tox_env: [py36, py37, py38, py39, py310, py311, py312]
tox_env: [py38, py39, py310, py311, py312]
# Use GitHub's Linux Docker host
runs-on: ubuntu-latest
steps:
Expand Down
38 changes: 14 additions & 24 deletions auto-merger
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,29 @@ import sys

import click

from auto_merger.merger import AutoMerger
from auto_merger.utils import setup_logger

from auto_merger.config import GlobalConfig
from auto_merger.utils import setup_logger
from auto_merger.cli.pr_checker import pr_checker
from auto_merger.cli.merger import merger
logger = logging.getLogger(__name__)


@click.command()
@click.group("auto-merger")
@click.option("-d", "--debug", is_flag=True, help="Enable debug logs")
@click.option("--print-results", is_flag=True, help="Prints readable summary")
@click.option("--github-labels", required=True, multiple=True,
help="Specify Git Hub labels to meet criteria")
@click.option("--blocking-labels", multiple=True,
help="Specify Git Hub labels that blocks PR to merge")
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
@click.option("--approvals",
default=2, type=int,
help="Specify number of approvals to automatically merge PR. Default 2")
def auto_merger(debug, print_results, github_labels, blocking_labels, approvals, send_email):
am = AutoMerger(github_labels, blocking_labels, approvals)
@click.pass_context
def auto_merger(ctx, debug):
ctx.obj = GlobalConfig(debug=debug)
if debug:
setup_logger("auto-merger", level=logging.DEBUG)
logger.debug("Logging set to DEBUG")
else:
setup_logger("auto-merger", level=logging.INFO)
ret_value = am.check_all_containers()
if ret_value != 0:
sys.exit(2)
if print_results:
am.print_blocked_pull_request()
am.print_approval_pull_request()
if not am.send_results(send_email):
sys.exit(1)
sys.exit(ret_value)
logger.debug("Logging set to INFO")

auto_merger.add_command(pr_checker)
auto_merger.add_command(merger)


if __name__ == "__main__":
auto_merger()
auto_merger(obj={})
60 changes: 60 additions & 0 deletions auto_merger/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# MIT License
#
# Copyright (c) 2018-2019 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 logging
import sys

from pathlib import Path
from typing import Dict

from auto_merger.pr_checker import PRStatusChecker
from auto_merger.merger import AutoMerger

logger = logging.getLogger(__name__)


def pr_checker(print_results, github_labels, blocking_labels, approvals, send_email) -> int:
"""
Checks NVR from brew build against pulp
"""
pr_status_checker = PRStatusChecker(github_labels, blocking_labels, approvals)
ret_value = pr_status_checker.check_all_containers()
if ret_value != 0:
return ret_value
if print_results:
pr_status_checker.print_blocked_pull_request()
pr_status_checker.print_approval_pull_request()
if not pr_status_checker.send_results(send_email):
return 1
return ret_value


def merger(print_results, merger_labels, approvals, pr_lifetime, send_email) -> int:
auto_merger = AutoMerger(merger_labels, approvals, pr_lifetime)
ret_value = auto_merger.check_all_containers()
if ret_value != 0:
return ret_value
if print_results:
auto_merger.print_pull_request_to_merge()
if not auto_merger.send_results(send_email):
return 1
return ret_value
Empty file added auto_merger/cli/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions auto_merger/cli/merger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# MIT License
#
# Copyright (c) 2018-2019 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 logging
import click
import sys

from auto_merger.config import pass_global_config
from auto_merger import api
logger = logging.getLogger(__name__)


@click.command("merger")
@click.option("--print-results", is_flag=True, help="Prints readable summary")
@click.option("--merger-labels", required=True, multiple=True,
help="Specify Git Hub labels to meet criteria")
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
@click.option("--approvals",
default=2, type=int,
help="Specify number of approvals to automatically merge PR. Default 2")
@click.option("--pr-lifetime", default=1, type=int, help="Specify a smallest time for which PR should opened")
@pass_global_config
def merger(ctx, print_results, merger_labels, approvals, pr_lifetime, send_email):
logger.debug(ctx.debug)
ret_value = api.merger(print_results, merger_labels, approvals, pr_lifetime, send_email)
sys.exit(ret_value)

49 changes: 49 additions & 0 deletions auto_merger/cli/pr_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# MIT License
#
# Copyright (c) 2018-2019 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 logging
import click
import sys

from auto_merger.config import pass_global_config
from auto_merger import api

logger = logging.getLogger(__name__)


@click.command("pr-checker")
@click.option("--print-results", is_flag=True, help="Prints readable summary")
@click.option("--github-labels", required=True, multiple=True,
help="Specify Git Hub labels to meet criteria")
@click.option("--blocking-labels", multiple=True,
help="Specify Git Hub labels that blocks PR to merge")
@click.option("--send-email", multiple=True, help="Specify email addresses to which the mail will be sent.")
@click.option("--approvals",
default=2, type=int,
help="Specify number of approvals to automatically merge PR. Default 2")
@pass_global_config
def pr_checker(ctx, print_results, github_labels, blocking_labels, approvals, send_email):
logger.debug(ctx.debug)
ret_value = api.pr_checker(print_results, github_labels, blocking_labels, approvals, send_email)
sys.exit(ret_value)

32 changes: 32 additions & 0 deletions auto_merger/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# MIT License
#
# Copyright (c) 2018-2019 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 click


class GlobalConfig(object):
def __init__(self, debug: bool = False):
self.debug = debug


pass_global_config = click.make_pass_decorator(GlobalConfig)
Loading
Loading