generated from credfeto/cs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
199 lines (173 loc) · 8.22 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
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: ubuntu-latest
permissions:
contents: read
steps:
- run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- run: |
echo "Repo: ${{github.repository}}"
echo "Owner: ${{github.repository_owner}}"
- run: sudo apt install -y jq
- uses: actions/[email protected]
with:
fetch-depth: 0
ref: main
repository: ${{github.repository}}
- id: set-matrix
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: ubuntu-latest
permissions:
contents: write
pull-requests: write
strategy:
matrix:
branch: ${{fromJson(needs.build-matrix.outputs.branches)}}
env:
BRANCH: ${{matrix.branch}}
CREATE_DRAFT: true
steps:
- run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- uses: actions/[email protected]
with:
fetch-depth: 0
ref: main
repository: ${{github.repository}}
- if: ${{github.repository_owner != 'funfair-tech'}}
name: "Check Repo Visibility"
uses: credfeto/[email protected]
id: visibility
with:
repository: ${{github.repository}}
token: ${{secrets.GITHUB_TOKEN}}
- if: ${{github.repository_owner != 'funfair-tech' && env.REPO_STATUS == 'private'}}
run: echo "CREATE_DRAFT=false" >> "$GITHUB_ENV"
- id: detection
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
- if: ${{steps.detection.outputs.defaultbranch == matrix.branch}}
run: echo "On Default branch - don't do anything"
# On Non-Default branch
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
run: echo "On non-default branch"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
uses: juliangruber/[email protected]
id: find-pull-request
with:
branch: "${{env.BRANCH}}"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
run: echo "Pull Request ${{steps.find-pull-request.outputs.number}} (${{steps.find-pull-request.outputs.head-sha}})"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
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}}
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
id: lastupdate
run: echo "time=$(git log -1 --format=@%ct origin/${{env.BRANCH}})" >> "$GITHUB_OUTPUT"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
id: aheadbehind
name: Check Ahead\Behind Status
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.
- 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
- 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}}
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7}}
name: New PR Details
run: |
echo "URL: ${{steps.open-pr.outputs.pr_url}}"
echo "PR: ${{steps.open-pr.outputs.pr_number}}"
echo "CF: ${{steps.open-pr.outputs.has_changed_files}}"
- if: ${{steps.open-pr.outputs.pr_number != ''}}
uses: actions/[email protected]
with:
repo-token: ${{secrets.SOURCE_PUSH_TOKEN}}
configuration-path: .github/labeler.yml
sync-labels: true
pr-number: ${{steps.open-pr.outputs.pr_number}}
- 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}}
name: Rebase
run: echo "Rebase?"
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead == '0'
&& steps.aheadbehind.outputs.dayssinceupdate > 60}}
name: Delete Branch
uses: dawidd6/[email protected]
with:
github_token: ${{secrets.SOURCE_PUSH_TOKEN}}
branches: ${{matrix.branch}}
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''}}
run: |
echo "Ahead: ${{steps.aheadbehind.outputs.ahead}}"
echo "Behind: ${{steps.aheadbehind.outputs.behind}}"
echo "Last Commit: ${{steps.aheadbehind.outputs.dayssinceupdate}} days ago"