Skip to content

Commit

Permalink
fix(ExternalTasks): more safely determinate task status
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Sep 24, 2024
1 parent eb99905 commit b3535f8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions generated/kysely/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export type ExternalTask = {
ownerEmail: string;
ownerName: string;
ownerId: string;
resolution: string | null;
resolutionId: string | null;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "ExternalTask" ADD COLUMN "resolution" TEXT,
ADD COLUMN "resolutionId" TEXT;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ model ExternalTask {
ownerEmail String
ownerName String
ownerId String
resolution String?
resolutionId String?
criteria GoalAchieveCriteria[] @relation("taskToCriteria")
createdAt DateTime @default(dbgenerated("timezone('utc'::text, now())")) @db.Timestamp()
Expand Down
6 changes: 5 additions & 1 deletion src/utils/db/calculatedGoalsFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export const calculateGoalCriteria = (list: Array<any>) => {
}

if (externalTask) {
const isFinishedStatus = jiraService.checkStatusIsFinished(externalTask.stateCategoryId);
const isFinishedStatus = jiraService.checkCompletedStatus({
statusCategory: externalTask.stateCategoryId,
statusName: externalTask.state,
resolutionName: externalTask.resolution,
});

return {
...baseCriteria,
Expand Down
24 changes: 24 additions & 0 deletions src/utils/integration/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export interface JiraPriority {
name: string;
id: string;
}
export interface JiraResolution {
self: string;
id: string;
description: string;
name: string;
}

export interface JiraIssue {
id: string;
Expand All @@ -78,6 +84,7 @@ export interface JiraIssue {
project: JiraProject;
priority: JiraProject;
issuetype: JiraIssueType;
resolution?: JiraResolution;
}

interface JiraServiceConfig {
Expand All @@ -86,6 +93,7 @@ interface JiraServiceConfig {
password: string;
apiVersion: string;
positiveStatusNames: string;
positiveResolutionNames: string;
finishedCategory: JiraIssueStatus['statusCategory'] | null;
mapStatusKey: Record<string, string>;
mapStatusIdToColor: Record<string, string>;
Expand Down Expand Up @@ -153,9 +161,25 @@ const initJiraClient = () => {
return false;
}

function checkCompletedStatus(data: {
statusName: string;
statusCategory: number;
resolutionName?: string | null;
}) {
if (config.finishedCategory?.id === data.statusCategory) {
return (
config.positiveStatusNames.includes(data.statusName) &&
Boolean(data.resolutionName && config.positiveResolutionNames.includes(data.resolutionName))
);
}

return false;
}

return {
instance,
checkStatusIsFinished,
checkCompletedStatus,
get positiveStatuses() {
return config.positiveStatusNames;
},
Expand Down
13 changes: 12 additions & 1 deletion trpc/queries/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ export const getOrCreateExternalTask = async ({ id }: { id: string }) => {

const [externalIssue] = await searchIssue({ value: id, limit: 1 });

const { summary: title, id: externalId, key, issuetype, status: state, project, reporter } = externalIssue;
const {
summary: title,
id: externalId,
key,
issuetype,
status: state,
project,
reporter,
resolution,
} = externalIssue;

return insertExternalTask({
title,
Expand All @@ -47,5 +56,7 @@ export const getOrCreateExternalTask = async ({ id }: { id: string }) => {
ownerName: reporter.displayName,
ownerEmail: reporter.emailAddress,
ownerId: reporter.key,
resolution: resolution?.name ?? null,
resolutionId: resolution?.id ?? null,
}).executeTakeFirstOrThrow();
};
10 changes: 7 additions & 3 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,9 +1117,13 @@ export const goal = router({
});

if (externalTask) {
isDoneByConnect = jiraService.checkStatusIsFinished(
(await searchIssue({ value: externalTask.externalKey, limit: 1 }))[0].status,
);
const issue = (await searchIssue({ value: externalTask.externalKey, limit: 1 }))[0];

isDoneByConnect = jiraService.checkCompletedStatus({
statusCategory: issue.status.statusCategory.id,
statusName: issue.status.name,
resolutionName: issue.resolution?.name,
});
criteriaTitle = externalTask?.title;
}
}
Expand Down
2 changes: 2 additions & 0 deletions trpc/router/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const jiraIssueToExternalTask = (
ownerEmail: issue.reporter.emailAddress,
ownerId: issue.reporter.key,
ownerName: issue.reporter.displayName || issue.reporter.name,
resolution: issue.resolution?.name ?? null,
resolutionId: issue.resolution?.id ?? null,
};
};

Expand Down

0 comments on commit b3535f8

Please sign in to comment.