Implement review feedback

This commit is contained in:
Simon Engledew 2020-11-19 12:36:40 +00:00
parent 68dedeaa57
commit f3ff4c84ba
No known key found for this signature in database
GPG key ID: 84302E7B02FE8BCE
3 changed files with 86 additions and 62 deletions

59
lib/actions-util.js generated
View file

@ -227,8 +227,9 @@ async function createStatusReportBase(actionName, status, actionStartedAt, cause
return statusReport; return statusReport;
} }
exports.createStatusReportBase = createStatusReportBase; exports.createStatusReportBase = createStatusReportBase;
function getStatus({ status } = {}) { function isHTTPError(arg) {
return status; var _a;
return ((_a = arg) === null || _a === void 0 ? void 0 : _a.status) !== undefined && Number.isInteger(arg.status);
} }
function errorMessage(message, notFatal) { function errorMessage(message, notFatal) {
(notFatal ? core.warning : core.setFailed)(message); (notFatal ? core.warning : core.setFailed)(message);
@ -253,29 +254,37 @@ async function sendStatusReport(statusReport, ignoreFailures = false) {
const nwo = getRequiredEnvParam("GITHUB_REPOSITORY"); const nwo = getRequiredEnvParam("GITHUB_REPOSITORY");
const [owner, repo] = nwo.split("/"); const [owner, repo] = nwo.split("/");
const client = api.getActionsApiClient(); const client = api.getActionsApiClient();
const status = await client try {
.request("PUT /repos/:owner/:repo/code-scanning/analysis/status", { await client.request("PUT /repos/:owner/:repo/code-scanning/analysis/status", {
owner, owner,
repo, repo,
data: statusReportJSON, data: statusReportJSON,
}) });
.then(getStatus, getStatus); return true;
switch (status) { }
case 200: catch (e) {
return true; if (isHTTPError(e)) {
case 403: switch (e.status) {
core.setFailed("The repo on which this action is running is not opted-in to CodeQL code scanning."); case 403:
return false; core.setFailed("The repo on which this action is running is not opted-in to CodeQL code scanning.");
case 404: return false;
core.setFailed("Not authorized to used the CodeQL code scanning feature on this repo."); case 404:
return false; core.setFailed("Not authorized to used the CodeQL code scanning feature on this repo.");
case 422: return false;
// schema incompatibility when reporting status case 422:
// on enterprise this may be down to an upgraded action sending new data and we should // schema incompatibility when reporting status
// not stop the scanning process // on enterprise this may be down to an upgraded action sending incompatible data and
return errorMessage("Invalid status report sent to code scanning.", getRequiredEnvParam("GITHUB_SERVER_URL") !== util_1.GITHUB_DOTCOM_URL); // we should not stop the scanning process
default: // on dotcom we always want to be notified if something has gone wrong
return errorMessage("Unexpected error when sending code scanning status report.", ignoreFailures); // as this is unlikely to be a transient failure
return errorMessage("Invalid status report sent to code scanning.", getRequiredEnvParam("GITHUB_SERVER_URL") !== util_1.GITHUB_DOTCOM_URL);
}
}
// something else has gone wrong and the request/response will be logged by octokit
// it's possible this is a transient error and we should continue scanning if we are early in the
// process
// if we are late in the process we need to halt the action and report it
return errorMessage("Unexpected error when sending code scanning status report.", ignoreFailures);
} }
} }
exports.sendStatusReport = sendStatusReport; exports.sendStatusReport = sendStatusReport;

File diff suppressed because one or more lines are too long

View file

@ -290,11 +290,15 @@ export async function createStatusReportBase(
return statusReport; return statusReport;
} }
function getStatus({ status }: { status?: number } = {}) { interface HTTPError {
return status; status: number;
} }
function errorMessage(message: string, notFatal: boolean) { function isHTTPError(arg: any): arg is HTTPError {
return arg?.status !== undefined && Number.isInteger(arg.status);
}
function errorMessage(message: string, notFatal: boolean): boolean {
(notFatal ? core.warning : core.setFailed)(message); (notFatal ? core.warning : core.setFailed)(message);
return notFatal; return notFatal;
} }
@ -324,40 +328,51 @@ export async function sendStatusReport<S extends StatusReportBase>(
const [owner, repo] = nwo.split("/"); const [owner, repo] = nwo.split("/");
const client = api.getActionsApiClient(); const client = api.getActionsApiClient();
const status = await client try {
.request("PUT /repos/:owner/:repo/code-scanning/analysis/status", { await client.request(
owner, "PUT /repos/:owner/:repo/code-scanning/analysis/status",
repo, {
data: statusReportJSON, owner,
}) repo,
.then(getStatus, getStatus); data: statusReportJSON,
}
);
switch (status) { return true;
case 200: } catch (e) {
return true; if (isHTTPError(e)) {
case 403: switch (e.status) {
core.setFailed( case 403:
"The repo on which this action is running is not opted-in to CodeQL code scanning." core.setFailed(
); "The repo on which this action is running is not opted-in to CodeQL code scanning."
return false; );
case 404: return false;
core.setFailed( case 404:
"Not authorized to used the CodeQL code scanning feature on this repo." core.setFailed(
); "Not authorized to used the CodeQL code scanning feature on this repo."
return false; );
case 422: return false;
// schema incompatibility when reporting status case 422:
// on enterprise this may be down to an upgraded action sending new data and we should // schema incompatibility when reporting status
// not stop the scanning process // on enterprise this may be down to an upgraded action sending incompatible data and
return errorMessage( // we should not stop the scanning process
"Invalid status report sent to code scanning.", // on dotcom we always want to be notified if something has gone wrong
getRequiredEnvParam("GITHUB_SERVER_URL") !== GITHUB_DOTCOM_URL // as this is unlikely to be a transient failure
); return errorMessage(
default: "Invalid status report sent to code scanning.",
return errorMessage( getRequiredEnvParam("GITHUB_SERVER_URL") !== GITHUB_DOTCOM_URL
"Unexpected error when sending code scanning status report.", );
ignoreFailures }
); }
// something else has gone wrong and the request/response will be logged by octokit
// it's possible this is a transient error and we should continue scanning if we are early in the
// process
// if we are late in the process we need to halt the action and report it
return errorMessage(
"Unexpected error when sending code scanning status report.",
ignoreFailures
);
} }
} }