Skip to content

Commit 6560515

Browse files
committed
fix: improve code
1 parent e344b12 commit 6560515

File tree

8 files changed

+48
-46
lines changed

8 files changed

+48
-46
lines changed

src/handlers/diagram.handler.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Handler } from '~common/interfaces';
66
import { Config, FileContent } from '~common/types';
77
import { aiConfig, appConfig } from '~configs';
88
import { SystemError } from '~errors';
9+
import { addMetadataToComment } from '~helpers';
910
import { AiService, CacheService, GitHubService, QueueService, TemplateService } from '~services';
1011
import { diagramCommentTemplate, diagramPromptTemplate, diagramSystemTemplate } from '~templates';
1112
import { DiagramSkipConditions } from '~utils';
@@ -29,21 +30,21 @@ export class DiagramHandler implements Handler<'pull_request'> {
2930
const existingComment = await this.findExistingComment(context);
3031

3132
if (existingComment?.id) {
32-
await this.queueService.schedule(() =>
33-
GitHubService.updateComment(
34-
context,
35-
existingComment.id,
36-
TemplateService.render(diagramCommentTemplate, {
37-
diagram,
38-
commentType: appConfig.metadata.diagram
39-
})
40-
)
33+
const commentBody = addMetadataToComment(
34+
TemplateService.render(diagramCommentTemplate, {
35+
diagram
36+
}),
37+
appConfig.metadata.diagram
4138
);
39+
40+
await this.queueService.schedule(() => GitHubService.updateComment(context, existingComment.id, commentBody));
4241
} else {
43-
const commentBody = TemplateService.render(diagramCommentTemplate, {
44-
diagram,
45-
commentType: appConfig.metadata.diagram
46-
});
42+
const commentBody = addMetadataToComment(
43+
TemplateService.render(diagramCommentTemplate, {
44+
diagram
45+
}),
46+
appConfig.metadata.diagram
47+
);
4748

4849
await this.queueService.schedule(() => GitHubService.createComment(context, commentBody));
4950
}

src/handlers/summary.handler.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Handler } from '~common/interfaces';
77
import { Config, FileContent, SummaryObject } from '~common/types';
88
import { aiConfig, appConfig } from '~configs';
99
import { SystemError } from '~errors';
10+
import { addMetadataToComment } from '~helpers';
1011
import { summaryObjectSchema } from '~schemas';
1112
import { AiService, CacheService, GitHubService, QueueService, TemplateService } from '~services';
1213
import { summaryCommentTemplate, summaryPromptTemplate, summarySystemTemplate } from '~templates';
@@ -32,21 +33,21 @@ export class SummaryHandler implements Handler<'pull_request'> {
3233
const existingComment = await this.findExistingComment(context);
3334

3435
if (existingComment?.id) {
35-
await this.queueService.schedule(() =>
36-
GitHubService.updateComment(
37-
context,
38-
existingComment.id,
39-
TemplateService.render(summaryCommentTemplate, {
40-
summary,
41-
commentType: appConfig.metadata.summary
42-
})
43-
)
36+
const commentBody = addMetadataToComment(
37+
TemplateService.render(summaryCommentTemplate, {
38+
summary
39+
}),
40+
appConfig.metadata.summary
4441
);
42+
43+
await this.queueService.schedule(() => GitHubService.updateComment(context, existingComment.id, commentBody));
4544
} else {
46-
const commentBody = TemplateService.render(summaryCommentTemplate, {
47-
summary,
48-
commentType: appConfig.metadata.summary
49-
});
45+
const commentBody = addMetadataToComment(
46+
TemplateService.render(summaryCommentTemplate, {
47+
summary
48+
}),
49+
appConfig.metadata.summary
50+
);
5051

5152
await this.queueService.schedule(() => GitHubService.createComment(context, commentBody));
5253
}

src/handlers/walkthrough.handler.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Handler } from '~common/interfaces';
77
import { Config, FileContent, WalkthroughObject } from '~common/types';
88
import { aiConfig, appConfig } from '~configs';
99
import { SystemError } from '~errors';
10+
import { addMetadataToComment } from '~helpers';
1011
import { walkthroughObjectSchema } from '~schemas';
1112
import { AiService, CacheService, GitHubService, QueueService, TemplateService } from '~services';
1213
import { walkthroughCommentTemplate, walkthroughPromptTemplate, walkthroughSystemTemplate } from '~templates';
@@ -32,23 +33,23 @@ export class WalkthroughHandler implements Handler<'pull_request'> {
3233
const changes = this.generateMarkdown(walkthroughObject.changes);
3334

3435
if (existingComment?.id) {
35-
await this.queueService.schedule(() =>
36-
GitHubService.updateComment(
37-
context,
38-
existingComment.id,
39-
TemplateService.render(walkthroughCommentTemplate, {
40-
description: walkthroughObject.description,
41-
changes,
42-
commentType: appConfig.metadata.walkthrough
43-
})
44-
)
36+
const commentBody = addMetadataToComment(
37+
TemplateService.render(walkthroughCommentTemplate, {
38+
description: walkthroughObject.description,
39+
changes
40+
}),
41+
appConfig.metadata.walkthrough
4542
);
43+
44+
await this.queueService.schedule(() => GitHubService.updateComment(context, existingComment.id, commentBody));
4645
} else {
47-
const commentBody = TemplateService.render(walkthroughCommentTemplate, {
48-
description: walkthroughObject.description,
49-
changes,
50-
commentType: appConfig.metadata.walkthrough
51-
});
46+
const commentBody = addMetadataToComment(
47+
TemplateService.render(walkthroughCommentTemplate, {
48+
description: walkthroughObject.description,
49+
changes
50+
}),
51+
appConfig.metadata.walkthrough
52+
);
5253

5354
await this.queueService.schedule(() => GitHubService.createComment(context, commentBody));
5455
}

src/helpers/comment.helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const addMetadataToComment = (comment: string, metadata: string): string => `${metadata}\n${comment}`;

src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './ai.helper';
22
export * from './cache.helper';
3+
export * from './comment.helper';
34
export * from './guard.helper';
45
export * from './hash.helper';
56
export * from './object.helper';
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{commentType}}
21
# Sequence Diagram(s) by Dr. Git
32

43
{{diagram}}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{commentType}}
21
# Summary by Dr. Git
3-
<!-- heading title summary -->
2+
43
{{summary}}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
{{commentType}}
21
# Walkthrough by Dr. Git
3-
<!-- heading title walkthrough -->
2+
43
{{description}}
54

65
{{changes}}

0 commit comments

Comments
 (0)