Skip to content

Commit

Permalink
feat: add toggle-label
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Aug 8, 2023
1 parent bcd85fd commit 0e1c60b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This is a collection of reusable composite actions for GitHub Actions workflows.
- [pr-assign-author](#pr-assign-author)
- [pr-label-by-review](#pr-label-by-review)
- [pr-validate-title-conventional](#pr-validate-title-conventional)
- [toggle-label](#toggle-label)
- [Miscellaneous](#miscellaneous)
- [bundlewatch](#bundlewatch)
- [cache-nx](#cache-nx)
Expand Down Expand Up @@ -775,6 +776,33 @@ Validate the title of a pull request based on the conventional commit format. Fo
| `success` | Whether the PR title is valid. | `true` |
| `error` | Error in case the PR title is not valid. | `(string containing the error)` |

#### toggle-label

[Source](toggle-label/action.yml)

Toggle a label on a pull request on or off based on a boolean input, or automatically if `toggle` is omitted. For use
with the `pull_request` event.

##### Example

```yaml
- uses: myparcelnl/actions/toggle-label@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
label: 'failing tests'
toggle: ${{ steps.tests.outputs.failed }}
```

##### Inputs

| Required | Name | Description | Example | Default |
| -------- | ------------- | ------------------------------------------------------------------------------------------------ | -------------------------------- | ------- |
| false | `token` | GitHub token to use. If passed, takes precedence over the `app-id` and `app-private-key` inputs. | `${{ secrets.GITHUB_TOKEN }}` ||
| false | `app-id` | The app ID of the app. | `${{ secrets.APP_ID }}` ||
| false | `private-key` | The private key of the app. | `${{ secrets.APP_PRIVATE_KEY }}` ||
| true | `label` | The label to toggle. | `failing tests` ||
| true | `toggle` | Whether to toggle the label on or off. | `true` | `auto` |

### Miscellaneous

#### bundlewatch
Expand Down
56 changes: 56 additions & 0 deletions toggle-label/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: 'Toggle label'
description: 'Toggle a label on a pull request'

inputs:
token:
description: 'GitHub token to use. If passed, takes precedence over the `app-id` and `app-private-key` inputs.'

app-id:
description: 'The app ID of the app.'

private-key:
description: 'The private key of the app.'

label:
description: 'The label to toggle'
required: true

toggle:
description: 'Whether to add or remove the label.'
default: 'auto'

runs:
using: composite
steps:
- uses: myparcelnl/actions/get-github-token@v3
id: token
with:
token: ${{ inputs.token }}
app-id: ${{ inputs.app-id }}
private-key: ${{ inputs.private-key }}

- name: 'Add label'
uses: actions/github-script@v6
if: (inputs.toggle == 'true' || inputs.toggle == 'auto') && !contains(github.event.pull_request.labels.*.name, '${{ inputs.label }}')
with:
github-token: ${{ steps.token.outputs.token }}
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['${{ inputs.label }}']
});
- name: 'Remove label'
uses: actions/github-script@v6
if: (inputs.toggle == 'false' || inputs.toggle == 'auto') && contains(github.event.pull_request.labels.*.name, '${{ inputs.label }}')
with:
github-token: ${{ steps.token.outputs.token }}
script: |
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['${{ inputs.label }}']
});

0 comments on commit 0e1c60b

Please sign in to comment.