forked from mirrors/probot
refactor: use webhooks type definitions from @octokit/webhooks
This commit is contained in:
parent
08047e25ca
commit
7e59638bb5
|
@ -1,70 +0,0 @@
|
|||
declare module '@octokit/webhooks' {
|
||||
import { Application } from 'express'
|
||||
|
||||
interface Options {
|
||||
path: string,
|
||||
secret: string
|
||||
}
|
||||
|
||||
declare class Webhooks {
|
||||
public middleware: Application
|
||||
|
||||
constructor (options: Options)
|
||||
|
||||
public on (event: 'error', callback: (error: Error) => void)
|
||||
public on (event: '*' | string[], callback: (event: Webhooks.WebhookEvent | Error) => void)
|
||||
public on (event: string, callback: (event: Webhooks.WebhookEvent) => void)
|
||||
public sign (data: Webhooks.WebhookPayloadWithRepository)
|
||||
}
|
||||
|
||||
declare namespace Webhooks {
|
||||
export interface WebhookEvent {
|
||||
id: string
|
||||
name: string
|
||||
payload: WebhookPayloadWithRepository
|
||||
protocol?: 'http' | 'https'
|
||||
host?: string
|
||||
url?: string
|
||||
}
|
||||
|
||||
export interface PayloadRepository {
|
||||
[key: string]: any
|
||||
full_name?: string
|
||||
name: string
|
||||
owner: {
|
||||
[key: string]: any
|
||||
login: string
|
||||
name?: string
|
||||
}
|
||||
html_url?: string
|
||||
}
|
||||
|
||||
export interface WebhookPayloadWithRepository {
|
||||
[key: string]: any
|
||||
repository?: PayloadRepository
|
||||
issue?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
html_url?: string
|
||||
body?: string
|
||||
}
|
||||
pull_request?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
html_url?: string
|
||||
body?: string
|
||||
}
|
||||
sender?: {
|
||||
[key: string]: any
|
||||
type: string
|
||||
}
|
||||
action?: string
|
||||
installation?: {
|
||||
id: number
|
||||
[key: string]: any
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export = Webhooks
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import OctokitApp from '@octokit/app'
|
||||
import { WebhookEvent } from '@octokit/webhooks'
|
||||
import Webhooks from '@octokit/webhooks'
|
||||
import express from 'express'
|
||||
import { EventEmitter } from 'promise-events'
|
||||
import { ApplicationFunction } from '.'
|
||||
|
@ -18,7 +18,7 @@ export interface Options {
|
|||
}
|
||||
|
||||
// Some events can't get an authenticated client (#382):
|
||||
function isUnauthenticatedEvent (event: WebhookEvent) {
|
||||
function isUnauthenticatedEvent (event: Webhooks.WebhookEvent<any>) {
|
||||
return !event.payload.installation ||
|
||||
(event.name === 'installation' && event.payload.action === 'deleted')
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ export class Application {
|
|||
return this
|
||||
}
|
||||
|
||||
public async receive (event: WebhookEvent) {
|
||||
public async receive (event: Webhooks.WebhookEvent<any>) {
|
||||
if ((event as any).event) {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.warn(new Error('Property `event` is deprecated, use `name`'))
|
||||
|
@ -158,7 +158,7 @@ export class Application {
|
|||
public on (eventName: string | string[], callback: (context: Context) => Promise<void>) {
|
||||
if (typeof eventName === 'string') {
|
||||
|
||||
return this.events.on(eventName, async (event: WebhookEvent) => {
|
||||
return this.events.on(eventName, async (event: Webhooks.WebhookEvent<any>) => {
|
||||
const log = this.log.child({ name: 'event', id: event.id })
|
||||
|
||||
try {
|
||||
|
@ -244,7 +244,7 @@ export class Application {
|
|||
return github
|
||||
}
|
||||
|
||||
private authenticateEvent (event: WebhookEvent, log: LoggerWithTarget): Promise<GitHubAPI> {
|
||||
private authenticateEvent (event: Webhooks.WebhookEvent<any>, log: LoggerWithTarget): Promise<GitHubAPI> {
|
||||
if (this.githubToken) {
|
||||
return this.auth()
|
||||
}
|
||||
|
@ -254,6 +254,6 @@ export class Application {
|
|||
return this.auth()
|
||||
}
|
||||
|
||||
return this.auth(event.payload.installation!.id, log)
|
||||
return this.auth(event.payload.installation.id, log)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,35 @@
|
|||
import { WebhookEvent, WebhookPayloadWithRepository } from '@octokit/webhooks'
|
||||
import { PayloadRepository, Webhooks } from '@octokit/webhooks'
|
||||
import yaml from 'js-yaml'
|
||||
import path from 'path'
|
||||
import { GitHubAPI } from './github'
|
||||
import { LoggerWithTarget } from './wrap-logger'
|
||||
|
||||
interface WebhookPayloadWithRepository {
|
||||
[key: string]: any
|
||||
repository?: PayloadRepository
|
||||
issue?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
html_url?: string
|
||||
body?: string
|
||||
}
|
||||
pull_request?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
html_url?: string
|
||||
body?: string
|
||||
}
|
||||
sender?: {
|
||||
[key: string]: any
|
||||
type: string
|
||||
}
|
||||
action?: string
|
||||
installation?: {
|
||||
id: number
|
||||
[key: string]: any
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The context of the event that was triggered, including the payload and
|
||||
* helpers for extracting information can be passed to GitHub API calls.
|
||||
|
@ -20,7 +47,7 @@ import { LoggerWithTarget } from './wrap-logger'
|
|||
* @property {logger} log - A logger
|
||||
*/
|
||||
|
||||
export class Context implements WebhookEvent {
|
||||
export class Context implements Webhooks.WebhookEvent<any> {
|
||||
public name: string
|
||||
public id: string
|
||||
public payload: WebhookPayloadWithRepository
|
||||
|
@ -31,7 +58,7 @@ export class Context implements WebhookEvent {
|
|||
public github: GitHubAPI
|
||||
public log: LoggerWithTarget
|
||||
|
||||
constructor (event: WebhookEvent, github: GitHubAPI, log: LoggerWithTarget) {
|
||||
constructor (event: Webhooks.WebhookEvent<any>, github: GitHubAPI, log: LoggerWithTarget) {
|
||||
this.name = event.name
|
||||
this.id = event.id
|
||||
this.payload = event.payload
|
||||
|
|
13
src/index.ts
13
src/index.ts
|
@ -1,5 +1,5 @@
|
|||
import OctokitApp from '@octokit/app'
|
||||
import Webhooks, { WebhookEvent } from '@octokit/webhooks'
|
||||
import Webhooks from '@octokit/webhooks'
|
||||
import Logger from 'bunyan'
|
||||
import express from 'express'
|
||||
|
||||
|
@ -40,7 +40,10 @@ export class Probot {
|
|||
this.options = options
|
||||
this.logger = logger
|
||||
this.apps = []
|
||||
this.webhook = new Webhooks({ path: options.webhookPath, secret: options.secret })
|
||||
this.webhook = new Webhooks({
|
||||
path: options.webhookPath,
|
||||
secret: options.secret
|
||||
})
|
||||
this.githubToken = options.githubToken
|
||||
if (this.options.id) {
|
||||
this.app = new OctokitApp({
|
||||
|
@ -48,10 +51,10 @@ export class Probot {
|
|||
privateKey: options.cert as string
|
||||
})
|
||||
}
|
||||
this.server = createServer({ webhook: this.webhook.middleware, logger })
|
||||
this.server = createServer({ webhook: (this.webhook as any).middleware, logger })
|
||||
|
||||
// Log all received webhooks
|
||||
this.webhook.on('*', async (event: WebhookEvent) => {
|
||||
this.webhook.on('*', async (event: Webhooks.WebhookEvent<any>) => {
|
||||
try {
|
||||
await this.receive(event)
|
||||
} catch {
|
||||
|
@ -74,7 +77,7 @@ export class Probot {
|
|||
}
|
||||
}
|
||||
|
||||
public receive (event: WebhookEvent) {
|
||||
public receive (event: Webhooks.WebhookEvent<any>) {
|
||||
this.logger.debug({ event }, 'Webhook received')
|
||||
return Promise.all(this.apps.map(app => app.receive(event)))
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { PayloadRepository, WebhookEvent } from '@octokit/webhooks'
|
||||
import { PayloadRepository, Webhooks } from '@octokit/webhooks'
|
||||
import bunyan from 'bunyan'
|
||||
import express from 'express'
|
||||
|
||||
export const serializers: bunyan.StdSerializers = {
|
||||
|
||||
event: (event: WebhookEvent | any) => {
|
||||
event: (event: Webhooks.WebhookEvent<any> | any) => {
|
||||
if (typeof event !== 'object' || !event.payload) {
|
||||
return event
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { WebhookEvent } from '@octokit/webhooks'
|
||||
import Webhooks from '@octokit/webhooks'
|
||||
import Bottleneck from 'bottleneck'
|
||||
|
||||
import { Application } from '../src/application'
|
||||
|
@ -8,7 +8,7 @@ import { logger } from '../src/logger'
|
|||
|
||||
describe('Application', () => {
|
||||
let app: Application
|
||||
let event: WebhookEvent
|
||||
let event: Webhooks.WebhookEvent<any>
|
||||
let output: any
|
||||
|
||||
beforeAll(() => {
|
||||
|
@ -75,7 +75,7 @@ describe('Application', () => {
|
|||
})
|
||||
|
||||
it('calls callback x amount of times when an array of x actions is passed', async () => {
|
||||
const event2: WebhookEvent = {
|
||||
const event2: Webhooks.WebhookEvent<any> = {
|
||||
id: '123',
|
||||
name: 'arrayTest',
|
||||
payload: {
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import fs = require('fs')
|
||||
import path = require('path')
|
||||
|
||||
import { WebhookEvent } from '@octokit/webhooks'
|
||||
import Webhooks from '@octokit/webhooks'
|
||||
import { Context } from '../src/context'
|
||||
import { GitHubAPI, OctokitError } from '../src/github'
|
||||
|
||||
describe('Context', () => {
|
||||
let event: WebhookEvent
|
||||
let event: Webhooks.WebhookEvent<any>
|
||||
let context: Context
|
||||
|
||||
beforeEach(() => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const {createProbot} = require('../src')
|
||||
const { createProbot } = require('../src')
|
||||
const request = require('supertest')
|
||||
const nock = require('nock')
|
||||
const helper = require('./apps/helper')
|
||||
|
@ -139,7 +139,7 @@ describe('Probot', () => {
|
|||
})
|
||||
|
||||
it('allows users to configure webhook paths', async () => {
|
||||
probot = createProbot({webhookPath: '/webhook', githubToken: 'faketoken'})
|
||||
probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken' })
|
||||
// Error handler to avoid printing logs
|
||||
// eslint-disable-next-line handle-callback-err
|
||||
probot.server.use((err, req, res, next) => { })
|
||||
|
@ -205,7 +205,7 @@ describe('Probot', () => {
|
|||
process.env.GHE_HOST = 'notreallygithub.com'
|
||||
|
||||
nock('https://notreallygithub.com/api/v3')
|
||||
.defaultReplyHeaders({'Content-Type': 'application/json'})
|
||||
.defaultReplyHeaders({ 'Content-Type': 'application/json' })
|
||||
.get('/app/installations').reply(200, ['I work!'])
|
||||
|
||||
app = helper.createApp()
|
||||
|
|
Loading…
Reference in New Issue