Skip to content

Commit

Permalink
feat(github): database definition
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 23, 2021
1 parent f2e5535 commit 9e962dc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/plugin-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
],
"devDependencies": {
"@types/marked": "^2.0.0",
"koishi-plugin-mongo": "^2.1.1",
"koishi-plugin-mysql": "^3.1.0",
"koishi-plugin-puppeteer": "^2.0.0",
"koishi-test-utils": "^6.0.0-beta.10"
},
Expand Down
15 changes: 4 additions & 11 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@
/* eslint-disable quote-props */

import { createHmac } from 'crypto'
import { Context } from 'koishi-core'
import { camelize, defineProperty, Time, Random } from 'koishi-utils'
import { encode } from 'querystring'
import { Context, camelize, Time, Random } from 'koishi-core'
import { CommonPayload, addListeners, defaultEvents } from './events'
import { Config, GitHub, ReplyHandler, EventData } from './server'
import { Config, GitHub, ReplyHandler, ReplySession, EventData } from './server'

export * from './server'

declare module 'koishi-core' {
interface App {
github?: GitHub
}
}

const defaultOptions: Config = {
secret: '',
messagePrefix: '[GitHub] ',
webhook: '/github/webhook',
authorize: '/github/authorize',
replyTimeout: Time.hour,
repos: {},
repos: [],
events: {},
}

Expand Down Expand Up @@ -111,7 +104,7 @@ export function apply(ctx: Context, config: Config = {}) {
}
})

ctx.middleware((session, next) => {
ctx.middleware((session: ReplySession, next) => {
if (!session.quote) return next()
const body = session.parsed.content.trim()
const payloads = history[session.quote.messageId.slice(0, 6)]
Expand Down
53 changes: 47 additions & 6 deletions packages/plugin-github/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

import { EventConfig } from './events'
import axios, { AxiosError, Method } from 'axios'
import { App, Session, User } from 'koishi-core'
import { App, Channel, Database, Session, User } from 'koishi-core'
import { segment, Logger } from 'koishi-utils'
import {} from 'koishi-plugin-puppeteer'
import {} from 'koishi-plugin-mysql'
import {} from 'koishi-plugin-mongo'

declare module 'koishi-core' {
interface App {
github?: GitHub
}

interface User {
ghAccessToken?: string
ghRefreshToken?: string
ghAccessToken: string
ghRefreshToken: string
}

interface Channel {
githubWebhooks: Record<string, {}>
}

interface Tables {
github: Repository
}
}

Expand All @@ -18,6 +32,33 @@ User.extend(() => ({
ghRefreshToken: '',
}))

Channel.extend(() => ({
githubWebhooks: {},
}))

Database.extend('koishi-plugin-mysql', ({ tables, Domain }) => {
tables.user.ghAccessToken = 'varchar(50)'
tables.user.ghRefreshToken = 'varchar(50)'
tables.channel.githubWebhooks = new Domain.Json()
tables.github = Object.assign<any, any>(['primary key (`name`)'], {
name: 'varchar(50)',
secret: 'varchar(50)',
})
})

Database.extend('koishi-plugin-mongo', ({ tables }) => {
tables.github = { primary: 'name' }
})

interface Repository {
name: string
secret: string
}

interface RepoConfig extends Repository {
targets: string[]
}

export interface Config {
secret?: string
webhook?: string
Expand All @@ -29,7 +70,7 @@ export interface Config {
promptTimeout?: number
replyTimeout?: number
requestTimeout?: number
repos?: Record<string, string[]>
repos?: RepoConfig[]
events?: EventConfig
}

Expand All @@ -42,7 +83,7 @@ export interface OAuth {
scope: string
}

type ReplySession = Session<'ghAccessToken' | 'ghRefreshToken'>
export type ReplySession = Session<'ghAccessToken' | 'ghRefreshToken'>

const logger = new Logger('github')

Expand Down Expand Up @@ -131,7 +172,7 @@ type ReplyPayloads = {
export type EventData<T = {}> = [string, (ReplyPayloads & T)?]

export class ReplyHandler {
constructor(public github: GitHub, public session: Session, public content?: string) {}
constructor(public github: GitHub, public session: ReplySession, public content?: string) {}

link(url: string) {
return this.session.send(url)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-mongo/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Config {
}

interface TableConfig<O> {
primary: keyof O
primary?: keyof O
type?: 'incremental'
}

Expand Down

0 comments on commit 9e962dc

Please sign in to comment.