-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
82 lines (79 loc) · 3.02 KB
/
md-link-check.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
name: Markdown Link Check
on:
pull_request_target:
paths:
- '**.md'
- '!.github/**'
workflow_dispatch:
permissions:
contents: read
pull-requests: write
jobs:
link-check:
runs-on: ubuntu-latest
steps:
- name: Setup Action
uses: actions/checkout@v3
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm install -g markdown-link-check@3.11.0
- name: Changed Files Exporter
if: github.event_name == 'pull_request_target'
id: files
# This uses the SHA for 4.0.1 - https://github.com/umani/changed-files/releases/tag/v4.0.1
uses: umani/changed-files@0239328a3a6268aad16af7c3e4efc78e32d6c0f0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: PR link check
if: github.event_name == 'pull_request_target'
env:
FILES: '${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}'
run: |
echo "The Following files were changed or created:"
echo $FILES
touch log err
for FILE in $FILES; do echo $FILE | grep -q .*\.md\$ && markdown-link-check -q -v -c .github/configs/markdown-link-check-config.json $FILE 1>> log 2>> err; done
if grep -q "ERROR:" err ; then exit 1 ; else echo -e "No broken links found."; fi
echo $(cat log)
echo $(cat err)
- name: Repository link check
if: github.event_name == 'workflow_dispatch'
run: |
touch log err
find . -name \*.md -exec markdown-link-check -q -v --config .github/configs/markdown-link-check-config.json {} 1>> log 2>> err \;
if grep -q "ERROR:" err ; then exit 1 ; else echo -e "No broken links found."; fi
echo $(cat log)
echo $(cat err)
- name: Show broken links
if: failure()
run: |
cat log | awk -v RS="FILE:" 'match($0, /(\S*\.md).*\[✖\].*([0-9]*\slinks\schecked\.)(.*)/, arr ) { print "FILE:"arr[1] arr[3] > "brokenlinks.txt"}'
cat brokenlinks.txt
rm -f err log
- name: Upload list of broken links
if: failure()
uses: actions/upload-artifact@v3
with:
name: broken-links
path: brokenlinks.txt
- name: Comment Broken Links
if: ${{ failure() && github.event_name == 'pull_request_target' }}
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require("fs");
const brokenLinksPath = `${process.env.GITHUB_WORKSPACE}/brokenlinks.txt`;
const brokenLinksString = fs.readFileSync(brokenLinksPath).toString().trimEnd();
github.rest.issues.createComment({
issue_number: ${{ github.event.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: `**The following links are broken:** \n${brokenLinksString}`
})