|
| 1 | +name: PR Comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Pull Request"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: |
| 9 | + issues: write |
| 10 | + actions: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + comment: |
| 14 | + if: > |
| 15 | + ${{ github.event.workflow_run.event == 'pull_request' && |
| 16 | + github.event.workflow_run.conclusion == 'success' && |
| 17 | + (github.event.workflow_run.pull_requests && github.event.workflow_run.pull_requests[0]) }} |
| 18 | + runs-on: ubuntu-24.04 |
| 19 | + steps: |
| 20 | + - name: Comment PR |
| 21 | + uses: actions/github-script@v7 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const run = context.payload.workflow_run |
| 25 | + const pr = (run.pull_requests && run.pull_requests[0]) || null |
| 26 | + if (!pr) { |
| 27 | + core.info('No associated PR found; skipping comment.') |
| 28 | + return |
| 29 | + } |
| 30 | +
|
| 31 | + const runId = run.id |
| 32 | + const artifactsUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/` |
| 33 | + const prNumber = pr.number |
| 34 | + const author = pr.user?.login || run.actor?.login || 'unknown' |
| 35 | + const forkRepo = (pr.head && pr.head.repo && pr.head.repo.full_name) ? pr.head.repo.full_name : `${author}:unknown-repo` |
| 36 | + const diffUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/pull/${prNumber}/files` |
| 37 | +
|
| 38 | + const comment = ` |
| 39 | + ## 🚀 Build artifacts are ready for testing! |
| 40 | +
|
| 41 | + > Security notice: You are viewing pre-release CI artifacts from PR #${prNumber} by @${author} (source: ${forkRepo}). These commands may execute code on your machine. Do NOT run them unless you have reviewed the [PR diff](${diffUrl}) and trust the source. The snippets include a confirmation prompt. |
| 42 | +
|
| 43 | + Download the wheel file and binaries with gh CLI or from the [workflow artifacts](${artifactsUrl}). |
| 44 | +
|
| 45 | + ### 📦 Install & Run |
| 46 | +
|
| 47 | + #### Pre-requisites |
| 48 | + \`\`\`bash |
| 49 | + # Install uv if needed |
| 50 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 51 | +
|
| 52 | + # Create and enter artifacts directory |
| 53 | + mkdir artifacts && cd artifacts |
| 54 | + \`\`\` |
| 55 | +
|
| 56 | + #### Quick Test with Python Package |
| 57 | + \`\`\`bash |
| 58 | + bash -c 'set -euo pipefail; printf "\n%s\n\n" "WARNING: You are about to download and execute CI artifacts from PR #${prNumber} by @${author} (source: ${forkRepo}). Do NOT proceed unless you have reviewed the PR diff and trust the source."; printf "%s" "Type I understand to continue: "; read -r C; [ "$C" = "I understand" ] || { echo Aborted.; exit 1; }; gh run download ${runId} -n dist -R ${context.repo.owner}/${context.repo.repo}; uvx ./dist/safety-*-py3-none-any.whl --version' |
| 59 | + \`\`\` |
| 60 | +
|
| 61 | + #### Run other Safety commands as follows |
| 62 | + \`\`\`bash |
| 63 | + uvx ./dist/safety-*-py3-none-any.whl auth status |
| 64 | + uvx ./dist/safety-*-py3-none-any.whl auth login |
| 65 | + uvx ./dist/safety-*-py3-none-any.whl scan |
| 66 | + \`\`\` |
| 67 | +
|
| 68 | + > Note: You need to be logged in to GitHub to access the artifacts. |
| 69 | + ` |
| 70 | +
|
| 71 | + const { data: comments } = await github.rest.issues.listComments({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + issue_number: pr.number, |
| 75 | + }) |
| 76 | +
|
| 77 | + const botComment = comments.find(c => |
| 78 | + c.user?.type === 'Bot' && |
| 79 | + c.body?.includes('Build artifacts are ready for testing!') |
| 80 | + ) |
| 81 | +
|
| 82 | + if (botComment) { |
| 83 | + await github.rest.issues.updateComment({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + comment_id: botComment.id, |
| 87 | + body: comment, |
| 88 | + }) |
| 89 | + } else { |
| 90 | + await github.rest.issues.createComment({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + issue_number: pr.number, |
| 94 | + body: comment, |
| 95 | + }) |
| 96 | + } |
0 commit comments