Skip to content

Commit d7473a8

Browse files
committed
Use the checkout_path for getting the commit oid
This commit also adds a new integration check to verify this. When running in test mode, payloads will not be uploaded. Instead, they will be saved to disk so that they can be inspected later.
1 parent 117a67b commit d7473a8

8 files changed

+245
-14
lines changed

.github/workflows/__with-checkout-path.yml

+100
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: "Use a custom `checkout_path`"
2+
description: "Checks that a custom `checkout_path` will find the proper commit_oid"
3+
# Build tracing currently does not support Windows 2022, so use `windows-2019` instead of
4+
# `windows-latest`.
5+
# Must test on all three platforms since this test does path manipulation
6+
os: [ubuntu-latest, macos-latest, windows-2019]
7+
steps:
8+
# Check out the actions repo again, but at a different location.
9+
# choose an arbitrary SHA so that we can later test that the commit_oid is not from main
10+
- uses: actions/checkout@v2
11+
with:
12+
ref: 474bbf07f9247ffe1856c6a0f94aeeb10e7afee6
13+
path: x/y/z/some-path
14+
- uses: ./../action/init
15+
with:
16+
tools: ${{ steps.prepare-test.outputs.tools-url }}
17+
# it's enough to test one compiled language and one interpreted language
18+
languages: java,javascript
19+
source-path: x/y/z/some-path/tests/multi-language-repo
20+
debug: true
21+
22+
- name: Build code
23+
shell: bash
24+
run: x/y/z/some-path/tests/multi-language-repo/build.sh
25+
- uses: ./../action/analyze
26+
with:
27+
checkout_path: x/y/z/some-path/tests/multi-language-repo
28+
ref: v1.1.0
29+
sha: 474bbf07f9247ffe1856c6a0f94aeeb10e7afee6
30+
upload: false
31+
env:
32+
TEST_MODE: true
33+
34+
- name: Verify SARIF after analyze
35+
shell: bash
36+
run: |
37+
EXPECTED_COMMIT_OID="474bbf07f9247ffe1856c6a0f94aeeb10e7afee6"
38+
EXPECTED_REF="v1.1.0"
39+
EXPECTED_CHECKOUT_URI_SUFFIX="/x/y/z/some-path/tests/multi-language-repo"
40+
41+
ACTUAL_COMMIT_OID="$(cat "$RUNNER_TEMP/payload.json" | jq -r .commit_oid)"
42+
ACTUAL_REF="$(cat "$RUNNER_TEMP/payload.json" | jq -r .ref)"
43+
ACTUAL_CHECKOUT_URI="$(cat "$RUNNER_TEMP/payload.json" | jq -r .checkout_uri)"
44+
45+
if [[ "$EXPECTED_COMMIT_OID" != "$ACTUAL_COMMIT_OID" ]]; then
46+
echo "::error Invalid commit oid. Expected: $EXPECTED_COMMIT_OID Actual: $ACTUAL_COMMIT_OID"
47+
echo "$RUNNER_TEMP/payload.json"
48+
exit 1
49+
fi
50+
51+
if [[ "$EXPECTED_REF" != "$ACTUAL_REF" ]]; then
52+
echo "::error Invalid ref. Expected: '$EXPECTED_REF' Actual: '$ACTUAL_REF'"
53+
echo "$RUNNER_TEMP/payload.json"
54+
exit 1
55+
fi
56+
57+
if [[ "$ACTUAL_CHECKOUT_URI" != *$EXPECTED_CHECKOUT_URI_SUFFIX ]]; then
58+
echo "::error Invalid checkout URI suffix. Expected suffix: $EXPECTED_CHECKOUT_URI_SUFFIX Actual uri: $ACTUAL_CHECKOUT_URI"
59+
echo "$RUNNER_TEMP/payload.json"
60+
exit 1
61+
fi
62+
63+
# payload.json will be recreated in the next step.
64+
rm -f "$RUNNER_TEMP/payload.json"
65+
66+
- uses: ./../action/upload-sarif
67+
with:
68+
ref: v1.1.0
69+
sha: 474bbf07f9247ffe1856c6a0f94aeeb10e7afee6
70+
checkout_path: x/y/z/some-path/tests/multi-language-repo
71+
env:
72+
TEST_MODE: true
73+
74+
- name: Verify SARIF after upload
75+
shell: bash
76+
run: |
77+
EXPECTED_COMMIT_OID="474bbf07f9247ffe1856c6a0f94aeeb10e7afee6"
78+
EXPECTED_REF="v1.1.0"
79+
EXPECTED_CHECKOUT_URI_SUFFIX="/x/y/z/some-path/tests/multi-language-repo"
80+
81+
ACTUAL_COMMIT_OID="$(cat "$RUNNER_TEMP/payload.json" | jq -r .commit_oid)"
82+
ACTUAL_REF="$(cat "$RUNNER_TEMP/payload.json" | jq -r .ref)"
83+
ACTUAL_CHECKOUT_URI="$(cat "$RUNNER_TEMP/payload.json" | jq -r .checkout_uri)"
84+
85+
if [[ "$EXPECTED_COMMIT_OID" != "$ACTUAL_COMMIT_OID" ]]; then
86+
echo "::error Invalid commit oid. Expected: $EXPECTED_COMMIT_OID Actual: $ACTUAL_COMMIT_OID"
87+
echo "$RUNNER_TEMP/payload.json"
88+
exit 1
89+
fi
90+
91+
if [[ "$EXPECTED_REF" != "$ACTUAL_REF" ]]; then
92+
echo "::error Invalid ref. Expected: '$EXPECTED_REF' Actual: '$ACTUAL_REF'"
93+
echo "$RUNNER_TEMP/payload.json"
94+
exit 1
95+
fi
96+
97+
if [[ "$ACTUAL_CHECKOUT_URI" != *$EXPECTED_CHECKOUT_URI_SUFFIX ]]; then
98+
echo "::error Invalid checkout URI suffix. Expected suffix: $EXPECTED_CHECKOUT_URI_SUFFIX Actual uri: $ACTUAL_CHECKOUT_URI"
99+
echo "$RUNNER_TEMP/payload.json"
100+
exit 1
101+
fi

src/actions-util.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export function getToolCacheDirectory(): string {
6060
/**
6161
* Gets the SHA of the commit that is currently checked out.
6262
*/
63-
export const getCommitOid = async function (ref = "HEAD"): Promise<string> {
63+
export const getCommitOid = async function (
64+
checkoutPath: string,
65+
ref = "HEAD"
66+
): Promise<string> {
6467
// Try to use git to get the current commit SHA. If that fails then
6568
// log but otherwise silently fall back to using the SHA from the environment.
6669
// The only time these two values will differ is during analysis of a PR when
@@ -83,6 +86,7 @@ export const getCommitOid = async function (ref = "HEAD"): Promise<string> {
8386
process.stderr.write(data);
8487
},
8588
},
89+
cwd: checkoutPath,
8690
}
8791
).exec();
8892
return commitOid.trim();
@@ -107,6 +111,7 @@ export const determineMergeBaseCommitOid = async function (): Promise<
107111
}
108112

109113
const mergeSha = getRequiredEnvParam("GITHUB_SHA");
114+
const checkoutPath = getRequiredInput("checkout_path");
110115

111116
try {
112117
let commitOid = "";
@@ -134,6 +139,7 @@ export const determineMergeBaseCommitOid = async function (): Promise<
134139
process.stderr.write(data);
135140
},
136141
},
142+
cwd: checkoutPath,
137143
}
138144
).exec();
139145

@@ -498,6 +504,10 @@ export async function getRef(): Promise<string> {
498504
// or in the form "refs/pull/N/merge" on a pull_request event
499505
const refInput = getOptionalInput("ref");
500506
const shaInput = getOptionalInput("sha");
507+
const checkoutPath =
508+
getOptionalInput("checkout_path") ||
509+
getOptionalInput("source-root") ||
510+
getRequiredEnvParam("GITHUB_WORKSPACE");
501511

502512
const hasRefInput = !!refInput;
503513
const hasShaInput = !!shaInput;
@@ -526,7 +536,7 @@ export async function getRef(): Promise<string> {
526536
return ref;
527537
}
528538

529-
const head = await getCommitOid("HEAD");
539+
const head = await getCommitOid(checkoutPath, "HEAD");
530540

531541
// in actions/checkout@v2 we can check if git rev-parse HEAD == GITHUB_SHA
532542
// in actions/checkout@v1 this may not be true as it checks out the repository
@@ -535,8 +545,10 @@ export async function getRef(): Promise<string> {
535545
// git git-parse GITHUB_REF == git rev-parse HEAD instead.
536546
const hasChangedRef =
537547
sha !== head &&
538-
(await getCommitOid(ref.replace(/^refs\/pull\//, "refs/remotes/pull/"))) !==
539-
head;
548+
(await getCommitOid(
549+
checkoutPath,
550+
ref.replace(/^refs\/pull\//, "refs/remotes/pull/")
551+
)) !== head;
540552

541553
if (hasChangedRef) {
542554
const newRef = ref.replace(pull_ref_regex, "refs/pull/$1/head");

src/upload-lib.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ async function uploadPayload(
100100
// If in test mode we don't want to upload the results
101101
const testMode = process.env["TEST_MODE"] === "true" || false;
102102
if (testMode) {
103-
logger.debug("In test mode. Results are not uploaded.");
103+
const payloadSaveFile = path.join(
104+
actionsUtil.getTemporaryDirectory(),
105+
"payload.json"
106+
);
107+
logger.info(
108+
`In test mode. Results are not uploaded. Saving to ${payloadSaveFile}`
109+
);
110+
logger.info(`Payload: ${JSON.stringify(payload, null, 2)}`);
111+
fs.writeFileSync(payloadSaveFile, JSON.stringify(payload, null, 2));
104112
return;
105113
}
106114

@@ -165,7 +173,9 @@ export async function uploadFromActions(
165173
return await uploadFiles(
166174
getSarifFilePaths(sarifPath),
167175
parseRepositoryNwo(util.getRequiredEnvParam("GITHUB_REPOSITORY")),
168-
await actionsUtil.getCommitOid(),
176+
await actionsUtil.getCommitOid(
177+
actionsUtil.getRequiredInput("checkout_path")
178+
),
169179
await actionsUtil.getRef(),
170180
await actionsUtil.getAnalysisKey(),
171181
actionsUtil.getOptionalInput("category"),

0 commit comments

Comments
 (0)