-
-
Notifications
You must be signed in to change notification settings - Fork 2
242 lines (227 loc) · 9.37 KB
/
markdown-link-check-handle-result.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
name: Markdown link check - handle result
on:
workflow_run:
workflows: ["Markdown link check"]
types:
- completed
defaults:
run:
shell: pwsh
env:
MLC_WORKFLOW_INFO_ARTIFACT : markdown-link-check-workflow-info
MLC_LABEL : markdown-link-check
WORKFLOW_HEAD_SHA : ${{ github.event.workflow_run.head_sha }}
jobs:
summary:
name: Set workflow summary
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- name: Run markdown link check log parser
id: mlc-log-parser
uses: edumserrano/markdown-link-check-log-parser@v1
with:
auth-token: '${{ secrets.GITHUB_TOKEN }}'
repo: '${{ github.repository }}'
run-id: '${{ github.event.workflow_run.id }}'
job-name: 'Markdown link check'
step-name: 'Markdown link check'
output: 'step-md'
- name: Set workflow summary
run: |
$mlcResult = @'
${{ steps.mlc-log-parser.outputs.mlc-result }}
'@
$mlcResult > $env:GITHUB_STEP_SUMMARY
workflow-info:
name: Read trigger workflow info
permissions:
contents: read
runs-on: ubuntu-latest
outputs:
has-broken-links: ${{ steps.mlc-workflow-info.outputs.has-broken-links }}
workflow: ${{ steps.mlc-workflow-info.outputs.workflow }}
workflow-url: ${{ steps.mlc-workflow-info.outputs.workflow-url }}
pr-number: ${{ steps.mlc-workflow-info.outputs.pr-number }}
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
- name: Download markdown link check workflow info artifact
uses: dawidd6/action-download-artifact@v6
with:
name: ${{ env.MLC_WORKFLOW_INFO_ARTIFACT }}
run_id: ${{ github.event.workflow_run.id }}
- name: Read markdown link check workflow info
id: mlc-workflow-info
run: |
$worfklowInfoFilePath = "./markdown-link-check-workflow-info.json"
$workflowInfoJson = Get-Content $worfklowInfoFilePath
Write-Output $workflowInfoJson
$workflowInfo = $workflowInfoJson | ConvertFrom-Json
$hasBrokenLinks = $workflowInfo.hasBrokenLinks
$workflow = $workflowInfo.workflow
$workflowUrl = $workflowInfo.workflowUrl
$isPullRequest = "${{ github.event.workflow_run.event }}" -eq "pull_request"
if($isPullRequest)
{
$prNumber = $workflowInfo.prNumber
}
Write-Output "has-broken-links=$hasBrokenLinks" >> $env:GITHUB_OUTPUT
Write-Output "workflow=$workflow" >> $env:GITHUB_OUTPUT
Write-Output "workflow-url=$workflowUrl" >> $env:GITHUB_OUTPUT
Write-Output "pr-number=$prNumber" >> $env:GITHUB_OUTPUT
on-push-check:
name: Check for open issue
needs: [workflow-info]
if: github.event.workflow_run.event == 'push'
permissions:
contents: read
issues: read
runs-on: ubuntu-latest
outputs:
can-create-issue: ${{ steps.open-issue-check.outputs.can-create-issue }}
create-issue: ${{ steps.needs-issue-check.outputs.create-issue }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ env.WORKFLOW_HEAD_SHA }}
- name: Check for open markdown link check issues
id: open-issue-check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Only create an issue if there isn't any markdown link check issue open. This issueevents multiple pull requests being open to fix markdown issues.
# Once one is merged, the workflows will create more markdown link check issues if needed.
$openIssuesResult = gh issue list --state open --label ${{ env.MLC_LABEL }}
$canCreateIssue = [string]::IsNullOrEmpty($openIssuesResult)
Write-Output "can-create-issue=$canCreateIssue" >> $env:GITHUB_OUTPUT
- name: Check if an issue needs to be created
id: needs-issue-check
run: |
$hasBrokenLinks = [System.Convert]::ToBoolean("${{ needs.workflow-info.outputs.has-broken-links }}")
$canCreateIssue = [System.Convert]::ToBoolean("${{ steps.open-issue-check.outputs.can-create-issue }}")
$createIssue = $canCreateIssue -and $hasBrokenLinks
Write-Output "create-issue=$createIssue" >> $env:GITHUB_OUTPUT
- name: Log info
run: |
$hasBrokenLinks = [System.Convert]::ToBoolean("${{ needs.workflow-info.outputs.has-broken-links }}")
$canCreateIssue = [System.Convert]::ToBoolean("${{ steps.open-issue-check.outputs.can-create-issue }}")
$createIssue = [System.Convert]::ToBoolean("${{ steps.needs-issue-check.outputs.create-issue }}")
if(!$hasBrokenLinks) {
Write-Output "::notice::Markdown link check did NOT find any broken links."
}
elseif($hasBrokenLinks -and $canCreateIssue) {
Write-Output "::warning::Markdown link check found broken links and so an issue will be created."
}
elseif($hasBrokenLinks -and !$canCreateIssue) {
Write-Output "::warning::Markdown link check found broken links but an issue from markdown link check is already open so a new one will not be created. Only one markdown link check issue can be open at a time."
}
on-push:
name: Handle MLC on push
needs: [workflow-info, on-push-check]
if: needs.on-push-check.outputs.create-issue == 'true'
permissions:
contents: read
issues: write
runs-on: ubuntu-latest
env:
WORKFLOW: ${{ needs.workflow-info.outputs.workflow }}
WORKFLOW_URL: ${{ needs.workflow-info.outputs.workflow-url }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ env.WORKFLOW_HEAD_SHA }}
- name: Render issue body template
uses: yukitsune/template-cli@v0.1.0
with:
args: --input .github/workflows/templates/markdown-link-check-handle-result/broken-links-found-issue-body.md \
--value "workflow=${{ env.WORKFLOW }}" \
--value "workflowUrl=${{ env.WORKFLOW_URL }}" \
--value "commitSha=${{ env.WORKFLOW_HEAD_SHA }}" \
--output .
- name: Set render issue body template as step output
id: render-issue-body-template
run: |
$fileContent = Get-Content ./broken-links-found-issue-body.md
$random = Get-Random
$delimiter = "EOF_$random"
Write-Output "result<<$delimiter" >> $env:GITHUB_OUTPUT
Write-Output $fileContent >> $env:GITHUB_OUTPUT
Write-Output $delimiter >> $env:GITHUB_OUTPUT
- name: Run markdown link check log parser
id: mlc-log-parser
uses: edumserrano/markdown-link-check-log-parser@v1
with:
auth-token: '${{ secrets.GITHUB_TOKEN }}'
repo: '${{ github.repository }}'
run-id: '${{ github.event.workflow_run.id }}'
job-name: 'Markdown link check'
step-name: 'Markdown link check'
output: 'step-md'
- name: Create markdown issue
id: create-issue
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$title = "Markdown link check found broken links"
$body = @'
${{ steps.render-issue-body-template.outputs.result }}
${{ steps.mlc-log-parser.outputs.mlc-result }}
'@
$issueUrl = gh issue create `
--title $title `
--body $body `
--label ${{ env.MLC_LABEL }} `
--label bot:robot:
Write-Output "issue-url=$issueUrl" >> $env:GITHUB_OUTPUT
- name: Log info
run: |
$issueUrl = "${{ steps.create-issue.outputs.issue-url }}"
Write-Output "::warning title=Markdown link check::An issue to report broken markdown links was created at $issueUrl."
on-pr:
name: Handle MLC on PR
needs: [workflow-info]
if: github.event.workflow_run.event == 'pull_request' && needs.workflow-info.outputs.has-broken-links == 'true'
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Run markdown link check log parser
id: mlc-log-parser
uses: edumserrano/markdown-link-check-log-parser@v1
with:
auth-token: '${{ secrets.GITHUB_TOKEN }}'
repo: '${{ github.repository }}'
run-id: '${{ github.event.workflow_run.id }}'
job-name: 'Markdown link check'
step-name: 'Markdown link check'
output: 'step-md'
- name: Add marker to MLC result
id: pr-comment-body
run: |
$commentBody = @'
<!-- on-pr-markdown-link-check -->
${{ steps.mlc-log-parser.outputs.mlc-result }}
'@
$random = Get-Random
$delimiter = "EOF_$random"
Write-Output "pr-comment-body<<$delimiter" >> $env:GITHUB_OUTPUT
Write-Output $commentBody >> $env:GITHUB_OUTPUT
Write-Output $delimiter >> $env:GITHUB_OUTPUT
- name: Update PR with markdown link check result
uses: edumserrano/find-create-or-update-comment@v3
with:
issue-number: ${{ needs.workflow-info.outputs.pr-number }}
body-includes: '<!-- on-pr-markdown-link-check -->'
comment-author: github-actions[bot]
body: ${{ steps.pr-comment-body.outputs.pr-comment-body }}
edit-mode: replace
- name: Log info
run: |
$prUrl = "https://github.com/${{ github.repository }}/pull/${{ needs.workflow-info.outputs.pr-number }}"
Write-Output "::warning title=Markdown Link Check::Markdown Link Check detected broken links and a comment was added to the PR at $prUrl."