-
Notifications
You must be signed in to change notification settings - Fork 415
87 lines (76 loc) · 3.13 KB
/
backport-prs.yml
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
name: Backport PRs
on:
pull_request_target:
types:
- closed
- labeled
jobs:
Backport:
runs-on: ubuntu-latest
if: github.event.pull_request.merged && contains( github.event.pull_request.labels.*.name, 'backport' )
permissions:
pull-requests: write
contents: write
env:
GH_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
steps:
- name: Checkout PR HEAD
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 100
- name: Fetch PR merge base
run: git fetch --no-tags --depth=100 origin "$GH_BASE_SHA"
- name: Set up git
run: |
git config --global user.email github-actions[bot]@users.noreply.github.com
git config --global user.name github-actions[bot]
- name: Get backport branch
id: get-branch
run: |
git fetch --no-tags --depth=100 origin 'refs/heads/*.x:refs/remotes/origin/*.x'
echo "backport-branch=$(git branch -r | grep '.*.x' | sort -V | tail -n 1 | cut -d / -f 2)" >> $GITHUB_OUTPUT
- name: Apply PR commits to ${{ steps.get-branch.outputs.backport-branch}} branch
run: |
git checkout -b ${{ steps.get-branch.outputs.backport-branch}} origin/${{ steps.get-branch.outputs.backport-branch}}
git cherry-pick -x "$GH_BASE_SHA".."$GH_HEAD_SHA"
- name: Create backport PR
id: create-pr
uses: peter-evans/create-pull-request@v7
with:
title: '${{ github.event.pull_request.title }} (backport)'
body: >
Backport \#${{ github.event.number }} onto the
${{ steps.get-branch.outputs.backport-branch}} stable branch.
base: ${{ steps.get-branch.outputs.backport-branch}}
branch: backport-pr-${{ github.event.number }}
delete-branch: true
labels: 'backported-pr,${{ join(github.event.pull_request.labels.*.name) }}'
- uses: actions-ecosystem/action-remove-labels@v1
with:
number: ${{ steps.create-pr.outputs.pull-request-number }}
labels: backport
- name: Comment on completion
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '🏁 Successfully backported as #${{ steps.create-pr.outputs.pull-request-number }}. 🏁'
})
- name: Comment on error
if: failure()
uses: actions/github-script@v7
with:
script: |
const workflow_url = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
body = `🚨 Unable to [cleanly backport](${workflow_url}) this PR! 🚨`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
})