Skip to content

Commit

Permalink
Merge pull request #8 from TheCleric/feature/enhance-debugging
Browse files Browse the repository at this point in the history
Update with latest enhance-debugging changes
  • Loading branch information
TheCleric committed Aug 25, 2020
2 parents 18b6d8e + 179127b commit 40b1374
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 76 deletions.
6 changes: 4 additions & 2 deletions __tests__/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import * as core from "@actions/core";
import { readFileSync } from "fs";
import "jest-extended";

// Dump core debug/error to console debug so that --silent works
// Dump core messaging to console so that --silent works
jest.spyOn(core, 'debug').mockImplementation(console.debug);
jest.spyOn(core, 'error').mockImplementation(console.debug);
jest.spyOn(core, 'info').mockImplementation(console.info);
jest.spyOn(core, 'error').mockImplementation(console.error);
jest.spyOn(core, 'warning').mockImplementation(console.warn);

function encodeContent(content: Buffer | ArrayBuffer | SharedArrayBuffer) {
return Buffer.from(content).toString("base64");
Expand Down
3 changes: 1 addition & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const path_1 = __importDefault(require("path"));
const CONFIG_PATH = ".github";
function getConfig(github, fileName, context) {
return __awaiter(this, void 0, void 0, function* () {
console.log('getConfig context', context);
try {
const configFile = {
owner: context.repo.owner,
Expand All @@ -43,7 +42,7 @@ function getConfig(github, fileName, context) {
return parseConfig(response.data.content);
}
catch (error) {
core.error(`ERROR! ${JSON.stringify(error)}`);
core.debug(`getConfig error: ${JSON.stringify(error)}`);
if (error.status === 404) {
return [];
}
Expand Down
69 changes: 35 additions & 34 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,38 @@ const defaults = [
exports.context = github.context;
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const repoToken = core.getInput("repo-token", { required: true });
core.debug(`context: ${exports.context ? JSON.stringify(exports.context) : ''}`);
if (exports.context && exports.context.payload && exports.context.payload.repository && exports.context.payload.pull_request) {
const octokit = new github.GitHub(repoToken);
const repoConfig = yield config_1.getConfig(octokit, CONFIG_FILENAME, exports.context);
core.debug(`repoConfig: ${JSON.stringify(repoConfig)}`);
const config = repoConfig.length > 0 ? repoConfig : defaults;
core.debug(`config: ${JSON.stringify(config)}`);
const headRef = exports.context.payload.pull_request.head.ref;
const baseRef = exports.context.payload.pull_request.base.ref;
const labelsToAdd = config.reduce((labels, entry) => {
if (entry.head && entry.base) {
if (isMatch(headRef, entry.head) && isMatch(baseRef, entry.base)) {
core.debug(`Matched "${headRef}" to "${entry.head}" and "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
}
else if (entry.head && isMatch(headRef, entry.head)) {
core.debug(`Matched "${headRef}" to "${entry.head}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
else if (entry.base && isMatch(baseRef, entry.base)) {
core.debug(`Matched "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
const repoToken = core.getInput("repo-token", { required: true });
core.debug(`context: ${exports.context ? JSON.stringify(exports.context) : ''}`);
if (exports.context && exports.context.payload && exports.context.payload.repository && exports.context.payload.pull_request) {
const octokit = new github.GitHub(repoToken);
const repoConfig = yield config_1.getConfig(octokit, CONFIG_FILENAME, exports.context);
core.debug(`repoConfig: ${JSON.stringify(repoConfig)}`);
const config = repoConfig.length > 0 ? repoConfig : defaults;
core.debug(`config: ${JSON.stringify(config)}`);
const headRef = exports.context.payload.pull_request.head.ref;
const baseRef = exports.context.payload.pull_request.base.ref;
const labelsToAdd = config.reduce((labels, entry) => {
if (entry.head && entry.base) {
if (isMatch(headRef, entry.head) && isMatch(baseRef, entry.base)) {
core.info(`Matched "${headRef}" to "${entry.head}" and "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
return labels;
}, []);
if (labelsToAdd.length > 0) {
core.debug(`Adding labels: ${labelsToAdd}`);
yield octokit.issues.addLabels(Object.assign({ issue_number: exports.context.payload.pull_request.number, labels: labelsToAdd }, exports.context.repo));
}
else if (entry.head && isMatch(headRef, entry.head)) {
core.info(`Matched "${headRef}" to "${entry.head}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
else if (entry.base && isMatch(baseRef, entry.base)) {
core.info(`Matched "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
return labels;
}, []);
if (labelsToAdd.length > 0) {
core.debug(`Adding labels: ${labelsToAdd}`);
yield octokit.issues.addLabels(Object.assign({ issue_number: exports.context.payload.pull_request.number, labels: labelsToAdd }, exports.context.repo));
}
}
catch (error) {
core.setFailed(error.message);
throw error;
}
});
}
exports.run = run;
Expand All @@ -78,4 +72,11 @@ function isMatch(ref, patterns) {
? patterns.some(pattern => matcher_1.default.isMatch(ref, pattern))
: matcher_1.default.isMatch(ref, patterns);
}
run();
try {
run();
}
catch (error) {
core.error(`ERROR! ${JSON.stringify(error)}`);
core.setFailed(error.message);
throw error;
}
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ConfigEntry } from "./ConfigEntry";
const CONFIG_PATH = ".github";

export async function getConfig(github: github.GitHub, fileName: string, context: Context.Context): Promise<ConfigEntry[]> {
console.log('getConfig context', context);
try {
const configFile = {
owner: context.repo.owner,
Expand All @@ -25,7 +24,7 @@ export async function getConfig(github: github.GitHub, fileName: string, context
}
return parseConfig(response.data.content);
} catch (error) {
core.error(`ERROR! ${JSON.stringify(error)}`);
core.debug(`getConfig error: ${JSON.stringify(error)}`);
if (error.status === 404) {
return [];
}
Expand Down
73 changes: 37 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,43 @@ const defaults: ConfigEntry[] = [
export const context = github.context;

export async function run() {
try {
const repoToken: string = core.getInput("repo-token", { required: true });
const repoToken: string = core.getInput("repo-token", { required: true });

core.debug(`context: ${context ? JSON.stringify(context) : ''}`);
core.debug(`context: ${context ? JSON.stringify(context) : ''}`);

if (context && context.payload && context.payload.repository && context.payload.pull_request) {
const octokit = new github.GitHub(repoToken);
const repoConfig: ConfigEntry[] = await getConfig(octokit, CONFIG_FILENAME, context);
core.debug(`repoConfig: ${JSON.stringify(repoConfig)}`);
const config: ConfigEntry[] = repoConfig.length > 0 ? repoConfig : defaults;
core.debug(`config: ${JSON.stringify(config)}`);
const headRef = context.payload.pull_request.head.ref;
const baseRef = context.payload.pull_request.base.ref;
const labelsToAdd = config.reduce((labels: string[], entry) => {
if (entry.head && entry.base) {
if (isMatch(headRef, entry.head) && isMatch(baseRef, entry.base)) {
core.debug(`Matched "${headRef}" to "${entry.head}" and "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
} else if (entry.head && isMatch(headRef, entry.head)) {
core.debug(`Matched "${headRef}" to "${entry.head}". Setting label to "${entry.label}"`);
labels.push(entry.label);
} else if (entry.base && isMatch(baseRef, entry.base)) {
core.debug(`Matched "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
if (context && context.payload && context.payload.repository && context.payload.pull_request) {
const octokit = new github.GitHub(repoToken);
const repoConfig: ConfigEntry[] = await getConfig(octokit, CONFIG_FILENAME, context);
core.debug(`repoConfig: ${JSON.stringify(repoConfig)}`);
const config: ConfigEntry[] = repoConfig.length > 0 ? repoConfig : defaults;
core.debug(`config: ${JSON.stringify(config)}`);
const headRef = context.payload.pull_request.head.ref;
const baseRef = context.payload.pull_request.base.ref;
const labelsToAdd = config.reduce((labels: string[], entry) => {
if (entry.head && entry.base) {
if (isMatch(headRef, entry.head) && isMatch(baseRef, entry.base)) {
core.info(`Matched "${headRef}" to "${entry.head}" and "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}
} else if (entry.head && isMatch(headRef, entry.head)) {
core.info(`Matched "${headRef}" to "${entry.head}". Setting label to "${entry.label}"`);
labels.push(entry.label);
} else if (entry.base && isMatch(baseRef, entry.base)) {
core.info(`Matched "${baseRef}" to "${entry.base}". Setting label to "${entry.label}"`);
labels.push(entry.label);
}

return labels;
}, []);
return labels;
}, []);

if (labelsToAdd.length > 0) {
core.debug(`Adding labels: ${labelsToAdd}`);
await octokit.issues.addLabels({
issue_number: context.payload.pull_request.number,
labels: labelsToAdd,
...context.repo
});
}
if (labelsToAdd.length > 0) {
core.debug(`Adding labels: ${labelsToAdd}`);
await octokit.issues.addLabels({
issue_number: context.payload.pull_request.number,
labels: labelsToAdd,
...context.repo
});
}
} catch (error) {
core.setFailed(error.message);
throw error;
}
}

Expand All @@ -66,4 +61,10 @@ function isMatch(ref: string, patterns: string | string[]): boolean {
: matcher.isMatch(ref, patterns);
}

run();
try {
run();
} catch (error) {
core.error(`ERROR! ${JSON.stringify(error)}`);
core.setFailed(error.message);
throw error;
}

0 comments on commit 40b1374

Please sign in to comment.