Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const RPC_REQUIRED_SCOPES = {
[WS_METHODS.issuesList]: AuthOrchestrationReadScope,
[WS_METHODS.issuesDetail]: AuthOrchestrationReadScope,
[WS_METHODS.issuesComments]: AuthOrchestrationReadScope,
[WS_METHODS.issuesTimeline]: AuthOrchestrationReadScope,
[WS_METHODS.issuesCandidates]: AuthOrchestrationReadScope,
[WS_METHODS.issuesTemplates]: AuthOrchestrationReadScope,
[WS_METHODS.issuesCreate]: AuthOrchestrationOperateScope,
Expand Down
26 changes: 23 additions & 3 deletions apps/server/src/issue/IssueCompletionReactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ function runCase(input: {
readonly repeat?: boolean;
readonly noIssue?: boolean;
readonly otherPullRequestState?: "open" | "closed" | "merged";
readonly linkState?: "open" | "closed";
}) {
return Effect.scoped(
Effect.gen(function* () {
const current = links(input.pullRequestState);
const projectLinks: ProjectWorktreeLinks.ProjectWorktreeLinkSet = {
worktreeIssues: input.noIssue ? [] : current.worktreeIssues,
worktreeIssues: input.noIssue
? []
: current.worktreeIssues.map((link) =>
input.linkState === undefined ? link : { ...link, state: input.linkState },
),
worktreePullRequests: input.otherPullRequestState
? [
...current.worktreePullRequests,
Expand All @@ -128,6 +133,7 @@ function runCase(input: {
const closes: IssueCloseInput[] = [];
const comments: IssueCommentCreateInput[] = [];
const calls: string[] = [];
let detailReads = 0;
const savedComments: IssueComment[] = input.existingComment
? [{ id: "existing", author: null, body: COMMENT_BODY, createdAt: NOW, url: null }]
: [];
Expand All @@ -148,7 +154,7 @@ function runCase(input: {
}),
ServerSettingsService.layerTest({ completeLinkedIssueOnMerge: input.enabled }),
Layer.mock(IssueService)({
detail: () => Effect.succeed(currentDetail),
detail: () => Effect.sync(() => (detailReads += 1)).pipe(Effect.as(currentDetail)),
invalidate: () => Effect.void,
close: (closeInput) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -206,7 +212,7 @@ function runCase(input: {
const cleanupAllowed = input.repeat
? yield* reactor.completeWorktree(PROJECT_ID, WORKTREE_PATH)
: undefined;
return { closes, comments, calls, cleanupAllowed };
return { closes, comments, calls, cleanupAllowed, detailReads };
}).pipe(Effect.provide(layer));
}),
);
Expand Down Expand Up @@ -263,6 +269,20 @@ describe("IssueCompletionReactor", () => {
}),
);

it.effect("reads linked issues once their worktree merges so their state refreshes", () =>
Effect.gen(function* () {
const merged = yield* runCase({ enabled: false, pullRequestState: "merged" });
assert.strictEqual(merged.detailReads, 1);
assert.deepStrictEqual(merged.calls, []);
for (const input of [
{ enabled: false, pullRequestState: "open" },
{ enabled: false, pullRequestState: "merged", linkState: "closed" },
] as const) {
assert.strictEqual((yield* runCase(input)).detailReads, 0);
}
}),
);

it.effect("finds a previous completion comment on later pages without posting it again", () =>
Effect.gen(function* () {
const result = yield* runCase({
Expand Down
42 changes: 41 additions & 1 deletion apps/server/src/issue/IssueCompletionReactor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ProjectId } from "@t3tools/contracts";
import { type IssueLinkedWork, type ProjectId } from "@t3tools/contracts";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import * as Context from "effect/Context";
Expand Down Expand Up @@ -87,6 +87,28 @@ export const make = Effect.gen(function* () {
),
);

// GitHub closes issues a merged PR references; reading one records its state on the link.
const refreshState = (projectId: ProjectId, link: IssueLinkedWork, urls: string) => {
const key = `refresh:${link.issue.host}/${link.issue.repository}#${link.issue.number}:${urls}`;
if (handled.has(key)) return Effect.void;
handled.add(key);
const reference = {
projectId,
host: link.issue.host,
repository: link.issue.repository,
number: link.issue.number,
};
return issues.invalidate({ reference }).pipe(
Effect.andThen(issues.detail(reference)),
Effect.asVoid,
Effect.catch((error) =>
Effect.logWarning("linked issue state refresh failed", { error }).pipe(
Effect.andThen(Effect.sync(() => handled.delete(key))),
),
),
);
};

const completeProject = Effect.fn("IssueCompletionReactor.completeProject")(function* (
projectId: ProjectId,
) {
Expand All @@ -103,6 +125,24 @@ export const make = Effect.gen(function* () {
concurrency: 4,
discard: true,
});
yield* Effect.forEach(
(project.worktreeIssues ?? []).filter(
(link) => link.state !== "closed" && !candidates.includes(link),
),
(link) => {
const pullRequests = (project.worktreePullRequests ?? []).filter(
(pullRequest) => pullRequest.worktreePath === link.worktreePath,
);
if (
pullRequests.length === 0 ||
pullRequests.some((pullRequest) => pullRequest.snapshot?.state !== "merged")
)
return Effect.void;
const urls = [...new Set(pullRequests.map((pullRequest) => pullRequest.url))].sort();
return refreshState(projectId, link, urls.join(","));
},
{ concurrency: 4, discard: true },
);
});

const worker = yield* makeDrainableWorker((projectId: ProjectId) =>
Expand Down
Loading
Loading