forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (201 loc) · 7.87 KB
/
Copy pathrebase-upstream.yml
File metadata and controls
238 lines (201 loc) · 7.87 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
name: Rebase Upstream
on:
issue_comment:
types: [created]
pull_request:
types: [opened]
jobs:
add-rebase-button:
name: Add Rebase Instructions
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Add comment with rebase instructions
uses: actions/github-script@v8
with:
script: |
const repoUrl = context.payload.repository.html_url;
const workflowUrl = `${repoUrl}/actions/workflows/rebase-upstream.yml`;
const comment = `## 🔄 Rebase onto Upstream
To rebase this PR onto the latest \`eclipse-jdt/eclipse.jdt.core:master\`, comment:
\`\`\`
/rebase-upstream
\`\`\`
This will rebase your PR branch onto the upstream repository's master branch.
📋 [View Workflow](${workflowUrl})`;
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
rebase-upstream:
name: Rebase onto Upstream Master
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, '/rebase-upstream')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Check user permission
id: check
uses: actions/github-script@v8
with:
result-encoding: string
script: |
const authorAssociation = context.payload.comment.author_association;
const allowedRoles = ['OWNER', 'MEMBER', 'COLLABORATOR'];
if (allowedRoles.includes(authorAssociation)) {
return 'true';
} else {
return 'false';
}
- name: Add unauthorized reaction
if: steps.check.outputs.result != 'true'
uses: actions/github-script@v8
with:
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '-1'
});
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '❌ Only repository collaborators can trigger the rebase.'
});
- name: Exit if unauthorized
if: steps.check.outputs.result != 'true'
run: exit 1
- name: Add rocket reaction
uses: actions/github-script@v8
with:
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
- name: Get PR branch info
id: pr
uses: actions/github-script@v8
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
core.setOutput('head_ref', pr.head.ref);
core.setOutput('head_repo', pr.head.repo.full_name);
core.setOutput('head_clone_url', pr.head.repo.clone_url);
// Get the number of commits in the PR
// Note: per_page is set to 250, which covers most PRs.
// For PRs with >250 commits, manual rebasing would be required.
const { data: commits } = await github.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
per_page: 250
});
core.setOutput('commit_count', commits.length);
- name: Checkout PR branch
uses: actions/checkout@v4
with:
repository: ${{ steps.pr.outputs.head_repo }}
ref: ${{ steps.pr.outputs.head_ref }}
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Add upstream remote and rebase
id: rebase
run: |
git remote add upstream https://github.com/eclipse-jdt/eclipse.jdt.core.git
git fetch upstream master
# Get the number of commits in the PR
PR_COMMIT_COUNT=${{ steps.pr.outputs.commit_count }}
echo "PR has $PR_COMMIT_COUNT commits"
# Validate commit count
if [ "$PR_COMMIT_COUNT" -eq 0 ]; then
echo "Error: PR has 0 commits"
echo "success=false" >> $GITHUB_OUTPUT
exit 0
fi
# Rebase only the PR commits onto upstream/master
if git rebase --onto upstream/master HEAD~${PR_COMMIT_COUNT}; then
echo "success=true" >> $GITHUB_OUTPUT
else
echo "success=false" >> $GITHUB_OUTPUT
git rebase --abort
fi
- name: Push rebased branch
if: steps.rebase.outputs.success == 'true'
run: |
git push --force-with-lease origin ${{ steps.pr.outputs.head_ref }}
- name: Add success comment
if: steps.rebase.outputs.success == 'true'
uses: actions/github-script@v8
with:
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '+1'
});
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '✅ Successfully rebased onto `eclipse-jdt/eclipse.jdt.core:master`'
});
- name: Add failure comment
if: steps.rebase.outputs.success == 'false'
uses: actions/github-script@v8
with:
script: |
github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '-1'
});
const comment = `❌ Rebase failed due to conflicts.
To resolve manually:
\`\`\`bash
# Add upstream remote
git remote add upstream https://github.com/eclipse-jdt/eclipse.jdt.core.git
# Fetch upstream
git fetch upstream master
# Count the number of commits in your PR (check GitHub PR page)
# For example, if your PR has 3 commits:
PR_COMMIT_COUNT=3
# Rebase only your PR commits onto upstream/master
git rebase --onto upstream/master HEAD~\${PR_COMMIT_COUNT}
# Resolve conflicts, then:
git add .
git rebase --continue
# Force push when done
git push --force-with-lease
\`\`\``;
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
- name: Fail workflow if rebase failed
if: steps.rebase.outputs.success == 'false'
run: exit 1