|
| 1 | +# Copyright (C) 2025 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: Check hyperlinks and relative path validity |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + |
| 9 | +on: |
| 10 | + pull_request: |
| 11 | + branches: [master] |
| 12 | + types: [opened, reopened, ready_for_review, synchronize] |
| 13 | + |
| 14 | +jobs: |
| 15 | + check-the-validity-of-hyperlinks-in-README: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Clean Up Working Directory |
| 19 | + run: sudo rm -rf ${{github.workspace}}/* |
| 20 | + |
| 21 | + - name: Checkout Repo |
| 22 | + uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Check the Validity of Hyperlinks |
| 27 | + env: |
| 28 | + BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 29 | + run: | |
| 30 | + cd ${{github.workspace}} |
| 31 | + delay=1 |
| 32 | + fail="FALSE" |
| 33 | + merged_commit=$(git log -1 --format='%H') |
| 34 | + changed_files="$(git diff --name-status --diff-filter=ARM $BASE_SHA ${merged_commit} | awk '/\.md$/ {print $NF}')" |
| 35 | + if [ -n "$changed_files" ]; then |
| 36 | + for changed_file in $changed_files; do |
| 37 | + # echo $changed_file |
| 38 | + url_lines=$(grep -H -Eo '\]\(http[s]?://[^)]+\)' "$changed_file") || true |
| 39 | + if [ -n "$url_lines" ]; then |
| 40 | + for url_line in $url_lines; do |
| 41 | + # echo $url_line |
| 42 | + url=$(echo "$url_line"|cut -d '(' -f2 | cut -d ')' -f1|sed 's/\.git$//') |
| 43 | + path=$(echo "$url_line"|cut -d':' -f1 | cut -d'/' -f2-) |
| 44 | + if [[ "$url" == "https://dgpu-docs.intel.com/installation-guides/ubuntu/ubuntu-focal-dc.html" || "$url" == "https://ai.cloud.intel.com/" ]]; then |
| 45 | + echo "Link "$url" from ${{github.workspace}}/$path needs to be verified by real person." |
| 46 | + else |
| 47 | + sleep $delay |
| 48 | + response=$(curl -L -s -o /dev/null -w "%{http_code}" -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" "$url")|| true |
| 49 | + if [ "$response" -ne 200 ]; then |
| 50 | + echo "**********Validation $url failed ($response), try again**********" |
| 51 | + response_retry=$(curl -s -o /dev/null -w "%{http_code}" "$url") || true |
| 52 | + if [ "$response_retry" -eq 200 ]; then |
| 53 | + echo "*****Retry successfully*****" |
| 54 | + else |
| 55 | + echo "******Retry $url failed ($response_retry), add simulated browser requests******" |
| 56 | + response_browser=$(curl -s -o /dev/null -w "%{http_code}" -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" "$url")|| true |
| 57 | + if [ "$response_browser" -eq 200 ]; then |
| 58 | + echo "*****Retry successfully*****" |
| 59 | + else |
| 60 | + echo -e "::error:: Invalid link ($response_retry) from ${{github.workspace}}/$(echo "$url_line"|cut -d':' -f1): $url" |
| 61 | + fail="TRUE" |
| 62 | + fi |
| 63 | + fi |
| 64 | + fi |
| 65 | + fi |
| 66 | + done |
| 67 | + fi |
| 68 | + done |
| 69 | + else |
| 70 | + echo "No changed .md file." |
| 71 | + fi |
| 72 | +
|
| 73 | + if [[ "$fail" == "TRUE" ]]; then |
| 74 | + exit 1 |
| 75 | + else |
| 76 | + echo "All hyperlinks are valid." |
| 77 | + fi |
| 78 | + shell: bash |
| 79 | + |
| 80 | + check-the-validity-of-relative-path: |
| 81 | + runs-on: ubuntu-latest |
| 82 | + steps: |
| 83 | + - name: Clean up Working Directory |
| 84 | + run: sudo rm -rf ${{github.workspace}}/* |
| 85 | + |
| 86 | + - name: Checkout Repo |
| 87 | + uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2 |
| 88 | + with: |
| 89 | + fetch-depth: 0 |
| 90 | + |
| 91 | + - name: Checking Relative Path Validity |
| 92 | + env: |
| 93 | + REPO_NAME: ${{ github.event.pull_request.head.repo.full_name }} |
| 94 | + HEAD_REF: ${{ github.event.pull_request.head.ref }} |
| 95 | + BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 96 | + run: | |
| 97 | + cd ${{github.workspace}} |
| 98 | + delay=1 |
| 99 | + fail="FALSE" |
| 100 | + branch="https://github.com/$REPO_NAME/blob/$HEAD_REF" |
| 101 | +
|
| 102 | + merged_commit=$(git log -1 --format='%H') |
| 103 | + changed_files="$(git diff --name-status --diff-filter=ARM $BASE_SHA ${merged_commit} | awk '/\.md$/ {print $NF}')" |
| 104 | + png_lines=$(grep -Eo '\]\([^)]+\)' --include='*.md' -r .|grep -Ev 'http' | grep -Ev 'shape=' | grep -Ev 'mailto:inc.maintainers@intel.com') |
| 105 | + if [ -n "$png_lines" ]; then |
| 106 | + for png_line in $png_lines; do |
| 107 | + # echo "No.1----->png_line is $png_line" |
| 108 | + refer_path=$(echo "$png_line"|cut -d':' -f1 | cut -d'/' -f2-) |
| 109 | + png_path=$(echo "$png_line"|cut -d '(' -f2 | cut -d ')' -f1) |
| 110 | + # echo "No.2----->refer_path is $refer_path, png_path is $png_path" |
| 111 | +
|
| 112 | + if [[ "${png_path:0:1}" == "/" ]]; then |
| 113 | + # absolute path |
| 114 | + check_path=$(echo "${png_path:1}" | cut -d '#' -f1) |
| 115 | + # echo "No.3----->check_path is $check_path" |
| 116 | + else |
| 117 | + # relative path |
| 118 | + check_path=${refer_path} |
| 119 | + relative_path=$(echo "$png_path" | cut -d '#' -f1) |
| 120 | + if [ -n "$relative_path" ]; then check_path=$(dirname "$refer_path")/$relative_path; fi |
| 121 | + # echo "No.4----->check_path is $check_path" |
| 122 | + fi |
| 123 | +
|
| 124 | + if [ -e "$check_path" ]; then |
| 125 | + real_path=$(realpath $check_path) |
| 126 | + # echo "No.5----->real_path is $real_path" |
| 127 | + if [[ "$png_path" == *#* ]]; then |
| 128 | + if [ -n "$changed_files" ] && echo "$changed_files" | grep -q "^${refer_path}$"; then |
| 129 | + url_dev=$branch$(echo "$real_path" | sed 's|.*/neural-compressor||')#$(echo "$png_path" | cut -d '#' -f2) |
| 130 | + # echo "No.6----->url_dev is $url_dev" |
| 131 | + sleep $delay |
| 132 | + response=$(curl -I -L -s -o /dev/null -w "%{http_code}" "$url_dev") |
| 133 | + if [ "$response" -ne 200 ]; then |
| 134 | + echo "**********Validation failed ($response), try again**********" |
| 135 | + response_retry=$(curl -s -o /dev/null -w "%{http_code}" "$url_dev") |
| 136 | + if [ "$response_retry" -eq 200 ]; then |
| 137 | + echo "*****Retry successfully*****" |
| 138 | + else |
| 139 | + echo -e "::error:: Invalid path ($response_retry) from ${{github.workspace}}/$refer_path: $png_path" |
| 140 | + fail="TRUE" |
| 141 | + fi |
| 142 | + else |
| 143 | + echo "Validation succeed $png_line" |
| 144 | + fi |
| 145 | + fi |
| 146 | + fi |
| 147 | + else |
| 148 | + echo -e "::error:: ${{github.workspace}}/$refer_path:$png_path does not exist." |
| 149 | + fail="TRUE" |
| 150 | + fi |
| 151 | + done |
| 152 | + fi |
| 153 | +
|
| 154 | + if [[ "$fail" == "TRUE" ]]; then |
| 155 | + exit 1 |
| 156 | + else |
| 157 | + echo "All relative path are valid." |
| 158 | + fi |
| 159 | + shell: bash |
0 commit comments