Skip to content

Commit

Permalink
feat(controller/github): add MR approval action
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 5, 2019
1 parent 61d82da commit 8556d30
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/controller/github/PRController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { CheckRBAC, Controller, ControllerData, Handler } from '..';
import { Command, CommandVerb } from '../../entity/Command';
import { Context } from '../../entity/Context';
import { GithubClientData } from '../../utils/github';
import { BaseController, BaseControllerOptions } from '../BaseController';
import { BaseController, BaseControllerOptions, ErrorReplyType } from '../BaseController';

export const NOUN_PULL_REQUEST = 'github-pull-request';

export interface GithubPRControllerData extends ControllerData {
client: GithubClientData;
}

export interface RequestOptions {
owner: string;
project: string;
pull_number: number;
}

@Inject()
export class GithubPRController extends BaseController<GithubPRControllerData> implements Controller {
protected readonly client: Octokit;
Expand Down Expand Up @@ -79,6 +85,17 @@ export class GithubPRController extends BaseController<GithubPRControllerData> i
@Handler(NOUN_PULL_REQUEST, CommandVerb.Update)
@CheckRBAC()
public async updateRequest(cmd: Command, ctx: Context): Promise<void> {
const action = cmd.getHead('action');
if (action === 'merge') {
return this.mergeRequest(cmd, ctx);
} else if (action === 'approve') {
return this.approveRequest(cmd, ctx);
} else {
return this.errorReply(ctx, ErrorReplyType.Unknown, 'unknown');
}
}

protected async mergeRequest(cmd: Command, ctx: Context): Promise<void> {
const message = cmd.getHead('message');
const owner = cmd.getHeadOrDefault('owner', ctx.name);
const project = cmd.getHead('project');
Expand All @@ -88,10 +105,25 @@ export class GithubPRController extends BaseController<GithubPRControllerData> i
await this.client.pulls.merge({
commit_message: message,
commit_title: message,
number: requestNumber,
owner,
pull_number: requestNumber,
repo: project,
});
return this.reply(ctx, `merged pull request ${requestNumber}`);
}

protected async approveRequest(cmd: Command, ctx: Context): Promise<void> {
const owner = cmd.getHeadOrDefault('owner', ctx.name);
const project = cmd.getHead('project');
const requestNumber = cmd.getNumber('number');

this.logger.debug({ owner, project, requestNumber }, 'approving pull request');
await this.client.pulls.createReview({
event: 'APPROVE',
owner,
pull_number: requestNumber,
repo: project,
});
return this.reply(ctx, `approved pull request ${requestNumber}`);
}
}

0 comments on commit 8556d30

Please sign in to comment.