|
| 1 | +name: 'Set Status' |
| 2 | +description: 'Set status on a specified head-sha' |
| 3 | +inputs: |
| 4 | + context: |
| 5 | + description: 'Name of the status' |
| 6 | + required: true |
| 7 | + description: |
| 8 | + description: 'Short description of the status' |
| 9 | + required: false |
| 10 | + state: |
| 11 | + description: >- |
| 12 | + State of the status with possible values of 'error', 'failure', 'pending', 'success' |
| 13 | + required: true |
| 14 | + sha: |
| 15 | + description: 'The commit ID to add the status to' |
| 16 | + required: true |
| 17 | + target-url: |
| 18 | + description: >- |
| 19 | + Target URL to associate with this status. |
| 20 | + This URL will be linked from the GitHub UI to allow users to easily see the source of the status.' |
| 21 | + required: false |
| 22 | +runs: |
| 23 | + using: 'composite' |
| 24 | + steps: |
| 25 | + - name: Set a commit status |
| 26 | + id: set-status |
| 27 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 https://github.com/actions/github-script/commit/60a0d83039c74a4aee543508d2ffcb1c3799cdea |
| 28 | + env: |
| 29 | + status_context: ${{ inputs.context }} |
| 30 | + description: ${{ inputs.description }} |
| 31 | + state: ${{ inputs.state }} |
| 32 | + sha: ${{ inputs.sha }} |
| 33 | + target_url: ${{ inputs.target-url }} |
| 34 | + owner: ${{ github.event.repository.owner.login }} |
| 35 | + repo: ${{ github.event.repository.name }} |
| 36 | + with: |
| 37 | + result-encoding: string |
| 38 | + script: | |
| 39 | + const { status_context, description, state, sha, target_url, owner, repo } = process.env; |
| 40 | + const inputValidationErrors = []; |
| 41 | + if (!state || state.length === 0) { |
| 42 | + inputValidationErrors.push('"state" input cannot be empty.'); |
| 43 | + } |
| 44 | + if (!["error", "failure", "pending", "success"].includes(state)) { |
| 45 | + inputValidationErrors.push('"state" must be a string input with possible value of "error", "failure", "pending", "success".'); |
| 46 | + } |
| 47 | + if (!status_context || status_context.length === 0) { |
| 48 | + inputValidationErrors.push('"context" input cannot be empty.'); |
| 49 | + } |
| 50 | + if (!sha || sha.length === 0) { |
| 51 | + inputValidationErrors.push('"sha" input cannot be empty.'); |
| 52 | + } |
| 53 | + if (!sha.match(/^[0-9a-z]+$/)) { |
| 54 | + inputValidationErrors.push('"sha" must be an alphanumeric string.'); |
| 55 | + } |
| 56 | + if (target_url && target_url.length > 0) { |
| 57 | + if (target_url.length > 2048) { |
| 58 | + inputValidationErrors.push('"target-url" must be less than 2048 characters.'); |
| 59 | + } |
| 60 | +
|
| 61 | + try { |
| 62 | + const url = new URL(target_url); |
| 63 | +
|
| 64 | + if (url.protocol !== 'https:') { |
| 65 | + inputValidationErrors.push('"target-url" must use HTTPS protocol.'); |
| 66 | + } |
| 67 | +
|
| 68 | + const allowedHostnames = ['github.com', 'api.github.com']; |
| 69 | + if (!allowedHostnames.includes(url.hostname)) { |
| 70 | + inputValidationErrors.push(`"target-url" must be one of: ${allowedHostnames.join(', ')}.`); |
| 71 | + } |
| 72 | +
|
| 73 | + } catch (error) { |
| 74 | + if (error instanceof TypeError && error.message.includes('Invalid URL')) { |
| 75 | + inputValidationErrors.push('"target-url" must be a valid URL format.'); |
| 76 | + } else { |
| 77 | + inputValidationErrors.push(`"target-url" validation failed: ${error.message}`); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + if (inputValidationErrors.length > 0) { |
| 82 | + inputValidationErrors.forEach(core.error); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + github.rest.repos.createCommitStatus({ |
| 86 | + owner, |
| 87 | + repo, |
| 88 | + sha, |
| 89 | + state, |
| 90 | + context: status_context, |
| 91 | + description: description && description.length > 0 ? description : undefined, |
| 92 | + target_url: target_url && target_url.length > 0 ? target_url : undefined, |
| 93 | + }); |
0 commit comments