Skip to content

Commit

Permalink
fix: commit check didn't update previous runs (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchabr authored Feb 5, 2021
1 parent 0d9e535 commit 9335ddc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Github Action to check if a PR's labels pass the specified rules
- `hasAll`: Comma separated list of labels, PR needs all of them
- `hasNone`: Comma separated list of labels, PR must not have any of them
- `hasNotAll`: Comma separated list of labels, PR must not have all of them
- `githubToken`: GitHub token

## Output
- `passed`: boolean
Expand All @@ -15,22 +16,24 @@ Github Action to check if a PR's labels pass the specified rules
name: Labels Check
on:
pull_request:
types: [opened, edited, labeled, unlabeled, synchronize]
types: [opened, labeled, unlabeled, synchronize]
jobs:
QA-check:
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- uses: danielchabr/pr-has-one-of-labels@master
- uses: danielchabr/pr-labels-checker@v3
id: checkLabel
with:
hasSome: QA:tested,QA:skipped
githubToken: ${{ secrets.GITHUB_TOKEN }}
Do_not_merge-check:
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- uses: danielchabr/pr-has-one-of-labels@master
- uses: danielchabr/pr-labels-checker@v3
id: checkLabel
with:
hasNone: do not merge,blocked
githubToken: ${{ secrets.GITHUB_TOKEN }}
```
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
hasNotAll:
description: 'Comma separated list of labels, PR must not have all of them'
required: false
githubToken:
description: 'The Github secret token to access PR check API'
required: true
outputs:
passed:
description: 'Have the provided labels passed all tests?'
Expand Down
49 changes: 45 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ if (!github.context.payload.pull_request) {
return core.setOutput('passed', true)
}

const token = core.getInput('githubToken');
const context = github.context
const octokit = github.getOctokit(token)

const hasSomeInput = core.getInput('hasSome')
const hasAllInput = core.getInput('hasAll')
const hasNoneInput = core.getInput('hasNone')
Expand All @@ -19,7 +23,7 @@ const hasNotAllLabels = hasNotAllInput.split(',')
const failMessages = []


const prLabels = github.context.payload.pull_request.labels.map(item => item.name)
const prLabels = context.payload.pull_request.labels.map(item => item.name)

const hasSomeResult = !hasSomeInput || hasSomeLabels.some((label) =>
prLabels.includes(label)
Expand Down Expand Up @@ -61,8 +65,45 @@ if (!hasNotAllResult) {
)}`)
}

if (failMessages.length) {
core.setFailed(failMessages.join('. '))
async function run () {
const checks = await octokit.checks.listForRef({
...context.repo,
ref: context.payload.pull_request.head.ref,
});

const checkRunIds = checks.data.check_runs.filter(check => check.name === context.job).map(check => check.id)

if (failMessages.length) {
// update old checks
for (const id of checkRunIds) {
await octokit.checks.update({
...context.repo,
check_run_id: id,
conclusion: 'failure',
output: {
title: 'Labels did not pass provided rules',
summary: failMessages.join('. ')
}
})
}

core.setFailed(failMessages.join('. '))
} else {
// update old checks
for (const id of checkRunIds) {
await octokit.checks.update({
...context.repo,
check_run_id: id,
conclusion: 'success',
output: {
title: 'Labels follow all the provided rules',
summary: ''
}
})
}

core.setOutput('passed', true)
}
}

core.setOutput('passed', failMessages.length === 0)
run()

0 comments on commit 9335ddc

Please sign in to comment.