Skip to content

Commit

Permalink
feat(controller/github): request approval controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Sep 16, 2020
1 parent 6a0493d commit 7ef781e
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ temp/

# types
*.bak
*.dot
*.pid
*.swp
*.tmp

noname.*.png
report.*.json
package-lock.json
yarn-error.log
2 changes: 2 additions & 0 deletions config/rollup/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import serve from 'rollup-plugin-serve';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import visualizer from 'rollup-plugin-visualizer';
import graph from 'rollup-plugin-graph';

const { chunkMap } = require('./map.js');
const { plugins } = require('./project.js');
Expand Down Expand Up @@ -100,6 +101,7 @@ const bundle = {
'application/javascript': ['mjs'],
},
}) : undefined),
graph({ prune: false }),
],
};

Expand Down
29 changes: 29 additions & 0 deletions docs/controller/github/approve-controller.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
metadata:
kind: github-approve-controller
name: test-approve-controller
data:
client:
agent: ssube/isolex/controller/github/approve
app:
id: !env ISOLEX_GITHUB_APP_ID
key: !env ISOLEX_GITHUB_APP_KEY
installation:
id: !env ISOLEX_GITHUB_APP_INSTALLATION
projects:
- owner: ssube
project: isolex
checks:
- app: codeclimate
conclusion: success
name: codeclimate
status: success
- app: gitlab/build
conclusion: success
name: gitlab/build
status: success

filters: []
redirect:
defaults: {}
forces: {}
transforms: []
1 change: 1 addition & 0 deletions docs/isolex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ data:
- !include ./docs/controller/completion-controller.yml
- !include ./docs/controller/dice-controller.yml
- !include ./docs/controller/echo-controller.yml
- !include ./docs/controller/github/approve-controller.yml
- !include ./docs/controller/github/commit-controller.yml
- !include ./docs/controller/github/pull-controller.yml
- !include ./docs/controller/github/endpoint/pull-controller.yml
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@
"rollup": "2.27.0",
"rollup-plugin-commonjs": "10.1.0",
"rollup-plugin-eslint": "7.0.0",
"rollup-plugin-graph": "^2.0.0",
"rollup-plugin-node-externals": "2.2.0",
"rollup-plugin-serve": "1.0.4",
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.27.2",
"rollup-plugin-visualizer": "4.1.1",
"rollup-plugin-yaml": "2.0.0",
"rxjs": "6.6.3",
"rollup-plugin-visualizer": "4.1.1",
"shiro-trie": "0.4.9",
"sinon": "9.0.3",
"sinon-chai": "3.5.0",
Expand Down
98 changes: 98 additions & 0 deletions src/controller/github/ApproveController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { doesExist, mustExist, mustFind } from '@apextoaster/js-utils';
import { Container } from 'noicejs';

import { CheckRBAC, Controller, ControllerData, Handler } from '..';
import { Command, CommandVerb } from '../../entity/Command';
import { Context } from '../../entity/Context';
import { GithubClient, GithubClientData } from '../../utils/github';
import { BaseController, BaseControllerOptions } from '../BaseController';

export const NOUN_APPROVE = 'github-approve';

export interface GithubApproveControllerData extends ControllerData {
client: GithubClientData;
projects: Array<{
checks: Array<{
app: string;
conclusion: string;
name: string;
status: string;
}>;
owner: string;
project: string;
}>;
}

export class GithubApproveController extends BaseController<GithubApproveControllerData> implements Controller {
protected client?: GithubClient;
protected readonly container: Container;

constructor(options: BaseControllerOptions<GithubApproveControllerData>) {
super(options, 'isolex#/definitions/service-controller-github-approve', [NOUN_APPROVE]);
this.container = options.container;
}

public async start() {
await super.start();

this.client = await this.container.create(GithubClient, {
data: this.data.client,
});
}

@Handler(NOUN_APPROVE, CommandVerb.Create)
@CheckRBAC()
public async approveRequest(cmd: Command, ctx: Context): Promise<void> {
const owner = cmd.getHeadOrDefault('owner', ctx.name);
const project = cmd.getHead('project');
const request = cmd.getHeadOrNumber('request', 0);

const client = mustExist(this.client).client;
const pull = await client.pulls.get({
owner,
/* eslint-disable-next-line camelcase, @typescript-eslint/naming-convention */
pull_number: request,
repo: project,
});

const options = {
owner,
ref: pull.data.head.sha,
repo: project,
};

const checkPromise = client.checks.listForRef(options);
const statusPromise = client.repos.getCombinedStatusForRef(options);
const [checks, status] = await Promise.all([checkPromise, statusPromise]);

const checkData = checks.data.check_runs.map((it) => ({
app: it.app.slug,
conclusion: it.conclusion,
name: it.name,
status: it.status,
}));
const statusData = status.data.statuses.map((it) => ({
app: it.context,
conclusion: it.state,
name: it.context,
status: it.state,
}));

const results = [...checkData, ...statusData];

const projectData = this.data.projects.find((it) => it.owner === owner && it.project === project);
if (doesExist(projectData)) {
const checkResults = projectData.checks.map((check) => {
const result = mustFind(results, (r) => r.app === check.app && r.name === check.name);
return check.conclusion === result.conclusion && check.status === result.status;
});

return this.reply(ctx, JSON.stringify({
checks,
results: checkResults,
}));
} else {
return this.reply(ctx, `project not found\n${JSON.stringify(results)}`);
}
}
}
2 changes: 2 additions & 0 deletions src/module/ControllerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CompletionController } from '../controller/CompletionController';
import { CountController } from '../controller/CountController';
import { DiceController } from '../controller/DiceController';
import { EchoController } from '../controller/EchoController';
import { GithubApproveController } from '../controller/github/ApproveController';
import { GithubCommitController } from '../controller/github/CommitController';
import { GithubPullController } from '../controller/github/PullController';
import { GitlabCIController } from '../controller/gitlab/CIController';
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ControllerModule extends BaseModule {
this.bindService(WeatherController);

// github controllers
this.bindService(GithubApproveController);
this.bindService(GithubCommitController);
this.bindService(GithubPullController);

Expand Down
13 changes: 13 additions & 0 deletions src/schema/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ definitions:
service-controller-echo:
$ref: "#/definitions/service-controller"

service-controller-github-approve:
allOf:
- $ref: "#/definitions/service-controller"
- type: object
required: [client, projects]
properties:
client:
$ref: "#/definitions/client-github"
projects:
type: array
items:
type: string

service-controller-github-commit:
allOf:
- $ref: "#/definitions/service-controller"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5811,6 +5811,11 @@ rollup-plugin-eslint@7.0.0:
eslint "^6.0.0"
rollup-pluginutils "^2.7.1"

rollup-plugin-graph@^2.0.0:
version "2.0.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/rollup-plugin-graph/-/rollup-plugin-graph-2.0.0.tgz#f93702c8104ceaf80871c9efc990be381c03fb2f"
integrity sha512-0onCt3IIH8pQCQ0PAIH1aeXc0zMm29x+yviSjobkloVav2TK7gBRNZjhL9x/40sc4YAmLIpwnwPZLvuZ7u5q8A==

rollup-plugin-json@^4.0.0:
version "4.0.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/rollup-plugin-json/-/rollup-plugin-json-4.0.0.tgz#a18da0a4b30bf5ca1ee76ddb1422afbb84ae2b9e"
Expand Down

0 comments on commit 7ef781e

Please sign in to comment.