Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

feat(functions): #1589 added PullRequestReviewCommentEvent #1591

Open
wants to merge 3 commits into
base: v0.11.9
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion functions/src/mappers/github/event.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
eddiejaoude marked this conversation as resolved.
Show resolved Hide resolved
url: string;
pull_request_review_id: string;
id: string;
node_id: number;
diff_hunk: string;
path: User;
postition: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo?

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

We use camelcase in our models

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':
{
Copy link
Contributor

Choose a reason for hiding this comment

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

{ not needed in the switch statement

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be done with es6

if (foundIndex > -1) {
repository.pullRequests[foundIndex].reviewComments += number;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface Links {
statuses: LinkObj;
}

interface PullRequest {
export interface PullRequest {
url: string;
id: string;
node_id: string;
Expand Down
13 changes: 13 additions & 0 deletions functions/src/repository/response-git-webhook-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -162,6 +167,14 @@ async function pullRequestEvent(data: PullRequestEventModel): Promise<void> {
await RepositoryModel.saveRepository(repository);
}

async function pullRequestReviewCommentEvent(data: PullRequestReviewCommentEventModel): Promise<void> {
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<void> {
Logger.info('releaseEvent');
const repository: DocumentData = await RepositoryModel.getRepositoryById(data.repository.id);
Expand Down