-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
78 lines (74 loc) · 3.1 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Why was this action created:
#
# This is used to add status checks on Pull Requests.
# I was using the niteoweb/pull_request_status_action@v1 action to create GitHub status.
# However this action adds a step at the start of the workflow where it builds a container:
#
# - This step has some python warnings: "WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour
# with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv"
#
# - At the time of writing this, this step adds around 12 secs to each job where this action is used.
#
# The warning might not be an issue but just to be safe and shave off ~12 secs on the workflows I've created this action that does the same.
#
# As a bonus this action shows how I can create custom actions to interact with the GitHub API.
#
name: 'Create GitHub status'
description: 'Creates a GitHub status'
inputs:
repo:
description: 'The repository for the which the status will be created. Expected format is {owner}/{repo}.'
required: true
pr-num:
description: 'The Pull Request number.'
required: true
context:
description: 'A string label to differentiate this status from the status of other systems. This field is case-insensitive.'
required: true
state:
description: 'The state of the status. Can be one of error, failure, pending, or success.'
required: true
description:
description: 'A short description of the status.'
required: true
target-url:
description: 'The target URL to associate with this status. This URL will be linked from the GitHub UI to allow users to easily see the source of the status.'
required: true
token:
description: 'The token to use when calling GitHub APIs.'
required: true
runs:
using: "composite"
steps:
- name: Get Pull request SHA
shell: pwsh
id: get-pr-sha
run: |
$uri = "https://api.github.com/repos/${{ inputs.repo }}/pulls/${{ inputs.pr-num }}"
$headers = @{
Accept = "application/vnd.github.v3+json"
Authorization = "Bearer ${{ inputs.token }}"
}
$response = Invoke-RestMethod -Method 'Get' -Uri $uri -Headers $headers
$sha = $response.head.sha
Write-Output "pr-sha=$sha" >> $env:GITHUB_OUTPUT
Write-Output "PR head sha is $sha"
- name: Dump outputs from previous step
shell: pwsh
run: |
Write-Output '${{ toJson(steps.get-pr-sha.outputs) }}'
- name: Create GitHub status
shell: pwsh
run: |
$uri = "https://api.github.com/repos/${{ inputs.repo }}/statuses/${{ steps.get-pr-sha.outputs.pr-sha }}"
$headers = @{
Accept = "application/vnd.github.v3+json"
Authorization = "Bearer ${{ inputs.token }}"
}
$body = @{
context = "${{ inputs.context }}"
description = "${{ inputs.description }}"
target_url = "${{ inputs.target-url }}"
state = "${{ inputs.state }}"
} | ConvertTo-Json -compress
Invoke-RestMethod -Method 'Post' -Uri $uri -Headers $headers -Body $body