Skip to content

Commit

Permalink
feat(github): support star & issue command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 29, 2021
1 parent 4569000 commit 104841c
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { encode } from 'querystring'
import { Context, camelize, Time, Random, sanitize, Logger, Session } from 'koishi-core'
import { CommonPayload, addListeners, defaultEvents, EventConfig } from './events'
import { Config, GitHub, ReplyHandler, ReplySession, ReplyPayloads } from './server'
import axios from 'axios'
import axios, { Method } from 'axios'

export * from './server'

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

const repoRegExp = /^[\w.-]+\/[\w.-]+$/

ctx.command('github.repos [name]', 'GitHub 仓库')
ctx.command('github.repos [name]', '管理监听的仓库')
.userFields(['ghAccessToken', 'ghRefreshToken'])
.option('add', '-a 监听一个新的仓库')
.option('delete', '-d 移除已监听的仓库')
Expand Down Expand Up @@ -196,6 +196,43 @@ export function apply(ctx: Context, config: Config = {}) {
}
})

async function request(method: Method, url: string, session: ReplySession, body: any, message: string) {
return ctx.app.github.request(method, 'https://api.github.com' + url, session, body)
.then(() => message + '成功!')
.catch((err) => {
logger.warn(err)
return message + '失败。'
})
}

ctx.command('github.issue [title] [body:text]', '创建和查看 issue')
.userFields(['ghAccessToken', 'ghRefreshToken'])
.option('repo', '-r [repo:string] 仓库名')
.action(async ({ session, options }, title, body) => {
if (!options.repo) return '请输入仓库名。'
if (!repoRegExp.test(options.repo)) return '请输入正确的仓库名。'
if (!session.user.ghAccessToken) {
return ctx.app.github.authorize(session, '要使用此功能,请对机器人进行授权。输入你的 GitHub 用户名。')
}

return request('POST', `/repos/${options.repo}/issues`, session, {
title,
body,
}, '创建')
})

ctx.command('github.star [repo]', '给仓库点个 star')
.userFields(['ghAccessToken', 'ghRefreshToken'])
.action(async ({ session }, repo) => {
if (!repo) return '请输入仓库名。'
if (!repoRegExp.test(repo)) return '请输入正确的仓库名。'
if (!session.user.ghAccessToken) {
return ctx.app.github.authorize(session, '要使用此功能,请对机器人进行授权。输入你的 GitHub 用户名。')
}

return request('PUT', `/user/starred/${repo}`, session, null, '创建')
})

ctx.on('connect', async () => {
const channels = await ctx.database.getAssignedChannels(['id', 'githubWebhooks'])
for (const { id, githubWebhooks } of channels) {
Expand Down

0 comments on commit 104841c

Please sign in to comment.