tibdex / probot-serverless-now

🤖 Probot Wrapper to run GitHub Apps as Lambdas in Zeit's Now 2.0
MIT License
33 stars 4 forks source link

Does this still function? #12

Open rgeraldporter opened 4 years ago

rgeraldporter commented 4 years ago

Hi -- I've been trying a couple days to get this working, but all I ever get is a 404 at the /api endpoint.

Additionally I cloned the repo down but the tests do not pass. Has something broken, or has there been a change in Now that might require changes here?

Thanks!

timbru31 commented 4 years ago

Hi Robert,

it didn't work for me either, so I've taken a look how the AWS Lambda and GCF implementations worked, below is my custom Zeit/Vercel NOW script. I've also added the feature to re-use an existing probot instance (I'm not 100% sure how long a serverless function is kept warm on Vercel):

// eslint-disable-next-line no-unused-vars
import { NowRequest, NowResponse } from '@now/node'
// eslint-disable-next-line no-unused-vars
import { createProbot, Probot } from 'probot'
import { findPrivateKey } from 'probot/lib/private-key'
import { logRequestErrors } from 'probot/lib/middleware/log-request-errors'
import sadCreeper from '../src'
import { promises as fsPromises } from 'fs'
import { resolve } from 'path'

let probot: Probot

const initializeProbot = () => {
  const options = {
    cert: String(findPrivateKey()),
    id: Number(process.env.APP_ID),
    secret: process.env.WEBHOOK_SECRET
  }
  // @ts-ignore Block-scoped variable 'probot' used before its declaration.ts(2448)
  // eslint-disable-next-line no-use-before-define
  const probot = probot || createProbot(options)

  process.on('unhandledRejection', probot.errorHandler as any)
  probot.load(sadCreeper)

  probot.server.use(logRequestErrors)
  return probot
}

export default async (request: NowRequest, response: NowResponse) => {
  if (request.method === 'GET') {
    const content = await fsPromises.readFile(
      resolve(__dirname, '../public/index.html'),
      {
        encoding: 'utf-8'
      }
    )
    response.status(200).send(content)
    return
  }

  const name =
    (request.headers['x-github-event'] as string) ||
    (request.headers['X-GitHub-Event'] as string)
  const id =
    (request.headers['x-github-delivery'] as string) ||
    (request.headers['X-GitHub-Delivery'] as string)

  probot = probot || initializeProbot()
  try {
    await probot.receive({
      id,
      name,
      payload: request.body
    })
    response.status(200)
  } catch (err) {
    console.error(err)
    response
      .status(500)
      .json({ error: JSON.stringify(err), req: JSON.stringify(request) })
  }
}
tibdex commented 4 years ago

Hi, I haven't been maintaining this package for a while. I was using it for Autorebase that is deprecated too since I moved to Autosquash (paired with github-app-token).