Skip to content

Commit

Permalink
Merge pull request #563 from github/robertbrignull/check_default_branch
Browse files Browse the repository at this point in the history
Check if on default branch before uploading database
  • Loading branch information
robertbrignull authored Jun 16, 2021
2 parents 4294711 + d693b3c commit 366b68e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion lib/analyze-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/analyze-action.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/actions-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 9 additions & 2 deletions src/analyze-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit 366b68e

Please sign in to comment.