fix(pull-requests): shared state + not settling? (#9332)
Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
1 parent
48ba76bc26
commit
854541a04e
9 files changed
+440
-166
No files matched your search
@@ -1,11 +1,15 @@
|
||||
import * as Cache from "effect/Cache";
|
||||
import * as Clock from "effect/Clock";
|
||||
import * as Context from "effect/Context";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as PubSub from "effect/PubSub";
|
||||
import * as Schema from "effect/Schema";
|
||||
import type * as Scope from "effect/Scope";
|
||||
import * as Stream from "effect/Stream";
|
||||
import {
|
||||
PullRequestOperationError,
|
||||
PullRequestUnavailableError,
|
||||
@@ -63,6 +67,10 @@ import {
|
||||
} from "./PullRequestProvider.ts";
|
||||
import { PullRequestProviderRegistry } from "./PullRequestProviderRegistry.ts";
|
||||
|
||||
export interface PullRequestMergeEvent extends PullRequestRef {
|
||||
readonly mergedAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows per repository when the client does not ask for a page size, and rows per slice when a
|
||||
* listing is carried on from a cursor.
|
||||
@@ -135,6 +143,11 @@ export class PullRequestService extends Context.Service<
|
||||
input: PullRequestRef,
|
||||
options?: { readonly recoverTransientFailure?: boolean },
|
||||
) => Effect.Effect<PullRequestSummary, PullRequestError>;
|
||||
readonly subscribeMerges: Effect.Effect<
|
||||
Stream.Stream<PullRequestMergeEvent>,
|
||||
never,
|
||||
Scope.Scope
|
||||
>;
|
||||
readonly detail: (input: PullRequestRef) => Effect.Effect<PullRequestDetail, PullRequestError>;
|
||||
readonly activity: (
|
||||
input: PullRequestRef,
|
||||
@@ -517,6 +530,7 @@ export function repositoryIdentityOf(project: OrchestrationProjectShell): string
|
||||
}
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const mergedPullRequests = yield* PubSub.sliding<PullRequestMergeEvent>(64);
|
||||
const registry = yield* PullRequestProviderRegistry;
|
||||
const projections = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
|
||||
const sourceControlProviders = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
|
||||
@@ -1410,9 +1424,9 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
const runAction: PullRequestService["Service"]["runAction"] = (input) =>
|
||||
const runAction = (input: PullRequestActionInput): Effect.Effect<string, PullRequestError> =>
|
||||
requireProject(input).pipe(
|
||||
Effect.flatMap((project): Effect.Effect<void, PullRequestError> => {
|
||||
Effect.flatMap((project): Effect.Effect<string, PullRequestError> => {
|
||||
// The surface hides what a host cannot do, and this refuses it as well: a request that
|
||||
// reached here anyway must not be handed to a provider that never claimed the action.
|
||||
if (!project.api.capabilities.actions.includes(input.action)) {
|
||||
@@ -1454,7 +1468,7 @@ export const make = Effect.gen(function* () {
|
||||
// have to say yes. The second is asked last, because it costs a request and the checks
|
||||
// above do not.
|
||||
return viewerPermissionsOf(project, input, "runAction").pipe(
|
||||
Effect.flatMap((viewer): Effect.Effect<void, PullRequestError> => {
|
||||
Effect.flatMap((viewer): Effect.Effect<string, PullRequestError> => {
|
||||
if (!viewer.actions.includes(input.action)) {
|
||||
return Effect.fail(
|
||||
new PullRequestOperationError({
|
||||
@@ -1484,7 +1498,10 @@ export const make = Effect.gen(function* () {
|
||||
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
|
||||
...(input.updateMethod === undefined ? {} : { updateMethod: input.updateMethod }),
|
||||
})
|
||||
.pipe(Effect.mapError(toPullRequestError("runAction")));
|
||||
.pipe(
|
||||
Effect.mapError(toPullRequestError("runAction")),
|
||||
Effect.as(project.repository),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}),
|
||||
@@ -2151,9 +2168,13 @@ export const make = Effect.gen(function* () {
|
||||
const summary: PullRequestService["Service"]["summary"] = (input, options) => {
|
||||
const key = refCacheKey(input);
|
||||
const cached = Cache.get(summaryCache, key);
|
||||
return options?.recoverTransientFailure === false
|
||||
? cached.pipe(Effect.tap((value) => lastGoodSummary.record(key, value)))
|
||||
: lastGoodSummary.serveHeld(key, cached, "reuse");
|
||||
if (options?.recoverTransientFailure !== false) {
|
||||
return lastGoodSummary.serveHeld(key, cached, "reuse");
|
||||
}
|
||||
const held = lastGoodSummary.peek(key);
|
||||
return held?.state === "merged"
|
||||
? Effect.succeed(held)
|
||||
: cached.pipe(Effect.tap((value) => lastGoodSummary.record(key, value)));
|
||||
};
|
||||
|
||||
// Keys serialize positionally and parse back in the lookup, so the cache is the only holder
|
||||
@@ -2391,17 +2412,35 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
),
|
||||
);
|
||||
const runActionAndInvalidate: PullRequestService["Service"]["runAction"] = Effect.fn(
|
||||
"PullRequestService.runActionAndInvalidate",
|
||||
)(function* (input) {
|
||||
const repository = yield* runAction(input);
|
||||
bumpRefEpoch({ ...input, repository });
|
||||
listingsEpoch = ++epochCounter;
|
||||
if (input.action === "merge") {
|
||||
yield* PubSub.publish(mergedPullRequests, {
|
||||
projectId: input.projectId,
|
||||
repository,
|
||||
number: input.number,
|
||||
mergedAt: DateTime.formatIso(yield* DateTime.now),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return PullRequestService.of({
|
||||
list,
|
||||
listStats,
|
||||
summary,
|
||||
subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
|
||||
Effect.map((subscription) => Stream.fromSubscription(subscription)),
|
||||
),
|
||||
detail,
|
||||
activity,
|
||||
threadComments,
|
||||
diff,
|
||||
diffFileContents,
|
||||
runAction: invalidatedByMutation(runAction),
|
||||
runAction: runActionAndInvalidate,
|
||||
update: invalidatedByMutation(update),
|
||||
comment: invalidatedByMutation(comment),
|
||||
updateComment: invalidatedByMutation(updateComment),
|
||||
|
||||
Reference in new issue
Block a user