From 6c55dc9f9846e49787d3cfc114d5bf2b9cebaf83 Mon Sep 17 00:00:00 2001 From: Zach Himsel Date: Wed, 31 May 2023 11:58:57 -0400 Subject: [PATCH] Support running multiple instances without collision 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. --- README.md | 41 ++++++++++++++++++++++++++++++++--------- action.yml | 3 +++ index.js | 4 +++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 41c39f5..2bcc315 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/action.yml b/action.yml index 6654ce7..b152428 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/index.js b/index.js index 1e5cce9..eeb005c 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ const core = require("@actions/core"); const github = require("@actions/github"); -const matchToken = ``; +const matchTokenSuffix = core.getInput("comment_match_token") || ""; +const matchToken = ``; + async function action() { try { const token = core.getInput("token", { required: true });