Compare commits

...

1 Commits

Author SHA1 Message Date
Robert
6ebdeafc2f Handle errors during database upload 2021-06-21 10:30:11 +01:00
9 changed files with 72 additions and 58 deletions

6
lib/actions-util.js generated
View File

@@ -448,10 +448,6 @@ async function createStatusReportBase(actionName, status, actionStartedAt, cause
return statusReport; return statusReport;
} }
exports.createStatusReportBase = createStatusReportBase; exports.createStatusReportBase = createStatusReportBase;
function isHTTPError(arg) {
var _a;
return ((_a = arg) === null || _a === void 0 ? void 0 : _a.status) !== undefined && Number.isInteger(arg.status);
}
const GENERIC_403_MSG = "The repo on which this action is running is not opted-in to CodeQL code scanning."; const GENERIC_403_MSG = "The repo on which this action is running is not opted-in to CodeQL code scanning.";
const GENERIC_404_MSG = "Not authorized to used the CodeQL code scanning feature on this repo."; const GENERIC_404_MSG = "Not authorized to used the CodeQL code scanning feature on this repo.";
const OUT_OF_DATE_MSG = "CodeQL Action is out-of-date. Please upgrade to the latest version of codeql-action."; const OUT_OF_DATE_MSG = "CodeQL Action is out-of-date. Please upgrade to the latest version of codeql-action.";
@@ -481,7 +477,7 @@ async function sendStatusReport(statusReport) {
} }
catch (e) { catch (e) {
console.log(e); console.log(e);
if (isHTTPError(e)) { if (util_1.isHTTPError(e)) {
switch (e.status) { switch (e.status) {
case 403: case 403:
if (workflowIsTriggeredByPushEvent() && isDependabotActor()) { if (workflowIsTriggeredByPushEvent() && isDependabotActor()) {

File diff suppressed because one or more lines are too long

42
lib/analyze-action.js generated
View File

@@ -49,14 +49,23 @@ async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
return; return;
} }
const client = api_client_1.getApiClient(apiDetails); const client = api_client_1.getApiClient(apiDetails);
const optInResponse = await client.request("GET /repos/:owner/:repo/code-scanning/databases", { try {
owner: repositoryNwo.owner, await client.request("GET /repos/:owner/:repo/code-scanning/databases", {
repo: repositoryNwo.repo, owner: repositoryNwo.owner,
}); repo: repositoryNwo.repo,
if (optInResponse.status !== 204) { });
// Repository is not opted in to database uploads. }
logger.debug("Repository is not opted in to database uploads. Skipping upload."); catch (e) {
return; console.log(e);
if (util.isHTTPError(e)) {
if (e.status === 404) {
logger.debug("Repository is not opted in to database uploads. Skipping upload.");
}
else {
logger.debug(`Skipping database upload due to unknown error: ${e}`);
}
return;
}
} }
const codeql = codeql_1.getCodeQL(config.codeQLCmd); const codeql = codeql_1.getCodeQL(config.codeQLCmd);
for (const language of config.languages) { for (const language of config.languages) {
@@ -66,17 +75,18 @@ async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
await codeql.databaseBundle(databasePath, databaseBundlePath); await codeql.databaseBundle(databasePath, databaseBundlePath);
// Upload the database bundle // Upload the database bundle
const payload = fs.readFileSync(databaseBundlePath); const payload = fs.readFileSync(databaseBundlePath);
const uploadResponse = await client.request(`PUT /repos/:owner/:repo/code-scanning/databases/${language}`, { try {
owner: repositoryNwo.owner, await client.request(`PUT /repos/:owner/:repo/code-scanning/databases/${language}`, {
repo: repositoryNwo.repo, owner: repositoryNwo.owner,
data: payload, repo: repositoryNwo.repo,
}); data: payload,
if (uploadResponse.status === 201) { });
logger.debug(`Successfully uploaded database for ${language}`); logger.debug(`Successfully uploaded database for ${language}`);
} }
else { catch (e) {
console.log(e);
// Log a warning but don't fail the workflow // Log a warning but don't fail the workflow
logger.warning(`Failed to upload database for ${language}. ${uploadResponse.data}`); logger.warning(`Failed to upload database for ${language}: ${e}`);
} }
} }
} }

File diff suppressed because one or more lines are too long

5
lib/util.js generated
View File

@@ -408,4 +408,9 @@ function getRequiredEnvParam(paramName) {
return value; return value;
} }
exports.getRequiredEnvParam = getRequiredEnvParam; exports.getRequiredEnvParam = getRequiredEnvParam;
function isHTTPError(arg) {
var _a;
return ((_a = arg) === null || _a === void 0 ? void 0 : _a.status) !== undefined && Number.isInteger(arg.status);
}
exports.isHTTPError = isHTTPError;
//# sourceMappingURL=util.js.map //# sourceMappingURL=util.js.map

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,7 @@ import * as yaml from "js-yaml";
import * as api from "./api-client"; import * as api from "./api-client";
import * as sharedEnv from "./shared-environment"; import * as sharedEnv from "./shared-environment";
import { getRequiredEnvParam, GITHUB_DOTCOM_URL } from "./util"; import { getRequiredEnvParam, GITHUB_DOTCOM_URL, isHTTPError } from "./util";
/** /**
* The utils in this module are meant to be run inside of the action only. * The utils in this module are meant to be run inside of the action only.
@@ -576,15 +576,6 @@ export async function createStatusReportBase(
return statusReport; return statusReport;
} }
interface HTTPError {
status: number;
message?: string;
}
function isHTTPError(arg: any): arg is HTTPError {
return arg?.status !== undefined && Number.isInteger(arg.status);
}
const GENERIC_403_MSG = const GENERIC_403_MSG =
"The repo on which this action is running is not opted-in to CodeQL code scanning."; "The repo on which this action is running is not opted-in to CodeQL code scanning.";
const GENERIC_404_MSG = const GENERIC_404_MSG =

View File

@@ -76,19 +76,23 @@ async function uploadDatabases(
} }
const client = getApiClient(apiDetails); const client = getApiClient(apiDetails);
const optInResponse = await client.request( try {
"GET /repos/:owner/:repo/code-scanning/databases", await client.request("GET /repos/:owner/:repo/code-scanning/databases", {
{
owner: repositoryNwo.owner, owner: repositoryNwo.owner,
repo: repositoryNwo.repo, repo: repositoryNwo.repo,
});
} catch (e) {
console.log(e);
if (util.isHTTPError(e)) {
if (e.status === 404) {
logger.debug(
"Repository is not opted in to database uploads. Skipping upload."
);
} else {
logger.debug(`Skipping database upload due to unknown error: ${e}`);
}
return;
} }
);
if (optInResponse.status !== 204) {
// Repository is not opted in to database uploads.
logger.debug(
"Repository is not opted in to database uploads. Skipping upload."
);
return;
} }
const codeql = getCodeQL(config.codeQLCmd); const codeql = getCodeQL(config.codeQLCmd);
@@ -100,21 +104,20 @@ async function uploadDatabases(
// Upload the database bundle // Upload the database bundle
const payload = fs.readFileSync(databaseBundlePath); const payload = fs.readFileSync(databaseBundlePath);
const uploadResponse = await client.request( try {
`PUT /repos/:owner/:repo/code-scanning/databases/${language}`, await client.request(
{ `PUT /repos/:owner/:repo/code-scanning/databases/${language}`,
owner: repositoryNwo.owner, {
repo: repositoryNwo.repo, owner: repositoryNwo.owner,
data: payload, repo: repositoryNwo.repo,
} data: payload,
); }
if (uploadResponse.status === 201) {
logger.debug(`Successfully uploaded database for ${language}`);
} else {
// Log a warning but don't fail the workflow
logger.warning(
`Failed to upload database for ${language}. ${uploadResponse.data}`
); );
logger.debug(`Successfully uploaded database for ${language}`);
} catch (e) {
console.log(e);
// Log a warning but don't fail the workflow
logger.warning(`Failed to upload database for ${language}: ${e}`);
} }
} }
} }

View File

@@ -478,3 +478,12 @@ export function getRequiredEnvParam(paramName: string): string {
} }
return value; return value;
} }
export interface HTTPError {
status: number;
message?: string;
}
export function isHTTPError(arg: any): arg is HTTPError {
return arg?.status !== undefined && Number.isInteger(arg.status);
}