Skip to content
Open
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
Binary file modified bun.lockb
Binary file not shown.
34 changes: 33 additions & 1 deletion src/handlers/app.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { Context, Probot } from 'probot';
import { envs } from '~common/consts';
import { Config } from '~common/types';
import { UserErrorListener } from '~events';
import { ChatHandler, DiagramHandler, ReviewHandler, SummaryHandler, WalkthroughHandler } from '~handlers';
import {
ChatCommandHandler,
ChatHandler,
DiagramHandler,
ReviewHandler,
SummaryHandler,
WalkthroughHandler
} from '~handlers';
import { systemLogger } from '~loggers';
import { AiService, AppService, CacheService, ConfigService, ErrorService, QueueService } from '~services';
import { ContributorHandler } from './contributor.handler';
Expand Down Expand Up @@ -37,6 +44,7 @@ export class AppHandler {
pushHandler: PushHandler;
diagramHandler: DiagramHandler;
installationHandler: installationHandler;
chatCommandHandler: ChatCommandHandler;
};

constructor() {
Expand Down Expand Up @@ -78,6 +86,11 @@ export class AppHandler {
this.services.queueService,
this.services.cacheService
),
chatCommandHandler: new ChatCommandHandler(
this.services.appService,
this.services.queueService,
this.services.cacheService
),
contributorHandler: new ContributorHandler(this.services.cacheService, this.services.queueService),
pushHandler: new PushHandler(this.services.cacheService, this.services.configService),
installationHandler: new installationHandler(this.services.cacheService)
Expand Down Expand Up @@ -134,6 +147,17 @@ export class AppHandler {
});
}

private async handleCommandEvents(context: Context<'pull_request_review_comment' | 'pull_request'>): Promise<void> {
await this.safeEventHandler(context, async (apiKey, config) => {
await this.handlers.contributorHandler.safeCacheContributors(context, apiKey);
const chatX = await this.handlers.chatCommandHandler.handle(context, apiKey, config);

if (chatX.reviewTriggered) {
await this.handlers.reviewHandler.handle(context, apiKey, config);
}
});
}

private async handleMemberEvents(context: Context<'member'>): Promise<void> {
await this.safeEventHandler(context, (apiKey) => this.handlers.contributorHandler.handle(context, apiKey));
}
Expand Down Expand Up @@ -167,6 +191,14 @@ export class AppHandler {
this.handleCommentEvents.bind(this)
);

app.on(
[
'pull_request_review_comment.created',
'pull_request'
],
Comment on lines +195 to +198
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ScreenTechnicals Users can use the commands on PR's title, issues, general comments, and PR's body

this.handleCommandEvents.bind(this)
);

app.on(
[
'member.added',
Expand Down
86 changes: 86 additions & 0 deletions src/handlers/chat-commands.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { WebhookEventName } from '@octokit/webhooks-types';
import { Context } from 'probot';
import { Config } from '~common/types';
import { SystemError } from '~errors';
import { isInlineComment } from '~helpers';
import { AppService, CacheService, GitHubService, QueueService } from '~services';
import { ChatSkipConditions } from '~utils';

type CommentEvent =
| 'issue_comment'
| 'discussion_comment'
| 'pull_request_review_comment'
| 'discussion'
| 'issues'
| 'pull_request';

interface Handler<Event extends WebhookEventName> {
handle(
context: Context<Event>,
apiKey: string,
config: Config
): Promise<{
reviewTriggered: boolean;
}>;
}

export class ChatCommandHandler implements Handler<CommentEvent> {
constructor(
private readonly appService: AppService,
private readonly queueService: QueueService,
private readonly cacheService: CacheService
) {}

async handle(
context: Context<CommentEvent>,
apiKey: string,
config: Config
): Promise<{
reviewTriggered: boolean;
}> {
const shouldSkip = await new ChatSkipConditions(
context,
config,
this.appService,
this.cacheService,
this.queueService,
apiKey
).shouldSkip();

if (shouldSkip) {
return {
reviewTriggered: false
};
}
const contextBody = await this.getContextBody(context);

if (contextBody?.includes('dr-github-pro review')) {
return { reviewTriggered: true };
}

return {
reviewTriggered: false
};
}

private async getContextBody(context: Context<CommentEvent>): Promise<string | null> {
return this.queueService.schedule(async () => {
if (isInlineComment(context.payload) && context.payload.comment.in_reply_to_id) {
const reviewComment = await GitHubService.getReviewComment(context, context.payload.comment.in_reply_to_id);

return reviewComment.data.body;
}

if ('comment' in context.payload) {
return context.payload.comment.body;
} else if ('discussion' in context.payload) {
return context.payload.discussion.body;
} else {
if ('issue' in context.payload) {
return context.payload.issue.body;
}
throw new SystemError('Unhandled payload type');
}
});
}
}
1 change: 1 addition & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './app.handler';
export * from './chat-commands.handler';
export * from './chat.handler';
export * from './contributor.handler';
export * from './diagram.handler';
Expand Down
Loading