Skip to content

Commit

Permalink
feat(engine): implement pr edit
Browse files Browse the repository at this point in the history
update the field that got changed and update the pr history
  • Loading branch information
thatkookooguy committed Apr 26, 2021
1 parent c13b071 commit a3ba8d3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
20 changes: 20 additions & 0 deletions server/src/api/pull-request/pull-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { InjectModel } from '@nestjs/mongoose';
import { ReturnModelType } from '@typegoose/typegoose';

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

export interface INewData {
title?: string;
description?: string;
}

@Injectable()
export class PullRequestService extends BaseService<PullRequest> {
constructor(
Expand Down Expand Up @@ -37,4 +43,18 @@ export class PullRequestService extends BaseService<PullRequest> {
...historyUpdate
}).exec();
}

async editPRData(prid: string, newData: INewData, changes: IGithubChanges) {
const historyUpdate = {
$addToSet: {
'history.title': changes.title?.from,
'history.description': changes.body?.from
}
};

await this.prModel.findOneAndUpdate({ prid }, {
...newData,
...historyUpdate
});
}
}
20 changes: 18 additions & 2 deletions server/src/engines/github.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,26 @@ export class GithubEngine extends Engine<IGithubPullRequestEvent> {

await this.pullRequestsService.removeLabels(pr.prid, eventData.label.name);
}
handlePullRequestEdited(
async handlePullRequestEdited(
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)
);

await this.pullRequestsService.editPRData(
pr.prid,
{ title: pr.title, description: pr.description },
eventData.changes
);
}
handlePullRequestAssigneeAdded(
eventData: IGithubPullRequestEvent
Expand Down
10 changes: 10 additions & 0 deletions server/src/interfaces/github-pr-payload.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ export interface IGithubPullRequest {
changed_files: number;
}

export interface IGithubChanges {
title?: {
from: string;
};
body?: {
from: string;
};
}

/**
* https://developer.github.com/v3/activity/events/types/#pullrequestevent
*/
Expand All @@ -179,4 +188,5 @@ export interface IGithubPullRequestEvent {
repository: IGithubRepo;
sender: IGithubUser;
label?: { name: string };
changes?: IGithubChanges;
}
54 changes: 54 additions & 0 deletions server/test/__snapshots__/github-events.e2e-spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,60 @@ Array [
]
`;

exports[`AppController (e2e) pr events / (POST) from github pull request edited 1`] = `"PullRequestEdited"`;

exports[`AppController (e2e) pr events / (POST) from github pull request edited 2`] = `
Array [
Object {
"achievements": Array [],
"avatar": "https://avatars3.githubusercontent.com/u/10427304?v=4",
"organization": false,
"organizations": Array [],
"repos": Array [],
"url": "https://github.com/Thatkookooguy",
"username": "Thatkookooguy",
"users": Array [],
},
]
`;

exports[`AppController (e2e) pr events / (POST) from github pull request edited 3`] = `
Array [
Object {
"fullname": "Thatkookooguy/test-new-achievibit-events",
"name": "test-new-achievibit-events",
"url": "https://github.com/Thatkookooguy/test-new-achievibit-events",
},
]
`;

exports[`AppController (e2e) pr events / (POST) from github pull request edited 4`] = `
Array [
Object {
"assignees": Array [],
"createdOn": 2019-12-12T13:48:34.000Z,
"creator": "Thatkookooguy",
"description": "",
"history": Object {
"description": Array [
null,
],
"title": Array [
"Create test-event.ts",
],
},
"labels": Array [],
"number": 1,
"organization": undefined,
"prid": "Thatkookooguy/test-new-achievibit-events/pull/1",
"repository": "Thatkookooguy/test-new-achievibit-events",
"title": "Create test-event.ts - title changed!",
"updatedDate": undefined,
"url": "https://github.com/Thatkookooguy/test-new-achievibit-events/pull/1",
},
]
`;

exports[`AppController (e2e) pr events / (POST) from github pull request label removed 1`] = `"PullRequestLabelRemoved"`;

exports[`AppController (e2e) pr events / (POST) from github pull request label removed 2`] = `
Expand Down
21 changes: 21 additions & 0 deletions server/test/github-events.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AppModule } from '@kb-app';
import { ConfigService } from '@kb-config';
import {
pullRequestCreatedEvent,
pullRequestEditedEvent,
pullRequestLabelAddedEvent,
pullRequestLabelRemovedEvent,
pullRequestLabelsInitializedEvent,
Expand Down Expand Up @@ -152,6 +153,26 @@ describe('AppController (e2e)', () => {
await confirmPrDataCreated();
});

test('/ (POST) from github pull request edited', async () => {
const server = app.getHttpServer();
await request(server)
.post('/api/webhook-event-manager')
.set('Accept', 'application/json')
.set('x-github-event', pullRequestCreatedEvent.event)
.send(pullRequestCreatedEvent.payload)
.expect(201);
const sendWebhookResponse = await request(server)
.post('/api/webhook-event-manager')
.set('Accept', 'application/json')
.set('x-github-event', pullRequestEditedEvent.event)
.send(pullRequestEditedEvent.payload)
.expect(201);

expect(sendWebhookResponse.text).toMatchSnapshot();

await confirmPrDataCreated();
});

async function confirmPrDataCreated() {
const server = app.getHttpServer();
const getAllUsersResponse = await request(server)
Expand Down

0 comments on commit a3ba8d3

Please sign in to comment.