mirror of
https://github.com/github/codeql-action.git
synced 2026-10-18 08:41:24 +00:00
Compare commits
7 Commits
default-se
...
mbg/use-gi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9e3972d38 | ||
|
|
1c350b1564 | ||
|
|
1772d44790 | ||
|
|
8884b5b423 | ||
|
|
b2bf75026d | ||
|
|
f2547e0e83 | ||
|
|
f728bb6678 |
17
.github/copilot-instructions.md
vendored
17
.github/copilot-instructions.md
vendored
@@ -11,12 +11,9 @@ The CodeQL Action is used in GitHub Actions workflows to run CodeQL scans using
|
||||
|
||||
## Review instructions
|
||||
|
||||
- Prioritise identifying critical issues, such as logic bugs, over comments on documentation or typos.
|
||||
- When wording review comments, be helpful and friendly. Assume that the PR author has written the code with the best of intentions. Never word suggestions as commands or using the imperative. Examples of what you should never write: "Preserve that non-fatal behaviour here" or "Please preserve that non-fatal behaviour here".
|
||||
- When writing feedback, assume that the PR author is competent and is able to understand whether the point you are making is valid or not, why you are raising it, and how to best address it. While you can suggest how to address a point you are making, don't phrase it as a requirement or assume that it is the only solution.
|
||||
- Always word your comments constructively as suggestions for improvements, for example: "It would make sense to ..." or "Could you add ...?" or "Would it make sense to ...?"
|
||||
- When wording review comments, be helpful and friendly. Assume that the PR author has written the code with the best of intentions. Word your comments constructively as suggestions for improvements. Do not word suggestions as commands.
|
||||
- If you want to comment on a change that you believe will fail a CI check, do not present the CI failure you expect as a fact. Instead, write that you think a change "may" lead to a failure in CI. Suggest that, if such a failure manifests, the changes you are commenting on may be the place responsible for the failure and are worth looking at.
|
||||
- If a suggestion you make is suitable for a follow-up, such as a refactoring that doesn't change the behaviour or fixing a typo in a comment, mention that it can be addressed in a later PR rather than blocking this one or prefix your comment with "Minor: ".
|
||||
- If a suggestion you make is suitable for a follow-up, such as a refactoring that doesn't change the behaviour or fixing a typo in a comment, mention that it can be addressed in a later PR rather than blocking this one.
|
||||
- If a change is a net improvement, for example because it improves on an existing limitation of existing code, do not complain about pre-existing problems that remain. You may comment on them, but you should make it clear that the thing you are commenting on is not new by writing e.g. "Not new in this PR, but [..]" followed by your description of the issue and a suggestion that it could be improved at the same time with e.g. "Consider whether this is worth addressing as part of this PR as well."
|
||||
|
||||
## Generated code
|
||||
@@ -25,16 +22,6 @@ The main codebase of the CodeQL Action is written in TypeScript and compiled to
|
||||
|
||||
GitHub Actions workflows in the `.github/workflows` directory whose filenames start with two underscores (e.g. `__all-platform-bundle.yml`) are automatically generated using the `pr-checks/sync.sh` script from template files in the `pr-checks/checks` directory. Therefore, you do not need to review files in the `.github/workflows` directory that start with two underscores. However, you should review changes to the `pr-checks` directory as well as workflows in the `.github/workflows` directory that do not start with underscores.
|
||||
|
||||
## Feature flags
|
||||
|
||||
Some functionality in the code may be gated behind feature flags, which are controlled server-side. If the functionality that is gated behind them is user-facing, then this should be documented in a change note at the time that the gated functionality is added. See `CHANGELOG.md` for existing notes that mention "experimental" for examples. These typically include information about when users may expect to see the feature rolled out.
|
||||
|
||||
When a feature flag that gates some functionality is removed, this does not need to be documented in a change note since the feature is already rolled out and the notes for the release which introduced the feature already describe it. The only exception is functionality which will start having an effect on GHES as a result of the feature flag removal:
|
||||
|
||||
- Most functionality gated behind feature flags does not affect GHES.
|
||||
- We can make functionality available on GHES by setting `defaultValue` to `true` first and waiting for this to have had an effect on GHES. No change note is needed if a feature flag is removed where `defaultValue` was `true`.
|
||||
- When removing a feature flag where `defaultValue` was `false` and the functionality starts to affect GHES, a change note may be appropriate.
|
||||
|
||||
## Release process
|
||||
|
||||
Changes that are merged to the `main` branch are not used in production straight away. Instead, changes from `main` must first be released using the release process, which has to be manually triggered by a staff member. Therefore, it is possible for further changes to be made to the code after some changes have been merged to `main` but before they are released. Once the release process automation is started, a series of PRs are automatically generated:
|
||||
|
||||
1
.github/workflows/__multi-language-autodetect.yml
generated
vendored
1
.github/workflows/__multi-language-autodetect.yml
generated
vendored
@@ -192,5 +192,6 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
env:
|
||||
CODEQL_ACTION_CLEANUP_TOOLCACHE_BUNDLES: true
|
||||
CODEQL_ACTION_RESOLVE_SUPPORTED_LANGUAGES_USING_CLI: true
|
||||
CODEQL_ACTION_TEST_MODE: true
|
||||
|
||||
12
.github/workflows/pr-checks.yml
vendored
12
.github/workflows/pr-checks.yml
vendored
@@ -2,9 +2,6 @@ name: PR Checks
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "main"
|
||||
- "releases/v*"
|
||||
pull_request:
|
||||
merge_group:
|
||||
types: [checks_requested]
|
||||
@@ -148,11 +145,12 @@ jobs:
|
||||
|
||||
- name: Fetch base commit
|
||||
id: fetch-base
|
||||
# Skip this (and subsequent steps) if the PR originates from a fork, since this workflow
|
||||
# won't have the necessary permissions to post comments on the PR.
|
||||
# Forks and Dependabot PRs don't have permission to write comments, so skip the repo size
|
||||
# check in those cases.
|
||||
if: >-
|
||||
github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name == github.repository
|
||||
github.event.pull_request.head.repo.full_name == github.repository &&
|
||||
github.event.pull_request.user.login != 'dependabot[bot]'
|
||||
env:
|
||||
BASE_SHA: ${{ github.event.pull_request.base.sha }}
|
||||
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
||||
@@ -211,7 +209,7 @@ jobs:
|
||||
if: >-
|
||||
github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.head.repo.full_name == github.repository &&
|
||||
github.actor != 'dependabot[bot]' &&
|
||||
github.event.pull_request.user.login != 'dependabot[bot]' &&
|
||||
needs.other-checks.result == 'success'
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
130
lib/entry-points.js
generated
130
lib/entry-points.js
generated
@@ -170632,6 +170632,7 @@ var import_undici = __toESM(require_undici());
|
||||
|
||||
// src/github.ts
|
||||
var GITHUB_DOTCOM_URL = "https://github.com";
|
||||
var GITHUB_DOTCOM_API_URL = "https://api.github.com";
|
||||
function parseGitHubUrl(inputUrl) {
|
||||
const originalUrl = inputUrl;
|
||||
if (inputUrl.indexOf("://") === -1) {
|
||||
@@ -170666,6 +170667,12 @@ function getCurrentInstance(env) {
|
||||
apiURL: env.getRequired("GITHUB_API_URL" /* GITHUB_API_URL */)
|
||||
};
|
||||
}
|
||||
function getDotcomInstance() {
|
||||
return {
|
||||
url: GITHUB_DOTCOM_URL,
|
||||
apiURL: GITHUB_DOTCOM_API_URL
|
||||
};
|
||||
}
|
||||
|
||||
// src/repository.ts
|
||||
function isRepositoryNwoStr(val) {
|
||||
@@ -170731,6 +170738,37 @@ function makeProxyRequestOptions(dispatcher) {
|
||||
}
|
||||
};
|
||||
}
|
||||
var InstanceApiClient = class {
|
||||
client;
|
||||
apiDetails;
|
||||
authorizationHeader;
|
||||
constructor(apiDetails, clientOptions = {}) {
|
||||
this.apiDetails = apiDetails;
|
||||
this.client = createApiClientWithDetails(apiDetails, clientOptions);
|
||||
const token = this.apiDetails.auth?.trim();
|
||||
if (token !== void 0 && token !== "") {
|
||||
this.authorizationHeader = `token ${token}`;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets a value for the `Authorization` header for a request to `url`; or `undefined` if the
|
||||
* `Authorization` header should not be set for `url`.
|
||||
*
|
||||
* @param logger The logger to use for debugging messages.
|
||||
* @param url The URL for which we want to add an `Authorization` header.
|
||||
*/
|
||||
getAuthorizationHeaderFor(logger2, url2) {
|
||||
if (this.authorizationHeader === void 0) {
|
||||
return void 0;
|
||||
}
|
||||
if (url2.startsWith(`${this.apiDetails.url}/`) || this.apiDetails.apiURL && url2.startsWith(`${this.apiDetails.apiURL}/`)) {
|
||||
logger2.debug(`Providing an authorization token.`);
|
||||
return this.authorizationHeader;
|
||||
}
|
||||
logger2.debug(`Not using an authorization token.`);
|
||||
return void 0;
|
||||
}
|
||||
};
|
||||
function createApiClientWithDetails(apiDetails, { allowExternal = false, proxy = void 0 } = {}) {
|
||||
const auth2 = allowExternal && apiDetails.externalRepoAuth !== void 0 ? apiDetails.externalRepoAuth : apiDetails.auth;
|
||||
const retryingOctokit = githubUtils.GitHub.plugin(retry);
|
||||
@@ -170761,21 +170799,19 @@ function getApiDetails(env = getEnv()) {
|
||||
function getApiClient(env = getEnv(), apiDetails) {
|
||||
return createApiClientWithDetails(apiDetails ?? getApiDetails(env));
|
||||
}
|
||||
function getDotcomApiDetails(env) {
|
||||
if (env.getRequired("GITHUB_SERVER_URL" /* GITHUB_SERVER_URL */) === GITHUB_DOTCOM_URL) {
|
||||
return getApiDetails(env);
|
||||
} else {
|
||||
return {
|
||||
auth: void 0,
|
||||
...getDotcomInstance()
|
||||
};
|
||||
}
|
||||
}
|
||||
function getApiClientWithExternalAuth(apiDetails, proxy) {
|
||||
return createApiClientWithDetails(apiDetails, { allowExternal: true, proxy });
|
||||
}
|
||||
function getAuthorizationHeaderFor(logger2, apiDetails, url2) {
|
||||
const token = apiDetails.auth?.trim();
|
||||
if (token === void 0 || token === "") {
|
||||
return void 0;
|
||||
}
|
||||
if (url2.startsWith(`${apiDetails.url}/`) || apiDetails.apiURL && url2.startsWith(`${apiDetails.apiURL}/`)) {
|
||||
logger2.debug(`Providing an authorization token.`);
|
||||
return `token ${token}`;
|
||||
}
|
||||
logger2.debug(`Not using an authorization token.`);
|
||||
return void 0;
|
||||
}
|
||||
var cachedGitHubVersion = void 0;
|
||||
async function getGitHubVersionFromApi(apiClient, apiDetails) {
|
||||
if (parseGitHubUrl(apiDetails.url) === GITHUB_DOTCOM_URL) {
|
||||
@@ -171932,6 +171968,11 @@ var featureConfig = {
|
||||
envVar: "CODEQL_ACTION_ALLOW_MULTIPLE_ANALYSIS_KINDS",
|
||||
minimumVersion: void 0
|
||||
},
|
||||
["cleanup_toolcache_bundles" /* CleanupToolcacheBundles */]: {
|
||||
defaultValue: false,
|
||||
envVar: "CODEQL_ACTION_CLEANUP_TOOLCACHE_BUNDLES",
|
||||
minimumVersion: void 0
|
||||
},
|
||||
["cleanup_trap_caches" /* CleanupTrapCaches */]: {
|
||||
defaultValue: false,
|
||||
envVar: "CODEQL_ACTION_CLEANUP_TRAP_CACHES",
|
||||
@@ -175776,38 +175817,38 @@ function getCodeQLActionRepository(action) {
|
||||
`The value of '${"GITHUB_ACTION_REPOSITORY" /* GITHUB_ACTION_REPOSITORY */}' is '${actionRepository}', which does not match the expected 'owner/repo' format.`
|
||||
);
|
||||
}
|
||||
var DEFAULT_DOWNLOAD_SOURCE = [
|
||||
GITHUB_DOTCOM_URL,
|
||||
CODEQL_DEFAULT_ACTION_REPOSITORY
|
||||
];
|
||||
var isSameDownloadSource = ([srcUrl, srcRepo], [otherUrl, otherRepo]) => {
|
||||
return srcUrl === otherUrl && srcRepo === otherRepo;
|
||||
var isSameDownloadSource = ([srcInstance, srcRepo], [otherInstance, otherRepo]) => {
|
||||
return srcInstance.url === otherInstance.url && srcRepo === otherRepo;
|
||||
};
|
||||
function getDefaultDotcomDownloadURL(tagName, assetName) {
|
||||
return `${GITHUB_DOTCOM_URL}/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${assetName}`;
|
||||
}
|
||||
async function getCodeQLAssetDownloadURL(action, apiDetails, tagName, assetName, assetKind = "CodeQL bundle") {
|
||||
const codeQLActionRepository = getCodeQLActionRepository(action);
|
||||
const defaultDownloadSource = [
|
||||
getDotcomApiDetails(action.env),
|
||||
CODEQL_DEFAULT_ACTION_REPOSITORY
|
||||
];
|
||||
const potentialDownloadSources = [
|
||||
// This GitHub instance, and this Action.
|
||||
[apiDetails.url, codeQLActionRepository],
|
||||
[apiDetails, codeQLActionRepository],
|
||||
// This GitHub instance, and the canonical Action.
|
||||
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
|
||||
[apiDetails, CODEQL_DEFAULT_ACTION_REPOSITORY],
|
||||
// GitHub.com, and the canonical Action.
|
||||
DEFAULT_DOWNLOAD_SOURCE
|
||||
defaultDownloadSource
|
||||
];
|
||||
const uniqueDownloadSources = potentialDownloadSources.filter(
|
||||
(source, index2, self2) => {
|
||||
return !self2.slice(0, index2).some((other) => isSameDownloadSource(source, other));
|
||||
}
|
||||
);
|
||||
for (const [instanceURL, repository] of uniqueDownloadSources) {
|
||||
if (isSameDownloadSource(DEFAULT_DOWNLOAD_SOURCE, [instanceURL, repository])) {
|
||||
for (const [instance8, repository] of uniqueDownloadSources) {
|
||||
if (isSameDownloadSource(defaultDownloadSource, [instance8, repository])) {
|
||||
break;
|
||||
}
|
||||
const { owner: repositoryOwner, repo: repositoryName } = parseRepositoryNwo(repository);
|
||||
try {
|
||||
const release2 = await getApiClient().rest.repos.getReleaseByTag({
|
||||
const release2 = await getApiClient(action.env, instance8).rest.repos.getReleaseByTag({
|
||||
owner: repositoryOwner,
|
||||
repo: repositoryName,
|
||||
tag: tagName
|
||||
@@ -175815,14 +175856,14 @@ async function getCodeQLAssetDownloadURL(action, apiDetails, tagName, assetName,
|
||||
for (const asset of release2.data.assets) {
|
||||
if (asset.name === assetName) {
|
||||
action.logger.info(
|
||||
`Found ${assetKind} ${assetName} in ${repository} on ${instanceURL} with URL ${asset.url}.`
|
||||
`Found ${assetKind} ${assetName} in ${repository} on ${instance8.apiURL} with URL ${asset.url}.`
|
||||
);
|
||||
return asset.url;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
action.logger.info(
|
||||
`Looked for ${assetKind} ${assetName} in ${repository} on ${instanceURL} but got error ${e}.`
|
||||
`Looked for ${assetKind} ${assetName} in ${repository} on ${instance8.apiURL} but got error ${e}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -176643,7 +176684,8 @@ async function getCodeQLSource(toolsInput, defaultCliVersion, rawLanguages, othe
|
||||
cliVersion: cliVersion2,
|
||||
compressionMethod,
|
||||
sourceType: "download",
|
||||
toolsVersion: resolvedVersion ?? "unknown"
|
||||
toolsVersion: resolvedVersion ?? "unknown",
|
||||
apiDetails
|
||||
};
|
||||
}
|
||||
async function tryGetFallbackToolcacheVersion(cliVersion2, tagName, logger2) {
|
||||
@@ -176657,7 +176699,7 @@ async function tryGetFallbackToolcacheVersion(cliVersion2, tagName, logger2) {
|
||||
);
|
||||
return fallbackVersion;
|
||||
}
|
||||
var downloadCodeQL = async function(source, apiDetails, tarVersion, tempDir, logger2) {
|
||||
var downloadCodeQL = async function(source, tarVersion, tempDir, logger2) {
|
||||
const { bundle, compressionMethod } = source;
|
||||
const codeqlURL = bundle.url;
|
||||
const parsedCodeQLURL = new URL(codeqlURL);
|
||||
@@ -176669,11 +176711,8 @@ var downloadCodeQL = async function(source, apiDetails, tarVersion, tempDir, log
|
||||
if (searchParams.has("token")) {
|
||||
logger2.debug("CodeQL tools URL contains an authorization token.");
|
||||
} else {
|
||||
authorization = getAuthorizationHeaderFor(
|
||||
logger2,
|
||||
apiDetails,
|
||||
codeqlURL
|
||||
);
|
||||
const client = new InstanceApiClient(source.apiDetails);
|
||||
authorization = client.getAuthorizationHeaderFor(logger2, codeqlURL);
|
||||
}
|
||||
const toolcacheDestination = getToolcacheDestination({ logger: logger2 }, source);
|
||||
const extractedBundlePath = toolcacheDestination.orElse(
|
||||
@@ -176724,6 +176763,7 @@ function getToolcacheDestination({ logger: logger2 }, source) {
|
||||
}
|
||||
async function tryDeleteToolcacheBundles({
|
||||
env,
|
||||
features,
|
||||
logger: logger2
|
||||
}) {
|
||||
if (env.getOptional("CODEQL_ACTION_HAS_SET_UP_CODEQL" /* HAS_SET_UP_CODEQL */) !== void 0) {
|
||||
@@ -176732,7 +176772,7 @@ async function tryDeleteToolcacheBundles({
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!isGitHubHostedRunner(env) || !isToolcacheOnWorkspaceFilesystem(logger2)) {
|
||||
if (!isGitHubHostedRunner() || !isToolcacheOnWorkspaceFilesystem(logger2) || !await features.getValue("cleanup_toolcache_bundles" /* CleanupToolcacheBundles */)) {
|
||||
return;
|
||||
}
|
||||
const result = await deleteToolcacheBundles({ env, logger: logger2 });
|
||||
@@ -176792,9 +176832,8 @@ async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defau
|
||||
break;
|
||||
case "download": {
|
||||
const result = await downloadCodeQLBundle(
|
||||
{ env: getEnv(), logger: logger2 },
|
||||
{ env: getEnv(), features, logger: logger2 },
|
||||
source,
|
||||
apiDetails,
|
||||
zstdAvailability.version,
|
||||
tempDir
|
||||
);
|
||||
@@ -176814,19 +176853,13 @@ async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defau
|
||||
toolsVersion: source.toolsVersion
|
||||
};
|
||||
}
|
||||
async function downloadCodeQLBundle(action, source, apiDetails, tarVersion, tempDir) {
|
||||
async function downloadCodeQLBundle(action, source, tarVersion, tempDir) {
|
||||
const { bundle } = source;
|
||||
const { logger: logger2 } = action;
|
||||
await tryDeleteToolcacheBundles(action);
|
||||
const startTime = import_perf_hooks4.performance.now();
|
||||
try {
|
||||
return await downloadCodeQL(
|
||||
source,
|
||||
apiDetails,
|
||||
tarVersion,
|
||||
tempDir,
|
||||
logger2
|
||||
);
|
||||
return await downloadCodeQL(source, tarVersion, tempDir, logger2);
|
||||
} catch (e) {
|
||||
if (bundle.kind !== "per-language" || bundle.combinedBundleURL === void 0 || asHTTPError(e)?.status !== 404) {
|
||||
throw e;
|
||||
@@ -176837,7 +176870,6 @@ async function downloadCodeQLBundle(action, source, apiDetails, tarVersion, temp
|
||||
...source,
|
||||
bundle: { kind: "combined", url: bundle.combinedBundleURL }
|
||||
},
|
||||
apiDetails,
|
||||
tarVersion,
|
||||
tempDir,
|
||||
logger2
|
||||
@@ -178495,8 +178527,8 @@ async function recordClearCleanupSizes(codeql, config, reports, logger2) {
|
||||
report.clear_cleanup_measurement_duration_ms = durationMs;
|
||||
}
|
||||
}
|
||||
async function uploadBundledDatabase(repositoryNwo, language, commitOid, bundledDb, bundledDbSize, apiDetails) {
|
||||
const client = getApiClient(getEnv(), apiDetails);
|
||||
async function uploadBundledDatabase(repositoryNwo, language, commitOid, bundledDb, bundledDbSize, apiDetails, env = getEnv()) {
|
||||
const client = getApiClient(env, apiDetails);
|
||||
const uploadsUrl = new URL(parseGitHubUrl(apiDetails.url));
|
||||
uploadsUrl.hostname = `uploads.${uploadsUrl.hostname}`;
|
||||
let uploadsBaseUrl = uploadsUrl.toString();
|
||||
@@ -187914,6 +187946,7 @@ async function getProxyBinaryPath(action, apiDetails) {
|
||||
const logger2 = action.logger;
|
||||
logger2.startGroup("Setting up authentication proxy");
|
||||
try {
|
||||
const client = new InstanceApiClient(apiDetails);
|
||||
const proxyFileName = getProxyFilename();
|
||||
const proxyInfo = await getDownloadUrl(action, apiDetails);
|
||||
logger2.info(
|
||||
@@ -187924,9 +187957,8 @@ async function getProxyBinaryPath(action, apiDetails) {
|
||||
logger2.info(
|
||||
`Authentication proxy is not in the toolcache. Will try to download it from '${proxyInfo.url}'.`
|
||||
);
|
||||
const authorization = getAuthorizationHeaderFor(
|
||||
const authorization = client.getAuthorizationHeaderFor(
|
||||
logger2,
|
||||
apiDetails,
|
||||
proxyInfo.url
|
||||
);
|
||||
const temp = await downloadProxy(logger2, proxyInfo.url, authorization);
|
||||
|
||||
@@ -14,6 +14,7 @@ operatingSystems:
|
||||
- stable-v2.21.4
|
||||
- stable-v2.22.4
|
||||
env:
|
||||
CODEQL_ACTION_CLEANUP_TOOLCACHE_BUNDLES: true
|
||||
CODEQL_ACTION_RESOLVE_SUPPORTED_LANGUAGES_USING_CLI: true
|
||||
installGo: true
|
||||
installDotNet: true
|
||||
|
||||
@@ -308,70 +308,45 @@ test.serial(
|
||||
test("getAuthorizationHeaderFor - returns undefined if there's no token", (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: undefined },
|
||||
`${GITHUB_DOTCOM_URL}/github`,
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: "" },
|
||||
`${GITHUB_DOTCOM_URL}/github`,
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
{ ...SAMPLE_DOTCOM_API_DETAILS, auth: " " },
|
||||
`${GITHUB_DOTCOM_URL}/github`,
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
for (const authValue of [undefined, "", " "]) {
|
||||
const client = new api.InstanceApiClient({
|
||||
...SAMPLE_DOTCOM_API_DETAILS,
|
||||
auth: authValue,
|
||||
});
|
||||
t.is(
|
||||
client.getAuthorizationHeaderFor(logger, `${GITHUB_DOTCOM_URL}/github`),
|
||||
undefined,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("getAuthorizationHeaderFor - returns header value with token if url is allowed", (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
const client = new api.InstanceApiClient({
|
||||
...SAMPLE_DOTCOM_API_DETAILS,
|
||||
});
|
||||
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
`${GITHUB_DOTCOM_URL}/github`,
|
||||
),
|
||||
`token ${SAMPLE_DOTCOM_API_DETAILS.auth}`,
|
||||
);
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
`${GITHUB_DOTCOM_API_URL}/github`,
|
||||
),
|
||||
`token ${SAMPLE_DOTCOM_API_DETAILS.auth}`,
|
||||
);
|
||||
for (const url of [
|
||||
`${GITHUB_DOTCOM_URL}/github`,
|
||||
`${GITHUB_DOTCOM_API_URL}/github`,
|
||||
]) {
|
||||
t.is(
|
||||
client.getAuthorizationHeaderFor(logger, url),
|
||||
`token ${SAMPLE_DOTCOM_API_DETAILS.auth}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("getAuthorizationHeaderFor - returns undefined if url is not allowed", (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
const client = new api.InstanceApiClient({
|
||||
...SAMPLE_DOTCOM_API_DETAILS,
|
||||
});
|
||||
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
`https://localhost/github`,
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
t.is(
|
||||
api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
`https://api.localhost/github`,
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
for (const url of [
|
||||
`https://localhost/github`,
|
||||
`https://api.localhost/github`,
|
||||
]) {
|
||||
t.is(client.getAuthorizationHeaderFor(logger, url), undefined);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -18,9 +18,11 @@ import {
|
||||
RegistryProxyVars,
|
||||
getEnv,
|
||||
exportEnvVar,
|
||||
ActionsEnvVars,
|
||||
} from "./environment";
|
||||
import {
|
||||
getCurrentInstance,
|
||||
getDotcomInstance,
|
||||
GITHUB_DOTCOM_URL,
|
||||
parseGitHubUrl,
|
||||
type GitHubApiCombinedDetails,
|
||||
@@ -120,6 +122,66 @@ export function makeProxyRequestOptions(
|
||||
/** The type of GitHub API client we use. */
|
||||
export type ApiClient = InstanceType<typeof githubUtils.GitHub>;
|
||||
|
||||
/**
|
||||
* Encapsulates a {@link GitHubApiCombinedDetails} along with operations which ensure that any credentials
|
||||
* are only used with the GitHub instance that is represented by the {@link GitHubApiCombinedDetails} value.
|
||||
*/
|
||||
export class InstanceApiClient {
|
||||
private readonly client: ApiClient;
|
||||
private readonly apiDetails: GitHubApiCombinedDetails;
|
||||
private readonly authorizationHeader: string | undefined;
|
||||
|
||||
constructor(
|
||||
apiDetails: GitHubApiCombinedDetails,
|
||||
clientOptions: CreateApiClientOptions = {},
|
||||
) {
|
||||
this.apiDetails = apiDetails;
|
||||
this.client = createApiClientWithDetails(apiDetails, clientOptions);
|
||||
|
||||
// Compute the Authorization header value if we have a token.
|
||||
const token = this.apiDetails.auth?.trim();
|
||||
if (token !== undefined && token !== "") {
|
||||
this.authorizationHeader = `token ${token}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In most cases, we use an {@link ApiClient} which automatically handles auth for requests
|
||||
* made with it and this method is not needed and should not be used in that setting.
|
||||
*
|
||||
* In some cases, we need to make requests that can't go through an {@link ApiClient}, but still
|
||||
* require auth. For example, this is the case when using the `toolcache` module to download
|
||||
* files. For these cases, this method gets a value for the `Authorization` header for a request
|
||||
* to `url`; or `undefined` if the `Authorization` header should not be set for `url`.
|
||||
*
|
||||
* @param logger The logger to use for debugging messages.
|
||||
* @param url The URL for which we want to add an `Authorization` header.
|
||||
*/
|
||||
public getAuthorizationHeaderFor(
|
||||
logger: Logger,
|
||||
url: string,
|
||||
): string | undefined {
|
||||
// We don't have to check the `url` if we don't have a token to begin with.
|
||||
if (this.authorizationHeader === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// We only want to provide an authorization header if we are downloading
|
||||
// from the same GitHub instance the Action is running on.
|
||||
// This avoids leaking Enterprise tokens to dotcom.
|
||||
if (
|
||||
url.startsWith(`${this.apiDetails.url}/`) ||
|
||||
(this.apiDetails.apiURL && url.startsWith(`${this.apiDetails.apiURL}/`))
|
||||
) {
|
||||
logger.debug(`Providing an authorization token.`);
|
||||
return this.authorizationHeader;
|
||||
}
|
||||
|
||||
logger.debug(`Not using an authorization token.`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/** Options for `createApiClientWithDetails`. */
|
||||
interface CreateApiClientOptions {
|
||||
allowExternal?: boolean;
|
||||
@@ -172,8 +234,8 @@ export function getApiDetails(env: ReadOnlyEnv = getEnv()): GitHubApiDetails {
|
||||
/**
|
||||
* Constructs an {@link ApiClient} using {@link apiDetails}.
|
||||
*
|
||||
* If {@link apiDetails} is not specified, then the GitHub instance URLs and auth token
|
||||
* are taken from {@link env}.
|
||||
* If {@link apiDetails} is not specified, then the GitHub instance URLs are taken from {@link env}
|
||||
* and the auth token is taken from the `token` input.
|
||||
*
|
||||
* If {@link env} is not specified, then the process environment is used.
|
||||
*
|
||||
@@ -187,6 +249,28 @@ export function getApiClient(
|
||||
return createApiClientWithDetails(apiDetails ?? getApiDetails(env));
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets a `GitHubApiDetails` object describing the GitHub Dotcom instance.
|
||||
*
|
||||
* @param env The environment variables to read.
|
||||
*/
|
||||
export function getDotcomApiDetails(env: ReadOnlyEnv): GitHubApiDetails {
|
||||
// If we are currently running on dotcom, then just call `getApiDetails` which will
|
||||
// construct the appropriate `GitHubApiDetails` from the environment and include the
|
||||
// value of the `token` input. The `token` input relates to the GitHub instance that
|
||||
// we are running on only. Therefore, if we are not currently running on dotcom,
|
||||
// then we construct `GitHubApiDetails` without the `token` value and using the
|
||||
// hard-coded instance and API URLs.
|
||||
if (env.getRequired(ActionsEnvVars.GITHUB_SERVER_URL) === GITHUB_DOTCOM_URL) {
|
||||
return getApiDetails(env);
|
||||
} else {
|
||||
return {
|
||||
auth: undefined,
|
||||
...getDotcomInstance(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function getApiClientWithExternalAuth(
|
||||
apiDetails: GitHubApiCombinedDetails,
|
||||
proxy?: ProxyAgent,
|
||||
@@ -194,42 +278,6 @@ export function getApiClientWithExternalAuth(
|
||||
return createApiClientWithDetails(apiDetails, { allowExternal: true, proxy });
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value for the `Authorization` header for a request to `url`; or `undefined` if the
|
||||
* `Authorization` header should not be set for `url`.
|
||||
*
|
||||
* @param logger The logger to use for debugging messages.
|
||||
* @param apiDetails Details of the GitHub API we are using.
|
||||
* @param url The URL for which we want to add an `Authorization` header.
|
||||
*
|
||||
* @returns The value for the `Authorization` header or `undefined` if it shouldn't be populated.
|
||||
*/
|
||||
export function getAuthorizationHeaderFor(
|
||||
logger: Logger,
|
||||
apiDetails: GitHubApiDetails,
|
||||
url: string,
|
||||
): string | undefined {
|
||||
// We don't have to check the `url` if we don't have a token to begin with.
|
||||
const token = apiDetails.auth?.trim();
|
||||
if (token === undefined || token === "") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// We only want to provide an authorization header if we are downloading
|
||||
// from the same GitHub instance the Action is running on.
|
||||
// This avoids leaking Enterprise tokens to dotcom.
|
||||
if (
|
||||
url.startsWith(`${apiDetails.url}/`) ||
|
||||
(apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`))
|
||||
) {
|
||||
logger.debug(`Providing an authorization token.`);
|
||||
return `token ${token}`;
|
||||
}
|
||||
|
||||
logger.debug(`Not using an authorization token.`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let cachedGitHubVersion: GitHubVersion | undefined = undefined;
|
||||
|
||||
export async function getGitHubVersionFromApi(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { GitHubApiDetails } from "./github";
|
||||
import { BuiltInLanguage, parseBuiltInLanguage } from "./languages";
|
||||
import { BundlePlatform } from "./platform";
|
||||
import type { CompressionMethod } from "./tar";
|
||||
@@ -17,6 +18,8 @@ export type CodeQLBundle =
|
||||
export interface CodeQLDownloadSource {
|
||||
/** Distinguishes downloads from local archives and cached installations. */
|
||||
sourceType: "download";
|
||||
/** Information about the GitHub instance that contains the asset. */
|
||||
apiDetails: GitHubApiDetails;
|
||||
/** The bundle to download. */
|
||||
bundle: CodeQLBundle;
|
||||
/** The compression format of the bundle archive. */
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
makeVersionInfo,
|
||||
createTestConfig,
|
||||
makeMacro,
|
||||
mockApiDetails,
|
||||
} from "./testing-utils";
|
||||
import { ToolsDownloadStatusReport } from "./tools-download";
|
||||
import * as util from "./util";
|
||||
@@ -126,19 +127,6 @@ function mockReleaseApi({
|
||||
});
|
||||
}
|
||||
|
||||
function mockApiDetails(apiDetails: Required<GitHubApiDetails>) {
|
||||
// This is a workaround to mock `api.getApiDetails()` since it doesn't seem to be possible to
|
||||
// mock this directly. The difficulty is that `getApiDetails()` is called locally in
|
||||
// `api-client.ts`, but `sinon.stub(api, "getApiDetails")` only affects calls to
|
||||
// `getApiDetails()` via an imported `api` module.
|
||||
sinon
|
||||
.stub(actionsUtil, "getRequiredInput")
|
||||
.withArgs("token")
|
||||
.returns(apiDetails.auth);
|
||||
process.env["GITHUB_SERVER_URL"] = apiDetails.url;
|
||||
process.env["GITHUB_API_URL"] = apiDetails.apiURL || "";
|
||||
}
|
||||
|
||||
async function stubCodeql(): Promise<codeql.CodeQL> {
|
||||
const codeqlObject = await codeql.getCodeQLForTesting();
|
||||
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6"));
|
||||
@@ -290,6 +278,7 @@ for (const { cliVersion, tagName, expectedToolcacheVersion } of [
|
||||
async (t) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
const result = await installIntoToolcache({
|
||||
cliVersion,
|
||||
@@ -470,6 +459,7 @@ test.serial(
|
||||
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
await installIntoToolcache({
|
||||
tagName: "codeql-bundle-20200601",
|
||||
@@ -517,6 +507,7 @@ test.serial(
|
||||
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
await installIntoToolcache({
|
||||
tagName: "codeql-bundle-20200601",
|
||||
|
||||
@@ -599,6 +599,7 @@ test.serial(
|
||||
const nwo: RepositoryNwo = { owner: "test", repo: "foo" };
|
||||
const language: BuiltInLanguage = BuiltInLanguage.java;
|
||||
|
||||
const env = getTestEnv();
|
||||
await t.notThrowsAsync(async () => {
|
||||
await uploadBundledDatabase(
|
||||
nwo,
|
||||
@@ -607,11 +608,14 @@ test.serial(
|
||||
dbPath,
|
||||
content.length,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
env,
|
||||
);
|
||||
});
|
||||
|
||||
// Check that our stubs were called and that they received the expected arguments.
|
||||
t.is(getApiClient.callCount, 1);
|
||||
t.deepEqual(getApiClient.args[0], [env, SAMPLE_DOTCOM_API_DETAILS]);
|
||||
|
||||
t.is(fetchStub.callCount, 1);
|
||||
|
||||
const args = fetchStub.args[0];
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from "./api-client";
|
||||
import { type CodeQL } from "./codeql";
|
||||
import { Config } from "./config-utils";
|
||||
import type { ReadOnlyEnv } from "./environment";
|
||||
import { Feature } from "./feature-flags";
|
||||
import * as gitUtils from "./git-utils";
|
||||
import { parseGitHubUrl } from "./github";
|
||||
@@ -266,11 +267,12 @@ export async function uploadBundledDatabase(
|
||||
bundledDb: string,
|
||||
bundledDbSize: number,
|
||||
apiDetails: GitHubApiDetails,
|
||||
env: ReadOnlyEnv = util.getEnv(),
|
||||
): Promise<number> {
|
||||
// Ensure that the `apiDetails` that `getApiClient` uses match the ones we were given here,
|
||||
// because we use `apiDetails.auth` below which must be used with the same instance that
|
||||
// `apiDetails` refers to.
|
||||
const client = getApiClient(util.getEnv(), apiDetails);
|
||||
// It is important that the `apiDetails` that `getApiClient` uses match the ones we were given,
|
||||
// because we use `apiDetails.url` below to construct the URL to upload the database to. That
|
||||
// URL will be specific to the GitHub instance given by `apiDetails` and so the client must match it.
|
||||
const client = getApiClient(env, apiDetails);
|
||||
|
||||
const uploadsUrl = new URL(parseGitHubUrl(apiDetails.url));
|
||||
uploadsUrl.hostname = `uploads.${uploadsUrl.hostname}`;
|
||||
|
||||
@@ -74,6 +74,11 @@ export enum Feature {
|
||||
AllowMergeConfigFiles = "allow_merge_config_files",
|
||||
/** Controls whether we allow multiple values for the `analysis-kinds` input. */
|
||||
AllowMultipleAnalysisKinds = "allow_multiple_analysis_kinds",
|
||||
/**
|
||||
* Controls whether we delete CodeQL bundles that we are not going to use from the toolcache
|
||||
* before downloading a different bundle, in order to reclaim disk space.
|
||||
*/
|
||||
CleanupToolcacheBundles = "cleanup_toolcache_bundles",
|
||||
CleanupTrapCaches = "cleanup_trap_caches",
|
||||
/** Whether to allow the `config-file` input to be specified via a repository property. */
|
||||
ConfigFileRepositoryProperty = "config_file_repository_property",
|
||||
@@ -216,6 +221,11 @@ export const featureConfig = {
|
||||
envVar: "CODEQL_ACTION_ALLOW_MULTIPLE_ANALYSIS_KINDS",
|
||||
minimumVersion: undefined,
|
||||
},
|
||||
[Feature.CleanupToolcacheBundles]: {
|
||||
defaultValue: false,
|
||||
envVar: "CODEQL_ACTION_CLEANUP_TOOLCACHE_BUNDLES",
|
||||
minimumVersion: undefined,
|
||||
},
|
||||
[Feature.CleanupTrapCaches]: {
|
||||
defaultValue: false,
|
||||
envVar: "CODEQL_ACTION_CLEANUP_TRAP_CACHES",
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
getTestEnv,
|
||||
initAllState,
|
||||
makeMacro,
|
||||
mockApiDetails,
|
||||
mockBundleDownloadApi,
|
||||
setupActionsVars,
|
||||
setupTests,
|
||||
@@ -155,6 +156,7 @@ test.serial(
|
||||
compressionMethod: "gzip",
|
||||
sourceType: "download",
|
||||
toolsVersion: "1.2.3",
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
} satisfies setupCodeql.CodeQLDownloadSource);
|
||||
});
|
||||
},
|
||||
@@ -221,6 +223,7 @@ for (const {
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const source = await setupCodeql.getCodeQLSource(
|
||||
"linked",
|
||||
SAMPLE_DEFAULT_CLI_VERSION,
|
||||
@@ -255,6 +258,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const source = await setupCodeql.getCodeQLSource(
|
||||
"latest",
|
||||
SAMPLE_DEFAULT_CLI_VERSION,
|
||||
@@ -298,6 +302,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const result = await setupCodeql.setupCodeQLBundle(
|
||||
"linked",
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
@@ -431,6 +436,7 @@ test.serial(
|
||||
compressionMethod: "zstd",
|
||||
sourceType: "download",
|
||||
toolsVersion: expectedVersion,
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
} satisfies setupCodeql.CodeQLToolsSource);
|
||||
|
||||
const result = await setupCodeql.setupCodeQLBundle(
|
||||
@@ -479,6 +485,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir, { GITHUB_EVENT_NAME: "dynamic" });
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
const source = await setupCodeql.getCodeQLSource(
|
||||
undefined,
|
||||
@@ -503,6 +510,7 @@ test.serial(
|
||||
compressionMethod: "zstd",
|
||||
sourceType: "download",
|
||||
toolsVersion: expectedVersion,
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
} satisfies setupCodeql.CodeQLToolsSource);
|
||||
|
||||
// Afterwards, ensure that we see the expected messages in the log.
|
||||
@@ -596,7 +604,7 @@ test.serial(
|
||||
undefined, // rawLanguages
|
||||
undefined, // otherLanguagePacksReason
|
||||
false, // useOverlayAwareDefaultCliVersion
|
||||
createFeatures([]),
|
||||
createFeatures([Feature.CleanupToolcacheBundles]),
|
||||
getRunnerLogger(true),
|
||||
);
|
||||
|
||||
@@ -624,6 +632,7 @@ for (const toolsInput of ["nightly", "nightly-latest"]) {
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const source = await setupCodeql.getCodeQLSource(
|
||||
toolsInput,
|
||||
SAMPLE_DEFAULT_CLI_VERSION,
|
||||
@@ -649,6 +658,7 @@ for (const toolsInput of ["nightly", "nightly-latest"]) {
|
||||
cliVersion: undefined,
|
||||
compressionMethod: "zstd",
|
||||
toolsVersion: "0.0.0-30260213",
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
} satisfies setupCodeql.CodeQLDownloadSource);
|
||||
t.true(latestNightlyRequest.calledOnce);
|
||||
});
|
||||
@@ -801,6 +811,7 @@ for (const date of ["20200101", "30260213"]) {
|
||||
cliVersion: undefined,
|
||||
compressionMethod: "zstd",
|
||||
toolsVersion: `0.0.0-${date}`,
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
} satisfies setupCodeql.CodeQLDownloadSource);
|
||||
|
||||
await t.throwsAsync(
|
||||
@@ -945,6 +956,7 @@ const toolcacheInputFallbackMacro = makeMacro({
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
for (const [k, v] of Object.entries(environment)) {
|
||||
process.env[k] = v;
|
||||
@@ -1165,6 +1177,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const source = await setupCodeql.getCodeQLSource(
|
||||
undefined,
|
||||
PER_LANGUAGE_CLI_VERSION,
|
||||
@@ -1207,6 +1220,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
for (const { otherLanguagePacksReason, features } of [
|
||||
// The per-language feature is disabled.
|
||||
{ otherLanguagePacksReason: undefined, features: createFeatures([]) },
|
||||
@@ -1276,7 +1290,14 @@ for (const fallback of [false, true]) {
|
||||
request: { fetch: fetchRelease },
|
||||
});
|
||||
sinon.stub(api, "getApiClient").value(() => client);
|
||||
const authorizationSpy = sinon.spy(api, "getAuthorizationHeaderFor");
|
||||
|
||||
const spyClient = new api.InstanceApiClient(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const authorizationSpy = sinon.spy(
|
||||
spyClient,
|
||||
"getAuthorizationHeaderFor",
|
||||
);
|
||||
sinon.stub(api, "InstanceApiClient").returns(spyClient);
|
||||
|
||||
const extractStub = stubDownloadAndExtract();
|
||||
if (fallback) {
|
||||
extractStub.onFirstCall().rejects(new HTTPError("Not Found", 404));
|
||||
@@ -1286,6 +1307,7 @@ for (const fallback of [false, true]) {
|
||||
setupActionsVars(tmpDir, tmpDir, {
|
||||
GITHUB_ACTION_REPOSITORY: "codeql-testing/action-fork",
|
||||
});
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
const result = await setupCodeql.setupCodeQLBundle(
|
||||
undefined,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
@@ -1308,9 +1330,9 @@ for (const fallback of [false, true]) {
|
||||
t.is(extractStub.firstCall.args[0], assetURL);
|
||||
t.is(extractStub.lastCall.args[0], fallback ? combinedURL : assetURL);
|
||||
t.is(authorizationSpy.callCount, extractStub.callCount);
|
||||
t.is(authorizationSpy.firstCall.args[2], assetURL);
|
||||
t.is(authorizationSpy.firstCall.args[1], assetURL);
|
||||
t.is(
|
||||
authorizationSpy.lastCall.args[2],
|
||||
authorizationSpy.lastCall.args[1],
|
||||
fallback ? combinedURL : assetURL,
|
||||
);
|
||||
t.is(extractStub.lastCall.args[3], "token token");
|
||||
@@ -1375,7 +1397,10 @@ for (const bundle of ["per-language", "combined", "fallback"] as const) {
|
||||
};
|
||||
});
|
||||
const addDiagnostic = sinon.stub(diagnostics, "addNoLanguageDiagnostic");
|
||||
const features = createFeatures([Feature.PerLanguageBundles]);
|
||||
const features = createFeatures([
|
||||
Feature.PerLanguageBundles,
|
||||
Feature.CleanupToolcacheBundles,
|
||||
]);
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
@@ -1550,6 +1575,7 @@ for (const error of [
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
await t.throwsAsync(
|
||||
setupCodeql.setupCodeQLBundle(
|
||||
undefined,
|
||||
@@ -1823,15 +1849,11 @@ function createToolcacheEntry(
|
||||
*/
|
||||
async function runDownloadCodeQL(
|
||||
toolcacheRoot: string,
|
||||
features: Feature[],
|
||||
bundleVersion: string | undefined,
|
||||
{
|
||||
env = getTestEnv({
|
||||
[ActionsEnvVars.RUNNER_ENVIRONMENT]: "github-hosted",
|
||||
[ActionsEnvVars.RUNNER_TOOL_CACHE]: toolcacheRoot,
|
||||
}),
|
||||
}: {
|
||||
env?: Env;
|
||||
} = {},
|
||||
env = getTestEnv({
|
||||
[ActionsEnvVars.RUNNER_TOOL_CACHE]: toolcacheRoot,
|
||||
}),
|
||||
): Promise<{
|
||||
codeqlFolder: string;
|
||||
cleanupDiagnostic: toolsDownload.ToolcacheCleanupResult | undefined;
|
||||
@@ -1842,6 +1864,7 @@ async function runDownloadCodeQL(
|
||||
const { codeqlFolder } = await setupCodeql.downloadCodeQLBundle(
|
||||
initAllState({
|
||||
env,
|
||||
features: createFeatures(features),
|
||||
}),
|
||||
{
|
||||
bundle: {
|
||||
@@ -1853,8 +1876,8 @@ async function runDownloadCodeQL(
|
||||
cliVersion: CLEANUP_CLI_VERSION,
|
||||
sourceType: "download",
|
||||
toolsVersion: CLEANUP_CLI_VERSION,
|
||||
apiDetails: SAMPLE_DOTCOM_API_DETAILS,
|
||||
},
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
undefined, // tarVersion
|
||||
toolcacheRoot, // tempDir
|
||||
);
|
||||
@@ -1879,9 +1902,11 @@ async function runDownloadCodeQL(
|
||||
async function testToolcacheCleanup(
|
||||
t: ExecutionContext<unknown>,
|
||||
{
|
||||
features,
|
||||
runnerEnvironment,
|
||||
setUp,
|
||||
}: {
|
||||
features: Feature[];
|
||||
runnerEnvironment: string | undefined;
|
||||
setUp?: (env: Env) => void;
|
||||
},
|
||||
@@ -1893,8 +1918,12 @@ async function testToolcacheCleanup(
|
||||
) {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
if (runnerEnvironment === undefined) {
|
||||
delete process.env[ActionsEnvVars.RUNNER_ENVIRONMENT];
|
||||
} else {
|
||||
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = runnerEnvironment;
|
||||
}
|
||||
const env = getTestEnv({
|
||||
[ActionsEnvVars.RUNNER_ENVIRONMENT]: runnerEnvironment,
|
||||
[ActionsEnvVars.RUNNER_TOOL_CACHE]: tmpDir,
|
||||
});
|
||||
setUp?.(env);
|
||||
@@ -1914,8 +1943,9 @@ async function testToolcacheCleanup(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
features,
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
{ env },
|
||||
env,
|
||||
);
|
||||
|
||||
t.true(
|
||||
@@ -1927,12 +1957,28 @@ async function testToolcacheCleanup(
|
||||
});
|
||||
}
|
||||
|
||||
test.serial(
|
||||
"downloadCodeQLBundle does not clean up the toolcache when the feature flag is disabled",
|
||||
async (t) => {
|
||||
await testToolcacheCleanup(
|
||||
t,
|
||||
{ features: [], runnerEnvironment: "github-hosted" },
|
||||
({ cleanupDiagnostic, destinationDirectory, staleDirectory }) => {
|
||||
t.true(fs.existsSync(staleDirectory));
|
||||
t.true(fs.existsSync(destinationDirectory));
|
||||
t.is(cleanupDiagnostic, undefined);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test.serial(
|
||||
"downloadCodeQLBundle does not clean up the toolcache when the runner is not GitHub-hosted",
|
||||
async (t) => {
|
||||
await testToolcacheCleanup(
|
||||
t,
|
||||
{
|
||||
features: [Feature.CleanupToolcacheBundles],
|
||||
runnerEnvironment: "self-hosted",
|
||||
},
|
||||
({ cleanupDiagnostic, destinationDirectory, staleDirectory }) => {
|
||||
@@ -1952,6 +1998,7 @@ test.serial(
|
||||
await testToolcacheCleanup(
|
||||
t,
|
||||
{
|
||||
features: [Feature.CleanupToolcacheBundles],
|
||||
runnerEnvironment: undefined,
|
||||
},
|
||||
({ cleanupDiagnostic, destinationDirectory, staleDirectory }) => {
|
||||
@@ -1964,11 +2011,12 @@ test.serial(
|
||||
);
|
||||
|
||||
test.serial(
|
||||
"downloadCodeQLBundle deletes other CodeQL bundles from the toolcache on a GitHub-hosted runner",
|
||||
"downloadCodeQLBundle deletes other CodeQL bundles from the toolcache when enabled on a GitHub-hosted runner",
|
||||
async (t) => {
|
||||
await testToolcacheCleanup(
|
||||
t,
|
||||
{
|
||||
features: [Feature.CleanupToolcacheBundles],
|
||||
runnerEnvironment: "github-hosted",
|
||||
},
|
||||
({ cleanupDiagnostic, destinationDirectory, staleDirectory }) => {
|
||||
@@ -2002,6 +2050,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
@@ -2031,6 +2080,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
@@ -2162,6 +2212,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
toolcacheRoot,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
@@ -2184,6 +2235,7 @@ test.serial(
|
||||
await testToolcacheCleanup(
|
||||
t,
|
||||
{
|
||||
features: [Feature.CleanupToolcacheBundles],
|
||||
runnerEnvironment: "github-hosted",
|
||||
setUp: (env) => {
|
||||
env.set(EnvVar.HAS_SET_UP_CODEQL, "true");
|
||||
@@ -2213,13 +2265,9 @@ test.serial(
|
||||
);
|
||||
const { codeqlFolder, cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
{
|
||||
env: getTestEnv({
|
||||
[ActionsEnvVars.RUNNER_ENVIRONMENT]: "github-hosted",
|
||||
[EnvVar.HAS_SET_UP_CODEQL]: "true",
|
||||
}),
|
||||
},
|
||||
getTestEnv({ [EnvVar.HAS_SET_UP_CODEQL]: "true" }),
|
||||
);
|
||||
|
||||
t.true(fs.existsSync(staleDirectory));
|
||||
@@ -2234,6 +2282,7 @@ test.serial(
|
||||
async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
delete process.env[EnvVar.HAS_SET_UP_CODEQL];
|
||||
|
||||
stubDownloadAndExtract();
|
||||
@@ -2269,6 +2318,7 @@ test.serial(
|
||||
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
setupActionsVars(tmpDir, tmpDir);
|
||||
mockApiDetails(SAMPLE_DOTCOM_API_DETAILS);
|
||||
process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted";
|
||||
delete process.env[EnvVar.HAS_SET_UP_CODEQL];
|
||||
const staleDirectory = createToolcacheEntry(
|
||||
@@ -2287,7 +2337,7 @@ test.serial(
|
||||
undefined, // rawLanguages
|
||||
undefined, // otherLanguagePacksReason
|
||||
false, // useOverlayAwareDefaultCliVersion
|
||||
createFeatures([]),
|
||||
createFeatures([Feature.CleanupToolcacheBundles]),
|
||||
getRunnerLogger(true),
|
||||
),
|
||||
{ is: error },
|
||||
@@ -2322,6 +2372,7 @@ test.serial(
|
||||
|
||||
const { codeqlFolder, cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
undefined, // bundleVersion
|
||||
);
|
||||
|
||||
@@ -2355,6 +2406,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
@@ -2390,6 +2442,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
tmpDir,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
@@ -2439,6 +2492,7 @@ test.serial(
|
||||
|
||||
const { cleanupDiagnostic } = await runDownloadCodeQL(
|
||||
toolcacheRoot,
|
||||
[Feature.CleanupToolcacheBundles],
|
||||
CLEANUP_BUNDLE_VERSION,
|
||||
);
|
||||
|
||||
|
||||
@@ -718,6 +718,7 @@ export async function getCodeQLSource(
|
||||
compressionMethod,
|
||||
sourceType: "download",
|
||||
toolsVersion: resolvedVersion ?? "unknown",
|
||||
apiDetails,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -746,7 +747,6 @@ async function tryGetFallbackToolcacheVersion(
|
||||
// be able to stub this function and have other functions in this file use that stub.
|
||||
export const downloadCodeQL = async function (
|
||||
source: CodeQLDownloadSource,
|
||||
apiDetails: api.GitHubApiDetails,
|
||||
tarVersion: tar.TarVersion | undefined,
|
||||
tempDir: string,
|
||||
logger: Logger,
|
||||
@@ -767,11 +767,8 @@ export const downloadCodeQL = async function (
|
||||
if (searchParams.has("token")) {
|
||||
logger.debug("CodeQL tools URL contains an authorization token.");
|
||||
} else {
|
||||
authorization = api.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
apiDetails,
|
||||
codeqlURL,
|
||||
);
|
||||
const client = new api.InstanceApiClient(source.apiDetails);
|
||||
authorization = client.getAuthorizationHeaderFor(logger, codeqlURL);
|
||||
}
|
||||
|
||||
const toolcacheDestination = getToolcacheDestination({ logger }, source);
|
||||
@@ -839,7 +836,7 @@ function getToolcacheDestination(
|
||||
}
|
||||
|
||||
/**
|
||||
* Reclaims disk space by deleting the CodeQL tools from the toolcache on GitHub-hosted runners.
|
||||
* Reclaims disk space by deleting the CodeQL tools from the toolcache, if enabled.
|
||||
*
|
||||
* On GitHub-hosted runners the toolcache shares a filesystem with the workspace, so tools left in
|
||||
* the toolcache take up space that the analysis could use instead. This holds wherever we extract
|
||||
@@ -847,8 +844,9 @@ function getToolcacheDestination(
|
||||
*/
|
||||
async function tryDeleteToolcacheBundles({
|
||||
env,
|
||||
features,
|
||||
logger,
|
||||
}: ActionState<["Logger", "ReadOnlyEnv"]>): Promise<void> {
|
||||
}: ActionState<["Logger", "ReadOnlyEnv", "FeatureFlags"]>): Promise<void> {
|
||||
// A step that has already set up CodeQL may hand out a path into the toolcache that a later step
|
||||
// runs, so only the first step to set it up can know that nothing else relies on the toolcache.
|
||||
if (env.getOptional(EnvVar.HAS_SET_UP_CODEQL) !== undefined) {
|
||||
@@ -859,7 +857,11 @@ async function tryDeleteToolcacheBundles({
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isGitHubHostedRunner(env) || !isToolcacheOnWorkspaceFilesystem(logger)) {
|
||||
if (
|
||||
!isGitHubHostedRunner() ||
|
||||
!isToolcacheOnWorkspaceFilesystem(logger) ||
|
||||
!(await features.getValue(Feature.CleanupToolcacheBundles))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -978,9 +980,8 @@ export async function setupCodeQLBundle(
|
||||
break;
|
||||
case "download": {
|
||||
const result = await downloadCodeQLBundle(
|
||||
{ env: getEnv(), logger },
|
||||
{ env: getEnv(), features, logger },
|
||||
source,
|
||||
apiDetails,
|
||||
zstdAvailability.version,
|
||||
tempDir,
|
||||
);
|
||||
@@ -1014,9 +1015,8 @@ export async function setupCodeQLBundle(
|
||||
* @returns The extraction directory and download timings.
|
||||
*/
|
||||
export async function downloadCodeQLBundle(
|
||||
action: ActionState<["Logger", "ReadOnlyEnv"]>,
|
||||
action: ActionState<["Logger", "ReadOnlyEnv", "FeatureFlags"]>,
|
||||
source: CodeQLDownloadSource,
|
||||
apiDetails: api.GitHubApiDetails,
|
||||
tarVersion: tar.TarVersion | undefined,
|
||||
tempDir: string,
|
||||
): Promise<{
|
||||
@@ -1030,13 +1030,7 @@ export async function downloadCodeQLBundle(
|
||||
|
||||
const startTime = performance.now();
|
||||
try {
|
||||
return await downloadCodeQL(
|
||||
source,
|
||||
apiDetails,
|
||||
tarVersion,
|
||||
tempDir,
|
||||
logger,
|
||||
);
|
||||
return await downloadCodeQL(source, tarVersion, tempDir, logger);
|
||||
} catch (e) {
|
||||
if (
|
||||
bundle.kind !== "per-language" ||
|
||||
@@ -1052,7 +1046,6 @@ export async function downloadCodeQLBundle(
|
||||
...source,
|
||||
bundle: { kind: "combined", url: bundle.combinedBundleURL },
|
||||
},
|
||||
apiDetails,
|
||||
tarVersion,
|
||||
tempDir,
|
||||
logger,
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
parseRepositoryNwo,
|
||||
type RepositoryNwoStr,
|
||||
} from "../repository";
|
||||
import { type InstanceURL } from "../util";
|
||||
|
||||
/** The NWO of the standard CodeQL Action repository. */
|
||||
export const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action";
|
||||
@@ -48,20 +47,14 @@ export function getCodeQLActionRepository(
|
||||
}
|
||||
|
||||
/**A download source is a pair of strings. */
|
||||
export type DownloadSource = [InstanceURL, RepositoryNwoStr];
|
||||
|
||||
/** GitHub.com, and the canonical Action. */
|
||||
export const DEFAULT_DOWNLOAD_SOURCE: DownloadSource = [
|
||||
GITHUB_DOTCOM_URL,
|
||||
CODEQL_DEFAULT_ACTION_REPOSITORY,
|
||||
];
|
||||
export type DownloadSource = [api.GitHubApiDetails, RepositoryNwoStr];
|
||||
|
||||
/** Decides if the two provided download sources are the same. */
|
||||
const isSameDownloadSource = (
|
||||
[srcUrl, srcRepo]: DownloadSource,
|
||||
[otherUrl, otherRepo]: DownloadSource,
|
||||
[srcInstance, srcRepo]: DownloadSource,
|
||||
[otherInstance, otherRepo]: DownloadSource,
|
||||
) => {
|
||||
return srcUrl === otherUrl && srcRepo === otherRepo;
|
||||
return srcInstance.url === otherInstance.url && srcRepo === otherRepo;
|
||||
};
|
||||
|
||||
/** A download URL is represented as a string. */
|
||||
@@ -104,13 +97,21 @@ export async function getCodeQLAssetDownloadURL(
|
||||
): Promise<DownloadURL> {
|
||||
const codeQLActionRepository = getCodeQLActionRepository(action);
|
||||
|
||||
// We construct the default GitHub.com source here, because `getDotcomApiDetails`
|
||||
// may retrieve the `token` input if we are currently running on GitHub.com and
|
||||
// we want to avoid doing that statically.
|
||||
const defaultDownloadSource: DownloadSource = [
|
||||
api.getDotcomApiDetails(action.env),
|
||||
CODEQL_DEFAULT_ACTION_REPOSITORY,
|
||||
];
|
||||
|
||||
const potentialDownloadSources: DownloadSource[] = [
|
||||
// This GitHub instance, and this Action.
|
||||
[apiDetails.url, codeQLActionRepository],
|
||||
[apiDetails, codeQLActionRepository],
|
||||
// This GitHub instance, and the canonical Action.
|
||||
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
|
||||
[apiDetails, CODEQL_DEFAULT_ACTION_REPOSITORY],
|
||||
// GitHub.com, and the canonical Action.
|
||||
DEFAULT_DOWNLOAD_SOURCE,
|
||||
defaultDownloadSource,
|
||||
];
|
||||
|
||||
// We now filter out any duplicates.
|
||||
@@ -123,34 +124,34 @@ export async function getCodeQLAssetDownloadURL(
|
||||
},
|
||||
);
|
||||
|
||||
for (const [instanceURL, repository] of uniqueDownloadSources) {
|
||||
for (const [instance, repository] of uniqueDownloadSources) {
|
||||
// If we've reached the final case, short-circuit the API check since we know the asset exists and is public.
|
||||
if (
|
||||
isSameDownloadSource(DEFAULT_DOWNLOAD_SOURCE, [instanceURL, repository])
|
||||
) {
|
||||
if (isSameDownloadSource(defaultDownloadSource, [instance, repository])) {
|
||||
break;
|
||||
}
|
||||
|
||||
const { owner: repositoryOwner, repo: repositoryName } =
|
||||
parseRepositoryNwo(repository);
|
||||
try {
|
||||
const release = await api.getApiClient().rest.repos.getReleaseByTag({
|
||||
owner: repositoryOwner,
|
||||
repo: repositoryName,
|
||||
tag: tagName,
|
||||
});
|
||||
const release = await api
|
||||
.getApiClient(action.env, instance)
|
||||
.rest.repos.getReleaseByTag({
|
||||
owner: repositoryOwner,
|
||||
repo: repositoryName,
|
||||
tag: tagName,
|
||||
});
|
||||
|
||||
for (const asset of release.data.assets) {
|
||||
if (asset.name === assetName) {
|
||||
action.logger.info(
|
||||
`Found ${assetKind} ${assetName} in ${repository} on ${instanceURL} with URL ${asset.url}.`,
|
||||
`Found ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} with URL ${asset.url}.`,
|
||||
);
|
||||
return asset.url;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
action.logger.info(
|
||||
`Looked for ${assetKind} ${assetName} in ${repository} on ${instanceURL} but got error ${e}.`,
|
||||
`Looked for ${assetKind} ${assetName} in ${repository} on ${instance.apiURL} but got error ${e}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { ActionState } from "./action-common";
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import * as apiClient from "./api-client";
|
||||
import * as defaults from "./defaults.json";
|
||||
import { ActionsEnvVars } from "./environment";
|
||||
import { ActionsEnvVars, type Env } from "./environment";
|
||||
import { setUpFeatureFlagTests } from "./feature-flags/testing-util";
|
||||
import { UnvalidatedObject, validateSchema } from "./json";
|
||||
import { makeFromSchema } from "./json/testing-util";
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
initAllState,
|
||||
makeMacro,
|
||||
makeTestToken,
|
||||
mockApiDetails,
|
||||
RecordingLogger,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
setupTests,
|
||||
@@ -613,18 +614,23 @@ test("getProxyPackage - includes platform in name", (t) => {
|
||||
}
|
||||
});
|
||||
|
||||
function mockGetApiClient(endpoints: any) {
|
||||
return (
|
||||
sinon
|
||||
.stub(apiClient, "getApiClient")
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
.returns({ rest: endpoints } as any)
|
||||
);
|
||||
function mockGetApiClient(env: Env, endpoints: any) {
|
||||
const getRequiredInput = mockApiDetails(SAMPLE_DOTCOM_API_DETAILS, env);
|
||||
const getApiClient = sinon
|
||||
.stub(apiClient, "getApiClient")
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
.returns({ rest: endpoints } as any);
|
||||
|
||||
return { getRequiredInput, getApiClient };
|
||||
}
|
||||
|
||||
type ReleaseAssets = Array<{ name: string; url?: string }>;
|
||||
|
||||
function mockGetReleaseByTag(expectedTag?: string, assets?: ReleaseAssets) {
|
||||
function mockGetReleaseByTag(
|
||||
env: Env,
|
||||
expectedTag?: string,
|
||||
assets?: ReleaseAssets,
|
||||
) {
|
||||
const getReleaseByTag =
|
||||
assets === undefined
|
||||
? sinon.stub().rejects()
|
||||
@@ -645,7 +651,7 @@ function mockGetReleaseByTag(expectedTag?: string, assets?: ReleaseAssets) {
|
||||
|
||||
return {
|
||||
getReleaseByTag,
|
||||
getApiClient: mockGetApiClient({ repos: { getReleaseByTag } }),
|
||||
...mockGetApiClient(env, { repos: { getReleaseByTag } }),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -671,7 +677,6 @@ test.serial(
|
||||
"getDownloadUrl logs unknown platforms/arch and defaults to linux64",
|
||||
async (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
mockGetReleaseByTag();
|
||||
|
||||
await withTmpDir(async (tempDir) => {
|
||||
const features = mockOfflineFeatures(tempDir, logger);
|
||||
@@ -681,6 +686,7 @@ test.serial(
|
||||
logger,
|
||||
features,
|
||||
});
|
||||
mockGetReleaseByTag(state.env);
|
||||
|
||||
// Stub this to always return `true` so we get the default repo for the tests.
|
||||
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
|
||||
@@ -710,11 +716,11 @@ test.serial(
|
||||
"getDownloadUrl returns default bundle URL for linked version",
|
||||
async (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
mockGetReleaseByTag();
|
||||
|
||||
await withTmpDir(async (tempDir) => {
|
||||
const features = mockOfflineFeatures(tempDir, logger);
|
||||
const state = initAllState({ logger, features });
|
||||
mockGetReleaseByTag(state.env);
|
||||
|
||||
// Stub this to always return `true` so we get the default repo for the tests.
|
||||
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
|
||||
@@ -750,7 +756,11 @@ test.serial(
|
||||
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
|
||||
|
||||
for (const assets of testAssets) {
|
||||
const stubs = mockGetReleaseByTag(defaults.bundleVersion, assets);
|
||||
const stubs = mockGetReleaseByTag(
|
||||
state.env,
|
||||
defaults.bundleVersion,
|
||||
assets,
|
||||
);
|
||||
const info = await startProxyExports.getDownloadUrl(
|
||||
state,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
@@ -768,6 +778,7 @@ test.serial(
|
||||
// mockGetReleaseByTag stubs the global getApiClient; since we are calling
|
||||
// that in a loop, we need to manually restore it here
|
||||
stubs.getApiClient.restore();
|
||||
stubs.getRequiredInput.restore();
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -783,7 +794,7 @@ test.serial("getDownloadUrl returns matching release asset", async (t) => {
|
||||
url: "url-we-want",
|
||||
},
|
||||
];
|
||||
const stubs = mockGetReleaseByTag(defaults.bundleVersion, assets);
|
||||
const stubs = mockGetReleaseByTag(state.env, defaults.bundleVersion, assets);
|
||||
|
||||
await withTmpDir(async (tempDir) => {
|
||||
state.env.set(ActionsEnvVars.RUNNER_TEMP, tempDir);
|
||||
@@ -925,7 +936,8 @@ test.serial(
|
||||
"getProxyBinaryPath - returns path from tool cache if available",
|
||||
async (t) => {
|
||||
const logger = new RecordingLogger();
|
||||
mockGetReleaseByTag();
|
||||
|
||||
sinon.stub(apiClient, "getApiDetails").returns(SAMPLE_DOTCOM_API_DETAILS);
|
||||
|
||||
await withTmpDir(async (tempDir) => {
|
||||
const toolcachePath = "/path/to/proxy/dir";
|
||||
@@ -935,8 +947,11 @@ test.serial(
|
||||
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
|
||||
|
||||
const features = mockOfflineFeatures(tempDir, logger);
|
||||
const state = initAllState({ logger, features });
|
||||
mockGetReleaseByTag(state.env);
|
||||
|
||||
const result = await startProxyExports.getProxyBinaryPath(
|
||||
initAllState({ logger, features }),
|
||||
state,
|
||||
SAMPLE_DOTCOM_API_DETAILS,
|
||||
);
|
||||
|
||||
@@ -956,7 +971,7 @@ test.serial(
|
||||
const logger = new RecordingLogger();
|
||||
const state = initAllState({ logger });
|
||||
const downloadUrl = "url-we-want";
|
||||
const stubs = mockGetReleaseByTag(defaults.bundleVersion, [
|
||||
const stubs = mockGetReleaseByTag(state.env, defaults.bundleVersion, [
|
||||
{
|
||||
name: startProxyExports.getProxyPackage(getTestPlatform(state)),
|
||||
url: downloadUrl,
|
||||
@@ -965,9 +980,16 @@ test.serial(
|
||||
|
||||
const toolcachePath = "/path/to/proxy/dir";
|
||||
const find = sinon.stub(toolcache, "find").returns("");
|
||||
const apiDetails = {
|
||||
auth: "",
|
||||
url: "",
|
||||
apiURL: "",
|
||||
} satisfies apiClient.GitHubApiDetails;
|
||||
const client = new apiClient.InstanceApiClient(apiDetails);
|
||||
const getAuthorizationHeaderFor = sinon
|
||||
.stub(apiClient, "getAuthorizationHeaderFor")
|
||||
.stub(client, "getAuthorizationHeaderFor")
|
||||
.returns(undefined);
|
||||
sinon.stub(apiClient, "InstanceApiClient").returns(client);
|
||||
const archivePath = "/path/to/archive";
|
||||
const downloadTool = sinon
|
||||
.stub(toolcache, "downloadTool")
|
||||
@@ -1007,7 +1029,7 @@ test.serial(
|
||||
t.is(result.version, defaults.cliVersion);
|
||||
|
||||
checkExpectedLogMessages(t, logger.messages, [
|
||||
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in test/repo on https://github.com with URL ${downloadUrl}`,
|
||||
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in test/repo on https://api.github.com with URL ${downloadUrl}`,
|
||||
]);
|
||||
},
|
||||
);
|
||||
@@ -1039,14 +1061,21 @@ test.serial(
|
||||
headers: {},
|
||||
url: "GET /repos/:owner/:repo/releases/tags/:tag",
|
||||
});
|
||||
mockGetApiClient({ repos: { getReleaseByTag } });
|
||||
mockGetApiClient(state.env, { repos: { getReleaseByTag } });
|
||||
|
||||
await withTmpDir(async (tempDir) => {
|
||||
const toolcachePath = "/path/to/proxy/dir";
|
||||
const find = sinon.stub(toolcache, "find").returns("");
|
||||
const apiDetails = {
|
||||
auth: "",
|
||||
url: "",
|
||||
apiURL: "",
|
||||
} satisfies apiClient.GitHubApiDetails;
|
||||
const client = new apiClient.InstanceApiClient(apiDetails);
|
||||
const getAuthorizationHeaderFor = sinon
|
||||
.stub(apiClient, "getAuthorizationHeaderFor")
|
||||
.stub(client, "getAuthorizationHeaderFor")
|
||||
.returns(undefined);
|
||||
sinon.stub(apiClient, "InstanceApiClient").returns(client);
|
||||
const archivePath = "/path/to/archive";
|
||||
const downloadTool = sinon
|
||||
.stub(toolcache, "downloadTool")
|
||||
@@ -1107,7 +1136,7 @@ test.serial(
|
||||
});
|
||||
|
||||
checkExpectedLogMessages(t, logger.messages, [
|
||||
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in test/repo on https://github.com with URL url-we-want.`,
|
||||
`Found private registry proxy ${startProxyExports.getProxyPackage(getTestPlatform(state))} in test/repo on https://api.github.com with URL url-we-want.`,
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -4,9 +4,9 @@ import * as toolcache from "@actions/tool-cache";
|
||||
|
||||
import { ActionState } from "./action-common";
|
||||
import {
|
||||
getAuthorizationHeaderFor,
|
||||
getGitHubVersion,
|
||||
GitHubApiDetails,
|
||||
InstanceApiClient,
|
||||
type GitHubApiDetails,
|
||||
} from "./api-client";
|
||||
import * as artifactScanner from "./artifact-scanner";
|
||||
import * as defaults from "./defaults.json";
|
||||
@@ -423,6 +423,7 @@ export async function getProxyBinaryPath(
|
||||
logger.startGroup("Setting up authentication proxy");
|
||||
|
||||
try {
|
||||
const client = new InstanceApiClient(apiDetails);
|
||||
const proxyFileName = getProxyFilename();
|
||||
const proxyInfo = await getDownloadUrl(action, apiDetails);
|
||||
|
||||
@@ -437,9 +438,8 @@ export async function getProxyBinaryPath(
|
||||
`Authentication proxy is not in the toolcache. Will try to download it from '${proxyInfo.url}'.`,
|
||||
);
|
||||
|
||||
const authorization = getAuthorizationHeaderFor(
|
||||
const authorization = client.getAuthorizationHeaderFor(
|
||||
logger,
|
||||
apiDetails,
|
||||
proxyInfo.url,
|
||||
);
|
||||
const temp = await downloadProxy(logger, proxyInfo.url, authorization);
|
||||
|
||||
@@ -14,6 +14,7 @@ import * as sinon from "sinon";
|
||||
|
||||
import { ActionState, StateFeature } from "./action-common";
|
||||
import { ActionsEnv, getActionVersion } from "./actions-util";
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import { AnalysisKind } from "./analyses";
|
||||
import * as apiClient from "./api-client";
|
||||
import { GitHubApiDetails } from "./api-client";
|
||||
@@ -52,6 +53,23 @@ export const SAMPLE_DOTCOM_API_DETAILS = {
|
||||
apiURL: "https://api.github.com",
|
||||
};
|
||||
|
||||
export function mockApiDetails(
|
||||
apiDetails: Required<GitHubApiDetails>,
|
||||
env: Env = getEnv(),
|
||||
) {
|
||||
// This is a workaround to mock `api.getApiDetails()` since it doesn't seem to be possible to
|
||||
// mock this directly. The difficulty is that `getApiDetails()` is called locally in
|
||||
// `api-client.ts`, but `sinon.stub(api, "getApiDetails")` only affects calls to
|
||||
// `getApiDetails()` via an imported `api` module.
|
||||
const getRequiredInput = sinon.stub(actionsUtil, "getRequiredInput");
|
||||
getRequiredInput.withArgs("token").returns(apiDetails.auth);
|
||||
|
||||
env.set("GITHUB_SERVER_URL", apiDetails.url);
|
||||
env.set("GITHUB_API_URL", apiDetails.apiURL ?? "");
|
||||
|
||||
return getRequiredInput;
|
||||
}
|
||||
|
||||
export const LINKED_CLI_VERSION = {
|
||||
cliVersion: defaults.cliVersion,
|
||||
tagName: defaults.bundleVersion,
|
||||
|
||||
@@ -2,16 +2,6 @@
|
||||
|
||||
Change-notes are Markdown files used to document user-facing changes. When making a change that affects users, create a Markdown file here that describes the change. During the next release, the change-note files in `unreleased-change-notes/` will automatically be added to `CHANGELOG.md`.
|
||||
|
||||
### What do document
|
||||
|
||||
Some guidelines to help you decide whether a change needs a change note:
|
||||
|
||||
- New functionality that is gated behind a feature flag should be documented when it is added, highlighting that the feature will only be rolled out later. Ideally, this should also include an estimate for when the feature is expected to become available.
|
||||
- When a feature flag is removed and the gated functionality becomes the default behaviour, this does not need to be documented.
|
||||
- Changes that may require users to make changes to their workflows must be documented.
|
||||
|
||||
If in doubt, see `CHANGELOG.md` for what we have included previously.
|
||||
|
||||
### Change-note file format
|
||||
|
||||
Change-note files must follow a certain format so that they can be automatically validated and processed. Failure to follow the format will result in a failed PR check.
|
||||
|
||||
Reference in New Issue
Block a user