-
Notifications
You must be signed in to change notification settings - Fork 538
168 lines (156 loc) · 6.34 KB
/
Copy pathbackport.yml
File metadata and controls
168 lines (156 loc) · 6.34 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
name: Backport
on:
pull_request:
types:
- closed
- labeled
jobs:
collect-backport-targets:
name: Collect Backport Targets
runs-on: ubuntu-latest
outputs:
target_branches_json: ${{ steps.collect-targets.outputs.target_branches_json }}
# Only react to merged PRs for security reasons.
# See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target.
if: >
github.event.pull_request.merged
&& (
(
github.event.action == 'closed'
&& contains(join(github.event.pull_request.labels.*.name, ','), 'backport ')
)
|| (
github.event.action == 'labeled'
&& contains(github.event.label.name, 'backport ')
)
)
steps:
- name: Collect Backport Labels (Labeled Event)
if: github.event.action == 'labeled'
env:
BACKPORT_LABEL: ${{ github.event.label.name }}
run: |
rm -f /tmp/backport-labels.sorted.txt
if echo "${BACKPORT_LABEL}" | grep -Eq '^backport [0-9]+\.[0-9]+$'; then
echo "${BACKPORT_LABEL}" > /tmp/backport-labels.sorted.txt
else
echo "Label '${BACKPORT_LABEL}' does not match expected format 'backport x.y'."
exit 1
fi
- name: Collect Backport Labels (Closed Event)
if: github.event.action == 'closed'
run: |
rm -f /tmp/backport-labels.sorted.txt
jq -r '.pull_request.labels[].name | select(test("^backport [0-9]+\\.[0-9]+$"))' "${GITHUB_EVENT_PATH}" \
| sort -u \
> /tmp/backport-labels.sorted.txt
if [ ! -s /tmp/backport-labels.sorted.txt ]; then
echo "No backport labels found for this merged PR."
exit 1
fi
- name: Collect Target Branches
id: collect-targets
run: |
TARGET_BRANCHES_JSON=$(awk '{ print $NF }' /tmp/backport-labels.sorted.txt | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "target_branches_json=${TARGET_BRANCHES_JSON}" >> "$GITHUB_OUTPUT"
backport:
name: Backport (${{ matrix.target_branch }})
needs: collect-backport-targets
if: needs.collect-backport-targets.outputs.target_branches_json != '' && needs.collect-backport-targets.outputs.target_branches_json != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target_branch: ${{ fromJSON(needs.collect-backport-targets.outputs.target_branches_json || '[]') }}
permissions:
contents: write
id-token: write
steps:
- uses: DataDog/dd-octo-sts-action@96a25462dbcb10ebf0bfd6e2ccc917d2ab235b9a # v1.0.4
id: octo-sts
with:
scope: DataDog/dd-trace-py
policy: self.backport.create-pr
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Execute Backport
id: create-commit
env:
TARGET_BRANCH: ${{ matrix.target_branch }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
MERGE_COMMIT=$(git rev-parse HEAD)
WORKTREE=".worktrees/backport-${TARGET_BRANCH}"
BACKPORT_BRANCH="backport-${PR_NUMBER}-to-${TARGET_BRANCH}"
echo "Backporting ${MERGE_COMMIT} to ${TARGET_BRANCH}"
git worktree add ${WORKTREE} ${TARGET_BRANCH}
cd ${WORKTREE}
git branch -D ${BACKPORT_BRANCH} || true
git switch --create ${BACKPORT_BRANCH}
echo "Cherrypicking"
set +e
CHERRYPICK_OUT=$(git cherry-pick -x --mainline 1 ${MERGE_COMMIT} 2>&1)
CHERRYPICK_STATUS=$?
set -e
echo "Cherrypicking complete"
echo "${CHERRYPICK_STATUS}"
if [ "${CHERRYPICK_STATUS}" -ne 0 ]; then
echo "Cherry-pick failed for ${TARGET_BRANCH}:"
echo "${CHERRYPICK_OUT}"
git cherry-pick --abort || true
cd "${GITHUB_WORKSPACE}"
git worktree remove -f ${WORKTREE}
exit 1
fi
echo "branch=${BACKPORT_BRANCH}" >> "$GITHUB_OUTPUT"
echo "branch-from=$(git rev-parse ${TARGET_BRANCH})" >> "$GITHUB_OUTPUT"
echo "commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
echo "target-branch=${TARGET_BRANCH}" >> "$GITHUB_OUTPUT"
- name: Push Commit
uses: DataDog/commit-headless@05d7b7ee023e2c7d01c47832d420c2503cd416f3 # action/v2.0.3
with:
branch: ${{ steps.create-commit.outputs.branch }}
head-sha: ${{ steps.create-commit.outputs.branch-from }}
target: "DataDog/dd-trace-py"
create-branch: true
command: push
commits: ${{ steps.create-commit.outputs.commit }}
force: true
- name: Create PR
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_NUMBER: ${{ github.event.pull_request.number }}
TARGET_BRANCH: ${{ steps.create-commit.outputs.target-branch }}
BACKPORT_BRANCH: ${{ steps.create-commit.outputs.branch }}
GH_TOKEN: ${{ steps.octo-sts.outputs.token }}
run: |
set +e
EXISTING_PR=$(gh pr list --state open --base ${TARGET_BRANCH} --head ${BACKPORT_BRANCH} --json number --jq '.[0].number' 2>&1)
GH_PR_LIST_STATUS=$?
set -e
if [ "${GH_PR_LIST_STATUS}" -ne 0 ]; then
echo "Failed to query existing backport PR for ${TARGET_BRANCH}:"
echo "${EXISTING_PR}"
exit 1
fi
if [ -z "${EXISTING_PR}" ]; then
set +e
CREATE_OUT=$(gh pr create \
--title "${PR_TITLE} [backport ${TARGET_BRANCH}]" \
--body "Backport #${PR_NUMBER} to ${TARGET_BRANCH}" \
--base ${TARGET_BRANCH} \
--head ${BACKPORT_BRANCH} 2>&1)
GH_PR_CREATE_STATUS=$?
set -e
if [ "${GH_PR_CREATE_STATUS}" -ne 0 ]; then
echo "Failed to create backport PR for ${TARGET_BRANCH}:"
echo "${CREATE_OUT}"
exit 1
fi
else
echo "Backport PR already exists for ${TARGET_BRANCH}: #${EXISTING_PR}"
fi