Skip to content

Commit bb4eee2

Browse files
authored
feat: support set status commit on push event (#307)
* feat: support push events * no-release: added workflow to test new feature * fix: pushed release version and fixed lint * fix: updated dependencies * fix: removed actions added to test the action * fix: use same name to set status
1 parent 397baa7 commit bb4eee2

File tree

10 files changed

+6955
-7386
lines changed

10 files changed

+6955
-7386
lines changed

.github/workflows/push.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ jobs:
1212
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1313
steps:
1414
- uses: actions/checkout@v2
15+
- uses: ./
16+
with:
17+
name: "release"
1518
- run: npm ci
1619
- run: npm run build
1720
- run: npm run format-check
1821
- run: npm run lint
1922
- run: npm run pack
2023
- run: npm test
2124
- run: npm run semantic-release
25+
- if: always()
26+
uses: ./
27+
with:
28+
name: "release"
29+
status: "${{ job.status }}"

README.md

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
## Overview
77

8-
A simple Github Action that allows us to update the status of the last commit in a pull request.
8+
A simple Github Action that allows us to update the status of a commit.
99

1010
GitHub does not update the status of a commit when running workflow and therefore tools that rely on the context/status of a given commit are not compatible with it.
1111

12+
Currently the action supports `pull_request` and `push` events:
13+
* When the event is `pull_request`, the action will set the status to the last commit in the pull request at the moment the workflow was triggered.
14+
* When the event is `push`, the action will set the status to the last commit pushed at the moment the workflow was triggered.
15+
1216
## Input Parameters
1317

1418
* token: Auth token used to add status commits
@@ -22,7 +26,7 @@ GitHub does not update the status of a commit when running workflow and therefor
2226

2327
* status: Commit or job status, based on this the action will set the correct status in the commit: Accepted values are: `error`, `failure`, `pending`, `success` and `cancelled`.
2428

25-
If the passed status is `pending` it wil set status commit `pending`.
29+
If the passed status is `pending` it will set status commit `pending`.
2630

2731
If the passed status is `failure` or `cancelled` it will set status commit `failure`.
2832

@@ -43,34 +47,85 @@ GitHub does not update the status of a commit when running workflow and therefor
4347
* optional
4448
* default: ""
4549

46-
* ignoreForks: If the pull request is from a fork the action won't add a status by default. This is because the action won't have a token with permissions to add the status to the commit. You can disable this, but then you'll have to provide a token with enough permissions to add status to the commits in the forks!
50+
* ignoreForks: If the pull request is from a fork the action won't add a status by default. This is because the action won't have a token with permissions to add the status to the commit. You can disable this, but then you'll have to provide a token with enough permissions to add status to the commits in the forks! __Will be used only for pull requests.__
4751

4852
* optional
4953
* default: "true"
5054

51-
* addHoldComment: If true the action will add a comment to the pull request. This is useful if you use prow, since prow won't detect the github actions, so you can use `/hold` and `/hold cancel` to avoid merging the PR before you want. __Important: this will be disabled for forks if `ignoreForks` is set to true, this is because the default github token won't have permissions to add comments if your PR comes from a fork__
55+
* addHoldComment: If true the action will add a comment to the pull request. This is useful if you use prow, since prow won't detect the github actions, so you can use `/hold` and `/hold cancel` to avoid merging the PR before you want. __Important: this will be disabled for forks if `ignoreForks` is set to true, this is because the default github token won't have permissions to add comments if your PR comes from a fork. Will be used only for pull requests.__
5256

5357
* optional
5458
* default: "false"
5559

56-
* pendingComment: This is the message to add to the pull request when the status is `pending`.
60+
* pendingComment: This is the message to add to the pull request when the status is `pending`. __Will be used only for pull requests.__
5761

5862
* optional
5963
* default: "/hold"
6064

61-
* successComment: This is the message to add to the pull request when the status is `success`.
65+
* successComment: This is the message to add to the pull request when the status is `success`. __Will be used only for pull requests.__
6266

6367
* optional
6468
* default: "/hold cancel"
6569

66-
* failComment: This is the message to add to the pull request when the status is `failure`, `error` or `cancelled`.
70+
* failComment: This is the message to add to the pull request when the status is `failure`, `error` or `cancelled`.__Will be used only for pull requests.__
6771

6872
* optional
6973
* default: "/hold"
7074

7175
## Examples
7276

73-
### Action sets commit to pending status without comment
77+
### Action sets push commit to pending status
78+
79+
```
80+
name: Test
81+
82+
on: [push]
83+
84+
jobs:
85+
build:
86+
runs-on: ubuntu-latest
87+
steps:
88+
- uses: actions/checkout@v2
89+
- uses: ouzi-dev/commit-status-updater@v1.1.0
90+
```
91+
92+
### Action sets push commit to pending status with custom name
93+
94+
```
95+
name: Test
96+
97+
on: [push]
98+
99+
jobs:
100+
build:
101+
runs-on: ubuntu-latest
102+
steps:
103+
- uses: actions/checkout@v2
104+
with:
105+
name: "name of my status check"
106+
- uses: ouzi-dev/commit-status-updater@v1.1.0
107+
```
108+
109+
### Action sets push commit to pending status on start, and updates check at the end of the workflow
110+
111+
```
112+
name: Test
113+
114+
on: [push]
115+
116+
jobs:
117+
build:
118+
runs-on: ubuntu-latest
119+
steps:
120+
- uses: actions/checkout@v2
121+
- uses: ouzi-dev/commit-status-updater@v1.1.0
122+
- if: always()
123+
uses: ouzi-dev/commit-status-updater@v1.1.0
124+
with:
125+
status: "${{ job.status }}"
126+
```
127+
128+
### Action sets pull request commit to pending status without comment
74129

75130
```
76131
name: Test
@@ -82,10 +137,10 @@ jobs:
82137
runs-on: ubuntu-latest
83138
steps:
84139
- uses: actions/checkout@v2
85-
- uses: ouzi-dev/commit-status-updater@v1.0.4
140+
- uses: ouzi-dev/commit-status-updater@v1.1.0
86141
```
87142

88-
### Action sets commit to error status without comment
143+
### Action sets pull request commit to error status without comment
89144

90145
```
91146
name: Test
@@ -97,12 +152,12 @@ jobs:
97152
runs-on: ubuntu-latest
98153
steps:
99154
- uses: actions/checkout@v2
100-
- uses: ouzi-dev/commit-status-updater@v1.0.4
155+
- uses: ouzi-dev/commit-status-updater@v1.1.0
101156
with:
102157
status: "error"
103158
```
104159

105-
### Action sets commit to pending status with comment, and updates check and adds comment at the end of the workflow
160+
### Action sets pull request commit to pending status with comment, and updates check and adds comment at the end of the workflow
106161

107162
```
108163
name: Test
@@ -114,11 +169,11 @@ jobs:
114169
runs-on: ubuntu-latest
115170
steps:
116171
- uses: actions/checkout@v2
117-
- uses: ouzi-dev/commit-status-updater@v1.0.4
172+
- uses: ouzi-dev/commit-status-updater@v1.1.0
118173
with:
119174
addHoldComment: "true"
120175
- if: always()
121-
uses: ouzi-dev/commit-status-updater@v1.0.4
176+
uses: ouzi-dev/commit-status-updater@v1.1.0
122177
with:
123178
addHoldComment: "true"
124179
status: "${{ job.status }}"
@@ -136,7 +191,7 @@ jobs:
136191
runs-on: ubuntu-latest
137192
steps:
138193
- uses: actions/checkout@v2
139-
- uses: ouzi-dev/commit-status-updater@v1.0.4
194+
- uses: ouzi-dev/commit-status-updater@v1.1.0
140195
with:
141196
status: "pending"
142197
addHoldComment: "true"
@@ -157,7 +212,7 @@ jobs:
157212
runs-on: ubuntu-latest
158213
steps:
159214
- uses: actions/checkout@v2
160-
- uses: ouzi-dev/commit-status-updater@v1.0.4
215+
- uses: ouzi-dev/commit-status-updater@v1.1.0
161216
with:
162217
status: "error"
163218
url: http://myurl.io/
@@ -177,7 +232,7 @@ jobs:
177232
runs-on: ubuntu-latest
178233
steps:
179234
- uses: actions/checkout@v2
180-
- uses: ouzi-dev/commit-status-updater@v1.0.4
235+
- uses: ouzi-dev/commit-status-updater@v1.1.0
181236
with:
182237
token: "my_custom_token"
183238
ignoreForks: "false"

__test__/runner.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const mockGithubHelper = jest.genMockFromModule('../lib/githubHelper') as any
1212
mockGithubHelper.getInputs = jest.fn()
1313

1414
const MockIGithubHelper = jest.fn<IGithubHelper, []>(() => ({
15+
isPullRequest: jest.fn(),
1516
isFork: jest.fn(),
1617
setStatus: jest.fn(),
1718
addComment: jest.fn()
@@ -35,7 +36,7 @@ describe('runner tests', () => {
3536
jest.resetModules()
3637
})
3738

38-
it('run sets status and comment', async () => {
39+
it('on push run sets status', async () => {
3940
const params = ({} as unknown) as IParams
4041
params.token = 'bleh'
4142
params.ignoreForks = true
@@ -44,6 +45,25 @@ describe('runner tests', () => {
4445
mockInputsHelper.getInputs.mockReturnValue(params)
4546
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
4647
mockIGithubHelper.isFork.mockReturnValue(false)
48+
mockIGithubHelper.isPullRequest.mockReturnValue(false)
49+
50+
await runner.run()
51+
52+
expect(mockUtils.validateEventType).toHaveBeenCalled()
53+
expect(mockIGithubHelper.setStatus).toHaveBeenCalledWith(params)
54+
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
55+
})
56+
57+
it('on PR run sets status and comment', async () => {
58+
const params = ({} as unknown) as IParams
59+
params.token = 'bleh'
60+
params.ignoreForks = true
61+
params.addHoldComment = true
62+
params.selectedComment = 'my comment'
63+
mockInputsHelper.getInputs.mockReturnValue(params)
64+
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
65+
mockIGithubHelper.isFork.mockReturnValue(false)
66+
mockIGithubHelper.isPullRequest.mockReturnValue(true)
4767

4868
await runner.run()
4969

@@ -52,7 +72,7 @@ describe('runner tests', () => {
5272
expect(mockIGithubHelper.addComment).toHaveBeenCalledWith('my comment')
5373
})
5474

55-
it('run sets status and no comment', async () => {
75+
it('on PR run sets status and no comment', async () => {
5676
const params = ({} as unknown) as IParams
5777
params.token = 'bleh'
5878
params.ignoreForks = true
@@ -61,6 +81,7 @@ describe('runner tests', () => {
6181
mockInputsHelper.getInputs.mockReturnValue(params)
6282
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
6383
mockIGithubHelper.isFork.mockReturnValue(false)
84+
mockIGithubHelper.isPullRequest.mockReturnValue(true)
6485

6586
await runner.run()
6687

@@ -69,7 +90,7 @@ describe('runner tests', () => {
6990
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
7091
})
7192

72-
it('run does not set status or comment', async () => {
93+
it('on PR run does not set status or comment', async () => {
7394
const params = ({} as unknown) as IParams
7495
params.token = 'bleh'
7596
params.ignoreForks = true
@@ -78,6 +99,7 @@ describe('runner tests', () => {
7899
mockInputsHelper.getInputs.mockReturnValue(params)
79100
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
80101
mockIGithubHelper.isFork.mockReturnValue(true)
102+
mockIGithubHelper.isPullRequest.mockReturnValue(true)
81103

82104
await runner.run()
83105

@@ -86,7 +108,7 @@ describe('runner tests', () => {
86108
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
87109
})
88110

89-
it('run does not set status or comment when it is a fork and add comment enabled', async () => {
111+
it('on PR run does not set status or comment when it is a fork and add comment enabled', async () => {
90112
const params = ({} as unknown) as IParams
91113
params.token = 'bleh'
92114
params.ignoreForks = true
@@ -95,6 +117,7 @@ describe('runner tests', () => {
95117
mockInputsHelper.getInputs.mockReturnValue(params)
96118
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
97119
mockIGithubHelper.isFork.mockReturnValue(true)
120+
mockIGithubHelper.isPullRequest.mockReturnValue(true)
98121

99122
await runner.run()
100123

@@ -103,14 +126,15 @@ describe('runner tests', () => {
103126
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
104127
})
105128

106-
it('run sets status if ignore fork false', async () => {
129+
it('on PR run sets status if ignore fork false', async () => {
107130
const params = ({} as unknown) as IParams
108131
params.token = 'bleh'
109132
params.ignoreForks = false
110133
params.addHoldComment = true
111134
params.selectedComment = 'my comment'
112135
mockInputsHelper.getInputs.mockReturnValue(params)
113136
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
137+
mockIGithubHelper.isPullRequest.mockReturnValue(true)
114138

115139
await runner.run()
116140

action.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: 'commit-status-updater'
2-
description: 'A simple Github Action that allows us to update the status of the last commit in a pull request'
3-
author: 'Ouzi, ltd. Ouzi Team <team@ouzi.dev>'
1+
name: "commit-status-updater"
2+
description: "A simple Github Action that allows us to update the status of the last commit in a pull request"
3+
author: "Ouzi, ltd. Ouzi Team <team@ouzi.dev>"
44
branding:
5-
icon: 'check-circle'
6-
color: 'purple'
5+
icon: "check-circle"
6+
color: "purple"
77
inputs:
88
token:
99
description: >
@@ -40,25 +40,30 @@ inputs:
4040
This is because the action won't have a token with permissions to add the status
4141
to the commit. You can disable this, but then you'll have to provide a token
4242
with enough permissions to add status to the commits in the forks!
43+
Will be used only for pull requests.
4344
default: "true"
4445
required: true
4546
addHoldComment:
4647
description: >
4748
If true the action will add a comment to the pull request. This is useful if you use prow and you get PRs from forks,
4849
you can use `/hold` and `/hold cancel` instead of the status check since the token won't have permissions to do that.
50+
Will be used only for pull requests.
4951
default: "false"
5052
pendingComment:
5153
description: >
5254
This is the message to add to the pull request when the status is pending.
55+
Will be used only for pull requests.
5356
default: "/hold"
5457
successComment:
5558
description: >
5659
This is the message to add to the pull request when the status is success.
60+
Will be used only for pull requests.
5761
default: "/hold cancel"
5862
failComment:
5963
description: >
6064
This is the message to add to the pull request when the status is 'failure', 'error' or 'cancelled'.
65+
Will be used only for pull requests.
6166
default: "/hold"
6267
runs:
6368
using: node12
64-
main: dist/index.js
69+
main: dist/index.js

0 commit comments

Comments
 (0)