|
| 1 | +name: Verify Redirects |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + |
| 6 | +jobs: |
| 7 | + verify-redirects: |
| 8 | + name: Verify Redirects |
| 9 | + runs-on: ubuntu-latest |
| 10 | + env: |
| 11 | + REDIRECTS_FILE: "pr/config/redirects" |
| 12 | + permissions: |
| 13 | + pull-requests: write |
| 14 | + steps: |
| 15 | + - name: Check Out Base Branch |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: Checkout PR Head Branch |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + ref: ${{ github.event.pull_request.head.ref }} |
| 23 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 24 | + path: pr |
| 25 | + |
| 26 | + - name: Get Changed Files |
| 27 | + id: changed-files |
| 28 | + uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c |
| 29 | + with: |
| 30 | + include_all_old_new_renamed_files: true |
| 31 | + |
| 32 | + - name: Find Missing Redirects for Renamed Files |
| 33 | + id: renamed-files |
| 34 | + if: steps.changed-files.outputs.renamed_files_count > 0 |
| 35 | + env: |
| 36 | + RENAMED_FILES: ${{ steps.changed-files.outputs.all_old_new_renamed_files }} |
| 37 | + run: | |
| 38 | + renamed_redirects="" |
| 39 | +
|
| 40 | + for file in $RENAMED_FILES; do |
| 41 | +
|
| 42 | + # only run for .txt files |
| 43 | + if [[ ! "$file" == *.txt ]]; then |
| 44 | + continue |
| 45 | + fi |
| 46 | +
|
| 47 | + # format old and new URLs |
| 48 | + old=$(echo "$file" | cut -d',' -f1) |
| 49 | + old="${old#source}" |
| 50 | + old="${old%.txt}" |
| 51 | + new=$(echo "$file" | cut -d',' -f2) |
| 52 | + new="${new#source}" |
| 53 | + new="${new%.txt}" |
| 54 | + |
| 55 | + redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}'"$new"'/' |
| 56 | +
|
| 57 | + # if redirect not already in file, add to string to add to PR description |
| 58 | + if ! grep -q "$redirect" $REDIRECTS_FILE; then |
| 59 | + renamed_redirects+="<li>[<v>-*]: $redirect</li>" |
| 60 | + fi |
| 61 | + done |
| 62 | +
|
| 63 | + echo "redirects=${renamed_redirects}" >> "$GITHUB_OUTPUT" |
| 64 | +
|
| 65 | + - name: Find Missing Redirects for Deleted Files |
| 66 | + id: deleted-files |
| 67 | + if: steps.changed-files.outputs.deleted_files_count > 0 |
| 68 | + env: |
| 69 | + DELETED_FILES: ${{ steps.changed-files.outputs.deleted_files }} |
| 70 | + run: | |
| 71 | + deleted_redirects="" |
| 72 | +
|
| 73 | + for file in $DELETED_FILES; do |
| 74 | +
|
| 75 | + # only run for .txt files |
| 76 | + if [[ ! "$file" == *.txt ]]; then |
| 77 | + continue |
| 78 | + fi |
| 79 | + |
| 80 | + # format old URL |
| 81 | + old=$(echo "$file" | cut -d',' -f1) |
| 82 | + old="${old#source}" |
| 83 | + old="${old%.txt}" |
| 84 | + |
| 85 | + redirect='${prefix}/${version}'"$old"'/ -> ${base}/${version}/' |
| 86 | + |
| 87 | + # escape special characters before searching for string |
| 88 | + escaped_redirect=$(printf '%s\n' "$redirect" | sed 's/[[*${}|\\]/\\&/g') |
| 89 | +
|
| 90 | + # if redirect not already in file, add to string to add to PR description |
| 91 | + if ! grep -qE "${escaped_redirect}$" $REDIRECTS_FILE; then |
| 92 | + deleted_redirects+="<li>[<v>-*]: $redirect</li>" |
| 93 | + fi |
| 94 | + done |
| 95 | + |
| 96 | + echo "redirects=${deleted_redirects}" >> "$GITHUB_OUTPUT" |
| 97 | +
|
| 98 | + - name: Build Redirect HTML |
| 99 | + id: build-redirect-html |
| 100 | + env: |
| 101 | + RENAMED_REDIRECTS: ${{ steps.renamed-files.outputs.redirects }} |
| 102 | + DELETED_REDIRECTS: ${{ steps.deleted-files.outputs.redirects }} |
| 103 | + run: | |
| 104 | + redirect_html='' |
| 105 | + combined_redirects="${RENAMED_REDIRECTS}${DELETED_REDIRECTS}" |
| 106 | +
|
| 107 | + if [ -n "$combined_redirects" ]; then |
| 108 | + redirect_html="<h3>Suggested redirects for moved, renamed, and deleted files:</h3><p><em>Replace <v> with the earliest backport target version</p></em>$combined_redirects" |
| 109 | + fi |
| 110 | +
|
| 111 | + echo "redirect_html=${redirect_html}" >> "$GITHUB_OUTPUT" |
| 112 | +
|
| 113 | + - name: Update PR Description |
| 114 | + uses: MongoCaleb/pr-description-action@4bdfe35b98f64532b419ad20b350a92546cd3aa1 |
| 115 | + with: |
| 116 | + regex: "- \\[( |x)\\] Did you add redirects\\?.*" |
| 117 | + appendContentOnMatchOnly: false |
| 118 | + regexFlags: is |
| 119 | + content: "- [ ] Did you add redirects?\n ${{ steps.build-redirect-html.outputs.redirect_html }}" |
| 120 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + |
| 122 | + - name: Check for duplicates in redirects file |
| 123 | + run: | |
| 124 | + if [[ -f "$REDIRECTS_FILE" ]]; then |
| 125 | + duplicates=$(sort "$REDIRECTS_FILE" | uniq -d) |
| 126 | + if [[ -n "$duplicates" ]]; then |
| 127 | + echo "Duplicate lines found in $REDIRECTS_FILE:" |
| 128 | + echo "$duplicates" |
| 129 | + exit 1 # error |
| 130 | + fi |
| 131 | + else |
| 132 | + "Redirects file doesn't exist. Skipping duplicate check." |
| 133 | + fi |
0 commit comments