Skip to content

Commit

Permalink
add "sync-labels" option to enable removing labels
Browse files Browse the repository at this point in the history
closes actions#14
  • Loading branch information
kevinbuhmann committed Apr 19, 2020
1 parent 6de8091 commit bed0dae
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ inputs:
configuration-path:
description: 'The path for the label configurations'
default: '.github/labeler.yml'
sync-labels:
description: 'Whether or not to remove labels when matching files are reverted'
default: false
required: false
runs:
using: 'node12'
main: 'dist/index.js'
23 changes: 23 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3510,25 +3510,38 @@ 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 prNumber = getPrNumber();
if (!prNumber) {
console.log("Could not get pull request number from context, exiting");
return;
}
const client = new github.GitHub(token);
const { data: pullRequest } = yield client.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles = yield getChangedFiles(client, prNumber);
const labelGlobs = yield getLabelGlobs(client, configPath);
const labels = [];
const labelsToRemove = [];
for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) {
labels.push(label);
}
else if (pullRequest.labels.find(l => l.name === label)) {
labelsToRemove.push(label);
}
}
if (labels.length > 0) {
yield addLabels(client, prNumber, labels);
}
if (syncLabels && labelsToRemove.length) {
yield removeLabels(client, prNumber, labelsToRemove);
}
}
catch (error) {
core.error(error);
Expand Down Expand Up @@ -3618,6 +3631,16 @@ function addLabels(client, prNumber, labels) {
});
});
}
function removeLabels(client, prNumber, labels) {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all(labels.map(label => client.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
name: label
})));
});
}
run();


Expand Down
31 changes: 31 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 prNumber = getPrNumber();
if (!prNumber) {
Expand All @@ -16,6 +17,12 @@ async function run() {

const client = new github.GitHub(token);

const { data: pullRequest } = await client.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});

core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber);
const labelGlobs: Map<string, string[]> = await getLabelGlobs(
Expand All @@ -24,16 +31,23 @@ async function run() {
);

const labels: string[] = [];
const labelsToRemove: string[] = [];
for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) {
labels.push(label);
} else if (pullRequest.labels.find(l => l.name === label)) {
labelsToRemove.push(label);
}
}

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

if (syncLabels && labelsToRemove.length) {
await removeLabels(client, prNumber, labelsToRemove);
}
} catch (error) {
core.error(error);
core.setFailed(error.message);
Expand Down Expand Up @@ -145,4 +159,21 @@ async function addLabels(
});
}

async function removeLabels(
client: github.GitHub,
prNumber: number,
labels: string[]
) {
await Promise.all(
labels.map(label =>
client.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
name: label
})
)
);
}

run();

0 comments on commit bed0dae

Please sign in to comment.