@@ -48,69 +48,143 @@ jobs:
48
48
HEAD_BRANCH : ${{ (github.event_name == 'workflow_dispatch' && inputs.head_branch) || 'main' }}
49
49
BASE_BRANCH : ${{ (github.event_name == 'workflow_dispatch' && inputs.base_branch) || 'develop' }}
50
50
SOURCE_PR : ${{ (github.event_name == 'pull_request' && github.event.pull_request.number) || inputs.source_pr_number || '' }}
51
+ GH_TOKEN : ${{ secrets.GH_PAT }}
51
52
52
53
steps :
53
54
- uses : actions/checkout@v4
54
55
with :
55
56
fetch-depth : 0
57
+ token : ${{ secrets.GH_PAT }}
58
+
59
+ - name : Configure git author
60
+ run : |
61
+ git config --local user.name "Apollo Bot"
62
+ git config --local user.email "[email protected] "
56
63
57
64
# Generate branch name from PR# when available, otherwise use first 7 commit SHA characters
58
65
- name : Compute branch/name metadata
59
66
id : meta
60
67
run : |
61
- if [ -n "${SOURCE_PR}" ]; then
62
- echo "branch=sync/${HEAD_BRANCH}-into-${BASE_BRANCH}-pr-${SOURCE_PR}" >> $GITHUB_OUTPUT
63
- echo "title=Sync ${HEAD_BRANCH} → ${BASE_BRANCH} (PR #${SOURCE_PR})" >> $GITHUB_OUTPUT
64
- echo "body=Auto-opened to merge \`${HEAD_BRANCH}\` into \`${BASE_BRANCH}\`. Source PR: #${SOURCE_PR}." >> $GITHUB_OUTPUT
65
- else
66
- short_sha=${GITHUB_SHA::7}
67
- echo "branch=sync/${HEAD_BRANCH}-into-${BASE_BRANCH}-${short_sha}" >> $GITHUB_OUTPUT
68
- echo "title=Sync ${HEAD_BRANCH} → ${BASE_BRANCH} (${short_sha})" >> $GITHUB_OUTPUT
69
- echo "body=Auto-opened to merge \`${HEAD_BRANCH}\` into \`${BASE_BRANCH}\` at \`${GITHUB_SHA}\`." >> $GITHUB_OUTPUT
70
- fi
68
+ pr=${{ github.event.pull_request.number }}
69
+ echo "sync_branch=sync/main-into-develop-pr-${pr}" >> $GITHUB_OUTPUT
70
+ echo "title_sync=Sync main → develop (PR #${pr})" >> $GITHUB_OUTPUT
71
+ echo "body_sync=Auto-opened after merging \`${{ github.event.pull_request.head.ref }}\` into \`main\`. Source PR: #${pr}." >> $GITHUB_OUTPUT
72
+ echo "conflict_branch=conflict/main-into-develop-pr-${pr}" >> $GITHUB_OUTPUT
73
+ echo "title_conflict=Sync main → develop (resolve conflicts)" >> $GITHUB_OUTPUT
74
+ echo "body_conflict=Opened from a copy of \`main\` so conflicts can be resolved without pushing to a protected branch." >> $GITHUB_OUTPUT
71
75
72
76
# Short-lived sync branch from develop and merge main into it (do NOT rebase)
73
77
# use +e to stop errors from short-circuiting the script
74
78
- name : Prepare sync branch
75
79
id : prep
76
80
run : |
81
+ set -e
77
82
git fetch origin "${BASE_BRANCH}" "${HEAD_BRANCH}"
78
- git switch -c "${{ steps.meta.outputs.branch }}" "origin/${BASE_BRANCH}"
83
+ git switch -c "${{ steps.meta.outputs.sync_branch }}" "origin/${BASE_BRANCH}"
79
84
set +e
80
85
git merge --no-ff "origin/${HEAD_BRANCH}"
81
86
rc=$?
82
87
set -e
83
88
git add -A || true
84
89
git commit -m "WIP: merge ${HEAD_BRANCH} into ${BASE_BRANCH} via ${{ steps.meta.outputs.branch }}" || true
85
90
git push origin HEAD
91
+
92
+ right=$(git rev-list --count --right-only "origin/${BASE_BRANCH}...HEAD")
93
+
86
94
echo "merge_status=$rc" >> "$GITHUB_OUTPUT"
95
+ echo "sync_right=$right" >> "$GITHUB_OUTPUT"
96
+ echo "Merge exit=$rc, sync branch ahead-by=$right"
87
97
88
- # Open the PR targeting develop
89
- - name : Open PR to develop
90
- id : syncpr
91
- uses : peter-evans/create-pull-request@v6
92
- with :
93
- branch : ${{ steps.meta.outputs.branch }}
94
- base : ${{ env.BASE_BRANCH }}
95
- title : ${{ steps.meta.outputs.title }}
96
- body : |
97
- ${{ steps.meta.outputs.body }}
98
+ # If no merge conflicts and there are changes, open the PR targeting develop
99
+ - name : Open clean PR to develop
100
+ id : sync_pr
101
+ if : ${{ steps.prep.outputs.merge_status == '0' && steps.prep.outputs.sync_right != '0' }}
102
+ run : |
103
+ # Avoid duplicate PRs
104
+ existing=$(gh pr list --base "${BASE_BRANCH}" --head "${{ steps.meta.outputs.sync_branch }}" --state open --json number --jq '.[0].number' || true)
105
+ if [ -n "$existing" ] && [ "$existing" != "null" ]; then
106
+ echo "pr_number=$existing" >> "$GITHUB_OUTPUT"
107
+ url=$(gh pr view "$existing" --json url --jq .url)
108
+ echo "pr_url=$url" >> "$GITHUB_OUTPUT"
109
+ exit 0
110
+ fi
98
111
99
- Merge status: ${{ steps.prep.outputs.merge_status == '0' && 'clean ✅' || 'conflicts ❗' }}
100
- labels : ${{ steps.prep.outputs.merge_status == '0' && 'back-merge,automation' || 'back-merge,automation,conflicts' }}
112
+ gh pr create \
113
+ --base "${BASE_BRANCH}" \
114
+ --head "${{ steps.meta.outputs.sync_branch }}" \
115
+ --title "${{ steps.meta.outputs.sync_title }}" \
116
+ --body "${{ steps.meta.outputs.sync_body }} (created via gh CLI)" \
117
+ --label back-merge \
118
+ --label automation
119
+
120
+ # Emit outputs for later steps
121
+ gh pr view --base "${BASE_BRANCH}" --head "${{ steps.meta.outputs.sync_branch }}" \
122
+ --json number,url | jq -r '"pr_number=\(.number)\npr_url=\(.url)"' >> "$GITHUB_OUTPUT"
123
+
124
+ # If the merge hit conflicts, open a DIRECT PR: HEAD_BRANCH -> BASE_BRANCH so conflicts can be resolved prior to merge
125
+ - name : Open conflict PR
126
+ id : conflict_pr
127
+ if : ${{ steps.prep.outputs.merge_status != '0' }}
128
+ run : |
129
+ set -e
130
+ git fetch origin "${HEAD_BRANCH}" "${BASE_BRANCH}"
131
+
132
+ git switch -c "${{ steps.meta.outputs.conflict_branch }}" "origin/${HEAD_BRANCH}"
133
+ git push -u origin HEAD
134
+
135
+ # Skip if no diff between conflict branch and base (should be unlikely)
136
+ right=$(git rev-list --right-only --count "origin/${BASE_BRANCH}...origin/${{ steps.meta.outputs.conflict_branch }}")
137
+ if [ "$right" -eq 0 ]; then
138
+ echo "No diff between ${HEAD_BRANCH} and ${BASE_BRANCH}; nothing to open."
139
+ exit 0
140
+ fi
141
+
142
+ # Reuse existing open PR if present
143
+ existing=$(gh pr list --base "${BASE_BRANCH}" --head "${{ steps.meta.outputs.conflict_branch }}" --state open --json number --jq '.[0].number' || true)
144
+ if [ -n "$existing" ] && [ "$existing" != "null" ]; then
145
+ echo "pr_number=$existing" >> "$GITHUB_OUTPUT"
146
+ url=$(gh pr view "$existing" --json url --jq .url)
147
+ echo "pr_url=$url" >> "$GITHUB_OUTPUT"
148
+ exit 0
149
+ fi
150
+
151
+ gh pr create \
152
+ --base "${BASE_BRANCH}" \
153
+ --head "${{ steps.meta.outputs.conflict_branch }}" \
154
+ --title "${{ steps.meta.outputs.title_conflict }}" \
155
+ --body "${{ steps.meta.outputs.body_conflict }}" \
156
+ --label back-merge \
157
+ --label automation \
158
+ --label conflicts
159
+
160
+ gh pr view --base "${BASE_BRANCH}" --head "${{ steps.meta.outputs.conflict_branch }}" \
161
+ --json number,url | jq -r '"pr_number=\(.number)\npr_url=\(.url)"' >> "$GITHUB_OUTPUT"
101
162
102
163
# Comment back on the ORIGINAL merged PR with a link to the sync PR
103
164
- name : Comment on source PR with sync PR link
104
- if : github.event_name == 'pull_request ' && steps.syncpr .outputs.pull-request-number != ''
165
+ if : ${{ env.SOURCE_PR != ' ' && ( steps.sync_pr .outputs.pr_number != '' || steps.conflict_pr.outputs.pr_number != '') }}
105
166
uses : actions/github-script@v7
106
167
with :
107
168
script : |
169
+ const owner = context.repo.owner;
170
+ const repo = context.repo.repo;
108
171
const issue_number = Number(process.env.SOURCE_PR);
109
- const syncUrl = `${{ toJson(steps.syncpr.outputs['pull-request-url']) }}`.replace(/^"|"$/g, '');
110
- const body = `Opened sync PR **${process.env.HEAD_BRANCH} → ${process.env.BASE_BRANCH}**: ${syncUrl}`;
111
- await github.rest.issues.createComment({
112
- owner: context.repo.owner,
113
- repo: context.repo.repo,
114
- issue_number,
115
- body,
116
- });
172
+
173
+ const hadConflicts = '${{ steps.prep.outputs.merge_status }}' !== '0';
174
+ const syncUrl = '${{ steps.sync_pr.outputs.pr_url || steps.conflict_pr.outputs.pr_url }}';
175
+ const head = process.env.HEAD_BRANCH;
176
+ const base = process.env.BASE_BRANCH;
177
+
178
+ const status = hadConflicts ? 'conflicts ❗' : ' clean ✅' ;
179
+ const note = hadConflicts
180
+ ? 'Opened from a copy of main so conflicts can be resolved safely.'
181
+ : 'Opened from a sync branch created off develop.';
182
+
183
+ const body = [
184
+ `Opened sync PR **${head} → ${base}** : ${syncUrl}`,
185
+ ` ` ,
186
+ `Merge status : **${status}**`,
187
+ note
188
+ ].join('\n');
189
+
190
+ await github.rest.issues.createComment({ owner, repo, issue_number, body });
0 commit comments