Skip to content
60 changes: 48 additions & 12 deletions apps/api/src/handlers/ado/handlePullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
import { enqueueTask } from '@roomote/cloud-agents/server';
import {
recordPrStatusChangeInTaskHistory,
PrStatusFastDeliveryError,
PrStatusHistoryRecordingError,
updateTaskPrStatus,
} from '@roomote/sdk/server';

Expand Down Expand Up @@ -79,6 +81,8 @@ async function notifyTerminalPullRequestThreads(
payload: AdoPullRequestWebhook,
repoFullName: string,
status: 'merged' | 'closed',
includeFastParentTargets: boolean,
includeFastParentTaskIds: string[],
): Promise<void> {
const prUrl = getAdoPullRequestUrl({
resourceContainers: payload.resourceContainers,
Expand Down Expand Up @@ -113,6 +117,8 @@ async function notifyTerminalPullRequestThreads(
actorLogin:
getAdoIdentityName(payload.resource.closedBy) ??
'someone in Azure DevOps',
...(includeFastParentTargets ? { includeFastParentTargets: true } : {}),
...(includeFastParentTaskIds.length ? { includeFastParentTaskIds } : {}),
},
`PR #${payload.resource.pullRequestId}`,
);
Expand Down Expand Up @@ -251,8 +257,10 @@ export async function handleAdoPullRequest(

scheduleAdoPullRequestFactSync(payload, repoFullName, 'closed');

await Promise.resolve(
recordPrStatusChangeInTaskHistory({
let includeFastParentTargets = false;
let includeFastParentTaskIds: string[] = [];
try {
await recordPrStatusChangeInTaskHistory({
sourceControlProvider: 'ado',
repository: repoFullName,
prNumber: pullRequest.pullRequestId,
Expand All @@ -266,16 +274,29 @@ export async function handleAdoPullRequest(
actorLogin:
getAdoIdentityName(payload.resource.closedBy) ??
'someone in Azure DevOps',
}),
).catch((error) => {
});
} catch (error) {
if (error instanceof PrStatusFastDeliveryError) {
includeFastParentTaskIds = error.taskIds;
} else {
includeFastParentTargets = !(
error instanceof PrStatusHistoryRecordingError
);
}
console.warn(
`[handleAdoPullRequest] Failed to record PR status in task history for ${repoFullName}#${pullRequest.pullRequestId}: ${
error instanceof Error ? error.message : String(error)
}`,
);
});
}

await notifyTerminalPullRequestThreads(payload, repoFullName, 'closed');
await notifyTerminalPullRequestThreads(
payload,
repoFullName,
'closed',
includeFastParentTargets,
includeFastParentTaskIds,
);

return { status: 'ok' };
}
Expand All @@ -297,8 +318,10 @@ export async function handleAdoPullRequest(

scheduleAdoPullRequestFactSync(payload, repoFullName, 'merged');

await Promise.resolve(
recordPrStatusChangeInTaskHistory({
let includeFastParentTargets = false;
let includeFastParentTaskIds: string[] = [];
try {
await recordPrStatusChangeInTaskHistory({
sourceControlProvider: 'ado',
repository: repoFullName,
prNumber: pullRequest.pullRequestId,
Expand All @@ -312,16 +335,29 @@ export async function handleAdoPullRequest(
actorLogin:
getAdoIdentityName(payload.resource.closedBy) ??
'someone in Azure DevOps',
}),
).catch((error) => {
});
} catch (error) {
if (error instanceof PrStatusFastDeliveryError) {
includeFastParentTaskIds = error.taskIds;
} else {
includeFastParentTargets = !(
error instanceof PrStatusHistoryRecordingError
);
}
console.warn(
`[handleAdoPullRequest] Failed to record PR status in task history for ${repoFullName}#${pullRequest.pullRequestId}: ${
error instanceof Error ? error.message : String(error)
}`,
);
});
}

await notifyTerminalPullRequestThreads(payload, repoFullName, 'merged');
await notifyTerminalPullRequestThreads(
payload,
repoFullName,
'merged',
includeFastParentTargets,
includeFastParentTaskIds,
);

return { status: 'ok' };
}
Expand Down
33 changes: 27 additions & 6 deletions apps/api/src/handlers/bitbucket/handlePullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
import { enqueueTask } from '@roomote/cloud-agents/server';
import {
recordPrStatusChangeInTaskHistory,
PrStatusFastDeliveryError,
PrStatusHistoryRecordingError,
updateTaskPrStatus,
} from '@roomote/sdk/server';

Expand Down Expand Up @@ -61,6 +63,8 @@ async function notifyTerminalPullRequestThreads(
payload: BitbucketPullRequestWebhook,
repoFullName: string,
status: 'merged' | 'closed',
includeFastParentTargets: boolean,
includeFastParentTaskIds: string[],
): Promise<void> {
const prUrl = getBitbucketPullRequestUrl(payload);
const webhookHost = toHostFromUrl(prUrl);
Expand Down Expand Up @@ -91,6 +95,8 @@ async function notifyTerminalPullRequestThreads(
prUrl,
status,
actorLogin: getBitbucketUsername(payload.actor) ?? 'someone on Bitbucket',
...(includeFastParentTargets ? { includeFastParentTargets: true } : {}),
...(includeFastParentTaskIds.length ? { includeFastParentTaskIds } : {}),
},
`PR #${prNumber}`,
);
Expand Down Expand Up @@ -131,8 +137,10 @@ export async function handleBitbucketPullRequest(
},
});

await Promise.resolve(
recordPrStatusChangeInTaskHistory({
let includeFastParentTargets = false;
let includeFastParentTaskIds: string[] = [];
try {
await recordPrStatusChangeInTaskHistory({
sourceControlProvider: 'bitbucket',
repository: repoFullName,
prNumber,
Expand All @@ -141,16 +149,29 @@ export async function handleBitbucketPullRequest(
status,
actorLogin:
getBitbucketUsername(payload.actor) ?? 'someone on Bitbucket',
}),
).catch((error) => {
});
} catch (error) {
if (error instanceof PrStatusFastDeliveryError) {
includeFastParentTaskIds = error.taskIds;
} else {
includeFastParentTargets = !(
error instanceof PrStatusHistoryRecordingError
);
}
console.warn(
`[handleBitbucketPullRequest] Failed to record PR status in task history for ${repoFullName}#${prNumber}: ${
error instanceof Error ? error.message : String(error)
}`,
);
});
}

await notifyTerminalPullRequestThreads(payload, repoFullName, status);
await notifyTerminalPullRequestThreads(
payload,
repoFullName,
status,
includeFastParentTargets,
includeFastParentTaskIds,
);

return { status: 'ok' };
}
Expand Down
33 changes: 27 additions & 6 deletions apps/api/src/handlers/gitea/handlePullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
import { enqueueTask } from '@roomote/cloud-agents/server';
import {
recordPrStatusChangeInTaskHistory,
PrStatusFastDeliveryError,
PrStatusHistoryRecordingError,
updateTaskPrStatus,
} from '@roomote/sdk/server';

Expand Down Expand Up @@ -64,6 +66,8 @@ async function notifyTerminalPullRequestThreads(
payload: GiteaPullRequestWebhook,
repoFullName: string,
status: 'merged' | 'closed',
includeFastParentTargets: boolean,
includeFastParentTaskIds: string[],
): Promise<void> {
const prUrl = getPullRequestUrl(payload);
const webhookHost = toHostFromUrl(prUrl);
Expand Down Expand Up @@ -92,6 +96,8 @@ async function notifyTerminalPullRequestThreads(
prUrl,
status,
actorLogin: getGiteaUsername(payload.sender) ?? 'someone on Gitea',
...(includeFastParentTargets ? { includeFastParentTargets: true } : {}),
...(includeFastParentTaskIds.length ? { includeFastParentTaskIds } : {}),
},
`PR #${payload.number}`,
);
Expand Down Expand Up @@ -127,25 +133,40 @@ export async function handleGiteaPullRequest(
},
});

await Promise.resolve(
recordPrStatusChangeInTaskHistory({
let includeFastParentTargets = false;
let includeFastParentTaskIds: string[] = [];
try {
await recordPrStatusChangeInTaskHistory({
sourceControlProvider: 'gitea',
repository: repoFullName,
prNumber: payload.number,
prTitle: pullRequest.title,
prUrl: getPullRequestUrl(payload),
status,
actorLogin: getGiteaUsername(payload.sender) ?? 'someone on Gitea',
}),
).catch((error) => {
});
} catch (error) {
if (error instanceof PrStatusFastDeliveryError) {
includeFastParentTaskIds = error.taskIds;
} else {
includeFastParentTargets = !(
error instanceof PrStatusHistoryRecordingError
);
}
console.warn(
`[handleGiteaPullRequest] Failed to record PR status in task history for ${repoFullName}#${payload.number}: ${
error instanceof Error ? error.message : String(error)
}`,
);
});
}

await notifyTerminalPullRequestThreads(payload, repoFullName, status);
await notifyTerminalPullRequestThreads(
payload,
repoFullName,
status,
includeFastParentTargets,
includeFastParentTaskIds,
);

return { status: 'ok' };
}
Expand Down
22 changes: 22 additions & 0 deletions apps/api/src/handlers/github/__tests__/handlePrMerge.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading