Skip to content

Commit

Permalink
Support running multiple instances without collision (#54)
Browse files Browse the repository at this point in the history
* Support running multiple instances without collision

This uses a combination of three Github-set env vars that, together, are
guaranteed to be unique within the scope of a repo and all its
workflows:

- GITHUB_WORKFLOW: the name of the workflow (or file path, if no name is given)
- GITHUB_JOB: the ID of the job
- GITHUB_ACTION: the ID of the step (or a formatted name of the repo
  where the action resides, i.e.
  `__mheap_github-action-required-labels`)

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

---------

Co-authored-by: Michael Heap <m@michaelheap.com>
  • Loading branch information
zhimsel and mheap authored Jun 8, 2023
1 parent ae0422e commit 418d9eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ The following tokens are available for use in custom messages:
labels: "community-reviewed, team-reviewed, codeowner-reviewed"
```

### Preventing comment collisions

This action uses a combination of the workflow name/path, job ID, and step ID to add an invisible "match token" to the beginning of any comments it creates. That way, it can later know which comments it owns when modifying them, while supporting multiple "instances" of this action to be run at the same time within a repo.

However, note that if any of those three identifiers change, any "in flight" comments on open PRs may be orphaned (since the final match token will have changed between runs). If you rename any of those identifiers, you will have to delete any orphaned comments manually.

### 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
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const core = require("@actions/core");
const github = require("@actions/github");

const matchToken = `<!-- reqlabelmessage -->\n`;
let matchToken;
async function action() {
// Use a guaranteed-unique (but persistent) string to match "our" comment
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
const matchTokenId = [
process.env.GITHUB_WORKFLOW,
process.env.GITHUB_JOB,
process.env.GITHUB_ACTION,
].join("/");

matchToken = `<!-- ${matchTokenId} -->\n`;

try {
const token = core.getInput("token", { required: true });
const octokit = github.getOctokit(token);
Expand Down
4 changes: 3 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ const mockedEnv = require("mocked-env");
const nock = require("nock");
nock.disableNetConnect();

const matchToken = `<!-- reqlabelmessage -->\n`;
// note: these need to match the mocked env vars below
const matchToken = `<!-- demo-workflow/demo-job/required-labels -->\n`;

describe("Required Labels", () => {
let restore;
let restoreTest;
beforeEach(() => {
restore = mockedEnv({
GITHUB_WORKFLOW: "demo-workflow",
GITHUB_JOB: "demo-job",
GITHUB_ACTION: "required-labels",
GITHUB_ACTOR: "mheap",
GITHUB_REPOSITORY: "mheap/missing-repo",
Expand Down

0 comments on commit 418d9eb

Please sign in to comment.