Paginate and remove old labels (#96)

* Paginate to get all changed files. Add option to remove labels

* build

* format and build
This commit is contained in:
David Kale
2020-09-08 13:09:16 -04:00
committed by GitHub
parent ebdf5d5f9f
commit fa244eaba5
4 changed files with 328 additions and 150 deletions

View File

@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as yaml from 'js-yaml';
import {Minimatch, IMinimatch} from 'minimatch';
import * as core from "@actions/core";
import * as github from "@actions/github";
import * as yaml from "js-yaml";
import { Minimatch, IMinimatch } from "minimatch";
interface MatchConfig {
all?: string[];
@@ -12,17 +12,24 @@ type StringOrMatchConfig = string | MatchConfig;
async function run() {
try {
const token = core.getInput('repo-token', {required: true});
const configPath = core.getInput('configuration-path', {required: true});
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');
console.log("Could not get pull request number from context, exiting");
return;
}
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, StringOrMatchConfig[]> = await getLabelGlobs(
@@ -31,16 +38,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);
@@ -60,17 +74,18 @@ async function getChangedFiles(
client: github.GitHub,
prNumber: number
): Promise<string[]> {
const listFilesResponse = await client.pulls.listFiles({
const listFilesOptions = client.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
const changedFiles = listFilesResponse.data.map(f => f.filename);
const listFilesResponse = await client.paginate(listFilesOptions);
const changedFiles = listFilesResponse.map(f => f.filename);
core.debug('found changed files:');
core.debug("found changed files:");
for (const file of changedFiles) {
core.debug(' ' + file);
core.debug(" " + file);
}
return changedFiles;
@@ -226,4 +241,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();