-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (130 loc) · 6.35 KB
/
release-bot.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
135
136
137
138
139
140
141
142
143
144
145
146
name: Cherry-pick to Release Branch
on:
issue_comment:
types: [created]
workflow_call:
jobs:
cherry_pick:
runs-on: ubuntu-latest
steps:
- name: Check for release command
id: check_command
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
console.log('Starting script');
const { issue, comment } = context.payload;
// Debugging the values of issue and comment
console.log('Issue:', JSON.stringify(issue, null, 2));
console.log('Comment:', JSON.stringify(comment, null, 2));
if (!issue) {
console.log('No issue object found in the payload.');
core.setOutput('release_valid', 'false');
return;
}
if (!issue.pull_request) {
console.log('This issue is not related to a pull request.');
core.setOutput('release_valid', 'false');
return;
}
if (!comment || !comment.body.startsWith('/release to ')) {
console.log(`Comment body does not start with '/release to '. Comment: ${comment ? comment.body : 'undefined'}`);
core.setOutput('release_valid', 'false');
return;
}
console.log('Valid release command');
const releaseBranch = comment.body.split('/release to ')[1].trim();
console.log('Release branch:', releaseBranch);
core.setOutput('release_valid', 'true');
core.setOutput('release_branch', releaseBranch);
core.setOutput('pr_number', issue.number);
# Conditionally skip subsequent steps if the release command is not valid
- name: Skip jobs if not a valid release command
if: steps.check_command.outputs.release_valid == 'false'
run: |
echo "Skipping cherry-pick as the release command is not valid."
continue-on-error: true
- name: Checkout repository
if: steps.check_command.outputs.release_valid == 'true'
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Git
if: steps.check_command.outputs.release_valid == 'true'
run: |
git config --global user.email "[email protected]"
git config --global user.name "Tyk Bot"
- name: Install GitHub CLI
if: steps.check_command.outputs.release_valid == 'true'
run: |
type -p curl >/dev/null || sudo apt install curl -y
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
- name: Get PR details
if: steps.check_command.outputs.release_valid == 'true'
id: pr_details
run: |
PR_DATA=$(gh pr view ${{ steps.check_command.outputs.pr_number }} --json headRefOid)
COMMIT_SHA=$(echo $PR_DATA | jq -r .headRefOid)
echo "COMMIT_SHA=${COMMIT_SHA}" >> $GITHUB_OUTPUT
- name: Cherry-pick commit
if: steps.check_command.outputs.release_valid == 'true'
id: cherry_pick
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPO: ${{ github.repository }}
GITHUB_BRANCH: ${{ steps.check_command.outputs.release_branch }}
GITHUB_CHERRY_PICK_COMMIT: ${{ steps.pr_details.outputs.COMMIT_SHA }}
run: |
set -e
BRANCH_NAME="merge/${GITHUB_BRANCH}/${GITHUB_CHERRY_PICK_COMMIT}"
git checkout $GITHUB_BRANCH || { echo "Failed to checkout branch $GITHUB_BRANCH"; exit 1; }
git pull
git checkout -b $BRANCH_NAME
if ! git cherry-pick -x $GITHUB_CHERRY_PICK_COMMIT; then
git add -A
git -c core.editor=true cherry-pick --continue --no-edit || true
MERGE_FAILED=1
else
MERGE_FAILED=0
fi
git push origin $BRANCH_NAME --force || { echo "Failed to push branch"; exit 1; }
TITLE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT | head -n 1)
MESSAGE=$(git log --format=%B -n 1 $GITHUB_CHERRY_PICK_COMMIT | tail -n +3)
if [ $MERGE_FAILED -eq 1 ]; then
PR_URL=$(gh pr create --draft --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --base $GITHUB_BRANCH --head $BRANCH_NAME)
else
PR_URL=$(gh pr create --title "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE" --base $GITHUB_BRANCH --head $BRANCH_NAME)
PR_ID="${PR_URL##*/}"
gh pr merge --squash $PR_ID --auto --subject "Merging to $GITHUB_BRANCH: $TITLE" --body "$MESSAGE"
fi
echo "PR_URL=$PR_URL" >> $GITHUB_OUTPUT
echo "MERGE_FAILED=$MERGE_FAILED" >> $GITHUB_OUTPUT
- name: Comment on PR
if: steps.check_command.outputs.release_valid == 'true' && always()
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const prUrl = '${{ steps.cherry_pick.outputs.PR_URL }}';
const mergeFailed = '${{ steps.cherry_pick.outputs.MERGE_FAILED }}' === '1';
let body;
if ('${{ job.status }}' === 'success') {
if (mergeFailed) {
body = `⚠️ Cherry-pick operation completed with conflicts. A draft pull request has been created: ${prUrl}\n\nPlease resolve the conflicts manually.`;
} else {
body = `✅ Cherry-pick operation completed successfully. New pull request created: ${prUrl}`;
}
} else {
body = '❌ Cherry-pick operation failed. Please check the action logs for more information.';
}
github.rest.issues.createComment({
issue_number: ${{ steps.check_command.outputs.pr_number }},
owner: context.repo.owner,
repo: context.repo.name,
body: body
});