Provide a better error message

This commit is contained in:
Robert 2021-06-16 17:00:26 +01:00
parent 366b68eda0
commit c989ee7b39
3 changed files with 29 additions and 7 deletions

15
lib/actions-util.js generated
View file

@ -540,6 +540,16 @@ function getRelativeScriptPath() {
return path.relative(actionsDirectory, __filename);
}
exports.getRelativeScriptPath = getRelativeScriptPath;
// Reads the contents of GITHUB_EVENT_PATH as a JSON object
function getWorkflowEvent() {
const eventJsonFile = util_1.getRequiredEnvParam("GITHUB_EVENT_PATH");
try {
return JSON.parse(fs.readFileSync(eventJsonFile, "utf-8"));
}
catch (e) {
throw new Error(`Unable to read workflow event JSON from ${eventJsonFile}: ${e}`);
}
}
// Is the version of the repository we are currently analyzing from the default branch,
// or alternatively from another branch or a pull request.
async function isAnalyzingDefaultBranch() {
@ -549,8 +559,9 @@ async function isAnalyzingDefaultBranch() {
currentRef = currentRef.startsWith("refs/heads/")
? currentRef.substr("refs/heads/".length)
: currentRef;
const eventJson = JSON.parse(fs.readFileSync(util_1.getRequiredEnvParam("GITHUB_EVENT_PATH"), "utf-8"));
return currentRef === ((_b = (_a = eventJson) === null || _a === void 0 ? void 0 : _a.repository) === null || _b === void 0 ? void 0 : _b.default_branch);
const event = getWorkflowEvent();
const defaultBranch = (_b = (_a = event) === null || _a === void 0 ? void 0 : _a.repository) === null || _b === void 0 ? void 0 : _b.default_branch;
return currentRef === defaultBranch;
}
exports.isAnalyzingDefaultBranch = isAnalyzingDefaultBranch;
//# sourceMappingURL=actions-util.js.map

File diff suppressed because one or more lines are too long

View file

@ -692,6 +692,18 @@ export function getRelativeScriptPath(): string {
return path.relative(actionsDirectory, __filename);
}
// Reads the contents of GITHUB_EVENT_PATH as a JSON object
function getWorkflowEvent(): any {
const eventJsonFile = getRequiredEnvParam("GITHUB_EVENT_PATH");
try {
return JSON.parse(fs.readFileSync(eventJsonFile, "utf-8"));
} catch (e) {
throw new Error(
`Unable to read workflow event JSON from ${eventJsonFile}: ${e}`
);
}
}
// Is the version of the repository we are currently analyzing from the default branch,
// or alternatively from another branch or a pull request.
export async function isAnalyzingDefaultBranch(): Promise<boolean> {
@ -701,9 +713,8 @@ export async function isAnalyzingDefaultBranch(): Promise<boolean> {
? currentRef.substr("refs/heads/".length)
: currentRef;
const eventJson = JSON.parse(
fs.readFileSync(getRequiredEnvParam("GITHUB_EVENT_PATH"), "utf-8")
);
const event = getWorkflowEvent();
const defaultBranch = event?.repository?.default_branch;
return currentRef === eventJson?.repository?.default_branch;
return currentRef === defaultBranch;
}