Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new event actions and examples from example payload files provided by @rachmari #85

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24,017 changes: 20,036 additions & 3,981 deletions index.json

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion lib/check-or-update-webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getHtml = require('./get-html')
const applyWorkarounds = require('./workarounds')
const getSections = require('./get-sections')
const toWebhook = require('./section-to-webhook')
const getActionsAndExamplesFromPayloads = require('./get-actions-and-examples-from-payloads')

async function checkOrUpdateWebhooks ({ cached, checkOnly }) {
const state = {
Expand All @@ -17,7 +18,24 @@ async function checkOrUpdateWebhooks ({ cached, checkOnly }) {

const html = await getHtml({ cached })
const sections = await getSections(state, html)
const webhooks = sections.map(toWebhook.bind(null, state)).filter(Boolean)
const webhooksFromScrapingDocs = sections.map(toWebhook.bind(null, state)).filter(Boolean)
const webhooksFromPayloadExamplesByName = getActionsAndExamplesFromPayloads()

const webhooks = webhooksFromScrapingDocs.map((webhook) => {
const name = webhook.name
const webhookFromPayloadExamples = webhooksFromPayloadExamplesByName[name]

if (!webhookFromPayloadExamples) {
console.warn(`No payload examples for ${name}`)
return webhook
}

return {
name,
actions: [...new Set(webhook.actions.concat(webhookFromPayloadExamples.actions))],
examples: webhook.examples.concat(webhookFromPayloadExamples.examples)
}
})

applyWorkarounds(webhooks)

Expand Down
26 changes: 26 additions & 0 deletions lib/get-actions-and-examples-from-payloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = getActionsAndExamplesFromPayloads

const { readdirSync } = require('fs')

function getActionsAndExamplesFromPayloads () {
const eventsByName = {}
for (const path of readdirSync('payload-examples/api.github.com')) {
const [name] = path.split('.')

const payload = require(`../payload-examples/api.github.com/${path}`)

if (!eventsByName[name]) {
eventsByName[name] = {
actions: [],
examples: []
}
}

if (payload.action) {
eventsByName[name].actions.push(payload.action)
}
eventsByName[name].examples.push(payload)
}

return eventsByName
}
30 changes: 8 additions & 22 deletions lib/workarounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,20 @@ function workarounds (webhooks) {

// https://github.com/octokit/webhooks/issues/4
webhooks.forEach(webhook => {
if (webhook.name === 'member') {
if (webhook.actions.length) {
throw new Error('Remove workaround to get "member" actions')
}

webhook.actions = [
'added',
'deleted',
'edited'
]
}

if (webhook.name === 'commit_comment') {
if (webhook.actions.length) {
throw new Error('Remove workaround to get "commit_comment" actions')
if (webhook.name === 'pull_request') {
if (webhook.actions.includes('synchronize')) {
throw new Error('Remove workaround that adds "synchronize" action to "pull_request" event')
}

webhook.actions = [
'created'
]
webhook.actions = webhook.actions.concat('synchronize').sort()
}

if (webhook.name === 'pull_request') {
if (webhook.actions.includes('synchronize')) {
throw new Error('Remove workaround add "synchronize" event to "pull_request"')
if (webhook.name === 'member') {
if (webhook.actions.includes('removed')) {
throw new Error('Remove workaround that adds "removed" action to "member" event')
}

webhook.actions.push('synchronize')
webhook.actions = webhook.actions.concat('removed').sort()
}
})
}
Loading