Skip to content

Commit

Permalink
feat: support set status commit on push event (#307)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
belitre authored Oct 26, 2020
1 parent 397baa7 commit bb4eee2
Show file tree
Hide file tree
Showing 10 changed files with 6,955 additions and 7,386 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: ./
with:
name: "release"
- run: npm ci
- run: npm run build
- run: npm run format-check
- run: npm run lint
- run: npm run pack
- run: npm test
- run: npm run semantic-release
- if: always()
uses: ./
with:
name: "release"
status: "${{ job.status }}"
89 changes: 72 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

## Overview

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

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.

Currently the action supports `pull_request` and `push` events:
* 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.
* When the event is `push`, the action will set the status to the last commit pushed at the moment the workflow was triggered.

## Input Parameters

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

* 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`.

If the passed status is `pending` it wil set status commit `pending`.
If the passed status is `pending` it will set status commit `pending`.

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

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

* 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!
* 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.__

* optional
* default: "true"

* 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__
* 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.__

* optional
* default: "false"

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

* optional
* default: "/hold"

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

* optional
* default: "/hold cancel"

* failComment: This is the message to add to the pull request when the status is `failure`, `error` or `cancelled`.
* 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.__

* optional
* default: "/hold"

## Examples

### Action sets commit to pending status without comment
### Action sets push commit to pending status

```
name: Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.1.0
```

### Action sets push commit to pending status with custom name

```
name: Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
name: "name of my status check"
- uses: ouzi-dev/commit-status-updater@v1.1.0
```

### Action sets push commit to pending status on start, and updates check at the end of the workflow

```
name: Test
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.1.0
- if: always()
uses: ouzi-dev/commit-status-updater@v1.1.0
with:
status: "${{ job.status }}"
```

### Action sets pull request commit to pending status without comment

```
name: Test
Expand All @@ -82,10 +137,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
```

### Action sets commit to error status without comment
### Action sets pull request commit to error status without comment

```
name: Test
Expand All @@ -97,12 +152,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
with:
status: "error"
```

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

```
name: Test
Expand All @@ -114,11 +169,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
with:
addHoldComment: "true"
- if: always()
uses: ouzi-dev/commit-status-updater@v1.0.4
uses: ouzi-dev/commit-status-updater@v1.1.0
with:
addHoldComment: "true"
status: "${{ job.status }}"
Expand All @@ -136,7 +191,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
with:
status: "pending"
addHoldComment: "true"
Expand All @@ -157,7 +212,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
with:
status: "error"
url: http://myurl.io/
Expand All @@ -177,7 +232,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ouzi-dev/commit-status-updater@v1.0.4
- uses: ouzi-dev/commit-status-updater@v1.1.0
with:
token: "my_custom_token"
ignoreForks: "false"
Expand Down
34 changes: 29 additions & 5 deletions __test__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mockGithubHelper = jest.genMockFromModule('../lib/githubHelper') as any
mockGithubHelper.getInputs = jest.fn()

const MockIGithubHelper = jest.fn<IGithubHelper, []>(() => ({
isPullRequest: jest.fn(),
isFork: jest.fn(),
setStatus: jest.fn(),
addComment: jest.fn()
Expand All @@ -35,7 +36,7 @@ describe('runner tests', () => {
jest.resetModules()
})

it('run sets status and comment', async () => {
it('on push run sets status', async () => {
const params = ({} as unknown) as IParams
params.token = 'bleh'
params.ignoreForks = true
Expand All @@ -44,6 +45,25 @@ describe('runner tests', () => {
mockInputsHelper.getInputs.mockReturnValue(params)
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
mockIGithubHelper.isFork.mockReturnValue(false)
mockIGithubHelper.isPullRequest.mockReturnValue(false)

await runner.run()

expect(mockUtils.validateEventType).toHaveBeenCalled()
expect(mockIGithubHelper.setStatus).toHaveBeenCalledWith(params)
expect(mockIGithubHelper.addComment).toHaveBeenCalledTimes(0)
})

it('on PR run sets status and comment', async () => {
const params = ({} as unknown) as IParams
params.token = 'bleh'
params.ignoreForks = true
params.addHoldComment = true
params.selectedComment = 'my comment'
mockInputsHelper.getInputs.mockReturnValue(params)
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
mockIGithubHelper.isFork.mockReturnValue(false)
mockIGithubHelper.isPullRequest.mockReturnValue(true)

await runner.run()

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

it('run sets status and no comment', async () => {
it('on PR run sets status and no comment', async () => {
const params = ({} as unknown) as IParams
params.token = 'bleh'
params.ignoreForks = true
Expand All @@ -61,6 +81,7 @@ describe('runner tests', () => {
mockInputsHelper.getInputs.mockReturnValue(params)
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
mockIGithubHelper.isFork.mockReturnValue(false)
mockIGithubHelper.isPullRequest.mockReturnValue(true)

await runner.run()

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

it('run does not set status or comment', async () => {
it('on PR run does not set status or comment', async () => {
const params = ({} as unknown) as IParams
params.token = 'bleh'
params.ignoreForks = true
Expand All @@ -78,6 +99,7 @@ describe('runner tests', () => {
mockInputsHelper.getInputs.mockReturnValue(params)
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
mockIGithubHelper.isFork.mockReturnValue(true)
mockIGithubHelper.isPullRequest.mockReturnValue(true)

await runner.run()

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

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

await runner.run()

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

it('run sets status if ignore fork false', async () => {
it('on PR run sets status if ignore fork false', async () => {
const params = ({} as unknown) as IParams
params.token = 'bleh'
params.ignoreForks = false
params.addHoldComment = true
params.selectedComment = 'my comment'
mockInputsHelper.getInputs.mockReturnValue(params)
mockGithubHelper.CreateGithubHelper.mockReturnValue(mockIGithubHelper)
mockIGithubHelper.isPullRequest.mockReturnValue(true)

await runner.run()

Expand Down
17 changes: 11 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: 'commit-status-updater'
description: 'A simple Github Action that allows us to update the status of the last commit in a pull request'
author: 'Ouzi, ltd. Ouzi Team <team@ouzi.dev>'
name: "commit-status-updater"
description: "A simple Github Action that allows us to update the status of the last commit in a pull request"
author: "Ouzi, ltd. Ouzi Team <team@ouzi.dev>"
branding:
icon: 'check-circle'
color: 'purple'
icon: "check-circle"
color: "purple"
inputs:
token:
description: >
Expand Down Expand Up @@ -40,25 +40,30 @@ inputs:
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.
default: "true"
required: true
addHoldComment:
description: >
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,
you can use `/hold` and `/hold cancel` instead of the status check since the token won't have permissions to do that.
Will be used only for pull requests.
default: "false"
pendingComment:
description: >
This is the message to add to the pull request when the status is pending.
Will be used only for pull requests.
default: "/hold"
successComment:
description: >
This is the message to add to the pull request when the status is success.
Will be used only for pull requests.
default: "/hold cancel"
failComment:
description: >
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.
default: "/hold"
runs:
using: node12
main: dist/index.js
main: dist/index.js
Loading

0 comments on commit bb4eee2

Please sign in to comment.