Skip to content

Commit

Permalink
Support running multiple instances without collision
Browse files Browse the repository at this point in the history
This adds a new optional input, `comment_match_token`, which lets the
user add a suffix to the `matchToken` comment string.

Without this, running multiple instances of this action with comments
enabled would result in them clobbering each other's comments.

The existing behavior can be maintained by just not setting the new
option.
  • Loading branch information
zhimsel authored and mheap committed Jun 5, 2023
1 parent 5384b5b commit df979de
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ This action allows you to fail the build if/unless a certain combination of labe

This action has three required inputs; `labels`, `mode` and `count`

| Name | Description | Required | Default |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- | ------------------- |
| `labels` | Comma separated list of labels to match | true |
| `mode` | The mode of comparison to use. One of: exactly, minimum, maximum | true |
| `count` | The required number of labels to match | true |
| `token` | The GitHub token to use when calling the API | false | ${{ github.token }} |
| `message` | The message to log and to add to the PR (if add_comment is true). See the README for available placeholders | false |
| `add_comment` | Add a comment to the PR if required labels are missing. If a comment already exists, it will be updated. When the action passes, the comment will be deleted | false | false |
| `exit_type` | The exit type of the action. One of: failure, success | false |
| Name | Description | Required | Default |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- | ------------------- |
| `labels` | Comma separated list of labels to match | true |
| `mode` | The mode of comparison to use. One of: exactly, minimum, maximum | true |
| `count` | The required number of labels to match | true |
| `token` | The GitHub token to use when calling the API | false | ${{ github.token }} |
| `message` | The message to log and to add to the PR (if add_comment is true). See the README for available placeholders | false |
| `add_comment` | Add a comment to the PR if required labels are missing. If a comment already exists, it will be updated. When the action passes, the comment will be deleted | false | false |
| `exit_type` | The exit type of the action. One of: failure, success | false |
| `comment_match_token` | Hidden token to add to PR comments for matching purposes. Change this if you use multiple jobs/steps of this action. | false |

This action calls the GitHub API to fetch labels for a PR rather than reading `event.json`. This allows the action to run as intended when an earlier step adds a label. It will use `github.token` by default, and you can set the `token` input to provide alternative authentication.

Expand Down Expand Up @@ -102,6 +103,28 @@ The following tokens are available for use in custom messages:
labels: "community-reviewed, team-reviewed, codeowner-reviewed"
```

### Preventing comment collisions

You can run multiple instances of this action (with `add_comment` set) without conflicting with each other by setting the `comment_match_token` input.
Simply set it to a different string for each instance:

```yaml
- uses: mheap/github-action-required-labels@v4
with:
mode: exactly
count: 1
labels: "semver:patch, semver:minor, semver:major"
add_comment: true
comment_match_token: "semver"
- uses: mheap/github-action-required-labels@v4
with:
mode: minimum
count: 2
labels: "community-reviewed, team-reviewed, codeowner-reviewed"
add_comment: true
comment_match_token: "reviews"
```

### Controlling failure

You can set `exit_type` to success then inspect `outputs.status` to see if the action passed or failed. This is useful when you want to perform additional actions if a label is not present, but not fail the entire build.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ inputs:
exit_type:
description: "The exit type of the action. One of: failure, success"
required: false
comment_match_token:
description: "Hidden token to add to PR comments for matching purposes. Change this if you use multiple jobs/steps of this action."
required: false
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const core = require("@actions/core");
const github = require("@actions/github");

const matchToken = `<!-- reqlabelmessage -->\n`;
const matchTokenSuffix = core.getInput("comment_match_token") || "";
const matchToken = `<!-- reqlabelmessage${matchTokenSuffix} -->\n`;

async function action() {
try {
const token = core.getInput("token", { required: true });
Expand Down

0 comments on commit df979de

Please sign in to comment.