|
1 | 1 | name: Add Netlify Links To Changed Pages
|
2 | 2 | on:
|
3 |
| - workflow_call: |
| 3 | + workflow_call: |
| 4 | + pull_request_target: |
4 | 5 | jobs:
|
5 |
| - get-pr-changes: |
6 |
| - name: Get Changed Files & Update PR Description |
7 |
| - runs-on: ubuntu-latest |
8 |
| - permissions: |
9 |
| - issues: write |
10 |
| - contents: write |
11 |
| - pull-requests: write |
12 |
| - repository-projects: write |
13 |
| - steps: |
14 |
| - - uses: actions/checkout@v4 |
15 |
| - - name: Get Changed Files |
16 |
| - id: changed-files |
17 |
| - uses: tj-actions/changed-files@v44 |
18 |
| - with: |
19 |
| - separator: "," |
20 |
| - files: source/** |
21 |
| - - name: Build Netlify Links for Changed Pages |
22 |
| - id: build_page_links |
23 |
| - run: | |
24 |
| - new_links="" |
25 |
| - base_link='https://deploy-preview-${{ github.event.number }}--mongodb-docs-csharp.netlify.app' |
26 |
| - changed_files=${{ steps.changed-files.outputs.all_changed_files }} |
27 |
| - files=$(echo $changed_files | tr "," "\n") |
28 |
| - for file in $files; do |
29 |
| - echo "processing ${file}" |
30 |
| - if (! grep -s "includes/" <<< $file) && |
31 |
| - (! grep -s "images/" <<< $file) && |
32 |
| - (! grep -s "examples/" <<< $file); then |
33 |
| - file="${file#source}" |
34 |
| - file="${file%.txt}" |
35 |
| - filenoslash="${file:1}" |
36 |
| - echo "${base_link}${file}" |
37 |
| - new_links+="<li><a href=${base_link}${file}>${filenoslash}</a></li>" |
38 |
| - else |
39 |
| - echo "(file skipped)" |
40 |
| - fi |
41 |
| - done |
42 |
| - if [ "$new_links" == "" ]; then |
43 |
| - new_links="No pages to preview" |
44 |
| - fi |
45 |
| - echo "Final new_links string: " |
46 |
| - echo "${new_links}" |
47 |
| - echo "staging_links=${new_links}" >> "$GITHUB_OUTPUT" |
48 |
| - - name: Update the PR Description |
49 |
| - uses: MongoCaleb/pr-description-action@master |
50 |
| - with: |
51 |
| - regex: "<!-- start insert-links -->.*<!-- end insert-links -->" |
52 |
| - appendContentOnMatchOnly: true |
53 |
| - regexFlags: is |
54 |
| - content: "<!-- start insert-links -->\n${{ steps.build_page_links.outputs.staging_links }}\n<!-- end insert-links -->" |
55 |
| - token: ${{ secrets.GITHUB_TOKEN }} |
| 6 | + get-pr-changes: |
| 7 | + name: Get Changed Files & Update PR Description |
| 8 | + runs-on: ubuntu-latest |
| 9 | + permissions: |
| 10 | + issues: write |
| 11 | + contents: write |
| 12 | + pull-requests: write |
| 13 | + repository-projects: write |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + - name: Get Changed Files |
| 17 | + id: changed-files |
| 18 | + # pin to a specific commit to ensure stability |
| 19 | + uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c |
| 20 | + with: |
| 21 | + separator: "," |
| 22 | + files: source/** |
| 23 | + - name: Build Netlify Links for Changed Pages |
| 24 | + id: build_page_links |
| 25 | + env: |
| 26 | + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
| 27 | + run: | |
| 28 | + # Function to validate file paths |
| 29 | + validate_file_path() { |
| 30 | + local file_path="$1" |
| 31 | + # Allow only alphanumeric characters, _ . / and - |
| 32 | + if [[ ! "$file_path" =~ ^[a-zA-Z0-9._/-]+$ ]]; then |
| 33 | + echo "Invalid file path detected: $file_path" >&2 |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + } |
| 37 | +
|
| 38 | + new_links="" |
| 39 | + base_link='https://deploy-preview-${{ github.event.number }}--mongodb-docs-csharp.netlify.app' |
| 40 | + files=$(echo "$CHANGED_FILES" | tr "," "\n") |
| 41 | + for file in $files; do |
| 42 | + echo "processing ${file}" |
| 43 | + |
| 44 | + # Validate file path and skip if invalid |
| 45 | + validate_file_path "$file" |
| 46 | + if [ $? -ne 0 ]; then |
| 47 | + continue |
| 48 | + fi |
| 49 | + |
| 50 | + if (! grep -s "includes/" <<< "$file") && |
| 51 | + (! grep -s "images/" <<< "$file") && |
| 52 | + (! grep -s "examples/" <<< "$file"); then |
| 53 | + file="${file#source}" |
| 54 | + file="${file%.txt}" |
| 55 | + filenoslash="${file:1}" |
| 56 | + echo "${base_link}${file}" |
| 57 | + new_links+="<li><a href=${base_link}${file}>${filenoslash}</a></li>" |
| 58 | + else |
| 59 | + echo "(file skipped)" |
| 60 | + fi |
| 61 | + done |
| 62 | + if [ "$new_links" == "" ]; then |
| 63 | + new_links="No pages to preview" |
| 64 | + fi |
| 65 | + echo "Final new_links string: " |
| 66 | + echo "${new_links}" |
| 67 | + echo "staging_links=${new_links}" >> "$GITHUB_OUTPUT" |
| 68 | + - name: Update the PR Description |
| 69 | + uses: MongoCaleb/pr-description-action@master |
| 70 | + with: |
| 71 | + regex: "<!-- start insert-links -->.*<!-- end insert-links -->" |
| 72 | + appendContentOnMatchOnly: true |
| 73 | + regexFlags: is |
| 74 | + content: "<!-- start insert-links -->\n${{ steps.build_page_links.outputs.staging_links }}\n<!-- end insert-links -->" |
| 75 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments