Skip to content

Commit

Permalink
feat(engine): handle assignee and reviewer events
Browse files Browse the repository at this point in the history
  • Loading branch information
thatkookooguy committed Apr 26, 2021
1 parent acee34c commit ae2bc97
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@
"github-actions.workflows.pinned.workflows": [
".github/workflows/server-unit-tests.yml",
".github/workflows/lint-server.yml"
]
],
"github-actions.workflows.pinned.refresh.enabled": true,
"github-actions.workflows.pinned.refresh.interval": 30
}
21 changes: 20 additions & 1 deletion server/src/api/pull-request/pull-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ReturnModelType } from '@typegoose/typegoose';

import { BaseService } from '@kb-abstracts';
import { IGithubChanges } from '@kb-interfaces';
import { PullRequest } from '@kb-models';
import { PullRequest, User } from '@kb-models';

export interface INewData {
title?: string;
Expand Down Expand Up @@ -57,4 +57,23 @@ export class PullRequestService extends BaseService<PullRequest> {
...historyUpdate
});
}

async updateAssignees(prid: string, assignees: User[]) {
await this.prModel.findOneAndUpdate({ prid }, {
assignees: assignees.map((user) => user.username)
});
}

async updateReviewers(prid: string, reviewer: User, forRemoval?: boolean) {
const changeQuery = {
$pull: {} as any,
$addToSet: {} as any
};
const addDelAttr = forRemoval ? '$pull' : '$addToSet';
changeQuery[addDelAttr].reviewers = reviewer.username;
if (forRemoval) {
changeQuery['$addToSet']['history.deletedReviewers'] = reviewer.username;
}
await this.prModel.findOneAndUpdate({ prid }, changeQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const pullReuqestReviewRequestAddedEvent = {
event: 'pull_request',
payload: {
action: 'review_requested',
number: 2,
number: 1,
pull_request: {
url: 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/2',
id: 353189935,
Expand All @@ -12,7 +12,7 @@ export const pullReuqestReviewRequestAddedEvent = {
diff_url: 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/2.diff',
patch_url: 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/2.patch',
issue_url: 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/2',
number: 2,
number: 1,
state: 'open',
locked: false,
title: 'Update README.md',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const pullRequestReviewRequestRemovedEvent = {
event: 'pull_request',
payload: {
action: 'review_request_removed',
number: 2,
number: 1,
pull_request: {
url: 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/pulls/2',
id: 353189935,
Expand All @@ -12,7 +12,7 @@ export const pullRequestReviewRequestRemovedEvent = {
diff_url: 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/2.diff',
patch_url: 'https://github.com/Thatkookooguy/test-new-achievibit-events/pull/2.patch',
issue_url: 'https://api.github.com/repos/Thatkookooguy/test-new-achievibit-events/issues/2',
number: 2,
number: 1,
state: 'open',
locked: false,
title: 'Update README.md',
Expand Down
77 changes: 69 additions & 8 deletions server/src/engines/github.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,86 @@ export class GithubEngine extends Engine<IGithubPullRequestEvent> {
eventData.changes
);
}
handlePullRequestAssigneeAdded(
async handlePullRequestAssigneeAdded(
eventData: IGithubPullRequestEvent
): Promise<void> {
throw new Error('Method not implemented.');
const {
githubCreator,
githubOwner,
githubPR
} = this.extractGithubEntities(eventData);
const pr = this.extractPullRequest(
githubPR,
this.extractUser(githubCreator),
this.extractRepo(eventData.repository),
this.extractUser(githubOwner)
);

const githubAssignees = eventData.pull_request.assignees;
const assignees = githubAssignees
.map((assignee) => this.extractUser(assignee));

// TODO: need to save these users!!!

await this.pullRequestsService.updateAssignees(pr.prid, assignees);
}
handlePullRequestAssigneeRemoved(
async handlePullRequestAssigneeRemoved(
eventData: IGithubPullRequestEvent
): Promise<void> {
throw new Error('Method not implemented.');
const {
githubCreator,
githubOwner,
githubPR
} = this.extractGithubEntities(eventData);
const pr = this.extractPullRequest(
githubPR,
this.extractUser(githubCreator),
this.extractRepo(eventData.repository),
this.extractUser(githubOwner)
);

const githubAssignees = eventData.pull_request.assignees;
const assignees = githubAssignees
.map((assignee) => this.extractUser(assignee));

await this.pullRequestsService.updateAssignees(pr.prid, assignees);
}
handlePullRequestReviewRequestAdded(
async handlePullRequestReviewRequestAdded(
eventData: IGithubPullRequestEvent
): Promise<void> {
throw new Error('Method not implemented.');
const {
githubCreator,
githubOwner,
githubPR
} = this.extractGithubEntities(eventData);
const pr = this.extractPullRequest(
githubPR,
this.extractUser(githubCreator),
this.extractRepo(eventData.repository),
this.extractUser(githubOwner)
);
const reviewer = this.extractUser(eventData.requested_reviewer);

// TODO: need to save these users!!!

await this.pullRequestsService.updateReviewers(pr.prid, reviewer);
}
handlePullRequestReviewRequestRemoved(
async handlePullRequestReviewRequestRemoved(
eventData: IGithubPullRequestEvent
): Promise<void> {
throw new Error('Method not implemented.');
const {
githubCreator,
githubOwner,
githubPR
} = this.extractGithubEntities(eventData);
const pr = this.extractPullRequest(
githubPR,
this.extractUser(githubCreator),
this.extractRepo(eventData.repository),
this.extractUser(githubOwner)
);
const reviewer = this.extractUser(eventData.requested_reviewer);
await this.pullRequestsService.updateReviewers(pr.prid, reviewer, true);
}
handlePullRequestReviewCommentAdded(
eventData: IGithubPullRequestEvent
Expand Down
3 changes: 3 additions & 0 deletions server/src/interfaces/github-pr-payload.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface IGithubPullRequest {
merged_at: string;
merge_commit_sha: string;
assignee: string;
assignees: IGithubUser[];
milestone: string;
commits_url: string;
review_comments_url: string;
Expand Down Expand Up @@ -189,4 +190,6 @@ export interface IGithubPullRequestEvent {
sender: IGithubUser;
label?: { name: string };
changes?: IGithubChanges;
requested_reviewer?: IGithubUser;
// assignees?: IGithubUser[];
}
5 changes: 5 additions & 0 deletions server/src/models/pull-request.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class PullRequest extends BaseModel {
@PersistInDb()
assignees?: string[];

@Expose()
@IsOptional()
@PersistInDb()
reviewers?: string[];

constructor(partial: Partial<PullRequest> = {}) {
super();
Object.assign(this, partial);
Expand Down
Loading

0 comments on commit ae2bc97

Please sign in to comment.