Merge pull request #563 from github/robertbrignull/check_default_branch
Check if on default branch before uploading database
This commit is contained in:
commit
366b68eda0
6 changed files with 49 additions and 5 deletions
13
lib/actions-util.js
generated
13
lib/actions-util.js
generated
|
|
@ -540,4 +540,17 @@ function getRelativeScriptPath() {
|
||||||
return path.relative(actionsDirectory, __filename);
|
return path.relative(actionsDirectory, __filename);
|
||||||
}
|
}
|
||||||
exports.getRelativeScriptPath = getRelativeScriptPath;
|
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
|
//# sourceMappingURL=actions-util.js.map
|
||||||
File diff suppressed because one or more lines are too long
10
lib/analyze-action.js
generated
10
lib/analyze-action.js
generated
|
|
@ -34,6 +34,11 @@ async function sendStatusReport(startedAt, stats, error) {
|
||||||
await actionsUtil.sendStatusReport(statusReport);
|
await actionsUtil.sendStatusReport(statusReport);
|
||||||
}
|
}
|
||||||
async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
|
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 client = api_client_1.getApiClient(apiDetails);
|
||||||
const optInResponse = await client.request("GET /repos/:owner/:repo/code-scanning/databases", {
|
const optInResponse = await client.request("GET /repos/:owner/:repo/code-scanning/databases", {
|
||||||
owner: repositoryNwo.owner,
|
owner: repositoryNwo.owner,
|
||||||
|
|
@ -57,7 +62,10 @@ async function uploadDatabases(repositoryNwo, config, apiDetails, logger) {
|
||||||
repo: repositoryNwo.repo,
|
repo: repositoryNwo.repo,
|
||||||
data: payload,
|
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
|
// Log a warning but don't fail the workflow
|
||||||
logger.warning(`Failed to upload database for ${language}. ${uploadResponse.data}`);
|
logger.warning(`Failed to upload database for ${language}. ${uploadResponse.data}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -691,3 +691,19 @@ export function getRelativeScriptPath(): string {
|
||||||
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
|
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
|
||||||
return path.relative(actionsDirectory, __filename);
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,13 @@ async function uploadDatabases(
|
||||||
apiDetails: GitHubApiDetails,
|
apiDetails: GitHubApiDetails,
|
||||||
logger: Logger
|
logger: Logger
|
||||||
): Promise<void> {
|
): 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(
|
const optInResponse = await client.request(
|
||||||
"GET /repos/:owner/:repo/code-scanning/databases",
|
"GET /repos/:owner/:repo/code-scanning/databases",
|
||||||
{
|
{
|
||||||
|
|
@ -92,7 +97,9 @@ async function uploadDatabases(
|
||||||
data: payload,
|
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
|
// Log a warning but don't fail the workflow
|
||||||
logger.warning(
|
logger.warning(
|
||||||
`Failed to upload database for ${language}. ${uploadResponse.data}`
|
`Failed to upload database for ${language}. ${uploadResponse.data}`
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue