Skip to content

Commit

Permalink
feat(engine): add pr closed handling
Browse files Browse the repository at this point in the history
for now, it deletes the pr. look at added TODO to see the wanted behavior. but for now, this is good
enough

related to #177
  • Loading branch information
thatkookooguy committed Apr 29, 2021
1 parent 23ce94c commit 4716037
Show file tree
Hide file tree
Showing 7 changed files with 579 additions and 3 deletions.
5 changes: 4 additions & 1 deletion server/src/abstracts/engine.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export enum AchievibitEventNames {
PullRequestReviewCommentRemoved = 'PullRequestReviewCommentRemoved',
PullRequestReviewCommentEdited = 'PullRequestReviewCommentEdited',
PullRequestReviewSubmitted = 'PullRequestReviewSubmitted',
PullRequestMerged = 'PullRequestMerged'
PullRequestMerged = 'PullRequestMerged',
PullRequestClosed = 'PullRequestClosed'
}

export abstract class Engine<IEventPayload> {
Expand Down Expand Up @@ -175,4 +176,6 @@ export abstract class Engine<IEventPayload> {
* @param eventData the event payload
*/
abstract handlePullRequestMerged(eventData: IEventPayload): Promise<void>;

abstract handlePullRequestClosed(eventData: IEventPayload): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class WebhookEventManagerService {
this.logger.debug('PullRequestMerged');
await this.githubEngine.handlePullRequestMerged(eventData);
return eventName;
case AchievibitEventNames.PullRequestClosed:
this.logger.debug('PullRequestClosed');
await this.githubEngine.handlePullRequestClosed(eventData);
return eventName;
}
}

Expand Down Expand Up @@ -179,6 +183,12 @@ export class WebhookEventManagerService {
return AchievibitEventNames.PullRequestMerged;
}

if (isEqual(eventName, 'pull_request') &&
isEqual(eventData.action, 'closed') &&
!eventData.pull_request.merged) {
return AchievibitEventNames.PullRequestClosed;
}

return;
}
}
487 changes: 487 additions & 0 deletions server/src/dev-tools/captured-events/pull-request-closed.event.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions server/src/dev-tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './dto.mock-generator';
export * from './in-memory-database.module';
export * from './captured-events/pull-request-assignee-added.event';
export * from './captured-events/pull-request-assignee-removed.event';
export * from './captured-events/pull-request-closed.event';
export * from './captured-events/pull-request-created-organization.event';
export * from './captured-events/pull-request-created.event';
export * from './captured-events/pull-request-edited.event';
Expand Down
27 changes: 27 additions & 0 deletions server/src/engines/github.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,33 @@ export class GithubEngine extends Engine<IGithubPullRequestEvent> {
throw new Error('Method not implemented.');
}

async handlePullRequestClosed(
eventData: IGithubPullRequestEvent
): Promise<void> {
const {
githubCreator,
githubOwner,
githubPR
} = this.extractGithubEntities(eventData);
const pr = this.extractPullRequest(
githubPR,
this.extractUser(githubCreator),
this.extractRepo(eventData.repository),
this.extractUser(githubOwner)
);

/**
* TODO@Thatkookooguy: #340 Combine PR deletion to a single task
* This should later just flag the PR as closed\merged and should
* be dealt with in the task that runs at the start of the week.
* This way, we can just make the task also delete closed or merged prs
* but give achievements for some actions we want to allow before deleting
* all the data. Something like commiting to a branch with a closed pr
* or re-opening a pr.
*/
await this.pullRequestsService.deleteAsync({ prid: pr.prid });
}

private extractGithubEntities(eventData: IGithubPullRequestEvent) {
return {
githubPR: eventData.pull_request,
Expand Down
29 changes: 29 additions & 0 deletions server/test/__snapshots__/github-events.e2e-spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ Array [
]
`;

exports[`AppController (e2e) pr events / (POST) github pull request closed 1`] = `"PullRequestClosed"`;

exports[`AppController (e2e) pr events / (POST) github pull request closed 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) github pull request closed 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) github pull request closed 4`] = `Array []`;

exports[`AppController (e2e) pr events / (POST) github pull request created event should create user 1`] = `"PullRequestOpened"`;

exports[`AppController (e2e) pr events / (POST) github pull request created event should create user 2`] = `
Expand Down
23 changes: 21 additions & 2 deletions server/test/github-events.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ConfigService } from '@kb-config';
import {
pullRequestAssigneeAddedEvent,
pullRequestAssigneeRemovedEvent,
pullRequestClosedEvent,
pullRequestCreatedEvent,
pullRequestEditedEvent,
pullRequestLabelAddedEvent,
Expand Down Expand Up @@ -372,8 +373,26 @@ describe('AppController (e2e)', () => {
await confirmPrDataCreated();
});

test.todo('review comment added');
test.todo('review comment removed');
test('/ (POST) github pull request closed', 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',
pullRequestClosedEvent.event)
.send(pullRequestClosedEvent.payload)
.expect(201);

expect(sendWebhookResponse.text).toMatchSnapshot();

await confirmPrDataCreated();
});

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

0 comments on commit 4716037

Please sign in to comment.