mirror of
https://github.com/github/codeql-action.git
synced 2026-12-01 19:32:49 +08:00
This PR ensures environment variables are set before any invocation of the CLI. Here is a list of vars that are set: https://github.com/github/codeql-coreql-team/issues/1124#issuecomment-852463521 This ensures the CLI knows the features and versions of the containing actions/runner. Additionally: - Fix the user agent so that it more closely aligns with user agent spec - Refactor environment variable initialization so that it all happens in one place and call. - Move Mode, getRequiredEnvParam, setMode, getMode out of actions-util and into util. actions-util is meant for utils only called by the action, not the runner. The `prepareLocalRunEnvironment()` method is most likely deprecated and should be removed. I originally added it because I had a way of working where I would run the action from my local machine to test out changes, but this was always a little flaky. So, I no longer use this way of working. I will probably remove it soon.
100 lines
2.1 KiB
TypeScript
100 lines
2.1 KiB
TypeScript
import * as githubUtils from "@actions/github/lib/utils";
|
|
import test, { ExecutionContext } from "ava";
|
|
import sinon from "sinon";
|
|
|
|
import { getApiClient } from "./api-client";
|
|
import { setupTests } from "./testing-utils";
|
|
import { Mode, initializeEnvironment } from "./util";
|
|
|
|
// eslint-disable-next-line import/no-commonjs
|
|
const pkg = require("../package.json");
|
|
|
|
setupTests(test);
|
|
|
|
let githubStub: sinon.SinonStub;
|
|
|
|
test.beforeEach(() => {
|
|
githubStub = sinon.stub(githubUtils, "GitHub");
|
|
initializeEnvironment(Mode.actions, pkg.version);
|
|
});
|
|
|
|
test("Get the client API", async (t) => {
|
|
doTest(
|
|
t,
|
|
{
|
|
auth: "xyz",
|
|
externalRepoAuth: "abc",
|
|
url: "http://hucairz",
|
|
},
|
|
undefined,
|
|
{
|
|
auth: "token xyz",
|
|
baseUrl: "http://hucairz/api/v3",
|
|
userAgent: `CodeQL-Action/${pkg.version}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
test("Get the client API external", async (t) => {
|
|
doTest(
|
|
t,
|
|
{
|
|
auth: "xyz",
|
|
externalRepoAuth: "abc",
|
|
url: "http://hucairz",
|
|
},
|
|
{ allowExternal: true },
|
|
{
|
|
auth: "token abc",
|
|
baseUrl: "http://hucairz/api/v3",
|
|
userAgent: `CodeQL-Action/${pkg.version}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
test("Get the client API external not present", async (t) => {
|
|
doTest(
|
|
t,
|
|
{
|
|
auth: "xyz",
|
|
url: "http://hucairz",
|
|
},
|
|
{ allowExternal: true },
|
|
{
|
|
auth: "token xyz",
|
|
baseUrl: "http://hucairz/api/v3",
|
|
userAgent: `CodeQL-Action/${pkg.version}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
test("Get the client API with github url", async (t) => {
|
|
doTest(
|
|
t,
|
|
{
|
|
auth: "xyz",
|
|
url: "https://github.com/some/invalid/url",
|
|
},
|
|
undefined,
|
|
{
|
|
auth: "token xyz",
|
|
baseUrl: "https://api.github.com",
|
|
userAgent: `CodeQL-Action/${pkg.version}`,
|
|
}
|
|
);
|
|
});
|
|
|
|
function doTest(
|
|
t: ExecutionContext<unknown>,
|
|
clientArgs: any,
|
|
clientOptions: any,
|
|
expected: any
|
|
) {
|
|
getApiClient(clientArgs, clientOptions);
|
|
|
|
const firstCallArgs = githubStub.args[0];
|
|
// log is a function, so we don't need to test for equality of it
|
|
delete firstCallArgs[0].log;
|
|
t.deepEqual(firstCallArgs, [expected]);
|
|
}
|