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