Merge branch 'webhook-types' into github-app-webhooks

* webhook-types:
  Test for context.isBot
  Add test for deprecated receive behavior
  Add strong typing for exported webhooks property
  Fix deprecation warnings
  Make most webhook payload properties optional for now
  Move webhook type declarations
This commit is contained in:
Brandon Keepers
2018-07-15 13:52:41 -05:00
9 changed files with 62 additions and 20 deletions

View File

@@ -32,4 +32,4 @@ const probot = createProbot({
probot.setup(program.args.slice(2))
probot.logger.debug('Simulating event', eventName)
probot.receive({event: eventName, payload})
probot.receive({name: eventName, payload})

View File

@@ -26,14 +26,14 @@ declare module '@octokit/webhooks' {
export interface PayloadRepository {
[key: string]: any
full_name: string
full_name?: string
name: string
owner: {
[key: string]: any
login: string
name: string
name?: string
}
html_url: string
html_url?: string
}
export interface WebhookPayloadWithRepository {
@@ -42,14 +42,14 @@ declare module '@octokit/webhooks' {
issue?: {
[key: string]: any
number: number
html_url: string
body: string
html_url?: string
body?: string
}
pull_request?: {
[key: string]: any
number: number
html_url: string
body: string
html_url?: string
body?: string
}
sender?: {
[key: string]: any

View File

@@ -46,10 +46,16 @@ export class Application {
}
public async receive (event: WebhookEvent) {
if ((event as any).event) {
// tslint:disable-next-line:no-console
console.warn(new Error('Propery `event` is deprecated, use `name`'))
event = { name: (event as any).event, ...event }
}
return Promise.all([
this.events.emit('*', event),
this.events.emit(event.name, event),
this.events.emit(`${event.name}.${event.payload.action}`, event)
this.events.emit(`${ event.name }.${ event.payload.action }`, event)
])
}

View File

@@ -67,7 +67,7 @@ export class Context implements WebhookEvent {
}
return Object.assign({
owner: repo.owner.login || repo.owner.name,
owner: repo.owner.login || repo.owner.name!,
repo: repo.name
}, object)
}

View File

@@ -9,6 +9,8 @@ import { resolve } from './resolver'
import { createServer } from './server'
import { createWebhookProxy } from './webhook-proxy'
// tslint:disable:no-var-requires
// These needs types
const logRequestErrors = require('./middleware/log-request-errors')
const defaultApps: ApplicationFunction[] = [

View File

@@ -1,14 +1,14 @@
import { PayloadRepository } from '@octokit/webhooks'
import { PayloadRepository, WebhookEvent } from '@octokit/webhooks'
import bunyan from 'bunyan'
import express from 'express'
export const serializers: bunyan.StdSerializers = {
event: (event: any) => {
event: (event: WebhookEvent | any) => {
if (typeof event !== 'object' || !event.payload) {
return event
} else {
let name = event.event
let name = event.name
if (event.payload && event.payload.action) {
name = `${name}.${event.payload.action}`
}

View File

@@ -76,13 +76,14 @@ describe('Application', () => {
})
it('calls callback x amount of times when an array of x actions is passed', async () => {
const event2 = {
const event2: WebhookEvent = {
id: '123',
name: 'arrayTest',
payload: {
action: 'bar',
installation: { id: 2 }
}
} as any
}
const spy = jest.fn()
app.on(['test.foo', 'arrayTest.bar'], spy)
@@ -269,5 +270,19 @@ describe('Application', () => {
expect(await app.auth(1, 'a logger' as any)).toEqual('a github client')
expect(github.auth).toHaveBeenCalledWith(1, 'a logger')
})
test('recieve() accepts param with {event}', async () => {
const spy = jest.fn()
app.events.on('deprecated', spy)
await app.receive({ event: 'deprecated', payload: { action: 'test' } } as any)
expect(spy).toHaveBeenCalled()
})
test('recieve() accepts param with {name,event}', async () => {
const spy = jest.fn()
app.events.on('real-event-name', spy)
await app.receive({ name: 'real-event-name', event: 'deprecated', payload: { action: 'test' } } as any)
expect(spy).toHaveBeenCalled()
})
})
})

View File

@@ -1,15 +1,17 @@
import fs = require('fs')
import path = require('path')
import { WebhookEvent } from '@octokit/webhooks'
import { Context } from '../src/context'
import { GitHubAPI, OctokitError } from '../src/github'
describe('Context', () => {
let event: any
let event: WebhookEvent
let context: Context
beforeEach(() => {
event = {
id: '123',
name: 'push',
payload: {
issue: { number: 4 },
@@ -27,8 +29,9 @@ describe('Context', () => {
expect(context.payload).toBe(event.payload)
})
it('aliases name to event', () => {
expect(context.event).toEqual(event.name)
it('aliases the event name', () => {
expect(context.name).toEqual('push')
expect(context.event).toEqual('push')
})
describe('repo', () => {
@@ -218,4 +221,20 @@ describe('Context', () => {
})
})
})
describe('isBot', () => {
test('returns true if sender is a bot', () => {
event.payload.sender = { type: 'Bot' }
context = new Context(event, {} as any, {} as any)
expect(context.isBot).toBe(true)
})
test('returns false if sender is not a bot', () => {
event.payload.sender = { type: 'User' }
context = new Context(event, {} as any, {} as any)
expect(context.isBot).toBe(false)
})
})
})

View File

@@ -23,7 +23,7 @@ describe('serializers', () => {
describe('event', () => {
it('works with a legit event', () => {
const event = {id: 1,
event: 'test',
name: 'test',
payload: {
action: 'test',
repository: {full_name: 'probot/test'},
@@ -40,7 +40,7 @@ describe('serializers', () => {
it('works a malformed event', () => {
const event = {id: 1,
event: 'test',
name: 'test',
payload: {}}
expect(serializers.event(event)).toEqual({
id: 1,