Skip to content

Commit

Permalink
Merge origin/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoref committed Dec 20, 2022
2 parents 34b1b36 + aa97f29 commit c6732c9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
- uses: actions/labeler@v4
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
label-fork: "${{ secrets.LABEL_FORK }}"
```

_Note: This grants access to the `GITHUB_TOKEN` so the action can make calls to GitHub's rest API_
Expand Down
29 changes: 0 additions & 29 deletions __tests__/labeler.test.ts

This file was deleted.

31 changes: 31 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ describe("run", () => {
});
});

it("(with sync-labels: 'true') it deletes preexisting PR labels that no longer match the glob pattern", async () => {
let mockInput = {
"repo-token": "foo",
"configuration-path": "bar",
"sync-labels": "true",
};

jest
.spyOn(core, "getInput")
.mockImplementation((name: string, ...opts) => mockInput[name]);

usingLabelerConfigYaml("only_pdfs.yml");
mockGitHubResponseChangedFiles("foo.txt");
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{ name: "touched-a-pdf-file" }],
},
});

await run();

expect(addLabelsMock).toHaveBeenCalledTimes(0);
expect(removeLabelMock).toHaveBeenCalledTimes(1);
expect(removeLabelMock).toHaveBeenCalledWith({
owner: "monalisa",
repo: "helloworld",
issue_number: 123,
name: "touched-a-pdf-file",
});
});

it("(with sync-labels: false) it issues no delete calls even when there are preexisting PR labels that no longer match the glob pattern", async () => {
let mockInput = {
"repo-token": "foo",
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ author: 'GitHub'
inputs:
repo-token:
description: 'The GITHUB_TOKEN secret'
required: false
configuration-path:
description: 'The path for the label configurations'
default: '.github/labeler.yml'
required: false
sync-labels:
description: 'Whether or not to remove labels when matching files are reverted'
default: false
default: 'false'
required: false
label-fork:
description: 'Label fork'
default: 'false'
required: false

runs:
Expand Down
39 changes: 34 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,27 @@ const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const yaml = __importStar(__nccwpck_require__(1917));
const minimatch_1 = __nccwpck_require__(3973);
function stringToBoolean(input) {
if (typeof input === 'undefined') {
return false;
}
if (typeof input === 'boolean') {
return input;
}
return input.toLowerCase() === 'true' || input === '1';
}
function run() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const token = core.getInput("repo-token", { required: true });
const configPath = core.getInput("configuration-path", { required: true });
const syncLabels = !!core.getInput("sync-labels", { required: false });
const syncLabels = stringToBoolean(core.getInput("sync-labels", { required: false }));
const labelFork = stringToBoolean(core.getInput("label-fork", { required: false }));
if (!labelFork && ((_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.fork)) {
console.log("Workflow is not configured to label forks, exiting");
return;
}
const prNumber = getPrNumber();
if (!prNumber) {
console.log("Could not get pull request number from context, exiting");
Expand All @@ -75,11 +90,25 @@ function run() {
labelsToRemove.push(label);
}
}
if (labels.length > 0) {
yield addLabels(client, prNumber, labels);
try {
if (labels.length > 0) {
yield addLabels(client, prNumber, labels);
}
if (syncLabels && labelsToRemove.length) {
yield removeLabels(client, prNumber, labelsToRemove);
}
}
if (syncLabels && labelsToRemove.length) {
yield removeLabels(client, prNumber, labelsToRemove);
catch (error) {
if (error.name === 'HttpError' &&
error.message === 'Resource not accessible by integration') {
core.warning(`It needs \`permissions: pull-requests: write\` to work. Update the workflow. See https://github.com/actions/labeler/blob/6a315d4ea58951035b498eef56668feaba24489f/README.md#create-workflow`, {
title: `${process.env['GITHUB_ACTION_REPOSITORY']} running under '${github.context.eventName}' is misconfigured`
});
core.setFailed(error.message);
}
else {
throw error;
}
}
}
catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "labeler",
"version": "4.0.1",
"version": "5.0.0",
"description": "Labels pull requests by files altered",
"main": "lib/main.js",
"scripts": {
Expand Down
44 changes: 38 additions & 6 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ interface MatchConfig {
type StringOrMatchConfig = string | MatchConfig;
type ClientType = ReturnType<typeof github.getOctokit>;

type StringOrBoolean = string | boolean
function stringToBoolean(input?: StringOrBoolean): Boolean {
if (typeof input === 'undefined') {
return false;
}
if (typeof input === 'boolean') {
return input;
}
return input.toLowerCase() === 'true' || input === '1';
}

export async function run() {
try {
const token = core.getInput("repo-token", { required: true });
const configPath = core.getInput("configuration-path", { required: true });
const syncLabels = !!core.getInput("sync-labels", { required: false });
const syncLabels = stringToBoolean(core.getInput("sync-labels", { required: false }));
const labelFork = stringToBoolean(core.getInput("label-fork", { required: false }));

if (!labelFork && github.context.payload.repository?.fork) {
console.log("Workflow is not configured to label forks, exiting");
return;
}

const prNumber = getPrNumber();
if (!prNumber) {
Expand Down Expand Up @@ -49,12 +66,27 @@ export async function run() {
}
}

if (labels.length > 0) {
await addLabels(client, prNumber, labels);
}
try {
if (labels.length > 0) {
await addLabels(client, prNumber, labels);
}

if (syncLabels && labelsToRemove.length) {
await removeLabels(client, prNumber, labelsToRemove);
if (syncLabels && labelsToRemove.length) {
await removeLabels(client, prNumber, labelsToRemove);
}
} catch (error: any) {
if (error.name === 'HttpError' &&
error.message === 'Resource not accessible by integration') {
core.warning(
`It needs \`permissions: pull-requests: write\` to work. Update the workflow. See https://github.com/actions/labeler/blob/6a315d4ea58951035b498eef56668feaba24489f/README.md#create-workflow`,
{
title: `${process.env['GITHUB_ACTION_REPOSITORY']} running under '${github.context.eventName}' is misconfigured`
}
);
core.setFailed(error.message);
} else {
throw error;
}
}
} catch (error: any) {
core.error(error);
Expand Down

0 comments on commit c6732c9

Please sign in to comment.