Skip to content

Markdown link check - handle result #600

Markdown link check - handle result

Markdown link check - handle result #600

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/[email protected]
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."