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
54 changes: 53 additions & 1 deletion apps/server/src/jira/JiraIssueBridge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
CommandId,
DEFAULT_PROVIDER_INTERACTION_MODE,
type EnvironmentId,
MessageId,
ProjectId,
ThreadId,
type OrchestrationThread,
type TurnId,
} from "@t3tools/contracts";
import { buildAgentAwarenessDeepLink } from "@t3tools/shared/agentAwareness";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
Expand All @@ -17,6 +19,8 @@ import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Semaphore from "effect/Semaphore";

import { hostedAppUrlConfig } from "../cloud/publicConfig.ts";
import { ServerEnvironment } from "../environment/ServerEnvironment.ts";
import {
discoverGitHubTargetTurnId,
githubFinalAnswerWithStats,
Expand Down Expand Up @@ -62,6 +66,40 @@ export function formatJiraComment(body: string): string {
return `${trimmed.slice(0, MAX_JIRA_COMMENT_LENGTH - 20)}\n\n…(truncated)`;
}

export function buildJiraT3ThreadUrl(input: {
readonly hostedAppUrl: string;
readonly environmentId: string;
readonly threadId: string;
}): string {
return new URL(
buildAgentAwarenessDeepLink({
environmentId: input.environmentId as EnvironmentId,
threadId: input.threadId as ThreadId,
}),
input.hostedAppUrl,
).toString();
}

export function appendJiraT3ThreadLink(
body: string,
input:
| {
readonly hostedAppUrl: string;
readonly environmentId: string;
readonly threadId: string;
}
| null
| undefined,
): string {
const base = body.trimEnd();
if (input === null || input === undefined) return base;
const url = buildJiraT3ThreadUrl(input);
if (base.includes(url)) return base;
const footer = `T3 thread: ${url}`;
if (base === "") return footer;
return `${base}\n\n${footer}`;
}

function isThreadBusy(thread: OrchestrationThread): boolean {
const latest = thread.latestTurn;
if (latest?.state === "running") return true;
Expand Down Expand Up @@ -89,9 +127,12 @@ const make = Effect.gen(function* () {
const jira = yield* JiraAppClient;
const engine = yield* OrchestrationEngineService;
const projection = yield* ProjectionSnapshotQuery;
const serverEnvironment = yield* ServerEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
const crypto = yield* Crypto.Crypto;
const createLock = yield* Semaphore.make(1);
const hostedAppUrl = yield* hostedAppUrlConfig;
const environmentId = yield* serverEnvironment.getEnvironmentId;

/**
* Post a bridge response as a **threaded reply** when possible.
Expand All @@ -101,7 +142,18 @@ const make = Effect.gen(function* () {
const postComment = (delivery: StoredJiraDelivery, body: string) =>
jira.addIssueComment({
issueKey: delivery.issueKey,
body: formatJiraComment(body),
body: formatJiraComment(
appendJiraT3ThreadLink(
body,
delivery.threadId === null
? null
: {
hostedAppUrl,
environmentId,
threadId: delivery.threadId,
},
),
),
parentCommentId: delivery.replyToCommentId || delivery.sourceCommentId || null,
});

Expand Down
33 changes: 32 additions & 1 deletion apps/server/src/jira/JiraWebhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
resolveMappedT3ProjectId,
resolveT3ProjectIdForJiraKey,
} from "./JiraAppConfig.ts";
import { formatJiraComment } from "./JiraIssueBridge.ts";
import {
appendJiraT3ThreadLink,
buildJiraT3ThreadUrl,
formatJiraComment,
} from "./JiraIssueBridge.ts";
import { resolveThreadIdForJiraIssue } from "./JiraThreadLookup.ts";
import {
classifyWebhookBodyFailure,
Expand Down Expand Up @@ -423,6 +427,33 @@ describe("Jira helpers", () => {
expect(resolveT3ProjectIdForJiraKey(map, "OTHER", projects)).toBeNull();
});

it("builds and appends a hosted T3 thread link for Jira replies", () => {
const url = buildJiraT3ThreadUrl({
hostedAppUrl: "https://nightly.app.t3.codes",
environmentId: "env-1",
threadId: "thread-1",
});
expect(url).toBe("https://nightly.app.t3.codes/threads/env-1/thread-1");
expect(
appendJiraT3ThreadLink("Ship it.", {
hostedAppUrl: "https://nightly.app.t3.codes",
environmentId: "env-1",
threadId: "thread-1",
}),
).toBe("Ship it.\n\nT3 thread: https://nightly.app.t3.codes/threads/env-1/thread-1");
});

it("does not duplicate the Jira T3 thread link footer", () => {
const body = "Ship it.\n\nT3 thread: https://nightly.app.t3.codes/threads/env-1/thread-1";
expect(
appendJiraT3ThreadLink(body, {
hostedAppUrl: "https://nightly.app.t3.codes",
environmentId: "env-1",
threadId: "thread-1",
}),
).toBe(body);
});

it("bodyMentionsIdentity distinguishes misses", () => {
expect(bodyMentionsIdentity("hello world", "omegent").matched).toBe(false);
expect(bodyMentionsIdentity("@omegent please", "omegent").matched).toBe(true);
Expand Down
Loading