Skip to content

Commit

Permalink
Allow overriding of base and head commits
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardopirovano committed May 9, 2024
1 parent e23dec1 commit 8e68af4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 12 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ inputs:
additional-ports:
description: Ports the client needs to access the application in addition to the one in the app URL, as a list of comma-separated values
required: false
base-sha:
description: |
The base commit SHA to compare against. This can usually be inferred automatically by Meticulous, so typically you do not need to set this.
required: false
head-sha:
description: |
The head commit SHA that we are running on. This can usually be inferred automatically by Meticulous, so typically you do not need to set this.
required: false

outputs: {}
runs:
Expand All @@ -80,6 +88,8 @@ runs:
TEST_SUITE_ID: ${{ inputs.test-suite-id }}
METICULOUS_TELEMETRY_SAMPLE_RATE: "0.01"
ADDITIONAL_PORTS: ${{ inputs.additional-ports }}
BASE_SHA: ${{ inputs.base-sha }}
HEAD_SHA: ${{ inputs.head-sha }}
branding:
color: purple
icon: camera
8 changes: 8 additions & 0 deletions cloud-compute/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ inputs:
description: |
The URL to execute the tests against. This URL should serve the code from the current commit (e.g. a localhost URL served up by a local server).
required: true
base-sha:
description: |
The base commit SHA to compare against. This can usually be inferred automatically by Meticulous, so typically you do not need to set this.
required: false
head-sha:
description: |
The head commit SHA that we are running on. This can usually be inferred automatically by Meticulous, so typically you do not need to set this.
required: false

outputs: {}
runs:
Expand Down
8 changes: 4 additions & 4 deletions out/cloud-compute.entrypoint.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/actions/cloud-compute/cloud-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const runMeticulousTestsCloudComputeAction = async (): Promise<void> => {
setLogLevel("trace");
}

const { apiToken, githubToken, appUrl } = getInCloudActionInputs();
const { apiToken, githubToken, appUrl, baseSha, headSha } =
getInCloudActionInputs();
const { payload } = context;
const event = getCodeChangeEvent(context.eventName, payload);
const { owner, repo } = context.repo;
Expand All @@ -61,6 +62,8 @@ export const runMeticulousTestsCloudComputeAction = async (): Promise<void> => {

const { base, head } = await getBaseAndHeadCommitShas(event, {
useDeploymentUrl: false,
baseSha,
headSha,
});

const { shaToCompareAgainst } = await safeEnsureBaseTestsExists({
Expand Down
4 changes: 4 additions & 0 deletions src/actions/cloud-compute/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ export const getInCloudActionInputs = () => {
const apiToken = getInput("api-token", { required: true });
const githubToken = getInput("github-token", { required: true });
const appUrl = getInput("app-url", { required: true });
const baseSha = getInput("base-sha");
const headSha = getInput("head-sha");

return {
apiToken,
githubToken,
appUrl,
baseSha,
headSha,
};
};
4 changes: 4 additions & 0 deletions src/actions/main/__tests__/get-inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const keys = [
"USE_DEPLOYMENT_URL",
"ALLOWED_ENVIRONMENTS",
"ADDITIONAL_PORTS",
"BASE_SHA",
"HEAD_SHA",
];

const EXPECTED_DEFAULT_VALUES = {
Expand All @@ -30,6 +32,8 @@ const EXPECTED_DEFAULT_VALUES = {
testsFile: null,
testSuiteId: null,
additionalPorts: null,
baseSha: null,
headSha: null,
};

describe("getMainActionInputs", () => {
Expand Down
12 changes: 12 additions & 0 deletions src/actions/main/get-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ export const getMainActionInputs = () => {
required: false,
type: "string",
});
const baseSha = getInputFromEnv({
name: "base-sha",
required: false,
type: "string",
});
const headSha = getInputFromEnv({
name: "head-sha",
required: false,
type: "string",
});

if (appUrl != null && appUrl != "" && useDeploymentUrl === true) {
throw new Error("Cannot use both app-url and use-deployment-url");
Expand Down Expand Up @@ -99,5 +109,7 @@ export const getMainActionInputs = () => {
testSuiteId,
allowedEnvironments,
additionalPorts,
baseSha,
headSha,
};
};
4 changes: 4 additions & 0 deletions src/actions/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const runMeticulousTestsAction = async (): Promise<void> => {
allowedEnvironments,
testSuiteId,
additionalPorts,
baseSha,
headSha,
} = getMainActionInputs();
const { payload } = context;
const event = getCodeChangeEvent(context.eventName, payload);
Expand All @@ -79,6 +81,8 @@ export const runMeticulousTestsAction = async (): Promise<void> => {

const { base, head } = await getBaseAndHeadCommitShas(event, {
useDeploymentUrl,
baseSha,
headSha,
});
const environment = getEnvironment({ event, head });

Expand Down
22 changes: 15 additions & 7 deletions src/common/get-base-and-head-commit-shas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,44 @@ interface BaseAndHeadCommitShas {

export const getBaseAndHeadCommitShas = async (
event: CodeChangeEvent,
options: { useDeploymentUrl: boolean }
options: {
useDeploymentUrl: boolean;
baseSha: string | null;
headSha: string | null;
}
): Promise<BaseAndHeadCommitShas> => {
if (event.type === "pull_request") {
const head = event.payload.pull_request.head.sha;
const head = options.headSha || event.payload.pull_request.head.sha;
const base = event.payload.pull_request.base.sha;
const baseRef = event.payload.pull_request.base.ref;
if (options.useDeploymentUrl) {
// Vercel deploys the head commit of the PR, not the github temporary merge commit
// The PR base can sometimes point to a commit ahead of the merge-base of the head commit
// (I believe it's based on the github temporary merge commit)
return {
base: (await tryGetMergeBaseOfHeadCommit(head, base, baseRef)) ?? base,
base:
options.baseSha ||
((await tryGetMergeBaseOfHeadCommit(head, base, baseRef)) ?? base),
head,
};
}
return {
base: (await tryGetMergeBaseOfTemporaryMergeCommit(head, base)) ?? base,
base:
options.baseSha ||
((await tryGetMergeBaseOfTemporaryMergeCommit(head, base)) ?? base),
head,
};
}
if (event.type === "push") {
return {
base: event.payload.before,
head: event.payload.after,
base: options.baseSha || event.payload.before,
head: options.headSha || event.payload.after,
};
}
if (event.type === "workflow_dispatch") {
return {
base: null,
head: context.sha,
head: options.headSha || context.sha,
};
}
return assertNever(event);
Expand Down

0 comments on commit 8e68af4

Please sign in to comment.