Add util.getFileContentsUsingAPI

This commit is contained in:
David Verdeguer
2020-08-20 17:24:39 +02:00
parent e9e2284547
commit 382c51457f
6 changed files with 178 additions and 5 deletions
Generated
+32
View File
@@ -378,4 +378,36 @@ function getCodeQLDatabasesDir() {
return path.resolve(getRequiredEnvParam('RUNNER_TEMP'), 'codeql_databases');
}
exports.getCodeQLDatabasesDir = getCodeQLDatabasesDir;
function fileDownloadError(file) {
return 'Error while trying to download `' + file + '`';
}
exports.fileDownloadError = fileDownloadError;
function fileIsADirectoryError(file) {
return '`' + file + '` is a directory';
}
exports.fileIsADirectoryError = fileIsADirectoryError;
async function getFileContentsUsingAPI(owner, repo, path, ref) {
const response = await api.getActionsApiClient(true).repos.getContents({
owner: owner,
repo: repo,
path: path,
ref: ref,
});
const file = [owner, repo, path].join('/') + '@' + ref;
if (response.status !== 200) {
throw new Error(fileDownloadError(file));
}
if (Array.isArray(response.data)) {
throw new Error(fileIsADirectoryError(file));
}
let fileContents;
if ("content" in response.data && response.data.content !== undefined) {
fileContents = response.data.content;
}
else {
throw new Error(fileDownloadError(file));
}
return Buffer.from(fileContents, 'base64').toString('binary');
}
exports.getFileContentsUsingAPI = getFileContentsUsingAPI;
//# sourceMappingURL=util.js.map
+1 -1
View File
File diff suppressed because one or more lines are too long
+53 -3
View File
@@ -1,7 +1,4 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
@@ -9,10 +6,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
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 github = __importStar(require("@actions/github"));
const ava_1 = __importDefault(require("ava"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const sinon_1 = __importDefault(require("sinon"));
const api = __importStar(require("./api-client"));
const testing_utils_1 = require("./testing-utils");
const util = __importStar(require("./util"));
testing_utils_1.setupTests(ava_1.default);
@@ -113,4 +116,51 @@ ava_1.default('getExtraOptionsEnvParam() fails on invalid JSON', t => {
t.throws(util.getExtraOptionsEnvParam);
process.env.CODEQL_ACTION_EXTRA_OPTIONS = origExtraOptions;
});
function mockGetContents(content, status, isDirectory = false) {
// Passing an auth token is required, so we just use a dummy value
let client = new github.GitHub('123');
const response = {
data: isDirectory ? [content] : content,
status: status
};
const spyGetContents = sinon_1.default.stub(client.repos, "getContents").resolves(response);
sinon_1.default.stub(api, "getApiClient").value(() => client);
return spyGetContents;
}
ava_1.default('getFileContentsUsingAPI() throws if the request does not succeed', async (t) => {
const spyGetContents = mockGetContents({}, 400);
try {
await util.getFileContentsUsingAPI('github', 'codeql-action', 'non-existing-file', 'main');
throw new Error('initConfig did not throw error');
}
catch (err) {
t.assert(spyGetContents.called);
t.deepEqual(err, new Error(util.fileDownloadError('github/codeql-action/non-existing-file@main')));
}
});
ava_1.default('getFileContentsUsingAPI() throws if the requested file is a directory', async (t) => {
const inputFileContents = `content content content`;
const dummyResponse = {
content: Buffer.from(inputFileContents).toString("base64"),
};
const spyGetContents = mockGetContents(dummyResponse, 200, true);
try {
await util.getFileContentsUsingAPI('github', 'codeql-action', 'non-existing-file', 'main');
throw new Error('initConfig did not throw error');
}
catch (err) {
t.assert(spyGetContents.called);
t.deepEqual(err, new Error(util.fileIsADirectoryError('github/codeql-action/non-existing-file@main')));
}
});
ava_1.default('getFileContentsUsingAPI() returns the right content', async (t) => {
const inputFileContents = `content content content`;
const dummyResponse = {
content: Buffer.from(inputFileContents).toString("base64"),
};
const spyGetContents = mockGetContents(dummyResponse, 200);
const content = await util.getFileContentsUsingAPI('github', 'codeql-action', 'non-existing-file', 'main');
t.deepEqual(content, inputFileContents);
t.assert(spyGetContents.called);
});
//# sourceMappingURL=util.test.js.map
+1 -1
View File
File diff suppressed because one or more lines are too long