-
Notifications
You must be signed in to change notification settings - Fork 1
246 lines (219 loc) · 9.51 KB
/
create-prs-for-stale-branches.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
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
239
240
241
242
243
244
245
246
name: "PR: Find branches without PR's"
on:
push:
branches:
- "main"
paths:
- ".github/workflows/create-prs-for-stale-branches.yml"
schedule:
- cron: "0 1 * * *"
concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: false
jobs:
build-matrix:
runs-on: [self-hosted, linux]
permissions:
contents: read
steps:
- name: "Initialise Workspace"
shell: bash
run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- name: "Info"
shell: bash
run: |
echo "Repo: ${{github.repository}}"
echo "Owner: ${{github.repository_owner}}"
- name: "Ensure JQ is installed"
shell: bash
run: sudo apt install -y jq
- name: "Checkout Source"
uses: actions/[email protected]
with:
fetch-depth: 0
fetch-tags: true
ref: main
repository: ${{github.repository}}
- name: "Create matrix of branches"
id: set-matrix
shell: bash
run: |
echo "branches=$(git branch --remote --format='%(refname:short)' | grep -Po 'origin/\K[^*]*' | grep -vE 'HEAD|release/|hotfix/' | jq -R -s -c 'split("\n")[:-1]')" >> "$GITHUB_OUTPUT"
outputs:
branches: ${{steps.set-matrix.outputs.branches}}
check-branch:
needs: build-matrix
runs-on: [self-hosted, linux]
permissions:
contents: write
pull-requests: write
strategy:
matrix:
branch: ${{fromJson(needs.build-matrix.outputs.branches)}}
env:
BRANCH: ${{matrix.branch}}
CREATE_DRAFT: true
REPO_STATUS: private
steps:
- name: "Initialise Workspace"
shell: bash
run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- name: "Checkout Source"
uses: actions/[email protected]
with:
fetch-depth: 0
fetch-tags: true
ref: main
repository: ${{github.repository}}
- name: "Check Repo Visibility"
if: github.repository_owner != 'funfair-tech'
uses: credfeto/[email protected]
id: visibility
with:
repository: ${{github.repository}}
token: ${{secrets.SOURCE_PUSH_TOKEN}}
- name: "Override DRAFT Flag"
if: github.repository_owner != 'funfair-tech' && env.REPO_STATUS == 'private'
shell: bash
run: echo "CREATE_DRAFT=false" >> "$GITHUB_ENV"
- name: "Branch Detection"
id: detection
shell: bash
run: |
echo "defaultbranch=$(git remote show origin | grep -Po 'HEAD\sbranch:\s+\K[^.*]*')" >> "$GITHUB_OUTPUT"
echo "DEFAULT_BRANCH=$(git remote show origin | grep -Po 'HEAD\sbranch:\s+\K[^.*]*')" >> "$GITHUB_ENV"
# On Default branch
- name: "On Default Branch"
if: steps.detection.outputs.defaultbranch == matrix.branch
uses: actions/[email protected]
with:
script: |
core.info("On Default branch - don't do anything");
# On Non-Default branch
- name: "On Non-Default branch"
if: steps.detection.outputs.defaultbranch != matrix.branch
uses: actions/[email protected]
with:
script: |
core.info('On non-default branch');
- name: "Find Existing PR"
if: steps.detection.outputs.defaultbranch != matrix.branch
uses: juliangruber/[email protected]
id: find-pull-request
with:
branch: "${{env.BRANCH}}"
- name: "Display Found PR"
if: steps.detection.outputs.defaultbranch != matrix.branch
shell: bash
run: echo "Pull Request ${{steps.find-pull-request.outputs.number}} (${{steps.find-pull-request.outputs.head-sha}})"
- name: "Check Branch Status"
if: steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''
shell: bash
run:
echo "Check to see if ${{env.DEFAULT_BRANCH}} is ahead or behind ${{matrix.branch}}..."
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}"
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}" | awk '{ print "${{env.BRANCH}} is behind ${{env.DEFAULT_BRANCH}} by "$1" commit(s)" }'
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}" | awk '{ print "${{matrix.BRANCH}} is ahead of ${{env.DEFAULT_BRANCH}} by "$2" commit(s)" }'
env:
DEFAULT_BRANCH: ${{steps.detection.outputs.defaultbranch}}
- name: "Get last update time of branch"
if: steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''
id: lastupdate
shell: bash
run: echo "time=$(git log -1 --format=@%ct origin/${{env.BRANCH}})" >> "$GITHUB_OUTPUT"
- name: "Check Ahead and Behind Status"
if: steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''
id: aheadbehind
shell: bash
run: >
{ \
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{env.BRANCH}}" \
| awk '{ print "behind="$1 }' ; \
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{env.BRANCH}}" \
| awk '{ print "ahead="$2 }' ; \
echo "dayssinceupdate=$((($(date +%s)-$(date +%s --date ${{env.LAST_UPDATE}}))/(3600*24)))" ; \
echo "COMMIT_MSG=$(git log -1 --pretty=%B "${{env.ORIGIN_DEFAULT_BRANCH}}")" ; \
} >> "$GITHUB_OUTPUT"
env:
DEFAULT_BRANCH: ${{steps.detection.outputs.defaultbranch}}
ORIGIN_DEFAULT_BRANCH: origin/${{steps.detection.outputs.defaultbranch}}
LAST_UPDATE: ${{steps.lastupdate.outputs.time}}
# Non-default - ahead of default branch, and no -re-existing PR then create one.
- name: "Read PR Template"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7
id: pr-template
uses: juliangruber/[email protected]
with:
path: ./.github/PULL_REQUEST_TEMPLATE.md
- name: "Create PR"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7
id: open-pr
uses: repo-sync/[email protected]
with:
source_branch: ${{matrix.branch}} # If blank, default: triggered branch
destination_branch: ${{steps.detection.outputs.defaultbranch}} # If blank, default: master
pr_assignee: "${{github.actor}}" # Comma-separated list (no spaces)
pr_label: "auto-pr,stale" # Comma-separated list (no spaces)
pr_draft: ${{env.CREATE_DRAFT}} # Creates pull request as draft
pr_title: "${{steps.aheadbehind.outputs.COMMIT_MSG}} [No commits for ${{steps.aheadbehind.outputs.dayssinceupdate}} days]"
pr_body: "${{steps.pr-template.outputs.content}}"
github_token: ${{github.token}}
- name: "New PR Details"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7
uses: actions/[email protected]
with:
script: |
core.info('URL: ${{steps.open-pr.outputs.pr_url}}');
core.info('PR: ${{steps.open-pr.outputs.pr_number}}');
core.info('CF: ${{steps.open-pr.outputs.has_changed_files}}');
- name: "Update PR Labels"
if: steps.open-pr.outputs.pr_number != ''
uses: actions/labeler@v5
with:
repo-token: ${{secrets.SOURCE_PUSH_TOKEN}}
configuration-path: .github/labeler.yml
sync-labels: true
pr-number: ${{steps.open-pr.outputs.pr_number}}
- name: "Rebase"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.behind != 0
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7
uses: actions/[email protected]
with:
script: |
core.info('Rebase?');
- name: "Delete Branch"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead == '0'
&& steps.aheadbehind.outputs.dayssinceupdate > 60
uses: dawidd6/[email protected]
with:
github_token: ${{secrets.SOURCE_PUSH_TOKEN}}
branches: ${{matrix.branch}}
- name: "Statistics"
if: |-
steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
uses: actions/[email protected]
with:
script: |
core.info('Ahead: ${{steps.aheadbehind.outputs.ahead}}');
core.info('Behind: ${{steps.aheadbehind.outputs.behind}}');
core.info('Last Commit: ${{steps.aheadbehind.outputs.dayssinceupdate}} days ago');