Skip to content

Commit

Permalink
fix(github): fix 404 not found & init tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 31, 2020
1 parent 753ca9f commit 44de27f
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 162 deletions.
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
'packages/plugin-github/tests/*.spec.ts',
'packages/plugin-teach/tests/*.spec.ts',
],
require: [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ koishi run
- [Mrs4s/go-cqhttp](https://github.com/Mrs4s/go-cqhttp)
- [yyuueexxiinngg/cqhttp-mirai](https://github.com/yyuueexxiinngg/cqhttp-mirai)

请注意:尽管 Koishi 使用的协议是 MIT,但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,**必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制所可能造成的法律后果不负任何责任
请注意:尽管 Koishi 使用的协议是 [MIT](https://choosealicense.com/licenses/mit/),但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,**必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制的行为所可能造成的法律后果概不负责

## 数据库支持

Expand Down
18 changes: 14 additions & 4 deletions packages/plugin-github/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */

import { Context, Middleware, User } from 'koishi-core'
import { Logger, Time } from 'koishi-utils'
import { defineProperty, Logger, Time } from 'koishi-utils'
import { Octokit } from '@octokit/rest'
import { Webhooks, EventNames, EventPayloads } from '@octokit/webhooks'
import { GetWebhookPayloadTypeFromEvent } from '@octokit/webhooks/dist-types/generated/get-webhook-payload-type-from-event'
Expand All @@ -16,6 +16,12 @@ type Issue = EventPayloads.WebhookPayloadIssuesIssue
| EventPayloads.WebhookPayloadPullRequestReviewPullRequest
type ReviewComment = EventPayloads.WebhookPayloadPullRequestReviewCommentComment

declare module 'koishi-core/dist/app' {
interface App {
githubWebhooks?: Webhooks
}
}

declare module 'koishi-core/dist/database' {
interface User {
githubToken?: string
Expand Down Expand Up @@ -62,7 +68,11 @@ export function apply(ctx: Context, config: Config = {}) {
if (!ctx.router) throw new Error('ctx.router is not defined')

config = { ...defaultOptions, ...config }
const webhook = new Webhooks(config)
const webhooks = new Webhooks({
...config,
path: config.webhook,
})
defineProperty(ctx.app, 'githubWebhooks', webhooks)
const github = new Octokit({
request: {
agent: config.agent,
Expand All @@ -73,7 +83,7 @@ export function apply(ctx: Context, config: Config = {}) {
const { router, database } = ctx

router.post(config.webhook, (ctx, next) => {
return webhook.middleware(ctx.req, ctx.res, next)
return webhooks.middleware(ctx.req, ctx.res, next)
})

router.get(config.authorize, async (ctx) => {
Expand Down Expand Up @@ -120,7 +130,7 @@ export function apply(ctx: Context, config: Config = {}) {
})

function registerHandler<T extends EventNames.All>(event: T, handler: (payload: Payload<T>) => [string, Middleware?]) {
webhook.on(event, async (callback) => {
webhooks.on(event, async (callback) => {
const { repository } = callback.payload
const groupIds = config.repos[repository.full_name]
if (!groupIds) return
Expand Down
141 changes: 0 additions & 141 deletions packages/plugin-github/tests/__snapshots__/index.spec.ts.snap

This file was deleted.

45 changes: 45 additions & 0 deletions packages/plugin-github/tests/index.snap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports[`commit_comment.created`] = `
[GitHub] Shigma commented on commit koishijs/koishi@bff469
URL: https://github.com/koishijs/koishi/commit/bff469eabe14d42683a4f7c3ccb659daec5e1c00#commitcomment-36878220
This will introduce a failure in test.`

module.exports[`issue_comment.created.1`] = `
[GitHub] simon300000 commented on issue koishijs/koishi#19
URL: https://github.com/koishijs/koishi/issues/19#issuecomment-576277946
Mich würde auch interessieren, was ist „CoolQ“?`

module.exports[`issue_comment.created.2`] = `
[GitHub] Kouchya commented on pull request koishijs/koishi#20
URL: https://github.com/koishijs/koishi/pull/20#issuecomment-576291300
C'est important!`

module.exports[`issues.opened`] = `
[GitHub] simon300000 opened an issue koishijs/koishi#19
URL: https://github.com/koishijs/koishi/issues/19
Title: Wie kann man um das Koishi zu installieren?
Ich verstecke Englisch und Chinesisch nicht! Gab es Personen, die mir helfen kann?`

module.exports[`pull_request.opened`] = `
[GitHub] simon300000 opened a pull request koishijs/koishi#20 (koishijs:develop <- koishijs:experimental)
URL: https://github.com/koishijs/koishi/pull/20
Das ist wichtig!`

module.exports[`pull_request_review.submitted.2`] = `
[GitHub] Kouchya reviewed pull request koishijs/koishi#20
URL: https://github.com/koishijs/koishi/pull/20#pullrequestreview-345349537
LGTM`

module.exports[`pull_request_review_comment.created`] = `
[GitHub] Shigma commented on pull request review koishijs/koishi#20
Path: packages/test-utils/src/mocks.ts
URL: https://github.com/koishijs/koishi/pull/20#discussion_r368570320
Naming is so hard......`

module.exports[`push.commit`] = `
[GitHub] Shigma pushed to koishijs/koishi:refs/heads/develop
Compare: https://github.com/koishijs/koishi/compare/976c6e8f09a4...3ae7e7044d06
[d7ff34] chore: adjust
[3ae7e7] fix(core): create major context at demand`

module.exports[`push.tag`] = `
[GitHub] Shigma published tag koishijs/koishi@1.5.0`
59 changes: 43 additions & 16 deletions packages/plugin-github/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
import { MockedApp } from 'koishi-test-utils'
import { apply, webhooks } from '../src'
import { App, BASE_SELF_ID, memory } from 'koishi-test-utils'
import { Random } from 'koishi-utils'
import { fn, spyOn } from 'jest-mock'
import { expect } from 'chai'
import { readdirSync } from 'fs-extra'
import { resolve } from 'path'
import * as github from 'koishi-plugin-github'

const app = new MockedApp({
githubWebhook: {
secret: 'secret',
},
const secret = Random.uuid()

const app = new App({ port: 10000 })

app.plugin(memory)

app.plugin(github, {
secret,
repos: { 'koishijs/koishi': [123] },
})

app.plugin(apply, {
'koishijs/koishi': [123],
// override listen
const listen = spyOn(app.server, 'listen')
listen.mockReturnValue(Promise.resolve())

// spy on sendGroupMsg
const sendGroupMsg = app.bots[0].sendGroupMsg = fn()

before(async () => {
await app.start()
await app.database.getGroup(123, BASE_SELF_ID)
})

const webhook = webhooks['/secret12140']
const snapshot = require('./index.snap')

readdirSync(resolve(__dirname, '__fixtures__')).forEach((file) => {
file = file.slice(0, -5)
const [name] = file.split('.', 1)
const payload = require(`./__fixtures__/${file}`)
function check(file: string) {
it(file, async () => {
sendGroupMsg.mockClear()
const payload = require(`./fixtures/${file}`)
const [name] = file.split('.', 1)
await app.githubWebhooks.receive({ id: Random.uuid(), name, payload })
if (snapshot[file]) {
expect(sendGroupMsg.mock.calls).to.have.length(1)
expect(sendGroupMsg.mock.calls[0][1]).to.equal(snapshot[file].trim())
} else {
expect(sendGroupMsg.mock.calls).to.have.length(0)
}
})
}

test(file, async () => {
await webhook.receive({ id: 'id', name, payload })
app.shouldMatchSnapshot(file)
describe('koishi-plugin-github', () => {
const files = readdirSync(resolve(__dirname, 'fixtures'))
files.forEach(file => {
check(file.slice(0, -5))
})
})

0 comments on commit 44de27f

Please sign in to comment.