Skip to content

Commit

Permalink
feat(no-access): send a list of commits
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Oct 11, 2024
1 parent 459eb33 commit 8c3ea7e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/api-client/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export interface operations {
prHeadCommit?: string | null;
referenceCommit?: string | null;
referenceBranch?: string | null;
parentCommits?: components["schemas"]["Sha1Hash"][] | null;
/** @enum {string|null} */
mode?: "ci" | "monitoring" | null;
ciProvider?: string | null;
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/ci-environment/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function getMergeBaseCommitSha(input: {
base: string;
head: string;
}): string | null {
let depth = 50;
let depth = 200;
while (depth < 1000) {
const mergeBase = getMergeBaseCommitShaWithDepth({
depth,
Expand All @@ -78,7 +78,18 @@ export function getMergeBaseCommitSha(input: {
if (mergeBase) {
return mergeBase;
}
depth += 50;
depth += 200;
}
return null;
}

export function listParentCommits(input: { sha: string }): string[] | null {
try {
execSync(`git fetch --depth 200 origin ${input.sha}`);
const raw = execSync(`git log --format "%H" -n 200 ${input.sha}`);
const shas = raw.toString().trim().split("\n");
return shas;
} catch {
return null;
}
}
11 changes: 11 additions & 0 deletions packages/core/src/ci-environment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export function getMergeBaseCommitSha(input: {
return service.getMergeBaseCommitSha(input, context);
}

/**
* Get the merge base commit.
*/
export function listParentCommits(input: { sha: string }): string[] | null {
const context = createContext();
const service = getCiService(context);
if (!service) {
return null;
}
return service.listParentCommits(input, context);
}
/**
* Get the CI environment.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/bitrise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMergeBaseCommitSha } from "../git";
import { getMergeBaseCommitSha, listParentCommits } from "../git";
import type { Service, Context } from "../types";

const getPrNumber = ({ env }: Context) => {
Expand All @@ -25,6 +25,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/buildkite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Service } from "../types";
import { head, branch, getMergeBaseCommitSha } from "../git";
import { head, branch, getMergeBaseCommitSha, listParentCommits } from "../git";

const service: Service = {
name: "Buildkite",
Expand All @@ -24,6 +24,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/circleci.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMergeBaseCommitSha } from "../git";
import { getMergeBaseCommitSha, listParentCommits } from "../git";
import type { Service, Context } from "../types";

const getPrNumber = ({ env }: Context) => {
Expand Down Expand Up @@ -31,6 +31,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
2 changes: 2 additions & 0 deletions packages/core/src/ci-environment/services/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
branch,
checkIsGitRepository,
getMergeBaseCommitSha,
listParentCommits,
} from "../git";

const service: Service = {
Expand All @@ -26,6 +27,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
import type { Service, Context } from "../types";
import axios from "axios";
import { debug } from "../../debug";
import { getMergeBaseCommitSha } from "../git";
import { getMergeBaseCommitSha, listParentCommits } from "../git";

type EventPayload = {
pull_request?: {
Expand Down Expand Up @@ -177,6 +177,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/gitlab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMergeBaseCommitSha } from "../git";
import { getMergeBaseCommitSha, listParentCommits } from "../git";
import type { Service } from "../types";

const service: Service = {
Expand All @@ -21,6 +21,7 @@ const service: Service = {
};
},
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
3 changes: 2 additions & 1 deletion packages/core/src/ci-environment/services/heroku.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Service } from "../types";
import { getMergeBaseCommitSha } from "../git";
import { getMergeBaseCommitSha, listParentCommits } from "../git";

const service: Service = {
name: "Heroku",
Expand All @@ -19,6 +19,7 @@ const service: Service = {
nonce: env.HEROKU_TEST_RUN_ID || null,
}),
getMergeBaseCommitSha,
listParentCommits,
};

export default service;
6 changes: 6 additions & 0 deletions packages/core/src/ci-environment/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ export interface Service {
},
ctx: Context,
): string | null;
listParentCommits(
input: {
sha: string;
},
ctx: Context,
): string[] | null;
}
22 changes: 17 additions & 5 deletions packages/core/src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { debug, debugTime, debugTimeEnd } from "./debug";
import { chunk } from "./util/chunk";
import { getPlaywrightTracePath, readMetadata } from "@argos-ci/util";
import { getArgosCoreSDKIdentifier } from "./version";
import { getMergeBaseCommitSha } from "./ci-environment";
import { getMergeBaseCommitSha, listParentCommits } from "./ci-environment";

/**
* Size of the chunks used to upload screenshots to Argos.
Expand Down Expand Up @@ -214,19 +214,21 @@ export async function upload(params: UploadParameters) {
throwAPIError(projectResponse.error);
}
const { defaultBaseBranch, hasRemoteContentAccess } = projectResponse.data;
const referenceBranch = config.referenceBranch || defaultBaseBranch;
const referenceCommit = (() => {
if (config.referenceCommit) {
debug("Found reference commit in config", config.referenceCommit);
return config.referenceCommit;
}

// If we have remote access, we will fetch it from the Git Provider.
if (hasRemoteContentAccess) {
return null;
}

// We use the pull request as base branch if possible.
const base = config.prBaseBranch || referenceBranch;
// We use the pull request as base branch if possible
// else branch specified by the user or the default branch.
const base =
config.referenceBranch || config.prBaseBranch || defaultBaseBranch;

const sha = getMergeBaseCommitSha({ base, head: config.branch });

Expand All @@ -239,6 +241,15 @@ export async function upload(params: UploadParameters) {
return sha;
})();

const parentCommits = (() => {
// If we have remote access, we will fetch them from the Git Provider.
if (hasRemoteContentAccess) {
return null;
}

return listParentCommits({ sha: config.commit });
})();

// Create build
debug("Creating build");
const [pwTraceKeys, screenshotKeys] = screenshots.reduce(
Expand Down Expand Up @@ -269,8 +280,9 @@ export async function upload(params: UploadParameters) {
pwTraceKeys,
prNumber: config.prNumber,
prHeadCommit: config.prHeadCommit,
referenceBranch,
referenceBranch: config.referenceBranch,
referenceCommit,
parentCommits,
argosSdk,
ciProvider: config.ciProvider,
runId: config.runId,
Expand Down

0 comments on commit 8c3ea7e

Please sign in to comment.