-
Notifications
You must be signed in to change notification settings - Fork 663
145 lines (126 loc) 路 4.16 KB
/
Copy pathcheck-links.yml
File metadata and controls
145 lines (126 loc) 路 4.16 KB
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
name: Check links
on:
workflow_dispatch:
schedule:
- cron: '23 7 * * *'
push:
branches:
- main
paths:
- '.github/workflows/check-links.yml'
- 'package.json'
- 'pnpm-lock.yaml'
- 'pnpm-workspace.yaml'
- 'web/**'
permissions:
contents: read
issues: write
jobs:
check-links:
name: Check links
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10.33.0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build site
run: pnpm build
- name: Check links
id: check-links
continue-on-error: true
run: pnpm -C web check:links -- --report=../link-check-report.json
- name: Create or update issue
if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const reportPath = 'link-check-report.json';
if (!fs.existsSync(reportPath)) {
core.warning('Link check report was not generated.');
return;
}
const report = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
const brokenLinks = report.brokenLinks ?? [];
const title = 'Enlaces rotos detectados';
const marker = '<!-- librosgratis-link-check -->';
const labels = ['broken-links'];
const { owner, repo } = context.repo;
const runUrl = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${context.runId}`;
const existingIssues = await github.paginate(github.rest.issues.listForRepo, {
owner,
repo,
state: 'open',
per_page: 100,
});
const existingIssue = existingIssues.find((issue) => !issue.pull_request && issue.title === title);
if (brokenLinks.length === 0) {
if (existingIssue) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: existingIssue.number,
body: `${marker}\nLa revision de enlaces ya no encuentra enlaces rotos. Cierro esta issue automaticamente.`,
});
await github.rest.issues.update({
owner,
repo,
issue_number: existingIssue.number,
state: 'closed',
state_reason: 'completed',
});
}
return;
}
const lines = brokenLinks.map((link) => {
const location = `${link.source}:${link.line}`;
const status = link.status ? `${link.status} ${link.statusText ?? ''}`.trim() : link.error;
const reason = link.reason ?? status ?? 'unknown';
return `- [ ] \`${link.url}\` (${reason}) en \`${location}\``;
});
const body = [
marker,
`La revision automatica de enlaces encontro ${brokenLinks.length} enlace(s) roto(s).`,
'',
lines.join('\n'),
'',
`Workflow: ${runUrl}`,
].join('\n');
if (existingIssue) {
await github.rest.issues.update({
owner,
repo,
issue_number: existingIssue.number,
body,
});
} else {
try {
await github.rest.issues.create({
owner,
repo,
title,
body,
labels,
});
} catch (error) {
if (error.status !== 422) {
throw error;
}
await github.rest.issues.create({
owner,
repo,
title,
body,
});
}
}