Skip to content

Commit

Permalink
Showing 6 changed files with 59 additions and 23 deletions.
23 changes: 22 additions & 1 deletion 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

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

12 changes: 4 additions & 8 deletions lib/codeql.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/codeql.js.map

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion src/actions-util.ts
@@ -1,3 +1,5 @@
import * as path from "path";

import * as core from "@actions/core";
import * as toolrunnner from "@actions/exec/lib/toolrunner";

@@ -249,6 +251,11 @@ export async function createStatusReportBase(
workflowStartedAt
);
}
// If running locally then the GITHUB_ACTION_REF cannot be trusted as it may be for the previous action
// See https://github.com/actions/runner/issues/803
const actionRef = isRunningLocalAction()
? undefined
: process.env["GITHUB_ACTION_REF"];

const statusReport: StatusReportBase = {
workflow_run_id: workflowRunID,
@@ -258,7 +265,7 @@ export async function createStatusReportBase(
commit_oid: commitOid,
ref,
action_name: actionName,
action_ref: process.env["GITHUB_ACTION_REF"],
action_ref: actionRef,
action_oid: "unknown", // TODO decide if it's possible to fill this in
started_at: workflowStartedAt,
action_started_at: actionStartedAt.toISOString(),
@@ -344,3 +351,20 @@ export async function sendStatusReport<S extends StatusReportBase>(

return true;
}

// Is the current action executing a local copy (i.e. we're running a workflow on the codeql-action repo itself)
// as opposed to running a remote action (i.e. when another repo references us)
export function isRunningLocalAction(): boolean {
const relativeScriptPath = getRelativeScriptPath();
return (
relativeScriptPath.startsWith("..") || path.isAbsolute(relativeScriptPath)
);
}

// Get the location where the action is runnning from.
// This can be used to get the actions name or tell if we're running a local action.
export function getRelativeScriptPath(): string {
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
return path.relative(actionsDirectory, __filename);
}
17 changes: 6 additions & 11 deletions src/codeql.ts
@@ -10,7 +10,7 @@ import * as toolcache from "@actions/tool-cache";
import * as semver from "semver";
import { v4 as uuidV4 } from "uuid";

import { getRequiredEnvParam } from "./actions-util";
import { isRunningLocalAction, getRelativeScriptPath } from "./actions-util";
import * as api from "./api-client";
import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool!
import { errorMatchers } from "./error-matcher";
@@ -143,15 +143,10 @@ function getCodeQLActionRepository(mode: util.Mode, logger: Logger): string {

// The Actions Runner used with GitHub Enterprise Server 2.22 did not set the GITHUB_ACTION_REPOSITORY variable.
// This fallback logic can be removed after the end-of-support for 2.22 on 2021-09-23.
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
const relativeScriptPath = path.relative(actionsDirectory, __filename);
// This handles the case where the Action does not come from an Action repository,
// e.g. our integration tests which use the Action code from the current checkout.
if (
relativeScriptPath.startsWith("..") ||
path.isAbsolute(relativeScriptPath)
) {

if (isRunningLocalAction()) {
// This handles the case where the Action does not come from an Action repository,
// e.g. our integration tests which use the Action code from the current checkout.
logger.info(
"The CodeQL Action is checked out locally. Using the default CodeQL Action repository."
);
@@ -160,7 +155,7 @@ function getCodeQLActionRepository(mode: util.Mode, logger: Logger): string {
logger.info(
"GITHUB_ACTION_REPOSITORY environment variable was not set. Falling back to legacy method of finding the GitHub Action."
);
const relativeScriptPathParts = relativeScriptPath.split(path.sep);
const relativeScriptPathParts = getRelativeScriptPath().split(path.sep);
return `${relativeScriptPathParts[0]}/${relativeScriptPathParts[1]}`;
}

0 comments on commit 1737b80

Please sign in to comment.