Merge pull request #563 from github/robertbrignull/check_default_branch

Check if on default branch before uploading database
This commit is contained in:
Robert 2021-06-16 15:11:45 +01:00 committed by GitHub
commit 366b68eda0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 5 deletions

13
lib/actions-util.js generated
View file

@ -540,4 +540,17 @@ function getRelativeScriptPath() {
return path.relative(actionsDirectory, __filename);
}
exports.getRelativeScriptPath = getRelativeScriptPath;
// Is the version of the repository we are currently analyzing from the default branch,
// or alternatively from another branch or a pull request.
async function isAnalyzingDefaultBranch() {
var _a, _b;
// Get the current ref and trim and refs/heads/ prefix
let currentRef = await getRef();
currentRef = currentRef.startsWith("refs/heads/")
? currentRef.substr("refs/heads/".length)
: currentRef;
const eventJson = JSON.parse(fs.readFileSync(util_1.getRequiredEnvParam("GITHUB_EVENT_PATH"), "utf-8"));
return currentRef === ((_b = (_a = eventJson) === null || _a === void 0 ? void 0 : _a.repository) === null || _b === void 0 ? void 0 : _b.default_branch);
}
exports.isAnalyzingDefaultBranch = isAnalyzingDefaultBranch;
//# sourceMappingURL=actions-util.js.map

File diff suppressed because one or more lines are too long

10
lib/analyze-action.js generated
View file

@ -34,6 +34,11 @@ async function sendStatusReport(startedAt, stats, error) {
await actionsUtil.sendStatusReport(statusReport);
}
async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
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 = api_client_1.getApiClient(apiDetails);
const optInResponse = await client.request("GET /repos/:owner/:repo/code-scanning/databases", {
owner: repositoryNwo.owner,
@ -57,7 +62,10 @@ async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
repo: repositoryNwo.repo,
data: payload,
});
if (uploadResponse.status !== 201) {
if (uploadResponse.status === 201) {
logger.debug(`Successfully uploaded database for ${language}`);
}
else {
// Log a warning but don't fail the workflow
logger.warning(`Failed to upload database for ${language}. ${uploadResponse.data}`);
}

File diff suppressed because one or more lines are too long

View file

@ -691,3 +691,19 @@ export function getRelativeScriptPath(): string {
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
return path.relative(actionsDirectory, __filename);
}
// Is the version of the repository we are currently analyzing from the default branch,
// or alternatively from another branch or a pull request.
export async function isAnalyzingDefaultBranch(): Promise<boolean> {
// Get the current ref and trim and refs/heads/ prefix
let currentRef = await getRef();
currentRef = currentRef.startsWith("refs/heads/")
? currentRef.substr("refs/heads/".length)
: currentRef;
const eventJson = JSON.parse(
fs.readFileSync(getRequiredEnvParam("GITHUB_EVENT_PATH"), "utf-8")
);
return currentRef === eventJson?.repository?.default_branch;
}

View file

@ -58,8 +58,13 @@ async function uploadDatabases(
apiDetails: GitHubApiDetails,
logger: Logger
): Promise<void> {
const client = getApiClient(apiDetails);
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);
const optInResponse = await client.request(
"GET /repos/:owner/:repo/code-scanning/databases",
{
@ -92,7 +97,9 @@ async function uploadDatabases(
data: payload,
}
);
if (uploadResponse.status !== 201) {
if (uploadResponse.status === 201) {
logger.debug(`Successfully uploaded database for ${language}`);
} else {
// Log a warning but don't fail the workflow
logger.warning(
`Failed to upload database for ${language}. ${uploadResponse.data}`