Skip to content

Commit

Permalink
feat(github): support config.prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 2, 2020
1 parent d14ab83 commit 5be435f
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface Config {
secret?: string
webhook?: string
authorize?: string
prefix?: string
appId?: string
appSecret?: string
redirect?: string
Expand All @@ -56,6 +57,7 @@ export interface Config {

const defaultOptions: Config = {
secret: '',
prefix: '.',
webhook: '/github/webhook',
authorize: '/github/authorize',
replyTimeout: Time.hour,
Expand All @@ -68,19 +70,16 @@ export const name = 'github'

export function apply(ctx: Context, config: Config = {}) {
config = { ...defaultOptions, ...config }

const webhooks = new Webhooks({
...config,
path: config.webhook,
})

const { app, database, router } = ctx
const { appId, appSecret, prefix, redirect, webhook: path } = config

const webhooks = new Webhooks({ ...config, path })
defineProperty(app, 'githubWebhooks', webhooks)

async function getTokens(params: any) {
const { data } = await axios.post<OAuth>('https://github.com/login/oauth/access_token', {
client_id: config.appId,
client_secret: config.appSecret,
client_id: appId,
client_secret: appSecret,
...params,
}, {
httpsAgent: config.agent,
Expand All @@ -93,7 +92,7 @@ export function apply(ctx: Context, config: Config = {}) {
const targetId = parseInt(ctx.query.state)
if (Number.isNaN(targetId)) throw new Error('Invalid targetId')
const { code, state } = ctx.query
const data = await getTokens({ code, state, redirect_uri: config.redirect })
const data = await getTokens({ code, state, redirect_uri: redirect })
await database.setUser(targetId, {
ghAccessToken: data.access_token,
ghRefreshToken: data.refresh_token,
Expand All @@ -105,9 +104,9 @@ export function apply(ctx: Context, config: Config = {}) {
.action(async ({ session }, user) => {
if (!user) return '请输入用户名。'
const url = 'https://github.com/login/oauth/authorize?' + encode({
client_id: config.appId,
client_id: appId,
state: session.userId,
redirect_uri: config.redirect,
redirect_uri: redirect,
scope: 'admin:repo_hook,repo',
login: user,
})
Expand Down Expand Up @@ -187,7 +186,7 @@ export function apply(ctx: Context, config: Config = {}) {

const interactions: Record<number, ReplyPayloads> = {}

router.post(config.webhook, (ctx, next) => {
router.post(path, (ctx, next) => {
// workaround @octokit/webhooks for koa
ctx.req['body'] = ctx.request.body
ctx.status = 200
Expand All @@ -207,9 +206,9 @@ export function apply(ctx: Context, config: Config = {}) {
if (!body || !payloads) return next()

let name: string, message: string
if (body.startsWith('.')) {
name = body.split(' ', 1)[0].slice(1)
message = body.slice(2 + name.length).trim()
if (body.startsWith(prefix)) {
name = body.split(' ', 1)[0].slice(prefix.length)
message = body.slice(prefix.length + name.length).trim()
} else {
name = reactions.includes(body) ? 'react' : 'reply'
message = body
Expand Down

0 comments on commit 5be435f

Please sign in to comment.