Skip to content

Commit

Permalink
feat: add stats endpoint (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam authored Jan 21, 2019
1 parent e212720 commit 6d1018f
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 96 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ideas not as issues yet:


### Testing serverless locally
`yarn add serverless-dotenv-plugin`
`yarn add serverless-dotenv-plugin --dev`

in `serverless.yml` plugins add:
`- serverless-dotenv-plugin`
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"test-ci": "jest --reporters ./node_modules/jest-junit --collectCoverage"
},
"dependencies": {
"@probot/serverless-lambda": "^0.2.0",
"all-contributors-cli": "^5.10.1",
"compromise": "^11.13.0",
"probot": "^8.0.0-octokit-16-preview"
Expand Down
9 changes: 8 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ provider:

functions:
githubWebhook:
handler: src/handler.probot
handler: src/serverless-webhook.handler
events:
- http:
path: /
method: post
cors: true
stats:
handler: src/serverless-stats.handler
events:
- http:
path: /stats
method: get
cors: true
5 changes: 0 additions & 5 deletions src/handler.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/probot/getProbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { createProbot } = require('probot')
const { findPrivateKey } = require('probot/lib/private-key')

let probot

function getProbot() {
probot =
probot ||
createProbot({
id: process.env.APP_ID,
secret: process.env.WEBHOOK_SECRET,
cert: findPrivateKey(),
})

if (process.env.SENTRY_DSN) {
probot.load(require('probot/lib/apps/sentry'))
}

return probot
}

module.exports = getProbot
84 changes: 0 additions & 84 deletions src/serverless-lambda.js

This file was deleted.

86 changes: 86 additions & 0 deletions src/serverless-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const getProbot = require('./probot/getProbot')

async function getInstallations(app) {
const github = await app.auth()

return github.paginate(
github.apps.listInstallations.endpoint.merge({
per_page: 100,
headers: {
accept: 'application/vnd.github.machine-man-preview+json',
},
}),
response => {
return response.data
},
)
}

async function popularInstallations({ app, installations }) {
let popular = await Promise.all(
installations.map(async installation => {
const { account } = installation

const github = await app.auth(installation.id)

const repositories = await github.paginate(
github.apps.listRepos.endpoint.merge({
per_page: 100,
headers: {
accept:
'application/vnd.github.machine-man-preview+json',
},
}),
response => {
return response.data.repositories.filter(
repository => !repository.private,
)
},
)

account.stars = repositories.reduce((stars, repository) => {
return stars + repository.stargazers_count
}, 0)

return account
}),
)

popular = popular.filter(installation => installation.stars > 0)
return popular.sort((a, b) => b.stars - a.stars).slice(0, 10)
}

async function getStats(probot) {
const app = probot.apps[0]

if (!app) {
throw new Error(
'Stats needs an app, we usually depend on sentry being loaded. You can load sentry by setting the SENTRY_DSN env variable)',
)
}

const installations = await getInstallations(app)
const popular = await popularInstallations({ app, installations })
return {
installations: installations.length,
popular,
}
}

module.exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false

try {
const probot = getProbot()
const stats = await getStats(probot)
return {
statusCode: 200,
body: JSON.stringify(stats),
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error.message),
}
}
}
31 changes: 31 additions & 0 deletions src/serverless-webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const getProbot = require('./probot/getProbot')
const appFn = require('./')

module.exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false

try {
const probot = getProbot()
probot.load(appFn)

const name =
event.headers['x-github-event'] || event.headers['X-GitHub-Event']
const payload =
typeof event.body === 'string' ? JSON.parse(event.body) : event.body
await probot.receive({
name,
payload,
})
return {
statusCode: 200,
body: JSON.stringify({
message: `Received ${name}.${payload.action}`,
}),
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error.message),
}
}
}
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@
buffer-equal-constant-time "^1.0.1"
debug "^4.0.0"

"@probot/serverless-lambda@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@probot/serverless-lambda/-/serverless-lambda-0.2.0.tgz#09ec3c5b77486d8e5e406337b8d46a38418d1019"

"@serverless/platform-sdk@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@serverless/platform-sdk/-/platform-sdk-0.3.0.tgz#335adeb2759760e4232c321d72b4022e4dce7818"
Expand Down

0 comments on commit 6d1018f

Please # to comment.