Skip to content

Ignore BranchNotFoundError in merge status task #178

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

Merged
merged 1 commit into from
Apr 3, 2022
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: 2 additions & 0 deletions newsfragments/178.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce error noise by suppressing BranchNotFoundError in then merge branch status
handler.
101 changes: 52 additions & 49 deletions src/oca_github_bot/tasks/merge_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) ACSONE SA/NV 2019
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

import contextlib
import random
from enum import Enum

Expand Down Expand Up @@ -379,58 +380,60 @@ def _get_commit_success(org, repo, pr, gh_commit):
@task()
@switchable("merge_bot")
def merge_bot_status(org, repo, merge_bot_branch, sha):
with github.temporary_clone(org, repo, merge_bot_branch) as clone_dir:
head_sha = github.git_get_head_sha(cwd=clone_dir)
if head_sha != sha:
# the branch has evolved, this means that this status
# does not correspond to the last commit of the bot, ignore it
return
pr, _, username, _ = parse_merge_bot_branch(merge_bot_branch)
with github.login() as gh:
gh_repo = gh.repository(org, repo)
gh_pr = gh.pull_request(org, repo, pr)
gh_commit = github.gh_call(gh_repo.commit, sha)
success = _get_commit_success(org, repo, pr, gh_commit)
if success is None:
# checks in progress
with contextlib.suppress(github.BranchNotFoundError):
with github.temporary_clone(org, repo, merge_bot_branch) as clone_dir:
head_sha = github.git_get_head_sha(cwd=clone_dir)
if head_sha != sha:
# the branch has evolved, this means that this status
# does not correspond to the last commit of the bot, ignore it
return
elif success:
try:
_merge_bot_merge_pr(org, repo, merge_bot_branch, clone_dir)
except CalledProcessError as e:
cmd = " ".join(e.cmd)
pr, _, username, _ = parse_merge_bot_branch(merge_bot_branch)
with github.login() as gh:
gh_repo = gh.repository(org, repo)
gh_pr = gh.pull_request(org, repo, pr)
gh_commit = github.gh_call(gh_repo.commit, sha)
success = _get_commit_success(org, repo, pr, gh_commit)
if success is None:
# checks in progress
return
elif success:
try:
_merge_bot_merge_pr(org, repo, merge_bot_branch, clone_dir)
except CalledProcessError as e:
cmd = " ".join(e.cmd)
github.gh_call(
gh_pr.create_comment,
hide_secrets(
f"@{username} The merge process could not be "
f"finalized, because "
f"command `{cmd}` failed with output:\n```\n"
f"{e.output}\n```"
),
)
_remove_merging_label(github, gh_pr)
raise
except Exception as e:
github.gh_call(
gh_pr.create_comment,
hide_secrets(
f"@{username} The merge process could not be "
f"finalized because an exception was raised: {e}."
),
)
_remove_merging_label(github, gh_pr)
raise
else:
github.gh_call(
gh_pr.create_comment,
hide_secrets(
f"@{username} The merge process could not be "
f"finalized, because "
f"command `{cmd}` failed with output:\n```\n{e.output}\n```"
),
f"@{username} your merge command was aborted due to failed "
f"check(s), which you can inspect on "
f"[this commit of {merge_bot_branch}]"
f"(https://github.com/{org}/{repo}/commits/{sha}).\n\n"
f"After fixing the problem, you can re-issue a merge command. "
f"Please refrain from merging manually as it will most "
f"probably make the target branch red.",
)
_remove_merging_label(github, gh_pr)
raise
except Exception as e:
github.gh_call(
gh_pr.create_comment,
hide_secrets(
f"@{username} The merge process could not be "
f"finalized because an exception was raised: {e}."
),
check_call(
["git", "push", "origin", f":{merge_bot_branch}"], cwd=clone_dir
)
_remove_merging_label(github, gh_pr)
raise
else:
github.gh_call(
gh_pr.create_comment,
f"@{username} your merge command was aborted due to failed "
f"check(s), which you can inspect on "
f"[this commit of {merge_bot_branch}]"
f"(https://github.com/{org}/{repo}/commits/{sha}).\n\n"
f"After fixing the problem, you can re-issue a merge command. "
f"Please refrain from merging manually as it will most probably "
f"make the target branch red.",
)
check_call(
["git", "push", "origin", f":{merge_bot_branch}"], cwd=clone_dir
)
_remove_merging_label(github, gh_pr)