|
| 1 | +name: Compare generated user guides |
| 2 | + |
| 3 | +permissions: |
| 4 | + pull-requests: write |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request_target: |
| 8 | + paths: |
| 9 | + - "docs/user_guide/templates/**" # Only run the workflow if files in this folder are changed |
| 10 | + - "docs-examples/inject_file/src" # Only run the workflow if files in this folder are changed |
| 11 | + |
| 12 | +jobs: |
| 13 | + compare-files: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout PR branch |
| 18 | + uses: actions/checkout@v3 |
| 19 | + with: |
| 20 | + repository: ${{ github.event.pull_request.head.repo.full_name }} # PR repository |
| 21 | + ref: ${{ github.head_ref }} # PR branch |
| 22 | + |
| 23 | + - name: Generate files for PR branch |
| 24 | + run: | |
| 25 | + ./generate_user_guides.sh |
| 26 | +
|
| 27 | + - name: Move PR generated files |
| 28 | + run: mkdir -p /tmp/pr_generated && mv docs/user_guide/* /tmp/pr_generated/ && mv /tmp/pr_generated/templates docs/user_guide/ |
| 29 | + |
| 30 | + - name: Checkout target branch |
| 31 | + uses: actions/checkout@v3 |
| 32 | + with: |
| 33 | + repository: ${{ github.event.pull_request.base.repo.full_name }} # Upstream repository |
| 34 | + ref: ${{ github.base_ref }} # Target branch |
| 35 | + |
| 36 | + - name: Generate files for target branch |
| 37 | + run: | |
| 38 | + ./generate_user_guides.sh || true |
| 39 | +
|
| 40 | + - name: Move target generated files |
| 41 | + run: mkdir -p /tmp/target_generated && mv docs/user_guide/* /tmp/target_generated/ && mv /tmp/target_generated/templates docs/user_guide/ || true |
| 42 | + |
| 43 | + # Step 6: Compare files |
| 44 | + - name: Compare generated files |
| 45 | + id: compare |
| 46 | + run: | |
| 47 | + diff -r /tmp/target_generated/ /tmp/pr_generated/ > comparison_result.diff || true |
| 48 | +
|
| 49 | + # Step 7: Post comparison results as a comment |
| 50 | + - name: Post PR comment with comparison results |
| 51 | + uses: actions/github-script@v6 |
| 52 | + with: |
| 53 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + script: | |
| 55 | + const fs = require('fs'); |
| 56 | + const comparisonResult = fs.readFileSync('comparison_result.diff', 'utf8'); |
| 57 | +
|
| 58 | + // List all comments |
| 59 | + const comments = await github.rest.issues.listComments({ |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + issue_number: context.issue.number, |
| 63 | + }); |
| 64 | +
|
| 65 | + // Delete previous bot comments |
| 66 | + for (const comment of comments.data) { |
| 67 | + if (comment.user.login === 'github-actions[bot]' && comment.body.startsWith('### Comparison Result')) { |
| 68 | + await github.rest.issues.deleteComment({ |
| 69 | + owner: context.repo.owner, |
| 70 | + repo: context.repo.repo, |
| 71 | + comment_id: comment.id, |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + // Construct the workflow run link |
| 77 | + const runLink = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`; |
| 78 | +
|
| 79 | + // Create the comment body |
| 80 | + const commentBody = |
| 81 | + `### Comparison Result\n\n` + |
| 82 | + `[View workflow run details](${runLink})` + |
| 83 | + `<details><summary>Userguide diff introduced by this PR</summary>\n` + |
| 84 | + `<p>\n\n` + |
| 85 | + `\`\`\`diff\n` + |
| 86 | + `${comparisonResult}\n` + |
| 87 | + `\`\`\`\n\n` ; |
| 88 | + `</p>\n` + |
| 89 | + `</details>`; |
| 90 | +
|
| 91 | + // Post new comment |
| 92 | + await github.rest.issues.createComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + issue_number: context.issue.number, |
| 96 | + body: commentBody, |
| 97 | + }); |
0 commit comments