Skip to content

Commit 663d52f

Browse files
authored
lint only modified links (#1819)
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 3d43335 commit 663d52f

File tree

8 files changed

+123
-25
lines changed

8 files changed

+123
-25
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ max_line_length = 120
1818
indent_size = 4
1919
max_line_length = 120
2020

21-
[{.mise/tasks/build-release.sh,.mise/tasks/lint/super-linter.sh,.github/workflows/multi-version-test.yml}]
22-
max_line_length = 200
21+
[{.mise/tasks/build-release.sh,.mise/tasks/lint/super-linter.sh,.mise/tasks/lint/links-in-modified-files.sh,.github/workflows/multi-version-test.yml,.github/workflows/lint-rest.yml}]
22+
max_line_length = 200

.github/config/lychee.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Lychee configuration file
2+
# See https://lychee.cli.rs/config/
3+
4+
timeout = 30
5+
retry_wait_time = 5
6+
max_retries = 6
7+
max_concurrency = 4
8+
9+
# Check link anchors
10+
include_fragments = true
11+
12+
base_url = "https://prometheus.github.io"
13+
exclude_path = ["docs/themes"]
14+
15+
remap = [
16+
# workaround for https://github.com/lycheeverse/lychee/issues/1729
17+
"https://github.com/(.*?)/(.*?)/blob/(.*?)/(.*#.*)$ https://raw.githubusercontent.com/$1/$2/$3/$4"
18+
]
19+
20+
exclude = [
21+
# excluding links to pull requests and issues is done for performance
22+
"^https://github.com/prometheus/client_java/(issues|pull)/\\d+$",
23+
24+
# exclude localhost URLs as they require running services
25+
"^http://localhost",
26+
"^https://localhost",
27+
28+
'#',
29+
'CONTRIBUTING.md',
30+
'LICENSE',
31+
'MAINTAINERS.md',
32+
33+
# exclude private GitHub settings pages
34+
"^https://github.com/prometheus/client_java/settings/",
35+
]

.github/workflows/lint-rest.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
---
22
name: Lint What Super Linter Can't
33

4-
on: [pull_request]
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
59

6-
permissions: {}
10+
permissions:
11+
contents: read
712

813
jobs:
914
lint:
@@ -12,10 +17,20 @@ jobs:
1217
- name: Check out
1318
with:
1419
persist-credentials: false
20+
fetch-depth: 0 # needed for merge-base used in lint:links-in-modified-files
1521
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1622
- uses: jdx/mise-action@6d1e696aa24c1aa1bcc1adea0212707c71ab78a8 # v3.6.1
1723
with:
1824
version: v2026.1.7
1925
sha256: d98523f15392ab17909a55560244667aa81122766209b816d9a9b9585109bfea
20-
- name: Lint
21-
run: mise run lint:rest
26+
27+
- name: Lint for pull requests
28+
if: github.event_name == 'pull_request'
29+
env:
30+
GITHUB_TOKEN: ${{ github.token }}
31+
GITHUB_BASE_REF: ${{ github.base_ref }}
32+
GITHUB_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
33+
run: |
34+
mise run lint:bom
35+
mise run lint:local-links
36+
mise run lint:links-in-modified-files --base origin/"${GITHUB_BASE_REF}" --head "${GITHUB_HEAD_SHA}"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
#MISE description="Lint links in modified files"
3+
4+
set -e
5+
6+
#USAGE flag "--base <base>" help="base branch to compare against (default: origin/main)" default="origin/main"
7+
#USAGE flag "--head <head>" help="head branch to compare against (empty for local changes) (default: empty)" default=""
8+
9+
# shellcheck disable=SC2154
10+
if [ "$usage_head" = "''" ]; then
11+
usage_head=""
12+
fi
13+
14+
# Check if lychee config was modified
15+
# - because usage_head may be empty
16+
# shellcheck disable=SC2086,SC2154
17+
config_modified=$(git diff --name-only --merge-base "$usage_base" $usage_head |
18+
grep -E '^(\.github/config/lychee\.toml|\.mise/tasks/lint/.*|mise\.toml)$' || true)
19+
20+
if [ -n "$config_modified" ]; then
21+
echo "config changes, checking all files."
22+
mise run lint:links
23+
else
24+
# Using lychee's default extension filter here to match when it runs against all files
25+
# Note: --diff-filter=d filters out deleted files
26+
# - because usage_head may be empty
27+
# shellcheck disable=SC2086
28+
modified_files=$(git diff --name-only --diff-filter=d "$usage_base" $usage_head |
29+
grep -E '\.(md|mkd|mdx|mdown|mdwn|mkdn|mkdown|markdown|html|htm|txt)$' |
30+
tr '\n' ' ' || true)
31+
32+
if [ -z "$modified_files" ]; then
33+
echo "No modified files, skipping link linting."
34+
exit 0
35+
fi
36+
37+
# shellcheck disable=SC2086
38+
mise run lint:links $modified_files
39+
fi

.mise/tasks/lint/links.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
#MISE description="Lint links in all files"
3+
4+
set -e
5+
6+
#USAGE arg "<file>" var=#true help="files to check" default="."
7+
8+
# shellcheck disable=SC2154
9+
for f in $usage_file; do
10+
echo "Checking links in file: $f"
11+
done
12+
13+
# shellcheck disable=SC2086
14+
lychee --verbose --config .github/config/lychee.toml $usage_file

.mise/tasks/lint/local-links.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
#MISE description="Lint links in local files"
3+
4+
set -e
5+
6+
#USAGE arg "<file>" var=#true help="files to check" default="."
7+
8+
# shellcheck disable=SC2154
9+
for f in $usage_file; do
10+
echo "Checking links in file: $f"
11+
done
12+
13+
# shellcheck disable=SC2086
14+
lychee --verbose --scheme file --include-fragments --config .github/config/lychee.toml $usage_file

lychee.toml

Lines changed: 0 additions & 15 deletions
This file was deleted.

mise.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ run = "./mvnw verify"
4343
description = "build all modules without tests"
4444
run = "./mvnw install -DskipTests -Dcoverage.skip=true"
4545

46-
[tasks."lint:links"]
47-
description = "Lint markdown links"
48-
run = "lychee --include-fragments ."
49-
5046
[tasks."lint:rest"]
5147
description = "All lints not covered by super linter"
5248
depends = ["lint:links", "lint:bom"]

0 commit comments

Comments
 (0)