Skip to content

Commit f5b8e09

Browse files
authored
Merge pull request #4198 from github/mbg/start-proxy/deprecate-fallback
Use `setup-codeql` logic for finding release assets in `start-proxy`
2 parents 2b07ff4 + 28d5dd9 commit f5b8e09

14 files changed

Lines changed: 612 additions & 419 deletions

‎lib/entry-points.js‎

Lines changed: 154 additions & 172 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package-lock.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@octokit/plugin-rest-endpoint-methods": "^18.0.0",
3636
"@octokit/plugin-retry": "^8.1.1",
3737
"archiver": "^8.0.0",
38-
"fast-deep-equal": "^3.1.3",
3938
"follow-redirects": "^1.16.1",
4039
"get-folder-size": "^5.0.0",
4140
"https-proxy-agent": "^7.0.6",

‎src/actions-util.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function getWorkflowEventName(env: Env = getEnv()) {
101101
* Returns whether the current workflow is executing a local copy of the Action, e.g. we're running
102102
* a workflow on the codeql-action repo itself.
103103
*/
104-
export function isRunningLocalAction(env: Env = getEnv()): boolean {
104+
export function isRunningLocalAction(env: ReadOnlyEnv = getEnv()): boolean {
105105
const relativeScriptPath = getRelativeScriptPath(env);
106106
return (
107107
relativeScriptPath.startsWith("..") || path.isAbsolute(relativeScriptPath)
@@ -113,7 +113,7 @@ export function isRunningLocalAction(env: Env = getEnv()): boolean {
113113
*
114114
* This can be used to get the Action's name or tell if we're running a local Action.
115115
*/
116-
function getRelativeScriptPath(env: Env): string {
116+
function getRelativeScriptPath(env: ReadOnlyEnv): string {
117117
const runnerTemp = env.getRequired(ActionsEnvVars.RUNNER_TEMP);
118118
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
119119
return path.relative(actionsDirectory, __filename);

‎src/repository.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export interface RepositoryNwo {
66
repo: string;
77
}
88

9+
/** An approximation of what a repository NWO string looks like. */
10+
export type RepositoryNwoStr = `${string}/${string}`;
11+
12+
/** A basic check that `val` looks like a `RepositoryNwoStr`. */
13+
export function isRepositoryNwoStr(val: string): val is RepositoryNwoStr {
14+
return val.includes("/");
15+
}
16+
917
/**
1018
* Get the repository name with owner from the environment variable
1119
* `GITHUB_REPOSITORY`.

‎src/setup-codeql.test.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,6 @@ test.serial("convert to semver", (t) => {
126126
}
127127
});
128128

129-
test.serial("getCodeQLActionRepository", (t) => {
130-
const logger = getRunnerLogger(true);
131-
132-
initializeEnvironment("1.2.3");
133-
134-
// isRunningLocalAction() === true
135-
delete process.env["GITHUB_ACTION_REPOSITORY"];
136-
process.env["RUNNER_TEMP"] = path.dirname(__dirname);
137-
const repoLocalRunner = setupCodeql.getCodeQLActionRepository(logger);
138-
t.deepEqual(repoLocalRunner, "github/codeql-action");
139-
140-
// isRunningLocalAction() === false
141-
sinon.stub(actionsUtil, "isRunningLocalAction").returns(false);
142-
process.env["GITHUB_ACTION_REPOSITORY"] = "xxx/yyy";
143-
const repoEnv = setupCodeql.getCodeQLActionRepository(logger);
144-
t.deepEqual(repoEnv, "xxx/yyy");
145-
});
146-
147129
test.serial(
148130
"getCodeQLSource sets CLI version for a semver tagged bundle",
149131
async (t) => {

‎src/setup-codeql.ts‎

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as path from "path";
44
import { performance } from "perf_hooks";
55

66
import * as toolcache from "@actions/tool-cache";
7-
import { default as deepEqual } from "fast-deep-equal";
87
import * as semver from "semver";
98
import { v4 as uuidV4 } from "uuid";
109

@@ -13,7 +12,6 @@ import {
1312
isAnalyzingPullRequest,
1413
isDynamicWorkflow,
1514
isGitHubHostedRunner,
16-
isRunningLocalAction,
1715
} from "./actions-util";
1816
import * as api from "./api-client";
1917
import {
@@ -44,6 +42,7 @@ import {
4442
logPerLanguageBundleFallback,
4543
} from "./per-language-bundles";
4644
import { getBundlePlatform } from "./platform";
45+
import { getCodeQLAssetDownloadURL } from "./setup/repository";
4746
import * as tar from "./tar";
4847
import {
4948
deleteToolcacheBundles,
@@ -65,83 +64,13 @@ export enum ToolsSource {
6564
Download = "DOWNLOAD",
6665
}
6766

68-
const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action";
6967
const CODEQL_NIGHTLIES_REPOSITORY_OWNER = "dsp-testing";
7068
const CODEQL_NIGHTLIES_REPOSITORY_NAME = "codeql-cli-nightlies";
7169

7270
const CODEQL_BUNDLE_VERSION_ALIAS: string[] = ["linked", "latest"];
7371
const CODEQL_NIGHTLY_TOOLS_INPUTS = ["nightly", "nightly-latest"];
7472
const CODEQL_TOOLCACHE_INPUT = "toolcache";
7573

76-
export function getCodeQLActionRepository(logger: Logger): string {
77-
if (isRunningLocalAction()) {
78-
// This handles the case where the Action does not come from an Action repository,
79-
// e.g. our integration tests which use the Action code from the current checkout.
80-
// In these cases, the GITHUB_ACTION_REPOSITORY environment variable is not set.
81-
logger.info(
82-
"The CodeQL Action is checked out locally. Using the default CodeQL Action repository.",
83-
);
84-
return CODEQL_DEFAULT_ACTION_REPOSITORY;
85-
}
86-
87-
return util.getRequiredEnvParam("GITHUB_ACTION_REPOSITORY");
88-
}
89-
90-
async function getCodeQLBundleDownloadURL(
91-
tagName: string,
92-
apiDetails: api.GitHubApiDetails,
93-
codeQLBundleName: string,
94-
logger: Logger,
95-
): Promise<string> {
96-
const codeQLActionRepository = getCodeQLActionRepository(logger);
97-
const potentialDownloadSources = [
98-
// This GitHub instance, and this Action.
99-
[apiDetails.url, codeQLActionRepository],
100-
// This GitHub instance, and the canonical Action.
101-
[apiDetails.url, CODEQL_DEFAULT_ACTION_REPOSITORY],
102-
// GitHub.com, and the canonical Action.
103-
[util.GITHUB_DOTCOM_URL, CODEQL_DEFAULT_ACTION_REPOSITORY],
104-
];
105-
// We now filter out any duplicates.
106-
// Duplicates will happen either because the GitHub instance is GitHub.com, or because the Action is not a fork.
107-
const uniqueDownloadSources = potentialDownloadSources.filter(
108-
(source, index, self) => {
109-
return !self.slice(0, index).some((other) => deepEqual(source, other));
110-
},
111-
);
112-
for (const downloadSource of uniqueDownloadSources) {
113-
const [apiURL, repository] = downloadSource;
114-
// If we've reached the final case, short-circuit the API check since we know the bundle exists and is public.
115-
if (
116-
apiURL === util.GITHUB_DOTCOM_URL &&
117-
repository === CODEQL_DEFAULT_ACTION_REPOSITORY
118-
) {
119-
break;
120-
}
121-
const [repositoryOwner, repositoryName] = repository.split("/");
122-
try {
123-
const release = await api.getApiClient().rest.repos.getReleaseByTag({
124-
owner: repositoryOwner,
125-
repo: repositoryName,
126-
tag: tagName,
127-
});
128-
for (const asset of release.data.assets) {
129-
if (asset.name === codeQLBundleName) {
130-
logger.info(
131-
`Found CodeQL bundle ${codeQLBundleName} in ${repository} on ${apiURL} with URL ${asset.url}.`,
132-
);
133-
return asset.url;
134-
}
135-
}
136-
} catch (e) {
137-
logger.info(
138-
`Looked for CodeQL bundle ${codeQLBundleName} in ${repository} on ${apiURL} but got error ${e}.`,
139-
);
140-
}
141-
}
142-
return `https://github.com/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`;
143-
}
144-
14574
function tryGetBundleVersionFromTagName(
14675
tagName: string,
14776
logger: Logger,
@@ -720,9 +649,10 @@ export async function getCodeQLSource(
720649
? "zstd"
721650
: "gzip";
722651

652+
const action = { env: getEnv(), logger };
723653
const platform = getBundlePlatform();
724654
const perLanguageBundleLanguage = await getPerLanguageBundleLanguage(
725-
{ env: getEnv(), features, logger },
655+
{ ...action, features },
726656
{
727657
rawLanguages,
728658
otherLanguagePacksReason,
@@ -735,11 +665,11 @@ export async function getCodeQLSource(
735665

736666
// Resolves the combined or per-language bundle URL for the requested release.
737667
const resolveBundleURL = (language?: BuiltInLanguage) =>
738-
getCodeQLBundleDownloadURL(
739-
bundleTagName,
668+
getCodeQLAssetDownloadURL(
669+
action,
740670
apiDetails,
671+
bundleTagName,
741672
getCodeQLBundleName(compressionMethod, platform, language),
742-
logger,
743673
);
744674

745675
const combinedBundleURL = await resolveBundleURL();

‎src/setup/repository.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as path from "path";
2+
3+
import test from "ava";
4+
import * as sinon from "sinon";
5+
6+
import * as actionsUtil from "../actions-util";
7+
import { ActionsEnvVars } from "../environment";
8+
import { callee, getTestEnv, setupTests } from "../testing-utils";
9+
import { initializeEnvironment } from "../util";
10+
11+
import { getCodeQLActionRepository } from "./repository";
12+
13+
setupTests(test);
14+
15+
test.serial("getCodeQLActionRepository", async (t) => {
16+
const initialEnv = getTestEnv({
17+
[ActionsEnvVars.RUNNER_TEMP]: path.dirname(__dirname),
18+
});
19+
initializeEnvironment("1.2.3", initialEnv);
20+
21+
const target = callee(getCodeQLActionRepository)
22+
.withArgs()
23+
.withEnv(initialEnv);
24+
25+
// isRunningLocalAction() === true
26+
await target.passes(t.deepEqual, "github/codeql-action");
27+
28+
// isRunningLocalAction() === false
29+
sinon.stub(actionsUtil, "isRunningLocalAction").returns(false);
30+
await target
31+
.withEnv((env) => {
32+
env.set(ActionsEnvVars.GITHUB_ACTION_REPOSITORY, "xxx/yyy");
33+
})
34+
.passes(t.deepEqual, "xxx/yyy");
35+
});

0 commit comments

Comments
 (0)