diff --git a/functions/src/mappers/github/event.mapper.ts b/functions/src/mappers/github/event.mapper.ts index beb6c83ab..a08a98556 100644 --- a/functions/src/mappers/github/event.mapper.ts +++ b/functions/src/mappers/github/event.mapper.ts @@ -7,7 +7,7 @@ import { GitHubPayloadInput, GitHubPayloadMapper, GitHubPayloadModel } from './p import { GitHubRepositoryInput, GitHubRepositoryMapper, GitHubRepositoryModel } from './repository.mapper'; import { GitHubUserInput, GitHubUserMapper, GitHubUserModel } from './user.mapper'; -export type GitHubEventType = 'PullRequestEvent' | 'IssueCommentEvent' | 'CreateEvent' | 'ReleaseEvent' | 'WatchEvent' | 'PushEvent' | 'IssuesEvent'; +export type GitHubEventType = 'PullRequestEvent' | 'PullRequestReviewCommentEvent' | 'IssueCommentEvent' | 'CreateEvent' | 'ReleaseEvent' | 'WatchEvent' | 'PushEvent' | 'IssuesEvent'; export interface GitHubEventInput { id: string; diff --git a/functions/src/mappers/github/webhook-event-response/pull-request-review-comment.ts b/functions/src/mappers/github/webhook-event-response/pull-request-review-comment.ts new file mode 100644 index 000000000..e92e71760 --- /dev/null +++ b/functions/src/mappers/github/webhook-event-response/pull-request-review-comment.ts @@ -0,0 +1,80 @@ +import { DocumentData } from '../../../client/firebase-admin'; + +// Dashboard mappers/models +import { GitHubPullRequestModel } from '../pullRequest.mapper'; +import { Logger } from './../../../client/logger'; +import { PullRequest } from './pull-request'; +import { isExistProperties, Repository, User } from './shared'; + +interface Comment { + url: string; + pull_request_review_id: string; + id: string; + node_id: number; + diff_hunk: string; + path: User; + postition: string; + original_position: string; + commit_id: string; + original_commit_id: string; + user: User; +} + +export type Action = 'created' | 'edited' | 'deleted'; + +export interface PullRequestReviewCommentEventInput { + action: Action; + comment: Comment; + pull_request: PullRequest; + repository: Repository; + sender: User; +} + +export class PullRequestReviewCommentEventModel implements PullRequestReviewCommentEventInput { + action: Action; + comment: Comment; + pull_request: PullRequest; + repository: Repository; + sender: User; + + constructor(input: PullRequestReviewCommentEventInput) { + Object.assign(this, input); + } + + public static isCurrentModel(input: any): boolean { + const requireKeys: string[] = ['action', 'comment', 'pull_request', 'repository', 'sender']; + return isExistProperties(input, requireKeys); + } + + updateData(repository: DocumentData): void { + if (!Array.isArray(repository.pullRequests)) { + repository.pullRequests = []; + } + + switch (this.action) { + case 'created': + { + this.updated(repository, 1); + break; + } + case 'deleted': + { + this.updated(repository, -1); + break; + } + + default: { + Logger.info('ACTION: ', this.action); + throw new Error('Not found action'); + } + } + + } + + private updated(repository: DocumentData, number: number): void { + const foundIndex: number = repository.pullRequests.findIndex((elem: GitHubPullRequestModel) => elem.uid === this.pull_request.id); + if (foundIndex > -1) { + repository.pullRequests[foundIndex].reviewComments += number; + } + } +} diff --git a/functions/src/mappers/github/webhook-event-response/pull-request.ts b/functions/src/mappers/github/webhook-event-response/pull-request.ts index 01cdfa379..939f14224 100644 --- a/functions/src/mappers/github/webhook-event-response/pull-request.ts +++ b/functions/src/mappers/github/webhook-event-response/pull-request.ts @@ -34,7 +34,7 @@ interface Links { statuses: LinkObj; } -interface PullRequest { +export interface PullRequest { url: string; id: string; node_id: string; diff --git a/functions/src/repository/response-git-webhook-repository.ts b/functions/src/repository/response-git-webhook-repository.ts index 203f401f5..f27c9fbc0 100644 --- a/functions/src/repository/response-git-webhook-repository.ts +++ b/functions/src/repository/response-git-webhook-repository.ts @@ -23,6 +23,7 @@ import { StatusEventModel, WatchEventModel, } from '../mappers/github/webhook-event-response'; +import { PullRequestReviewCommentEventModel } from '../mappers/github/webhook-event-response/pull-request-review-comment'; import { addHubEventToCollection, HubEventActions } from '../mappers/github/webhook-event-response/shared'; import { RepositoryModel } from '../models/index.model'; import { DocumentData, FieldPath, FirebaseAdmin, QuerySnapshot } from './../client/firebase-admin'; @@ -73,6 +74,10 @@ export const onResponseGitWebhookRepository: HttpsFunction = https.onRequest((re result = pullRequestEvent(new PullRequestEventModel(inputData)); + } else if (PullRequestReviewCommentEventModel.isCurrentModel(inputData)) { + + result = pullRequestReviewCommentEvent(new PullRequestReviewCommentEventModel(inputData)); + } else if (ReleaseEventModel.isCurrentModel(inputData)) { result = releaseEvent(new ReleaseEventModel(inputData)); @@ -162,6 +167,14 @@ async function pullRequestEvent(data: PullRequestEventModel): Promise { await RepositoryModel.saveRepository(repository); } +async function pullRequestReviewCommentEvent(data: PullRequestReviewCommentEventModel): Promise { + Logger.info('pullRequestReviewCommentEvent'); + const repository: DocumentData = await RepositoryModel.getRepositoryById(data.repository.id); + + data.updateData(repository); + await RepositoryModel.saveRepository(repository); +} + async function releaseEvent(data: ReleaseEventModel): Promise { Logger.info('releaseEvent'); const repository: DocumentData = await RepositoryModel.getRepositoryById(data.repository.id);