Skip to content

Commit fc0f2b3

Browse files
committed
fix: project scoping for runs
1 parent d10281e commit fc0f2b3

File tree

8 files changed

+104
-58
lines changed

8 files changed

+104
-58
lines changed

apps/webapp/app/presenters/v3/RunPresenter.server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ export class RunPresenter {
2828
public async call({
2929
userId,
3030
projectSlug,
31-
organizationSlug,
3231
environmentSlug,
3332
runFriendlyId,
3433
showDeletedLogs,
3534
showDebug,
3635
}: {
3736
userId: string;
3837
projectSlug: string;
39-
organizationSlug: string;
4038
environmentSlug: string;
4139
runFriendlyId: string;
4240
showDeletedLogs: boolean;
@@ -93,6 +91,13 @@ export class RunPresenter {
9391
friendlyId: runFriendlyId,
9492
project: {
9593
slug: projectSlug,
94+
organization: {
95+
members: {
96+
some: {
97+
userId,
98+
},
99+
},
100+
},
96101
},
97102
},
98103
});

apps/webapp/app/presenters/v3/SpanPresenter.server.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,26 @@ type GetSpanResult = NonNullable<Awaited<ReturnType<(typeof eventRepository)["ge
2828

2929
export class SpanPresenter extends BasePresenter {
3030
public async call({
31+
userId,
3132
projectSlug,
3233
spanId,
3334
runFriendlyId,
3435
}: {
36+
userId: string;
3537
projectSlug: string;
3638
spanId: string;
3739
runFriendlyId: string;
3840
}) {
3941
const project = await this._replica.project.findFirst({
4042
where: {
4143
slug: projectSlug,
44+
organization: {
45+
members: {
46+
some: {
47+
userId,
48+
},
49+
},
50+
},
4251
},
4352
});
4453

@@ -57,20 +66,19 @@ export class SpanPresenter extends BasePresenter {
5766
},
5867
where: {
5968
friendlyId: runFriendlyId,
69+
projectId: project.id,
6070
},
6171
});
6272

6373
if (!parentRun) {
6474
return;
6575
}
6676

67-
const { traceId } = parentRun;
68-
6977
const eventStore = getTaskEventStoreTableForRun(parentRun);
7078

7179
const run = await this.getRun({
7280
eventStore,
73-
traceId,
81+
environmentId: parentRun.runtimeEnvironmentId,
7482
spanId,
7583
createdAt: parentRun.createdAt,
7684
completedAt: parentRun.completedAt,
@@ -82,10 +90,8 @@ export class SpanPresenter extends BasePresenter {
8290
};
8391
}
8492

85-
//get the run
8693
const span = await this.#getSpan({
8794
eventStore,
88-
traceId,
8995
spanId,
9096
environmentId: parentRun.runtimeEnvironmentId,
9197
projectId: parentRun.projectId,
@@ -105,24 +111,24 @@ export class SpanPresenter extends BasePresenter {
105111

106112
async getRun({
107113
eventStore,
108-
traceId,
114+
environmentId,
109115
spanId,
110116
createdAt,
111117
completedAt,
112118
}: {
113119
eventStore: TaskEventStoreTable;
114-
traceId: string;
120+
environmentId: string;
115121
spanId: string;
116122
createdAt: Date;
117123
completedAt: Date | null;
118124
}) {
119-
const span = await eventRepository.getSpan(
120-
eventStore,
125+
const span = await eventRepository.getSpan({
126+
storeTable: eventStore,
121127
spanId,
122-
traceId,
123-
createdAt,
124-
completedAt ?? undefined
125-
);
128+
environmentId,
129+
startCreatedAt: createdAt,
130+
endCreatedAt: completedAt ?? undefined,
131+
});
126132

127133
if (!span) {
128134
return;
@@ -412,29 +418,28 @@ export class SpanPresenter extends BasePresenter {
412418

413419
async #getSpan({
414420
eventStore,
415-
traceId,
416421
spanId,
417422
environmentId,
418423
projectId,
419424
createdAt,
420425
completedAt,
421426
}: {
422-
traceId: string;
423427
spanId: string;
424428
environmentId: string;
425429
projectId: string;
426430
eventStore: TaskEventStoreTable;
427431
createdAt: Date;
428432
completedAt: Date | null;
429433
}) {
430-
const span = await eventRepository.getSpan(
431-
eventStore,
434+
const span = await eventRepository.getSpan({
435+
storeTable: eventStore,
432436
spanId,
433-
traceId,
434-
createdAt,
435-
completedAt ?? undefined,
436-
{ includeDebugLogs: true }
437-
);
437+
environmentId,
438+
startCreatedAt: createdAt,
439+
endCreatedAt: completedAt ?? undefined,
440+
options: { includeDebugLogs: true },
441+
});
442+
438443
if (!span) {
439444
return;
440445
}

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.$runParam/route.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
142142
const [error, result] = await tryCatch(
143143
presenter.call({
144144
userId,
145-
organizationSlug,
146145
showDeletedLogs: !!impersonationId,
147146
projectSlug: projectParam,
148147
runFriendlyId: runParam,

apps/webapp/app/routes/orgs.$organizationSlug.projects.$projectParam.runs.$runParam.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@ import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
33
import { z } from "zod";
44
import { prisma } from "~/db.server";
55
import { requireUserId } from "~/services/session.server";
6-
import { ProjectParamSchema, v3DeploymentPath, v3RunPath } from "~/utils/pathBuilder";
6+
import { ProjectParamSchema, v3RunPath } from "~/utils/pathBuilder";
77

88
const ParamSchema = ProjectParamSchema.extend({
99
runParam: z.string(),
1010
});
1111

1212
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
13-
await requireUserId(request);
13+
const userId = await requireUserId(request);
1414
const { organizationSlug, projectParam, runParam } = ParamSchema.parse(params);
1515

1616
const run = await prisma.taskRun.findFirst({
1717
where: {
1818
friendlyId: runParam,
1919
project: {
2020
slug: projectParam,
21+
organization: {
22+
members: {
23+
some: {
24+
userId,
25+
},
26+
},
27+
},
2128
},
2229
},
2330
select: {

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.runs.$runParam.spans.$spanParam/route.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ import {
7878
} from "~/utils/pathBuilder";
7979
import { createTimelineSpanEventsFromSpanEvents } from "~/utils/timelineSpanEvents";
8080
import { CompleteWaitpointForm } from "../resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.waitpoints.$waitpointFriendlyId.complete/route";
81+
import { requireUserId } from "~/services/session.server";
8182

8283
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
84+
const userId = await requireUserId(request);
8385
const { projectParam, organizationSlug, envParam, runParam, spanParam } =
8486
v3SpanParamsSchema.parse(params);
8587

@@ -90,6 +92,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
9092
projectSlug: projectParam,
9193
spanId: spanParam,
9294
runFriendlyId: runParam,
95+
userId,
9396
});
9497

9598
return typedjson(result);

apps/webapp/app/routes/resources.taskruns.$runParam.cancel.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { parse } from "@conform-to/zod";
2-
import { ActionFunction, json } from "@remix-run/node";
2+
import { type ActionFunction, json } from "@remix-run/node";
33
import { z } from "zod";
44
import { prisma } from "~/db.server";
55
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
66
import { logger } from "~/services/logger.server";
7+
import { requireUserId } from "~/services/session.server";
78
import { CancelTaskRunService } from "~/v3/services/cancelTaskRun.server";
89

910
export const cancelSchema = z.object({
@@ -15,6 +16,7 @@ const ParamSchema = z.object({
1516
});
1617

1718
export const action: ActionFunction = async ({ request, params }) => {
19+
const userId = await requireUserId(request);
1820
const { runParam } = ParamSchema.parse(params);
1921

2022
const formData = await request.formData();
@@ -25,9 +27,18 @@ export const action: ActionFunction = async ({ request, params }) => {
2527
}
2628

2729
try {
28-
const taskRun = await prisma.taskRun.findUnique({
30+
const taskRun = await prisma.taskRun.findFirst({
2931
where: {
3032
friendlyId: runParam,
33+
project: {
34+
organization: {
35+
members: {
36+
some: {
37+
userId,
38+
},
39+
},
40+
},
41+
},
3142
},
3243
});
3344

apps/webapp/app/routes/resources.taskruns.$runParam.debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LoaderFunctionArgs } from "@remix-run/node";
1+
import { type LoaderFunctionArgs } from "@remix-run/node";
22
import { typedjson } from "remix-typedjson";
33
import { z } from "zod";
44
import { $replica } from "~/db.server";

apps/webapp/app/v3/eventRepository.server.ts

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -970,22 +970,30 @@ export class EventRepository {
970970

971971
// A Span can be cancelled if it is partial and has a parent that is cancelled
972972
// And a span's duration, if it is partial and has a cancelled parent, is the time between the start of the span and the time of the cancellation event of the parent
973-
public async getSpan(
974-
storeTable: TaskEventStoreTable,
975-
spanId: string,
976-
traceId: string,
977-
startCreatedAt: Date,
978-
endCreatedAt?: Date,
979-
options?: { includeDebugLogs?: boolean }
980-
) {
981-
return await startActiveSpan("getSpan", async (s) => {
982-
const spanEvent = await this.#getSpanEvent(
973+
public async getSpan({
974+
storeTable,
975+
spanId,
976+
environmentId,
977+
startCreatedAt,
978+
endCreatedAt,
979+
options,
980+
}: {
981+
storeTable: TaskEventStoreTable;
982+
spanId: string;
983+
environmentId: string;
984+
startCreatedAt: Date;
985+
endCreatedAt?: Date;
986+
options?: { includeDebugLogs?: boolean };
987+
}) {
988+
return await startActiveSpan("getSpan", async () => {
989+
const spanEvent = await this.#getSpanEvent({
983990
storeTable,
984991
spanId,
992+
environmentId,
985993
startCreatedAt,
986994
endCreatedAt,
987-
options
988-
);
995+
options,
996+
});
989997

990998
if (!spanEvent) {
991999
return;
@@ -1195,12 +1203,12 @@ export class EventRepository {
11951203
}
11961204

11971205
await startActiveSpan("walkSpanAncestors", async (s) => {
1198-
let parentEvent = await this.#getSpanEvent(
1206+
let parentEvent = await this.#getSpanEvent({
11991207
storeTable,
1200-
parentId,
1208+
spanId: parentId,
12011209
startCreatedAt,
1202-
endCreatedAt
1203-
);
1210+
endCreatedAt,
1211+
});
12041212
let level = 1;
12051213

12061214
while (parentEvent) {
@@ -1216,29 +1224,37 @@ export class EventRepository {
12161224
return;
12171225
}
12181226

1219-
parentEvent = await this.#getSpanEvent(
1227+
parentEvent = await this.#getSpanEvent({
12201228
storeTable,
1221-
preparedParentEvent.parentId,
1229+
spanId: preparedParentEvent.parentId,
12221230
startCreatedAt,
1223-
endCreatedAt
1224-
);
1231+
endCreatedAt,
1232+
});
12251233

12261234
level++;
12271235
}
12281236
});
12291237
}
12301238

1231-
async #getSpanEvent(
1232-
storeTable: TaskEventStoreTable,
1233-
spanId: string,
1234-
startCreatedAt: Date,
1235-
endCreatedAt?: Date,
1236-
options?: { includeDebugLogs?: boolean }
1237-
) {
1239+
async #getSpanEvent({
1240+
storeTable,
1241+
spanId,
1242+
environmentId,
1243+
startCreatedAt,
1244+
endCreatedAt,
1245+
options,
1246+
}: {
1247+
storeTable: TaskEventStoreTable;
1248+
spanId: string;
1249+
environmentId?: string;
1250+
startCreatedAt: Date;
1251+
endCreatedAt?: Date;
1252+
options?: { includeDebugLogs?: boolean };
1253+
}) {
12381254
return await startActiveSpan("getSpanEvent", async (s) => {
12391255
const events = await this.taskEventStore.findMany(
12401256
storeTable,
1241-
{ spanId },
1257+
{ spanId, ...(environmentId ? { environmentId } : {}) },
12421258
startCreatedAt,
12431259
endCreatedAt,
12441260
undefined,

0 commit comments

Comments
 (0)