Skip to content

Commit

Permalink
Merge pull request #1394 from argos-ci/support-parent-commits-in-api
Browse files Browse the repository at this point in the history
feat(gh-light): support parent commits
  • Loading branch information
gregberge authored Oct 11, 2024
2 parents c88f4b5 + 6c44230 commit 88f8ff7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
17 changes: 17 additions & 0 deletions apps/backend/db/migrations/20241011152207_parent-commits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {import('knex').Knex} knex
*/
export const up = async (knex) => {
await knex.schema.alterTable("builds", async (table) => {
table.jsonb("parentCommits");
});
};

/**
* @param {import('knex').Knex} knex
*/
export const down = async (knex) => {
await knex.schema.alterTable("builds", async (table) => {
table.dropColumn("parentCommits");
});
};
4 changes: 3 additions & 1 deletion apps/backend/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ CREATE TABLE public.builds (
partial boolean DEFAULT false NOT NULL,
metadata jsonb,
"baseBranchResolvedFrom" text,
"parentCommits" jsonb,
CONSTRAINT "builds_baseBranchResolvedFrom_check" CHECK (("baseBranchResolvedFrom" = ANY (ARRAY['user'::text, 'pull-request'::text, 'project'::text]))),
CONSTRAINT builds_mode_check CHECK ((mode = ANY (ARRAY['ci'::text, 'monitoring'::text]))),
CONSTRAINT builds_type_check CHECK ((type = ANY (ARRAY['reference'::text, 'check'::text, 'orphan'::text])))
Expand Down Expand Up @@ -2837,4 +2838,5 @@ INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('2024070
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240822082247_github-light.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20240901150444_build-metadata.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20241006153157_reference-branch.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20241008035928_fix-enum.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20241008035928_fix-enum.js', 1, NOW());
INSERT INTO public.knex_migrations(name, batch, migration_time) VALUES ('20241011152207_parent-commits.js', 1, NOW());
2 changes: 2 additions & 0 deletions apps/backend/src/api/handlers/createBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const RequestBodySchema = z
referenceCommit: z.string().nullable().optional(),
// To avoid breaking change, we keep referenceBranch instead of baseBranch
referenceBranch: z.string().nullable().optional(),
parentCommits: z.array(Sha1HashSchema).nullable().optional(),
mode: z.enum(["ci", "monitoring"]).nullable().optional(),
ciProvider: z.string().nullable().optional(),
argosSdk: z.string().nullable().optional(),
Expand Down Expand Up @@ -259,6 +260,7 @@ async function createBuildFromRequest(ctx: BuildContext) {
prNumber: body.prNumber ?? null,
prHeadCommit: body.prHeadCommit ?? null,
baseCommit: body.referenceCommit ?? null,
parentCommits: body.parentCommits ?? null,
baseBranch: body.referenceBranch ?? null,
runId: body.runId ?? null,
runAttempt: body.runAttempt ?? null,
Expand Down
18 changes: 10 additions & 8 deletions apps/backend/src/build/createBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function createBuild(params: {
prNumber: number | null;
prHeadCommit: string | null;
baseCommit: string | null;
parentCommits: string[] | null;
baseBranch: string | null;
mode: BuildMode | null;
ciProvider: string | null;
Expand Down Expand Up @@ -146,18 +147,19 @@ export async function createBuild(params: {
batchCount: params.parallel ? 0 : null,
projectId: params.project.id,
name: buildName,
prNumber: params.prNumber ?? null,
prHeadCommit: params.prHeadCommit ?? null,
prNumber: params.prNumber,
prHeadCommit: params.prHeadCommit,
githubPullRequestId: pullRequest?.id ? String(pullRequest?.id) : null,
baseCommit: params.baseCommit ?? null,
baseBranch: params.baseBranch ?? null,
baseCommit: params.baseCommit,
parentCommits: params.parentCommits,
baseBranch: params.baseBranch,
baseBranchResolvedFrom: params.baseBranch ? "user" : null,
compareScreenshotBucketId: bucket.id,
mode,
ciProvider: params.ciProvider ?? null,
argosSdk: params.argosSdk ?? null,
runId: params.runId ?? null,
runAttempt: params.runAttempt ?? null,
ciProvider: params.ciProvider,
argosSdk: params.argosSdk,
runId: params.runId,
runAttempt: params.runAttempt,
partial: isPartial,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { checkErrorStatus, getInstallationOctokit } from "@/github/index.js";
import { UnretryableError } from "@/job-core/index.js";

import { queryBaseBucket } from "../query.js";
import { MergeBaseStrategy } from "../types.js";

type Octokit = NonNullable<Awaited<ReturnType<typeof getInstallationOctokit>>>;
Expand Down Expand Up @@ -81,16 +80,7 @@ export const GithubStrategy: MergeBaseStrategy<{
// We can't know for sure that it's a parent, but it's the best we can do.
// It can result into diffs that includes changes more recent than the current branch.
if (args.ctx.installation.app === "light") {
if (!args.build.baseBranch) {
return [];
}

const lastBucket = await queryBaseBucket(args.build)
.where("branch", args.build.baseBranch)
.orderBy("id", "desc")
.first();

return lastBucket ? [lastBucket.commit] : [];
return args.build.parentCommits ?? [];
}

try {
Expand Down
18 changes: 17 additions & 1 deletion apps/backend/src/database/models/Build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
import { z } from "zod";

import config from "@/config/index.js";
import { SHA1_REGEX } from "@/web/constants.js";

import {
BuildMetadata,
Expand Down Expand Up @@ -89,7 +90,21 @@ export class Build extends Model {
prNumber: { type: ["integer", "null"] },
prHeadCommit: { type: ["string", "null"] },
githubPullRequestId: { type: ["string", "null"] },
baseCommit: { type: ["string", "null"] },
baseCommit: {
oneOf: [
{ type: "null" },
{ type: "string", pattern: SHA1_REGEX.source },
],
},
parentCommits: {
oneOf: [
{ type: "null" },
{
type: "array",
items: { type: "string", pattern: SHA1_REGEX.source },
},
],
},
baseBranch: { type: ["string", "null"] },
baseBranchResolvedFrom: {
oneOf: [
Expand Down Expand Up @@ -123,6 +138,7 @@ export class Build extends Model {
prHeadCommit!: string | null;
githubPullRequestId!: string | null;
baseCommit!: string | null;
parentCommits!: string[] | null;
baseBranch!: string | null;
baseBranchResolvedFrom!: "user" | "pull-request" | "project" | null;
mode!: BuildMode;
Expand Down

0 comments on commit 88f8ff7

Please sign in to comment.