Merge pull request #2286 from github/koesie10/ghec-dr-db-upload

Enable database uploads for GHEC-DR
This commit is contained in:
Koen Vlaswinkel 2024-05-15 14:49:47 +02:00 committed by GitHub
commit a57c67b895
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 65 additions and 21 deletions

View file

@ -53,7 +53,7 @@ async function mockHttpRequests(databaseUploadStatusCode: number) {
const requestSpy = sinon.stub(client, "request");
const url =
"POST https://uploads.github.com/repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid";
"POST /repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid";
const databaseUploadSpy = requestSpy.withArgs(url);
if (databaseUploadStatusCode < 300) {
databaseUploadSpy.resolves(undefined);
@ -64,6 +64,8 @@ async function mockHttpRequests(databaseUploadStatusCode: number) {
}
sinon.stub(apiClient, "getApiClient").value(() => client);
return databaseUploadSpy;
}
test("Abort database upload if 'upload-database' input set to false", async (t) => {
@ -116,7 +118,8 @@ test("Abort database upload if running against GHES", async (t) => {
loggedMessages.find(
(v: LoggedMessage) =>
v.type === "debug" &&
v.message === "Not running against github.com. Skipping upload.",
v.message ===
"Not running against github.com or GHEC-DR. Skipping upload.",
) !== undefined,
);
});
@ -184,7 +187,7 @@ test("Don't crash if uploading a database fails", async (t) => {
});
});
test("Successfully uploading a database to api.github.com", async (t) => {
test("Successfully uploading a database to github.com", async (t) => {
await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
sinon
@ -218,7 +221,7 @@ test("Successfully uploading a database to api.github.com", async (t) => {
});
});
test("Successfully uploading a database to uploads.github.com", async (t) => {
test("Successfully uploading a database to GHEC-DR", async (t) => {
await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
sinon
@ -227,7 +230,7 @@ test("Successfully uploading a database to uploads.github.com", async (t) => {
.returns("true");
sinon.stub(actionsUtil, "isAnalyzingDefaultBranch").resolves(true);
await mockHttpRequests(201);
const databaseUploadSpy = await mockHttpRequests(201);
setCodeQL({
async databaseBundle(_: string, outputFilePath: string) {
@ -239,7 +242,11 @@ test("Successfully uploading a database to uploads.github.com", async (t) => {
await uploadDatabases(
testRepoName,
getTestConfig(tmpDir),
testApiDetails,
{
auth: "1234",
url: "https://tenant.ghe.com",
apiURL: undefined,
},
getRecordingLogger(loggedMessages),
);
t.assert(
@ -249,5 +256,11 @@ test("Successfully uploading a database to uploads.github.com", async (t) => {
v.message === "Successfully uploaded database for javascript",
) !== undefined,
);
t.assert(
databaseUploadSpy.calledOnceWith(
sinon.match.string,
sinon.match.has("baseUrl", "https://uploads.tenant.ghe.com"),
),
);
});
});

View file

@ -7,7 +7,7 @@ import { Config } from "./config-utils";
import { Logger } from "./logging";
import { RepositoryNwo } from "./repository";
import * as util from "./util";
import { bundleDb } from "./util";
import { bundleDb, parseGitHubUrl } from "./util";
export async function uploadDatabases(
repositoryNwo: RepositoryNwo,
@ -21,8 +21,11 @@ export async function uploadDatabases(
}
// Do nothing when not running against github.com
if (config.gitHubVersion.type !== util.GitHubVariant.DOTCOM) {
logger.debug("Not running against github.com. Skipping upload.");
if (
config.gitHubVersion.type !== util.GitHubVariant.DOTCOM &&
config.gitHubVersion.type !== util.GitHubVariant.GHE_DOTCOM
) {
logger.debug("Not running against github.com or GHEC-DR. Skipping upload.");
return;
}
@ -35,6 +38,16 @@ export async function uploadDatabases(
const client = getApiClient();
const codeql = await getCodeQL(config.codeQLCmd);
const uploadsUrl = new URL(parseGitHubUrl(apiDetails.url));
uploadsUrl.hostname = `uploads.${uploadsUrl.hostname}`;
// Octokit expects the baseUrl to not have a trailing slash,
// but it is included by default in a URL.
let uploadsBaseUrl = uploadsUrl.toString();
if (uploadsBaseUrl.endsWith("/")) {
uploadsBaseUrl = uploadsBaseUrl.slice(0, -1);
}
for (const language of config.languages) {
try {
// Upload the database bundle.
@ -49,8 +62,9 @@ export async function uploadDatabases(
);
try {
await client.request(
`POST https://uploads.github.com/repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid`,
`POST /repos/:owner/:repo/code-scanning/codeql/databases/:language?name=:name&commit_oid=:commit_oid`,
{
baseUrl: uploadsBaseUrl,
owner: repositoryNwo.owner,
repo: repositoryNwo.repo,
language,