Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/src/database-upload.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
80 lines (73 sloc)
2.47 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from "fs"; | |
import * as actionsUtil from "./actions-util"; | |
import { getApiClient, GitHubApiDetails } from "./api-client"; | |
import { getCodeQL } from "./codeql"; | |
import { Config } from "./config-utils"; | |
import { Logger } from "./logging"; | |
import { RepositoryNwo } from "./repository"; | |
import * as util from "./util"; | |
export async function uploadDatabases( | |
repositoryNwo: RepositoryNwo, | |
config: Config, | |
apiDetails: GitHubApiDetails, | |
logger: Logger | |
): Promise<void> { | |
if (actionsUtil.getRequiredInput("upload-database") !== "true") { | |
logger.debug("Database upload disabled in workflow. Skipping upload."); | |
return; | |
} | |
// Do nothing when not running against github.com | |
if (config.gitHubVersion.type !== util.GitHubVariant.DOTCOM) { | |
logger.debug("Not running against github.com. Skipping upload."); | |
return; | |
} | |
if (!(await actionsUtil.isAnalyzingDefaultBranch())) { | |
// We only want to upload a database if we are analyzing the default branch. | |
logger.debug("Not analyzing default branch. Skipping upload."); | |
return; | |
} | |
const client = getApiClient(apiDetails); | |
try { | |
await client.request( | |
"GET /repos/:owner/:repo/code-scanning/codeql/databases", | |
{ | |
owner: repositoryNwo.owner, | |
repo: repositoryNwo.repo, | |
} | |
); | |
} catch (e) { | |
if (util.isHTTPError(e) && e.status === 404) { | |
logger.debug( | |
"Repository is not opted in to database uploads. Skipping upload." | |
); | |
} else { | |
console.log(e); | |
logger.info(`Skipping database upload due to unknown error: ${e}`); | |
} | |
return; | |
} | |
const codeql = getCodeQL(config.codeQLCmd); | |
for (const language of config.languages) { | |
// Bundle the database up into a single zip file | |
const databasePath = util.getCodeQLDatabasePath(config, language); | |
const databaseBundlePath = `${databasePath}.zip`; | |
await codeql.databaseBundle(databasePath, databaseBundlePath); | |
// Upload the database bundle | |
const payload = fs.readFileSync(databaseBundlePath); | |
try { | |
await client.request( | |
`PUT /repos/:owner/:repo/code-scanning/codeql/databases/${language}`, | |
{ | |
owner: repositoryNwo.owner, | |
repo: repositoryNwo.repo, | |
data: payload, | |
} | |
); | |
logger.debug(`Successfully uploaded database for ${language}`); | |
} catch (e) { | |
console.log(e); | |
// Log a warning but don't fail the workflow | |
logger.warning(`Failed to upload database for ${language}: ${e}`); | |
} | |
} | |
} |