Skip to content

Commit 6eff260

Browse files
committed
ci: check if rebase is needed before requesting one
When the /queue command is issued, the workflow now checks if the PR actually needs a rebase by comparing the merge base with the target branch. If no rebase is needed, it skips the Mergify comment and directly adds the ok-to-test label. This avoids unnecessary rebase operations and Mergify comments when the PR is already up-to-date with the base branch. Assisted-by: AskBob <askbob@ibm.com> Signed-off-by: Niels de Vos <ndevos@ibm.com>
1 parent 2f2e45e commit 6eff260

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

.github/workflows/queue-rebase-and-label.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,53 @@ jobs:
2929
# yamllint enable rule:line-length
3030
runs-on: ubuntu-latest
3131
steps:
32+
- name: Get PR details
33+
id: pr
34+
# yamllint disable-line rule:line-length
35+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
36+
with:
37+
github-token: ${{ secrets.CEPH_CSI_BOT_TOKEN }}
38+
script: |
39+
const pr = await github.rest.pulls.get({
40+
owner: context.repo.owner,
41+
repo: context.repo.repo,
42+
pull_number: context.issue.number
43+
})
44+
core.setOutput('head_sha', pr.data.head.sha)
45+
core.setOutput('base_ref', pr.data.base.ref)
46+
core.setOutput('head_ref', pr.data.head.ref)
47+
48+
- name: Checkout PR branch
49+
# yamllint disable-line rule:line-length
50+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
51+
with:
52+
ref: ${{ steps.pr.outputs.head_sha }}
53+
fetch-depth: 0
54+
55+
- name: Check if rebase is needed
56+
id: check_rebase
57+
run: |
58+
set -euo pipefail
59+
BASE_REF="${{ steps.pr.outputs.base_ref }}"
60+
git fetch origin -- "$BASE_REF"
61+
62+
# Get the merge base between PR and target branch
63+
MERGE_BASE=$(git merge-base HEAD "origin/$BASE_REF")
64+
65+
# Get the latest commit on the target branch
66+
BASE_SHA=$(git rev-parse "origin/$BASE_REF")
67+
68+
# If merge base equals latest commit on base, no rebase needed
69+
if [ "$MERGE_BASE" = "$BASE_SHA" ]; then
70+
echo "needs_rebase=false" >> "$GITHUB_OUTPUT"
71+
echo "PR is up to date with base branch, no rebase needed"
72+
else
73+
echo "needs_rebase=true" >> "$GITHUB_OUTPUT"
74+
echo "PR needs rebase"
75+
fi
76+
3277
- name: Add queued/rebase label
78+
if: steps.check_rebase.outputs.needs_rebase == 'true'
3379
# yamllint disable-line rule:line-length
3480
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
3581
with:
@@ -43,6 +89,7 @@ jobs:
4389
})
4490
4591
- name: Request rebase through Mergify
92+
if: steps.check_rebase.outputs.needs_rebase == 'true'
4693
# yamllint disable-line rule:line-length
4794
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
4895
with:
@@ -51,6 +98,20 @@ jobs:
5198
body: |
5299
@mergifyio rebase
53100
101+
- name: Add ok-to-test label directly
102+
if: steps.check_rebase.outputs.needs_rebase == 'false'
103+
# yamllint disable-line rule:line-length
104+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
105+
with:
106+
github-token: ${{ secrets.CEPH_CSI_BOT_TOKEN }}
107+
script: |
108+
await github.rest.issues.addLabels({
109+
issue_number: context.issue.number,
110+
owner: context.repo.owner,
111+
repo: context.repo.repo,
112+
labels: ['ok-to-test']
113+
})
114+
54115
add-ok-to-test-label:
55116
if: >
56117
github.repository == 'ceph/ceph-csi' &&

0 commit comments

Comments
 (0)