Skip to content

Commit afa5a25

Browse files
authored
Merge pull request #1134 from equalizedigital/copilot/fix-d3e28254-55d5-4ef2-b283-d7941ace93ec
Add automated backport workflow for main to develop branch synchronization
2 parents e97bff8 + 1223351 commit afa5a25

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

.github/CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ Make sure your problem does not exist as a ticket already by searching through [
4444

4545
We will review your pull request and merge when everything is in order. We will help you to make sure the code complies with the standards described above.
4646

47+
### Automated Backport Process
48+
When a pull request is merged into the `main` branch, an automated workflow will create a backport pull request to merge the same feature branch into the `develop` branch. This ensures that changes in `main` are also applied to the development branch without directly merging `main` into `develop`.
49+
50+
**How it works:**
51+
- The workflow triggers automatically when a PR is merged into `main`
52+
- It extracts the original branch name that was merged
53+
- If the branch still exists, it creates a new PR to merge that branch into `develop`
54+
- The backport PR is labeled with `backport` and `automated` labels
55+
- If the branch no longer exists, the workflow logs a message indicating manual backport may be needed
56+
57+
**No action required** - this process is fully automated and requires no manual intervention in most cases.
58+
4759
#### 'Patch welcome' issues
4860
Some issues are labeled 'patch-welcome'. This means we see the value in the particular enhancement being suggested but have decided for now not to prioritize it. If you however decide to write a patch for it, we'll gladly include it after some code review.
4961

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Automatic Backport to Develop
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches: [main]
7+
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
issues: write
12+
13+
jobs:
14+
create-backport-pr:
15+
# Only run if the PR was actually merged (not just closed)
16+
if: github.event.pull_request.merged == true
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
token: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- name: Get merged branch name
27+
id: get-branch
28+
run: |
29+
# Get the head branch name from the merged PR
30+
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
31+
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
32+
echo "Merged branch: $BRANCH_NAME"
33+
34+
- name: Check if branch exists
35+
id: check-branch
36+
run: |
37+
BRANCH_NAME="${{ steps.get-branch.outputs.branch-name }}"
38+
39+
# Check if the branch still exists on the remote
40+
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
41+
echo "branch-exists=true" >> $GITHUB_OUTPUT
42+
echo "Branch $BRANCH_NAME exists and can be used for backport"
43+
else
44+
echo "branch-exists=false" >> $GITHUB_OUTPUT
45+
echo "Branch $BRANCH_NAME no longer exists, cannot create backport PR"
46+
fi
47+
48+
- name: Create backport PR
49+
if: steps.check-branch.outputs.branch-exists == 'true'
50+
uses: actions/github-script@v7
51+
with:
52+
github-token: ${{ secrets.GITHUB_TOKEN }}
53+
script: |
54+
const branchName = '${{ steps.get-branch.outputs.branch-name }}';
55+
const originalPrNumber = context.payload.pull_request.number;
56+
const originalPrTitle = context.payload.pull_request.title;
57+
const originalPrBody = context.payload.pull_request.body || '';
58+
const originalPrAuthor = context.payload.pull_request.user.login;
59+
60+
// Create the backport PR
61+
try {
62+
const response = await github.rest.pulls.create({
63+
owner: context.repo.owner,
64+
repo: context.repo.repo,
65+
title: `[Backport] ${originalPrTitle}`,
66+
head: branchName,
67+
base: 'develop',
68+
body: `## Automatic Backport\n\nThis is an automatic backport of PR #${originalPrNumber} by @${originalPrAuthor} to the \`develop\` branch.\n\n### Original PR Details:\n${originalPrBody}\n\n---\n*This PR was created automatically when #${originalPrNumber} was merged into \`main\`.*`,
69+
maintainer_can_modify: true
70+
});
71+
72+
console.log(`✅ Successfully created backport PR #${response.data.number}`);
73+
console.log(`🔗 PR URL: ${response.data.html_url}`);
74+
75+
// Add labels to the backport PR
76+
await github.rest.issues.addLabels({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
issue_number: response.data.number,
80+
labels: ['backport', 'automated']
81+
});
82+
83+
} catch (error) {
84+
console.error('❌ Failed to create backport PR:', error.message);
85+
86+
// Check if it's a conflict or other specific error
87+
if (error.message.includes('No commits between')) {
88+
console.log('ℹ️ The branch is already up to date with develop - no backport needed');
89+
} else if (
90+
error.status === 422 &&
91+
error.response &&
92+
Array.isArray(error.response.data?.errors) &&
93+
error.response.data.errors.some(e =>
94+
e.message && e.message.includes('A pull request already exists')
95+
)
96+
) {
97+
console.log('ℹ️ A pull request already exists for this branch to develop');
98+
} else {
99+
// Re-throw for other errors to fail the workflow
100+
throw error;
101+
}
102+
}
103+
104+
- name: Handle missing branch
105+
if: steps.check-branch.outputs.branch-exists == 'false'
106+
run: |
107+
echo "⚠️ Cannot create backport PR because the source branch no longer exists."
108+
echo "This typically happens when the branch was deleted after merging."
109+
echo "If a backport to develop is needed, it should be done manually."

0 commit comments

Comments
 (0)