-
Notifications
You must be signed in to change notification settings - Fork 1.4k
134 lines (118 loc) · 4.95 KB
/
apply-hotfix.yaml
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
name: Apply hotfix to branch
on:
workflow_dispatch:
inputs:
pr_number:
description: 'Number of merged pull request containing the hotfix.'
required: true
branch_name:
description: 'Name of branch (release/* or rc/*) to apply hotfix to. Defaults to latest release branch.'
jobs:
resolve-branch:
runs-on: ubuntu-latest
outputs:
branch_name: ${{ steps.resolve_branch.outputs.branch_name }}
branch_type: ${{ steps.resolve_branch.outputs.branch_type }}
steps:
- name: Check out hosted repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Resolve branch name
id: resolve_branch
run: |
if [ "${{ github.event.inputs.branch_name }}" != "" ]; then
branch_name="${{ github.event.inputs.branch_name }}"
else
branch_name=$(git branch -r --list 'release/*' | sort -r | head -n 1 | xargs)
fi
if [ -z "$branch_name" ]; then
echo "No release branch found."
exit 1
fi
branch_type=$(echo $branch_name | awk -F'[-/]' '{print $1}' )
echo "branch_name=$branch_name" >> $GITHUB_OUTPUT
echo "branch_type=$branch_type" >> $GITHUB_OUTPUT
create-hotfix-pr:
runs-on: ubuntu-latest
needs:
- resolve-branch
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Checkout branch
run: git checkout ${{ needs.resolve-branch.outputs.branch_name }}
- name: Get merge commit SHA
id: get_merge_commit_sha
run: |
MERGE_COMMIT_SHA=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.inputs.pr_number }} --jq '.merge_commit_sha')
echo "Merge commit SHA: $MERGE_COMMIT_SHA"
echo "MERGE_COMMIT_SHA=$MERGE_COMMIT_SHA" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Attempt cherry-pick
run: |
BRANCH_NAME="hotfix-${{ github.event.inputs.pr_number }}/${{ needs.resolve-branch.outputs.branch_name }}"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
git checkout -b $BRANCH_NAME
set +e
git cherry-pick ${{ steps.get_merge_commit_sha.outputs.MERGE_COMMIT_SHA }} -m 1
status=$?
set -e
if [ $status -eq 0 ]; then
echo "Cherry-pick succeeded."
else
echo "Cherry-pick resulted in merge conflicts. Committing conflicts as-is."
git add -A
# Commit the conflicted state as-is. This will include conflict markers in the committed files.
# The user will have to resolve them manually on the PR.
git commit -m "Cherry-pick with conflicts: ${{ steps.get_merge_commit_sha.outputs.MERGE_COMMIT_SHA }}"
fi
- name: Push new branch
run: |
git push origin $BRANCH_NAME
- name: Create Pull Request
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const baseBranch = "${{ needs.resolve-branch.outputs.branch_name }}"
const headBranch = process.env.BRANCH_NAME
const cherryCommit = "${{ steps.get_merge_commit_sha.outputs.MERGE_COMMIT_SHA }}"
const assignee = context.actor
const { data: pr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `[HOTFIX] applying PR #${context.payload.inputs.pr_number} to ${{ needs.resolve-branch.outputs.branch_name}}`,
head: headBranch,
base: baseBranch,
body: `This PR cherry-picks the commit ${cherryCommit} onto ${{ needs.resolve-branch.outputs.branch_name }}. If there are unresolved conflicts, please resolve them manually.`,
assignees: [assignee],
requested_reviewers: [assignee],
})
core.info(`Created PR #${pr.number}: ${pr.html_url}`)
// Assign the PR to the user who triggered the workflow
await github.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
assignees: [assignee],
})
core.info(`Assigned PR #${pr.number} to ${assignee}`)
// Request a review from the same user
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
reviewers: [assignee]
})
core.info(`Requested review from ${assignee} on PR #${pr.number}`)