|
7 | 7 | runs-on: ubuntu-24.04 |
8 | 8 | timeout-minutes: 5 |
9 | 9 | # Only run for PRs with 'website' in the branch name |
10 | | - if: ${{ contains(github.head_ref, 'website') && contains(github.head_ref, '-') }} |
| 10 | + if: ${{ contains(github.head_ref, 'website') }} |
11 | 11 | steps: |
12 | | - - name: Echo approval |
| 12 | + # Validate branch name |
| 13 | + - name: Validate branch name and set output |
| 14 | + id: validate |
13 | 15 | run: | |
14 | | - echo "Workflow has been allowed to run for PR ${{ github.event.number }}. Setting artifacts and then continuing workflow runs" |
15 | | -
|
16 | | - # Use GitHub Action to safely validate and store PR information |
| 16 | + BRANCH="${{ github.head_ref }}" |
| 17 | + if [[ ! "$BRANCH" =~ ^[a-zA-Z0-9_-]+$ ]]; then |
| 18 | + echo "valid=false" >> $GITHUB_OUTPUT |
| 19 | + else |
| 20 | + echo "valid=true" >> $GITHUB_OUTPUT |
| 21 | + fi |
| 22 | +
|
| 23 | + # Save PR information (only if branch is valid) |
17 | 24 | - name: Validate and save PR information |
| 25 | + if: steps.validate.outputs.valid == 'true' |
18 | 26 | uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
19 | 27 | with: |
20 | 28 | script: | |
21 | 29 | const fs = require('fs').promises; |
22 | 30 | const path = require('path'); |
23 | 31 | const crypto = require('crypto'); |
| 32 | + const prNumber = context.payload.number; |
| 33 | + const branchName = context.payload.pull_request.head.ref; |
24 | 34 |
|
25 | | - async function createAndValidateArtifact() { |
26 | | - try { |
27 | | - // Create directory for artifact |
28 | | - await fs.mkdir('./pr', { recursive: true }); |
29 | | -
|
30 | | - // Get PR number and validate |
31 | | - const prNumber = context.payload.number; |
32 | | - if (typeof prNumber !== 'number' || !Number.isInteger(prNumber) || prNumber <= 0) { |
33 | | - core.setFailed(`Invalid PR number: ${prNumber}`); |
34 | | - return; |
35 | | - } |
36 | | -
|
37 | | - // Get branch name and validate |
38 | | - const branchName = context.payload.pull_request.head.ref; |
39 | | - // Validate branch name (only allow alphanumeric, dash, and underscore) |
40 | | - const branchNameRegex = /^[a-zA-Z0-9_\-]+$/; |
41 | | - if (!branchNameRegex.test(branchName)) { |
42 | | - core.setFailed(`Invalid branch name detected: ${branchName}`); |
43 | | - return; |
44 | | - } |
45 | | -
|
46 | | - // Write validated information to files |
47 | | - await fs.writeFile('./pr/number', prNumber.toString()); |
48 | | - await fs.writeFile('./pr/branch', branchName); |
49 | | -
|
50 | | - // Log success |
51 | | - core.info(`Successfully validated and saved PR #${prNumber} with branch ${branchName}`); |
| 35 | + await fs.mkdir('./pr', { recursive: true }); |
| 36 | + await fs.writeFile('./pr/number', prNumber.toString()); |
| 37 | + await fs.writeFile('./pr/branch', branchName); |
52 | 38 |
|
53 | | - // Create hash signature of the data |
54 | | - const numberHash = crypto.createHash('sha256').update(prNumber.toString()).digest('hex'); |
55 | | - const branchHash = crypto.createHash('sha256').update(branchName).digest('hex'); |
56 | | - await fs.writeFile('./pr/integrity', `${numberHash}:${branchHash}`); |
57 | | - } catch (error) { |
58 | | - core.setFailed(`An error occurred: ${error.message}`); |
59 | | - } |
60 | | - } |
| 39 | + const numberHash = crypto.createHash('sha256').update(prNumber.toString()).digest('hex'); |
| 40 | + const branchHash = crypto.createHash('sha256').update(branchName).digest('hex'); |
| 41 | + await fs.writeFile('./pr/integrity', `${numberHash}:${branchHash}`); |
61 | 42 |
|
62 | | - createAndValidateArtifact(); |
| 43 | + core.info(`Saved PR #${prNumber} and branch ${branchName}`); |
63 | 44 |
|
64 | | - # Upload the artifact using latest version |
| 45 | + # Upload the artifact using latest version (only if branch is valid) |
65 | 46 | - name: Upload PR information artifact |
| 47 | + if: steps.validate.outputs.valid == 'true' |
66 | 48 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
67 | 49 | with: |
68 | 50 | name: pr |
|
0 commit comments