Skip to content

Commit

Permalink
load webhook versions at runtime instead of import-time (github#24788)
Browse files Browse the repository at this point in the history
* load webhook versions at runtime instead of import-time

* fix tests
  • Loading branch information
peterbe authored Jan 28, 2022
1 parent 1da82ef commit 7acccda
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
53 changes: 28 additions & 25 deletions lib/webhooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,39 @@ import { set } from 'lodash-es'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const staticDir = path.join(__dirname, 'static')

// compile contents of individual .payload.json files into a single object, with versions as top-level keys
const payloads = {}
export default function getWebhookPayloads() {
// Compile contents of individual .payload.json files into a single
// object, with versions as top-level keys.
const payloads = {}

// array of versions based on subdirectory names: lib/webhooks/static/<version>
const versions = fs.readdirSync(staticDir)
// array of versions based on subdirectory names: lib/webhooks/static/<version>
const versions = fs.readdirSync(staticDir)

versions.forEach((version) => {
const payloadsPerVersion = {}
const versionSubdir = path.join(staticDir, version)
versions.forEach((version) => {
const payloadsPerVersion = {}
const versionSubdir = path.join(staticDir, version)

walk(versionSubdir, { includeBasePath: true }).forEach((payloadFile) => {
// payload file: /path/to/check_run.completed.payload.json
// payload path: check_run.completed
let payloadPath = path.basename(payloadFile).replace('.payload.json', '')
if (!payloadPath.includes('.') && payloadsPerVersion[payloadPath]) {
// append the key `default` to the payload path to
// prevent overwriting existing object
payloadPath = `${payloadPath}.default`
}
set(
payloadsPerVersion,
payloadPath,
formatAsJsonCodeBlock(JSON.parse(fs.readFileSync(payloadFile)))
)
walk(versionSubdir, { includeBasePath: true }).forEach((payloadFile) => {
// payload file: /path/to/check_run.completed.payload.json
// payload path: check_run.completed
let payloadPath = path.basename(payloadFile).replace('.payload.json', '')
if (!payloadPath.includes('.') && payloadsPerVersion[payloadPath]) {
// append the key `default` to the payload path to
// prevent overwriting existing object
payloadPath = `${payloadPath}.default`
}
set(
payloadsPerVersion,
payloadPath,
formatAsJsonCodeBlock(JSON.parse(fs.readFileSync(payloadFile)))
)
})

payloads[version] = payloadsPerVersion
})

payloads[version] = payloadsPerVersion
})
return payloads
}

function formatAsJsonCodeBlock(payloadObj) {
// Note the use of `data-highlight="json"`. This is important because
Expand All @@ -48,5 +53,3 @@ function formatAsJsonCodeBlock(payloadObj) {
'\n```\n\n</div>'
)
}

export default payloads
9 changes: 8 additions & 1 deletion middleware/contextualizers/webhooks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { defaults } from 'lodash-es'
import webhookPayloads from '../../lib/webhooks/index.js'
import getWebhookPayloads from '../../lib/webhooks/index.js'
import nonEnterpriseDefaultVersion from '../../lib/non-enterprise-default-version.js'
import { allVersions } from '../../lib/all-versions.js'

let webhookPayloads = null

export default function webhooksContext(req, res, next) {
const currentVersionObj = allVersions[req.context.currentVersion]
// ignore requests to non-webhook reference paths
Expand All @@ -11,6 +13,11 @@ export default function webhooksContext(req, res, next) {
return next()
}

// Idempotent for consecutive calls
if (!webhookPayloads) {
webhookPayloads = getWebhookPayloads()
}

// Get the name of the dir under lib/webhooks/static
// For example, free-pro-team@latest corresponds to dotcom,
// [email protected] corresponds to ghes-2.22,
Expand Down
4 changes: 3 additions & 1 deletion tests/content/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { difference } from 'lodash-es'
import { getJSON } from '../helpers/supertest.js'
import { latest } from '../../lib/enterprise-server-releases.js'
import { allVersions } from '../../lib/all-versions.js'
import webhookPayloads from '../../lib/webhooks'
import getWebhookPayloads from '../../lib/webhooks'
import { jest } from '@jest/globals'

const allVersionValues = Object.values(allVersions)
Expand All @@ -25,6 +25,8 @@ const ghaePayloadVersion = allVersionValues.find(
describe('webhook payloads', () => {
jest.setTimeout(3 * 60 * 1000)

const webhookPayloads = getWebhookPayloads()

test('have expected top-level keys', () => {
payloadVersions.forEach((version) => {
// todo: remove if check once we have API/webhook versions for ghec
Expand Down

0 comments on commit 7acccda

Please sign in to comment.