-
Notifications
You must be signed in to change notification settings - Fork 710
workflow: optimize external link checks (#22894) #22896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ti-chi-bot
merged 2 commits into
pingcap:release-8.5
from
ti-chi-bot:cherry-pick-22894-to-release-8.5
May 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| use strict; | ||
| use warnings; | ||
| use File::Basename qw(dirname); | ||
| use File::Path qw(make_path); | ||
|
|
||
| my ($out_root, $list_path) = @ARGV; | ||
| die "usage: $0 OUT_ROOT LIST_PATH\n" unless defined $out_root && defined $list_path; | ||
|
|
||
| my %added_lines_by_file; | ||
| my %has_link_candidate; | ||
| my $file; | ||
|
|
||
| while (my $line = <STDIN>) { | ||
| chomp $line; | ||
|
|
||
| if ($line =~ m{^\+\+\+ b/(.+)$}) { | ||
| $file = $1; | ||
| next; | ||
| } | ||
|
|
||
| next unless defined $file; | ||
| next unless $line =~ /^\+(?!\+\+)(.*)$/; | ||
|
|
||
| my $content = $1; | ||
| push @{$added_lines_by_file{$file}}, $content; | ||
| $has_link_candidate{$file} = 1 if $content =~ m{https?://}i || $content =~ /\bhref\s*=/i; | ||
| } | ||
|
|
||
| make_path($out_root); | ||
| open my $list_fh, ">", $list_path or die "cannot write $list_path: $!"; | ||
|
|
||
| for my $file (sort keys %added_lines_by_file) { | ||
| next unless $has_link_candidate{$file}; | ||
| next if $file =~ m{(?:^|/)\.\.(?:/|$)}; | ||
|
|
||
| my $out_path = "$out_root/$file"; | ||
| make_path(dirname($out_path)); | ||
| open my $out_fh, ">", $out_path or die "cannot write $out_path: $!"; | ||
| for my $line (@{$added_lines_by_file{$file}}) { | ||
| print {$out_fh} "$line\n"; | ||
| } | ||
| close $out_fh; | ||
| print {$list_fh} "$out_path\n"; | ||
| } | ||
|
|
||
| close $list_fh; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use strict; | ||
| use warnings; | ||
| use File::Basename qw(dirname); | ||
| use File::Path qw(make_path); | ||
|
|
||
| my ($out_root, $list_path) = @ARGV; | ||
| die "usage: $0 OUT_ROOT LIST_PATH\n" unless defined $out_root && defined $list_path; | ||
|
|
||
| my $site_base_url = $ENV{DOCS_SITE_BASE_URL}; | ||
| die "DOCS_SITE_BASE_URL is not set\n" unless defined $site_base_url && $site_base_url ne ""; | ||
| $site_base_url =~ s{/+\z}{}; | ||
|
|
||
| make_path($out_root); | ||
| open my $list_fh, ">", $list_path or die "cannot write $list_path: $!"; | ||
|
|
||
| { | ||
| local $/ = "\0"; | ||
| while (my $file = <STDIN>) { | ||
| chomp $file; | ||
| next if $file =~ m{(?:^|/)\.\.(?:/|$)}; | ||
| next unless -f $file; | ||
|
|
||
| open my $in_fh, "<", $file or die "cannot read $file: $!"; | ||
| my $content = do { local $/; <$in_fh> }; | ||
| close $in_fh; | ||
| next unless defined $content; | ||
|
|
||
| my %seen; | ||
| while ($content =~ /\bhref\s*=\s*(["'])(.*?)\1/gi) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| my $href = $2; | ||
| $href =~ s/^\s+|\s+$//g; | ||
| next if $href eq ""; | ||
| next if $href =~ m{^https?://}i; | ||
| next if $href =~ m{^(?:#|[a-z][a-z0-9+.-]*:)}i; | ||
|
|
||
| my $url; | ||
| if ($href =~ m{^//}) { | ||
| $url = "https:$href"; | ||
| } elsif ($href =~ m{^/}) { | ||
| $url = "$site_base_url$href"; | ||
| } else { | ||
| next; | ||
| } | ||
| $seen{$url} = 1; | ||
| } | ||
|
|
||
| next unless %seen; | ||
| my $out_path = "$out_root/$file"; | ||
| make_path(dirname($out_path)); | ||
| open my $out_fh, ">", $out_path or die "cannot write $out_path: $!"; | ||
| for my $url (sort keys %seen) { | ||
| print {$out_fh} "<$url>\n"; | ||
| } | ||
| close $out_fh; | ||
| print {$list_fh} "$out_path\n"; | ||
| } | ||
| } | ||
| close $list_fh; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,56 @@ | ||
| name: Links (Fail Fast) | ||
| name: ci / external-links-in-changed-lines (pull_request) | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| env: | ||
| DOCS_SITE_BASE_URL: "https://docs.pingcap.com" | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| linkChecker: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/checkout@v6 | ||
| with: | ||
| fetch-depth: 2 | ||
|
|
||
| - name: 'Get a list of changed markdown files to process' | ||
| id: changed-files | ||
| - name: Collect changed markdown lines with links | ||
| id: changed-lines | ||
| run: | | ||
| CHANGED_FILES=$(git diff-tree --name-only --diff-filter 'AM' -r HEAD^1 HEAD -- "*.md" | sed -z "s/\n$//;s/\n/' '/g") | ||
| echo "all_changed_files=${CHANGED_FILES}" >> $GITHUB_OUTPUT | ||
| git -c core.quotePath=false diff --unified=0 --diff-filter=AM --no-ext-diff --no-color HEAD^1 HEAD -- '*.md' | | ||
| perl .github/scripts/extract-changed-markdown-lines.pl .lychee-pr-changed-lines .lychee-pr-inputs.txt | ||
|
|
||
| count=$(wc -l < .lychee-pr-inputs.txt | tr -d ' ') | ||
| echo "count=${count}" >> "$GITHUB_OUTPUT" | ||
|
|
||
| if [ "$count" -gt 0 ]; then | ||
| echo "has_inputs=true" >> "$GITHUB_OUTPUT" | ||
| sed 's/^/- /' .lychee-pr-inputs.txt | ||
| else | ||
| echo "has_inputs=false" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Collect doc site href URLs | ||
| if: ${{ steps.changed-lines.outputs.has_inputs == 'true' }} | ||
| run: | | ||
| tr '\n' '\0' < .lychee-pr-inputs.txt | | ||
| perl .github/scripts/extract-site-hrefs.pl .lychee-site-hrefs .lychee-site-href-files.txt | ||
|
|
||
| count=$(wc -l < .lychee-site-href-files.txt | tr -d ' ') | ||
| if [ "$count" -gt 0 ]; then | ||
| cat .lychee-site-href-files.txt >> .lychee-pr-inputs.txt | ||
| sed 's/^/- /' .lychee-site-href-files.txt | ||
| fi | ||
|
|
||
| - name: Link Checker | ||
| if: ${{ steps.changed-files.outputs.all_changed_files }} | ||
| uses: lycheeverse/lychee-action@v2.3.0 | ||
| if: ${{ steps.changed-lines.outputs.has_inputs == 'true' }} | ||
| uses: lycheeverse/lychee-action@v2 | ||
| with: | ||
| fail: true | ||
| args: --root-dir $(pwd) -E -i -n -t 45 -- '${{ steps.changed-files.outputs.all_changed_files }}' | ||
| failIfEmpty: false | ||
| args: --root-dir $(pwd) --exclude '^file://' -E -i -n -t 45 --files-from .lychee-pr-inputs.txt | ||
| env: | ||
| GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.