Skip to content

Commit

Permalink
feat: new event actions and examples from example payload files provi…
Browse files Browse the repository at this point in the history
…ded by @rachmari (#85)
  • Loading branch information
gr2m committed Apr 28, 2020
1 parent 3ba3171 commit 6106c4c
Show file tree
Hide file tree
Showing 95 changed files with 36,579 additions and 4,004 deletions.
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

0 comments on commit 6106c4c

Please sign in to comment.