1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-08-04 14:43:38 +02:00

Add 'git-obs pr cancel-scheduled-merge' command

This commit is contained in:
2025-07-23 09:33:20 +02:00
parent c5d482c614
commit 476e764472
2 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import osc.commandline_git
class PullRequestCancelScheduledMergeCommand(osc.commandline_git.GitObsCommand):
"""
Cancel scheduled merge of pull requests
"""
name = "cancel-scheduled-merge"
parent = "PullRequestCommand"
def init_arguments(self):
self.add_argument(
"id",
nargs="+",
help="Pull request ID in <owner>/<repo>#<number> format",
)
def run(self, args):
from osc import gitea_api
self.print_gitea_settings()
pull_request_ids = args.id
for pr_index, pr_id in enumerate(pull_request_ids):
print(f"Canceling scheduled merge of {pr_id}...")
owner, repo, number = gitea_api.PullRequest.split_id(pr_id)
gitea_api.PullRequest.cancel_scheduled_merge(
self.gitea_conn,
owner,
repo,
number,
)

View File

@@ -506,3 +506,25 @@ class PullRequest:
conn.request("POST", url, json_data=json_data, context={"owner": owner, "repo": repo})
except AutoMergeAlreadyScheduled:
pass
@classmethod
def cancel_scheduled_merge(
cls,
conn: Connection,
owner: str,
repo: str,
number: int,
) -> GiteaHTTPResponse:
"""
Cancel scheduled merge of a pull request.
"""
from .exceptions import GiteaException
url = conn.makeurl("repos", owner, repo, "pulls", str(number), "merge")
try:
conn.request("DELETE", url, context={"owner": owner, "repo": repo})
except GiteaException as e:
# Gitea returns 404 when there's no scheduled merge or if the pull request doesn't exist
# the error message is the same and it's not possible to distinguish between the two cases.
if e.status != 404:
raise