|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: Check Paths and Hyperlinks |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + types: [opened, reopened, ready_for_review, synchronize] |
| 10 | + |
| 11 | +jobs: |
| 12 | + check-the-validity-of-hyperlinks-in-README: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Clean Up Working Directory |
| 16 | + run: sudo rm -rf ${{github.workspace}}/* |
| 17 | + |
| 18 | + - name: Checkout Repo GenAIInfra |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Check the Validity of Hyperlinks |
| 24 | + run: | |
| 25 | + cd ${{github.workspace}} |
| 26 | + fail="FALSE" |
| 27 | + merged_commit=$(git log -1 --format='%H') |
| 28 | + changed_files="$(git diff --name-status --diff-filter=ARM ${{ github.event.pull_request.base.sha }} ${merged_commit} | awk '/\.md$/ {print $NF}')" |
| 29 | + if [ -n "$changed_files" ]; then |
| 30 | + for changed_file in $changed_files; do |
| 31 | + url_lines=$(grep -H -Eo '\]\(http[s]?://[^)]+\)' "$changed_file" | grep -Ev 'GenAIEval/blob/main') || true |
| 32 | + if [ -n "$url_lines" ]; then |
| 33 | + for url_line in $url_lines; do |
| 34 | + url=$(echo "$url_line"|cut -d '(' -f2 | cut -d ')' -f1|sed 's/\.git$//') |
| 35 | + path=$(echo "$url_line"|cut -d':' -f1 | cut -d'/' -f2-) |
| 36 | + response=$(curl -L -s -o /dev/null -w "%{http_code}" "$url") |
| 37 | + if [ "$response" -ne 200 ]; then |
| 38 | + echo "**********Validation failed, try again**********" |
| 39 | + response_retry=$(curl -s -o /dev/null -w "%{http_code}" "$url") |
| 40 | + if [ "$response_retry" -eq 200 ]; then |
| 41 | + echo "*****Retry successful*****" |
| 42 | + else |
| 43 | + echo "Invalid link from ${{github.workspace}}/$path: $url" |
| 44 | + fail="TRUE" |
| 45 | + fi |
| 46 | + fi |
| 47 | + done |
| 48 | + fi |
| 49 | + done |
| 50 | + else |
| 51 | + echo "No changed .md file." |
| 52 | + fi |
| 53 | +
|
| 54 | + if [[ "$fail" == "TRUE" ]]; then |
| 55 | + exit 1 |
| 56 | + else |
| 57 | + echo "All hyperlinks are valid." |
| 58 | + fi |
| 59 | + shell: bash |
| 60 | + |
| 61 | + check-the-validity-of-relative-path: |
| 62 | + runs-on: ubuntu-latest |
| 63 | + steps: |
| 64 | + - name: Clean up Working Directory |
| 65 | + run: sudo rm -rf ${{github.workspace}}/* |
| 66 | + |
| 67 | + - name: Checkout Repo GenAIInfra |
| 68 | + uses: actions/checkout@v4 |
| 69 | + with: |
| 70 | + fetch-depth: 0 |
| 71 | + |
| 72 | + - name: Checking Relative Path Validity |
| 73 | + run: | |
| 74 | + cd ${{github.workspace}} |
| 75 | + fail="FALSE" |
| 76 | + repo_name=${{ github.event.pull_request.head.repo.full_name }} |
| 77 | + if [ "$(echo "$repo_name"|cut -d'/' -f1)" != "opea-project" ]; then |
| 78 | + owner=$(echo "${{ github.event.pull_request.head.repo.full_name }}" |cut -d'/' -f1) |
| 79 | + branch="https://github.com/$owner/GenAIInfra/tree/${{ github.event.pull_request.head.ref }}" |
| 80 | + else |
| 81 | + branch="https://github.com/opea-project/GenAIInfra/blob/${{ github.event.pull_request.head.ref }}" |
| 82 | + fi |
| 83 | + link_head="https://github.com/opea-project/GenAIInfra/blob/main" |
| 84 | +
|
| 85 | + merged_commit=$(git log -1 --format='%H') |
| 86 | + changed_files="$(git diff --name-status --diff-filter=ARM ${{ github.event.pull_request.base.sha }} ${merged_commit} | awk '/\.md$/ {print $NF}')" |
| 87 | + png_lines=$(grep -Eo '\]\([^)]+\)' --include='*.md' -r .|grep -Ev 'http') |
| 88 | + if [ -n "$png_lines" ]; then |
| 89 | + for png_line in $png_lines; do |
| 90 | + refer_path=$(echo "$png_line"|cut -d':' -f1 | cut -d'/' -f2-) |
| 91 | + png_path=$(echo "$png_line"|cut -d '(' -f2 | cut -d ')' -f1) |
| 92 | + if [[ "${png_path:0:1}" == "/" ]]; then |
| 93 | + check_path=$png_path |
| 94 | + elif [[ "$png_path" == *#* ]]; then |
| 95 | + relative_path=$(echo "$png_path" | cut -d '#' -f1) |
| 96 | + if [ -n "$relative_path" ]; then |
| 97 | + check_path=$(dirname "$refer_path")/$relative_path |
| 98 | + png_path=$(echo "$png_path" | awk -F'#' '{print "#" $2}') |
| 99 | + else |
| 100 | + check_path=$refer_path |
| 101 | + fi |
| 102 | + else |
| 103 | + check_path=$(dirname "$refer_path")/$png_path |
| 104 | + fi |
| 105 | +
|
| 106 | + if [ -e "$check_path" ]; then |
| 107 | + real_path=$(realpath $check_path) |
| 108 | + if [[ "$png_line" == *#* ]]; then |
| 109 | + if [ -n "changed_files" ] && echo "$changed_files" | grep -q "^${refer_path}$"; then |
| 110 | + url_dev=$branch$(echo "$real_path" | sed 's|.*/GenAIInfra||')$png_path |
| 111 | + response=$(curl -I -L -s -o /dev/null -w "%{http_code}" "$url_dev") |
| 112 | + if [ "$response" -ne 200 ]; then |
| 113 | + echo "**********Validation failed, try again**********" |
| 114 | + response_retry=$(curl -s -o /dev/null -w "%{http_code}" "$url_dev") |
| 115 | + if [ "$response_retry" -eq 200 ]; then |
| 116 | + echo "*****Retry successfully*****" |
| 117 | + else |
| 118 | + echo "Invalid path from ${{github.workspace}}/$refer_path: $png_path, link: $url_dev" |
| 119 | + fail="TRUE" |
| 120 | + fi |
| 121 | + else |
| 122 | + echo "Validation succeed $png_line" |
| 123 | + fi |
| 124 | + fi |
| 125 | + fi |
| 126 | + else |
| 127 | + echo "$check_path does not exist" |
| 128 | + fail="TRUE" |
| 129 | + fi |
| 130 | + done |
| 131 | + fi |
| 132 | +
|
| 133 | + if [[ "$fail" == "TRUE" ]]; then |
| 134 | + exit 1 |
| 135 | + else |
| 136 | + echo "All hyperlinks are valid." |
| 137 | + fi |
| 138 | + shell: bash |
0 commit comments