-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controller/github): request approval controller
- Loading branch information
Showing
9 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,12 @@ temp/ | |
|
||
# types | ||
*.bak | ||
*.dot | ||
*.pid | ||
*.swp | ||
*.tmp | ||
|
||
noname.*.png | ||
report.*.json | ||
package-lock.json | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters