Use a single Octokit client for everything rather than a bunch of Octokits and an HTTP client.

This commit is contained in:
Chris Gavin 2020-06-23 19:08:30 +01:00
parent bc21c8f6f3
commit 74c48f71fa
No known key found for this signature in database
GPG key ID: 07F950B80C27E4DA
9 changed files with 92 additions and 91 deletions

23
lib/api-client.js generated Normal file
View file

@ -0,0 +1,23 @@
"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const octokit = __importStar(require("@octokit/rest"));
const console_log_level_1 = __importDefault(require("console-log-level"));
const githubAPIURL = process.env["GITHUB_API_URL"] || "https://api.github.com";
exports.client = new octokit.Octokit({
auth: core.getInput("token"),
baseUrl: githubAPIURL,
userAgent: "CodeQL Action",
log: console_log_level_1.default({ level: "debug" })
});
//# sourceMappingURL=api-client.js.map

1
lib/api-client.js.map Normal file
View file

@ -0,0 +1 @@
{"version":3,"file":"api-client.js","sourceRoot":"","sources":["../src/api-client.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAAsC;AACtC,uDAAyC;AACzC,0EAAgD;AAEhD,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,wBAAwB,CAAC;AAClE,QAAA,MAAM,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC;IACxC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;IAC5B,OAAO,EAAE,YAAY;IACrB,SAAS,EAAE,eAAe;IAC1B,GAAG,EAAE,2BAAe,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;CACzC,CAAC,CAAC"}

26
lib/upload-lib.js generated
View file

@ -11,13 +11,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const http = __importStar(require("@actions/http-client"));
const auth = __importStar(require("@actions/http-client/auth"));
const file_url_1 = __importDefault(require("file-url")); const file_url_1 = __importDefault(require("file-url"));
const fs = __importStar(require("fs")); const fs = __importStar(require("fs"));
const jsonschema = __importStar(require("jsonschema")); const jsonschema = __importStar(require("jsonschema"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
const zlib_1 = __importDefault(require("zlib")); const zlib_1 = __importDefault(require("zlib"));
const api = __importStar(require("./api-client"));
const fingerprints = __importStar(require("./fingerprints")); const fingerprints = __importStar(require("./fingerprints"));
const sharedEnv = __importStar(require("./shared-environment")); const sharedEnv = __importStar(require("./shared-environment"));
const util = __importStar(require("./util")); const util = __importStar(require("./util"));
@ -51,27 +50,28 @@ async function uploadPayload(payload) {
if (testMode) { if (testMode) {
return true; return true;
} }
const githubToken = core.getInput('token'); const [owner, repo] = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const ph = new auth.BearerCredentialHandler(githubToken);
const client = new http.HttpClient('Code Scanning : Upload SARIF', [ph]);
const url = 'https://api.github.com/repos/' + process.env['GITHUB_REPOSITORY'] + '/code-scanning/analysis';
// Make up to 4 attempts to upload, and sleep for these // Make up to 4 attempts to upload, and sleep for these
// number of seconds between each attempt. // number of seconds between each attempt.
// We don't want to backoff too much to avoid wasting action // We don't want to backoff too much to avoid wasting action
// minutes, but just waiting a little bit could maybe help. // minutes, but just waiting a little bit could maybe help.
const backoffPeriods = [1, 5, 15]; const backoffPeriods = [1, 5, 15];
for (let attempt = 0; attempt <= backoffPeriods.length; attempt++) { for (let attempt = 0; attempt <= backoffPeriods.length; attempt++) {
const res = await client.put(url, payload); const response = await api.client.request("PUT /repos/:owner/:repo/code-scanning/analysis", ({
core.debug('response status: ' + res.message.statusCode); owner: owner,
const statusCode = res.message.statusCode; repo: repo,
data: payload,
}));
core.debug('response status: ' + response.status);
const statusCode = response.status;
if (statusCode === 202) { if (statusCode === 202) {
core.info("Successfully uploaded results"); core.info("Successfully uploaded results");
return true; return true;
} }
const requestID = res.message.headers["x-github-request-id"]; const requestID = response.headers["x-github-request-id"];
// On any other status code that's not 5xx mark the upload as failed // On any other status code that's not 5xx mark the upload as failed
if (!statusCode || statusCode < 500 || statusCode >= 600) { if (!statusCode || statusCode < 500 || statusCode >= 600) {
core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + response.data);
return false; return false;
} }
// On a 5xx status code we may retry the request // On a 5xx status code we may retry the request
@ -79,7 +79,7 @@ async function uploadPayload(payload) {
// Log the failure as a warning but don't mark the action as failed yet // Log the failure as a warning but don't mark the action as failed yet
core.warning('Upload attempt (' + (attempt + 1) + ' of ' + (backoffPeriods.length + 1) + core.warning('Upload attempt (' + (attempt + 1) + ' of ' + (backoffPeriods.length + 1) +
') failed (' + requestID + '). Retrying in ' + backoffPeriods[attempt] + ') failed (' + requestID + '). Retrying in ' + backoffPeriods[attempt] +
' seconds: (' + statusCode + ') ' + await res.readBody()); ' seconds: (' + statusCode + ') ' + response.data);
// Sleep for the backoff period // Sleep for the backoff period
await new Promise(r => setTimeout(r, backoffPeriods[attempt] * 1000)); await new Promise(r => setTimeout(r, backoffPeriods[attempt] * 1000));
continue; continue;
@ -88,7 +88,7 @@ async function uploadPayload(payload) {
// If the upload fails with 5xx then we assume it is a temporary problem // If the upload fails with 5xx then we assume it is a temporary problem
// and not an error that the user has caused or can fix. // and not an error that the user has caused or can fix.
// We avoid marking the job as failed to avoid breaking CI workflows. // We avoid marking the job as failed to avoid breaking CI workflows.
core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + response.data);
return false; return false;
} }
} }

File diff suppressed because one or more lines are too long

46
lib/util.js generated
View file

@ -6,19 +6,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
result["default"] = mod; result["default"] = mod;
return result; return result;
}; };
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const http = __importStar(require("@actions/http-client"));
const auth = __importStar(require("@actions/http-client/auth"));
const octokit = __importStar(require("@octokit/rest"));
const console_log_level_1 = __importDefault(require("console-log-level"));
const fs = __importStar(require("fs")); const fs = __importStar(require("fs"));
const os = __importStar(require("os")); const os = __importStar(require("os"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
const api = __importStar(require("./api-client"));
const sharedEnv = __importStar(require("./shared-environment")); const sharedEnv = __importStar(require("./shared-environment"));
/** /**
* Should the current action be aborted? * Should the current action be aborted?
@ -75,12 +69,7 @@ async function getLanguagesInRepo() {
let owner = repo_nwo[0]; let owner = repo_nwo[0];
let repo = repo_nwo[1]; let repo = repo_nwo[1];
core.debug(`GitHub repo ${owner} ${repo}`); core.debug(`GitHub repo ${owner} ${repo}`);
let ok = new octokit.Octokit({ const response = await api.client.request("GET /repos/:owner/:repo/languages", ({
auth: core.getInput('token'),
userAgent: "CodeQL Action",
log: console_log_level_1.default({ level: "debug" })
});
const response = await ok.request("GET /repos/:owner/:repo/languages", ({
owner, owner,
repo repo
})); }));
@ -158,19 +147,14 @@ async function getWorkflowPath() {
const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/"); const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/");
const owner = repo_nwo[0]; const owner = repo_nwo[0];
const repo = repo_nwo[1]; const repo = repo_nwo[1];
const run_id = getRequiredEnvParam('GITHUB_RUN_ID'); const run_id = Number(getRequiredEnvParam('GITHUB_RUN_ID'));
const ok = new octokit.Octokit({ const runsResponse = await api.client.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
auth: core.getInput('token'),
userAgent: "CodeQL Action",
log: console_log_level_1.default({ level: 'debug' })
});
const runsResponse = await ok.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
owner, owner,
repo, repo,
run_id run_id
}); });
const workflowUrl = runsResponse.data.workflow_url; const workflowUrl = runsResponse.data.workflow_url;
const workflowResponse = await ok.request('GET ' + workflowUrl); const workflowResponse = await api.client.request('GET ' + workflowUrl);
return workflowResponse.data.path; return workflowResponse.data.path;
} }
/** /**
@ -264,21 +248,19 @@ async function createStatusReport(actionName, status, cause, exception) {
/** /**
* Send a status report to the code_scanning/analysis/status endpoint. * Send a status report to the code_scanning/analysis/status endpoint.
* *
* Returns the status code of the response to the status request, or * Returns the status code of the response to the status request.
* undefined if the given statusReport is undefined or no response was
* received.
*/ */
async function sendStatusReport(statusReport) { async function sendStatusReport(statusReport) {
var _a;
const statusReportJSON = JSON.stringify(statusReport); const statusReportJSON = JSON.stringify(statusReport);
core.debug('Sending status report: ' + statusReportJSON); core.debug('Sending status report: ' + statusReportJSON);
const githubToken = core.getInput('token'); const nwo = getRequiredEnvParam("GITHUB_REPOSITORY");
const ph = new auth.BearerCredentialHandler(githubToken); const [owner, repo] = nwo.split("/");
const client = new http.HttpClient('Code Scanning : Status Report', [ph]); const statusResponse = await api.client.request('PUT /repos/:owner/:repo/code-scanning/analysis/status', {
const url = 'https://api.github.com/repos/' + process.env['GITHUB_REPOSITORY'] owner: owner,
+ '/code-scanning/analysis/status'; repo: repo,
const res = await client.put(url, statusReportJSON); data: statusReportJSON,
return (_a = res.message) === null || _a === void 0 ? void 0 : _a.statusCode; });
return statusResponse.status;
} }
/** /**
* Send a status report that an action is starting. * Send a status report that an action is starting.

File diff suppressed because one or more lines are too long

11
src/api-client.ts Normal file
View file

@ -0,0 +1,11 @@
import * as core from "@actions/core";
import * as octokit from "@octokit/rest";
import consoleLogLevel from "console-log-level";
const githubAPIURL = process.env["GITHUB_API_URL"] || "https://api.github.com";
export const client = new octokit.Octokit({
auth: core.getInput("token"),
baseUrl: githubAPIURL,
userAgent: "CodeQL Action",
log: consoleLogLevel({ level: "debug" })
});

View file

@ -1,12 +1,11 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as http from '@actions/http-client';
import * as auth from '@actions/http-client/auth';
import fileUrl from 'file-url'; import fileUrl from 'file-url';
import * as fs from 'fs'; import * as fs from 'fs';
import * as jsonschema from 'jsonschema'; import * as jsonschema from 'jsonschema';
import * as path from 'path'; import * as path from 'path';
import zlib from 'zlib'; import zlib from 'zlib';
import * as api from './api-client';
import * as fingerprints from './fingerprints'; import * as fingerprints from './fingerprints';
import * as sharedEnv from './shared-environment'; import * as sharedEnv from './shared-environment';
import * as util from './util'; import * as util from './util';
@ -45,10 +44,7 @@ async function uploadPayload(payload): Promise<boolean> {
return true; return true;
} }
const githubToken = core.getInput('token'); const [owner, repo] = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const ph: auth.BearerCredentialHandler = new auth.BearerCredentialHandler(githubToken);
const client = new http.HttpClient('Code Scanning : Upload SARIF', [ph]);
const url = 'https://api.github.com/repos/' + process.env['GITHUB_REPOSITORY'] + '/code-scanning/analysis';
// Make up to 4 attempts to upload, and sleep for these // Make up to 4 attempts to upload, and sleep for these
// number of seconds between each attempt. // number of seconds between each attempt.
@ -57,21 +53,25 @@ async function uploadPayload(payload): Promise<boolean> {
const backoffPeriods = [1, 5, 15]; const backoffPeriods = [1, 5, 15];
for (let attempt = 0; attempt <= backoffPeriods.length; attempt++) { for (let attempt = 0; attempt <= backoffPeriods.length; attempt++) {
const response = await api.client.request("PUT /repos/:owner/:repo/code-scanning/analysis", ({
owner: owner,
repo: repo,
data: payload,
}));
const res: http.HttpClientResponse = await client.put(url, payload); core.debug('response status: ' + response.status);
core.debug('response status: ' + res.message.statusCode);
const statusCode = res.message.statusCode; const statusCode = response.status;
if (statusCode === 202) { if (statusCode === 202) {
core.info("Successfully uploaded results"); core.info("Successfully uploaded results");
return true; return true;
} }
const requestID = res.message.headers["x-github-request-id"]; const requestID = response.headers["x-github-request-id"];
// On any other status code that's not 5xx mark the upload as failed // On any other status code that's not 5xx mark the upload as failed
if (!statusCode || statusCode < 500 || statusCode >= 600) { if (!statusCode || statusCode < 500 || statusCode >= 600) {
core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); core.setFailed('Upload failed (' + requestID + '): (' + statusCode + ') ' + response.data);
return false; return false;
} }
@ -80,7 +80,7 @@ async function uploadPayload(payload): Promise<boolean> {
// Log the failure as a warning but don't mark the action as failed yet // Log the failure as a warning but don't mark the action as failed yet
core.warning('Upload attempt (' + (attempt + 1) + ' of ' + (backoffPeriods.length + 1) + core.warning('Upload attempt (' + (attempt + 1) + ' of ' + (backoffPeriods.length + 1) +
') failed (' + requestID + '). Retrying in ' + backoffPeriods[attempt] + ') failed (' + requestID + '). Retrying in ' + backoffPeriods[attempt] +
' seconds: (' + statusCode + ') ' + await res.readBody()); ' seconds: (' + statusCode + ') ' + response.data);
// Sleep for the backoff period // Sleep for the backoff period
await new Promise(r => setTimeout(r, backoffPeriods[attempt] * 1000)); await new Promise(r => setTimeout(r, backoffPeriods[attempt] * 1000));
continue; continue;
@ -89,7 +89,7 @@ async function uploadPayload(payload): Promise<boolean> {
// If the upload fails with 5xx then we assume it is a temporary problem // If the upload fails with 5xx then we assume it is a temporary problem
// and not an error that the user has caused or can fix. // and not an error that the user has caused or can fix.
// We avoid marking the job as failed to avoid breaking CI workflows. // We avoid marking the job as failed to avoid breaking CI workflows.
core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + await res.readBody()); core.error('Upload failed (' + requestID + '): (' + statusCode + ') ' + response.data);
return false; return false;
} }
} }

View file

@ -1,13 +1,10 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as http from '@actions/http-client';
import * as auth from '@actions/http-client/auth';
import * as octokit from '@octokit/rest';
import consoleLogLevel from 'console-log-level';
import * as fs from "fs"; import * as fs from "fs";
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as api from './api-client';
import * as sharedEnv from './shared-environment'; import * as sharedEnv from './shared-environment';
/** /**
@ -68,12 +65,7 @@ async function getLanguagesInRepo(): Promise<string[]> {
let repo = repo_nwo[1]; let repo = repo_nwo[1];
core.debug(`GitHub repo ${owner} ${repo}`); core.debug(`GitHub repo ${owner} ${repo}`);
let ok = new octokit.Octokit({ const response = await api.client.request("GET /repos/:owner/:repo/languages", ({
auth: core.getInput('token'),
userAgent: "CodeQL Action",
log: consoleLogLevel({ level: "debug" })
});
const response = await ok.request("GET /repos/:owner/:repo/languages", ({
owner, owner,
repo repo
})); }));
@ -157,22 +149,16 @@ async function getWorkflowPath(): Promise<string> {
const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/"); const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/");
const owner = repo_nwo[0]; const owner = repo_nwo[0];
const repo = repo_nwo[1]; const repo = repo_nwo[1];
const run_id = getRequiredEnvParam('GITHUB_RUN_ID'); const run_id = Number(getRequiredEnvParam('GITHUB_RUN_ID'));
const ok = new octokit.Octokit({ const runsResponse = await api.client.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
auth: core.getInput('token'),
userAgent: "CodeQL Action",
log: consoleLogLevel({ level: 'debug' })
});
const runsResponse = await ok.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
owner, owner,
repo, repo,
run_id run_id
}); });
const workflowUrl = runsResponse.data.workflow_url; const workflowUrl = runsResponse.data.workflow_url;
const workflowResponse = await ok.request('GET ' + workflowUrl); const workflowResponse = await api.client.request('GET ' + workflowUrl);
return workflowResponse.data.path; return workflowResponse.data.path;
} }
@ -297,23 +283,21 @@ async function createStatusReport(
/** /**
* Send a status report to the code_scanning/analysis/status endpoint. * Send a status report to the code_scanning/analysis/status endpoint.
* *
* Returns the status code of the response to the status request, or * Returns the status code of the response to the status request.
* undefined if the given statusReport is undefined or no response was
* received.
*/ */
async function sendStatusReport(statusReport: StatusReport): Promise<number | undefined> { async function sendStatusReport(statusReport: StatusReport): Promise<number> {
const statusReportJSON = JSON.stringify(statusReport); const statusReportJSON = JSON.stringify(statusReport);
core.debug('Sending status report: ' + statusReportJSON); core.debug('Sending status report: ' + statusReportJSON);
const githubToken = core.getInput('token'); const nwo = getRequiredEnvParam("GITHUB_REPOSITORY");
const ph: auth.BearerCredentialHandler = new auth.BearerCredentialHandler(githubToken); const [owner, repo] = nwo.split("/");
const client = new http.HttpClient('Code Scanning : Status Report', [ph]); const statusResponse = await api.client.request('PUT /repos/:owner/:repo/code-scanning/analysis/status', {
const url = 'https://api.github.com/repos/' + process.env['GITHUB_REPOSITORY'] owner: owner,
+ '/code-scanning/analysis/status'; repo: repo,
const res: http.HttpClientResponse = await client.put(url, statusReportJSON); data: statusReportJSON,
});
return res.message?.statusCode; return statusResponse.status;
} }
/** /**