Skip to content

Commit 70bf2e2

Browse files
authored
Update release-bot.yaml
1 parent cb40a38 commit 70bf2e2

1 file changed

Lines changed: 94 additions & 39 deletions

File tree

.github/workflows/release-bot.yaml

Lines changed: 94 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,45 @@ jobs:
4141
git config --global user.email "bot@tyk.io"
4242
git config --global user.name "Tyk Bot"
4343
44-
- name: Read PR details
44+
- name: Get PR base and merge SHAs
4545
id: pr_details
4646
if: steps.check_command.outputs.release_valid == 'true'
4747
env:
4848
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4949
run: |
5050
set -eo pipefail
51-
data=$(gh pr view ${{ steps.check_command.outputs.pr_number }} --json headRefOid,title)
52-
echo "COMMIT_SHA=$(jq -r '.headRefOid' <<<"$data")" >> "$GITHUB_OUTPUT"
53-
echo "PR_TITLE=$(jq -r '.title' <<<"$data")" >> "$GITHUB_OUTPUT"
51+
52+
PR_NUMBER=${{ steps.check_command.outputs.pr_number }}
53+
OWNER_REPO=${{ github.repository }}
54+
DEFAULT_BRANCH=${{ env.DEFAULT_BRANCH }}
55+
56+
# Ask GitHub for the merge commit SHA (works for "Merge" & "Squash" merges).
57+
PR_JSON=$(gh api repos/${OWNER_REPO}/pulls/${PR_NUMBER})
58+
PR_TITLE=$(echo "$PR_JSON" | jq -r '.title')
59+
MERGE_SHA=$(echo "$PR_JSON" | jq -r '.merge_commit_sha // empty')
60+
61+
# Fallback 1: For classic merge commits with default message
62+
if [ -z "$MERGE_SHA" ] || [ "$MERGE_SHA" = "null" ]; then
63+
git fetch origin "${DEFAULT_BRANCH}" --quiet
64+
MERGE_SHA=$(git log origin/${DEFAULT_BRANCH} --merges \
65+
--grep="Merge pull request #${PR_NUMBER}" \
66+
-n 1 --pretty=format:%H || true)
67+
fi
68+
69+
# Fallback 2: For squash merges (default squash message ends with "(#<PR>)")
70+
if [ -z "$MERGE_SHA" ]; then
71+
MERGE_SHA=$(git log origin/${DEFAULT_BRANCH} \
72+
--grep="(#${PR_NUMBER})" \
73+
-n 1 --pretty=format:%H || true)
74+
fi
75+
76+
if [ -z "$MERGE_SHA" ]; then
77+
echo "Could not determine merge commit for PR #${PR_NUMBER}" >&2
78+
exit 1
79+
fi
80+
81+
echo "COMMIT_SHA=${MERGE_SHA}" >> "$GITHUB_OUTPUT"
82+
echo "PR_TITLE=${PR_TITLE}" >> "$GITHUB_OUTPUT"
5483
5584
- name: Cherry-pick PR head commit onto target release
5685
id: cherry_pick
@@ -62,57 +91,83 @@ jobs:
6291
PR_NUMBER: ${{ steps.check_command.outputs.pr_number }}
6392
COMMIT_SHA: ${{ steps.pr_details.outputs.COMMIT_SHA }}
6493
PR_TITLE: ${{ steps.pr_details.outputs.PR_TITLE }}
94+
run: |
6595
run: |
6696
set -eo pipefail
6797
68-
# Ensure the PR head commit object exists locally (works for forks).
69-
git fetch origin "pull/${PR_NUMBER}/head:refs/remotes/origin/pr-${PR_NUMBER}"
98+
JIRA_ID=$(echo "$PR_TITLE" | grep -oE '[A-Z]{1,10}-[0-9]{1,10}' | head -n 1 || true)
99+
100+
# Clone the repository (kept for your act/local testing flow)
101+
export FOLDER=$(echo $GITHUB_REPO | cut -d '/' -f2)
102+
rm -rf "$FOLDER"
103+
git clone "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPO}"
104+
cd "$FOLDER"
70105
71-
# Update and switch to the target release branch.
72-
git checkout "${TARGET_BRANCH}"
106+
# Ensure target exists & is up to date
107+
git checkout "$GITHUB_BRANCH"
73108
git pull --ff-only
74109
75-
# Build a clean branch name.
76-
JIRA_ID=$(echo "${PR_TITLE}" | grep -oE '[A-Z]{1,10}-[0-9]{1,10}' | head -n 1 || true)
77-
BRANCH_NAME="merge/${TARGET_BRANCH}/${COMMIT_SHA}"
78-
if [ -n "${JIRA_ID}" ]; then
79-
BRANCH_NAME="${BRANCH_NAME}/${JIRA_ID}"
110+
# Build branch name
111+
if [ -n "$JIRA_ID" ]; then
112+
BRANCH_NAME="merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT/$JIRA_ID"
113+
else
114+
BRANCH_NAME="merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT"
80115
fi
81116
82-
# Remove any stale branch locally/remotely.
83-
git branch -D "${BRANCH_NAME}" 2>/dev/null || true
84-
git push origin --delete "${BRANCH_NAME}" 2>/dev/null || true
85-
86-
# Create the integration branch.
87-
git switch -c "${BRANCH_NAME}"
117+
# Clean any stale branches
118+
git branch -D "$BRANCH_NAME" 2>/dev/null || true
119+
git push origin --delete "$BRANCH_NAME" 2>/dev/null || true
120+
git branch -D "merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT" 2>/dev/null || true
121+
git push origin --delete "merge/$GITHUB_BRANCH/$GITHUB_CHERRY_PICK_COMMIT" 2>/dev/null || true
122+
123+
# Create working branch
124+
git checkout -b "$BRANCH_NAME"
125+
126+
# Determine if the selected commit is a merge commit (has >1 parent)
127+
PARENT_FIELDS=$(git rev-list --parents -n 1 "$GITHUB_CHERRY_PICK_COMMIT" | wc -w)
128+
# rev-list prints: <sha> <parent1> [parent2 ...]
129+
# non-merge -> 2 fields; merge -> >=3 fields
130+
if [ "$PARENT_FIELDS" -gt 2 ]; then
131+
CHERRYPICK_OPTS="-m 1"
132+
else
133+
CHERRYPICK_OPTS=""
134+
fi
88135
89-
# Cherry-pick the PR head commit.
90136
MERGE_FAILED=0
91-
git cherry-pick -x "${COMMIT_SHA}" || MERGE_FAILED=$?
137+
git cherry-pick -x ${CHERRYPICK_OPTS} "$GITHUB_CHERRY_PICK_COMMIT" || MERGE_FAILED=$?
92138
93-
# If it conflicted, try auto-resolving and continue as draft.
94-
if [ "${MERGE_FAILED}" -ne 0 ]; then
139+
# Try to auto-continue if there were conflicts
140+
if [ "$MERGE_FAILED" -ne 0 ]; then
95141
git add -A || true
96142
git -c core.editor=true cherry-pick --continue --no-edit || true
97143
fi
98144
99-
git push origin "${BRANCH_NAME}" --force
100-
101-
# Prepare PR title/body from the original commit.
102-
TITLE=$(git show -s --format=%s "${COMMIT_SHA}")
103-
BODY=$(git show -s --format=%B "${COMMIT_SHA}")
104-
105-
# Create the PR (draft if conflicts occurred).
106-
PR_URL=$(gh pr create \
107-
--repo "${REPO}" \
108-
--base "${TARGET_BRANCH}" \
109-
--head "${BRANCH_NAME}" \
110-
${MERGE_FAILED:+--draft} \
111-
--title "Merging to ${TARGET_BRANCH}: ${TITLE}" \
112-
--body "${BODY}")
145+
echo "Push the new branch"
146+
git push origin "$BRANCH_NAME" --force || true
147+
148+
echo "Prepare the message and title for the PR"
149+
MESSAGE=$(git show -s --format=%B "$GITHUB_CHERRY_PICK_COMMIT")
150+
TITLE=$(echo "$MESSAGE" | head -n 1)
151+
152+
# Create the PR (draft if conflicts observed)
153+
if [ "$MERGE_FAILED" -ne 0 ]; then
154+
PR_URL=$(gh pr create --draft \
155+
--title "Merging to $GITHUB_BRANCH: $TITLE" \
156+
--body "$MESSAGE" \
157+
--repo "$GITHUB_REPO" \
158+
--base "$GITHUB_BRANCH" \
159+
--head "$BRANCH_NAME")
160+
else
161+
PR_URL=$(gh pr create \
162+
--title "Merging to $GITHUB_BRANCH: $TITLE" \
163+
--body "$MESSAGE" \
164+
--repo "$GITHUB_REPO" \
165+
--base "$GITHUB_BRANCH" \
166+
--head "$BRANCH_NAME")
167+
fi
113168
114-
# Export step outputs for the comment step.
115-
echo "PR_URL=${PR_URL}" >> "$GITHUB_OUTPUT"
169+
# Export outputs for the comment step
170+
echo "PR_URL=${PR_URL}" >> "$GITHUB_OUTPUT"
116171
echo "MERGE_FAILED=${MERGE_FAILED}" >> "$GITHUB_OUTPUT"
117172
118173
- name: Comment back on original PR

0 commit comments

Comments
 (0)