mirror of
https://github.com/actions/labeler.git
synced 2025-12-11 12:07:32 +00:00
Fixed build/pack steps to be more predictable
This commit is contained in:
31389
dist/index.js
vendored
31389
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
135
lib/main.js
Normal file
135
lib/main.js
Normal file
@@ -0,0 +1,135 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||
result["default"] = mod;
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
const yaml = __importStar(require("js-yaml"));
|
||||
const minimatch_1 = require("minimatch");
|
||||
function run() {
|
||||
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 prNumber = getPrNumber();
|
||||
if (!prNumber) {
|
||||
console.log('Could not get pull request number from context, exiting');
|
||||
return;
|
||||
}
|
||||
const client = new github.GitHub(token);
|
||||
core.debug(`fetching changed files for pr #${prNumber}`);
|
||||
const changedFiles = yield getChangedFiles(client, prNumber);
|
||||
const labelGlobs = yield getLabelGlobs(client, configPath);
|
||||
const labels = [];
|
||||
for (const [label, globs] of labelGlobs.entries()) {
|
||||
core.debug(`processing ${label}`);
|
||||
if (checkGlobs(changedFiles, globs)) {
|
||||
labels.push(label);
|
||||
}
|
||||
}
|
||||
if (labels.length > 0) {
|
||||
yield addLabels(client, prNumber, labels);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
core.error(error);
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
function getPrNumber() {
|
||||
const pullRequest = github.context.payload.pull_request;
|
||||
if (!pullRequest) {
|
||||
return undefined;
|
||||
}
|
||||
return pullRequest.number;
|
||||
}
|
||||
function getChangedFiles(client, prNumber) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const listFilesResponse = yield client.pulls.listFiles({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
pull_number: prNumber
|
||||
});
|
||||
const changedFiles = listFilesResponse.data.map(f => f.filename);
|
||||
core.debug('found changed files:');
|
||||
for (const file of changedFiles) {
|
||||
core.debug(' ' + file);
|
||||
}
|
||||
return changedFiles;
|
||||
});
|
||||
}
|
||||
function getLabelGlobs(client, configurationPath) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const configurationContent = yield fetchContent(client, configurationPath);
|
||||
// loads (hopefully) a `{[label:string]: string | string[]}`, but is `any`:
|
||||
const configObject = yaml.safeLoad(configurationContent);
|
||||
// transform `any` => `Map<string,string[]>` or throw if yaml is malformed:
|
||||
return getLabelGlobMapFromObject(configObject);
|
||||
});
|
||||
}
|
||||
function fetchContent(client, repoPath) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const response = yield client.repos.getContents({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
path: repoPath,
|
||||
ref: github.context.sha
|
||||
});
|
||||
return Buffer.from(response.data.content, response.data.encoding).toString();
|
||||
});
|
||||
}
|
||||
function getLabelGlobMapFromObject(configObject) {
|
||||
const labelGlobs = new Map();
|
||||
for (const label in configObject) {
|
||||
if (typeof configObject[label] === 'string') {
|
||||
labelGlobs.set(label, [configObject[label]]);
|
||||
}
|
||||
else if (configObject[label] instanceof Array) {
|
||||
labelGlobs.set(label, configObject[label]);
|
||||
}
|
||||
else {
|
||||
throw Error(`found unexpected type for label ${label} (should be string or array of globs)`);
|
||||
}
|
||||
}
|
||||
return labelGlobs;
|
||||
}
|
||||
function checkGlobs(changedFiles, globs) {
|
||||
for (const glob of globs) {
|
||||
core.debug(` checking pattern ${glob}`);
|
||||
const matcher = new minimatch_1.Minimatch(glob);
|
||||
for (const changedFile of changedFiles) {
|
||||
core.debug(` - ${changedFile}`);
|
||||
if (matcher.match(changedFile)) {
|
||||
core.debug(` ${changedFile} matches`);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function addLabels(client, prNumber, labels) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield client.issues.addLabels({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
labels: labels
|
||||
});
|
||||
});
|
||||
}
|
||||
run();
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "labeler",
|
||||
"version": "3.0.0",
|
||||
"description": "Labels pull requests by files altered",
|
||||
"main": "dist/index.js",
|
||||
"main": "lib/main.js",
|
||||
"scripts": {
|
||||
"build": "tsc && ncc build",
|
||||
"format": "prettier --write **/*.ts",
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||
"outDir": "./lib", /* Redirect output structure to the directory. */
|
||||
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||
// "composite": true, /* Enable project compilation */
|
||||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||
|
||||
Reference in New Issue
Block a user