diff --git a/.github/workflows/chatOps.yml b/.github/workflows/chatOps.yml index 7f06f598d..bdfe39986 100644 --- a/.github/workflows/chatOps.yml +++ b/.github/workflows/chatOps.yml @@ -1,3 +1,5 @@ +# SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. +# SPDX-License-Identifier: GPL-3.0-or-later name: chatOps on: workflow_call: @@ -371,22 +373,45 @@ jobs: pull_number: context.issue.number, }) - if( pull.data.mergeable_state != "clean" ){ + // GitHub rejects a merge method that cannot be applied to this PR with + // 405/409, e.g. "This branch can't be rebased" for a sync PR that merges + // master into a release branch. Try the methods in order of preference: + // rebase keeps the history linear, squash flattens the PR, and a merge + // commit is the last resort. + let mergeMethod + const mergeFailures = [] + for (const method of ['rebase', 'squash', 'merge']) { + try { + await github.rest.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number, + merge_method: method + }) + mergeMethod = method + break + } catch (error) { + if (error.status !== 405 && error.status !== 409) { + throw error + } + mergeFailures.push(`${method}: ${error.status} ${error.message}`) + } + } + if( !mergeMethod ){ + throw new Error(`could not merge #${context.issue.number} with any merge method (${mergeFailures.join('; ')})`) + } + + // Report the merge whenever a fallback method was used, even if the PR + // was already clean, so the method switch is not silent. + if( mergeMethod != "rebase" || pull.data.mergeable_state != "clean" ){ await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: `This pr force merged! (status: ${pull.data.mergeable_state})` + body: `This pr force merged! (status: ${pull.data.mergeable_state}, merge_method: ${mergeMethod})` }) } - await github.rest.pulls.merge({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: context.issue.number, - merge_method: 'rebase' - }); - pr-info: runs-on: ubuntu-latest outputs: