Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running multiple instances without collision #54

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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