-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
148 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters