This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (124 loc) · 4.84 KB
/
combine-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
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
name: 'Combine PRs'
on:
workflow_dispatch:
inputs:
label-name:
description: 'Include PRs with this label'
required: true
default: 'combine'
must-be-green:
description: 'Only combine PRs that are green (status is success)'
required: true
default: 'true'
combine-branch-name:
description: 'Name of the branch to combine PRs into'
required: true
default: 'combine-prs-branch'
jobs:
combine-prs:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v3
id: fetch-branch-names
name: Fetch branch names
env:
MUST_BE_GREEN: ${{ github.event.inputs['must-be-green'] }}
LABEL_NAME: ${{ github.event.inputs['label-name'] }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let pulls = await github.paginate('GET /repos/:owner/:repo/pulls', {
owner: context.repo.owner,
repo: context.repo.repo,
})
pulls.reverse()
branches = []
pullsForCombine = []
baseBranch = null
for (const pull of pulls) {
const labels = pull.labels
console.log('Pull: ' + pull.title)
for (const label of labels) {
if (label.name === process.env.LABEL_NAME) {
statusOk = true
console.log('Label matched: ' + label.name)
if (process.env.MUST_BE_GREEN === 'true') {
const statuses = await github.paginate(
'GET /repos/{owner}/{repo}/commits/{ref}/status',
{
owner: context.repo.owner,
repo: context.repo.repo,
ref: pull.base.ref,
}
)
console.log('Checking green status: ' + pull.title)
if (statuses.length > 0) {
const latestStatus = statuses[0].state
console.log('Validating status: ' + latestStatus)
if (latestStatus !== 'success') {
console.log(
'Discarding ' + pull.title + ' with status ' + latestStatus
)
statusOk = false
}
}
}
if (statusOk) {
console.log('Adding pull to array: ' + pull.title)
branches.push(pull.head.ref)
pullsForCombine.push(
`${pull.number}: [${pull.title}](${pull.number})`
)
baseBranch = pull.base.ref
}
}
}
}
if (branches.length == 0) {
core.setFailed('No PRs matched criteria')
return
}
core.setOutput('base-branch', baseBranch)
core.setOutput('pulls', pullsForCombine.join('\n'))
branches = branches.join(' ')
console.log('Combined: ' + branches)
return branches
- uses: actions/[email protected]
with:
fetch-depth: 0
token: ${{ secrets.COMBINE_PRS_TOKEN }}
- name: Create combined branch
env:
BASE_BRANCH: ${{ steps.fetch-branch-names.outputs.baseBranch }}
BRANCHES_TO_COMBINE: ${{ steps.fetch-branch-names.outputs.result }}
COMBINE_BRANCH_NAME: ${{ github.event.inputs.combine-branch-name }}
run: |
echo "$BRANCHES_TO_COMBINE"
sourceBranches="${BRANCHES_TO_COMBINE%\"}"
sourceBranches="${sourceBranches#\"}"
baseBranch="${BASE_BRANCH%\"}"
baseBranch="${baseBranch#\"}"
git config pull.rebase false
git config user.name github-actions
git config user.email [email protected]
git branch $COMBINE_BRANCH_NAME $baseBranch
git checkout $COMBINE_BRANCH_NAME
git pull origin $sourceBranches --no-edit
git push origin $COMBINE_BRANCH_NAME
- uses: actions/github-script@v3
name: Create Combined Pull Request
env:
PULLS: ${{ steps.fetch-branch-names.outputs.pulls }}
with:
github-token: ${{ secrets.COMBINE_PRS_TOKEN }}
script: |
const pulls = process.env.PULLS
const body = 'This PR was created by combining the following PRs:\n' + pulls
await github.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: 'Combined PR',
head: '${{ github.event.inputs.combine-branch-name }}',
base: '${{ steps.fetch-branch-names.outputs.base-branch }}',
body: body,
})