Skip to content

Commit

Permalink
translate openapi versioning (github#16202)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachmari authored Oct 26, 2020
1 parent d743ff9 commit f22a82f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
9 changes: 6 additions & 3 deletions lib/all-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ const plans = [
planTitle: 'Free, Pro, and Team',
releases: [latestNonNumberedRelease],
latestRelease: latestNonNumberedRelease,
nonEnterpriseDefault: true // permanent way to refer to this plan if the name changes
nonEnterpriseDefault: true, // permanent way to refer to this plan if the name changes
openApiBaseName: 'dotcom'
},
{
plan: 'enterprise-server',
planTitle: 'Enterprise Server',
releases: enterpriseServerReleases.supported,
latestRelease: enterpriseServerReleases.latest,
hasNumberedReleases: true
hasNumberedReleases: true,
openApiBaseName: ''
}
]

Expand All @@ -35,7 +37,8 @@ plans.forEach(planObj => {
version,
versionTitle: planObj.hasNumberedReleases ? `${planObj.planTitle} ${release}` : planObj.planTitle,
latestVersion: `${planObj.plan}${versionDelimiter}${planObj.latestRelease}`,
currentRelease: release
currentRelease: release,
openApiVersionName: planObj.hasNumberedReleases ? `${planObj.openApiBaseName}${release}` : planObj.openApiBaseName
}

allVersions[version] = Object.assign(versionObj, planObj)
Expand Down
41 changes: 22 additions & 19 deletions lib/rest.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
const { chain, get, union, flatten, groupBy } = require('lodash')
const { supported } = require('./enterprise-server-releases')
const { chain, get, groupBy } = require('lodash')
const operations = require('@github/rest-api-operations')
const { getOldVersionFromNewVersion } = require('./old-versions-utils')
const allVersions = Object.keys(require('./all-versions'))
const allVersions = require('./all-versions')
const allVersionKeys = Object.keys(allVersions)

// This list is generated for use in the tests,
// so we can verify that the names of the markdown files
// in content/rest/reference/*.md are congruous with the
// set of REST resource names like activity, gists, repos, etc.
function getCategories (operations) {
return chain(operations).map('category').sort().uniq().value()
}
const dotcomCategories = getCategories(operations.dotcom)
const enterpriseCategories = flatten(supported.map(v => getCategories(operations[v])))
const categories = union(dotcomCategories, enterpriseCategories)
let allCategories = []
allVersionKeys.forEach(currentVersion => {
// Translate the versions from the openapi to versions used in the docs
const openApiVersion = allVersions[currentVersion].openApiVersionName
operations[currentVersion] = operations[openApiVersion]
delete operations[openApiVersion]

// This list is generated for use in the tests,
// so we can verify that the names of the markdown files
// in content/rest/reference/*.md are congruous with the
// set of REST resource names like activity, gists, repos, etc.
allCategories = allCategories.concat(chain(operations[currentVersion]).map('category').sort().uniq().value())

// Attach convenience properties to each operation that can't easily be created in Liquid
allVersions.forEach(currentVersion => {
operations[getOldVersionFromNewVersion(currentVersion)].forEach(operation => {
// Attach convenience properties to each operation that can't easily be created in Liquid
operations[currentVersion].forEach(operation => {
operation.hasRequiredPreviews = get(operation, 'x-github.previews', []).some(preview => preview.required)
})
})

// Get the unique set of categories
const categories = [...new Set(allCategories)]

// This is a collection of operations that have `enabledForGitHubApps = true`
// It's grouped by resource title to make rendering easier
const operationsEnabledForGitHubApps = allVersions.reduce((acc, currentVersion) => {
acc[currentVersion] = chain(operations[getOldVersionFromNewVersion(currentVersion)] || [])
const operationsEnabledForGitHubApps = allVersionKeys.reduce((acc, currentVersion) => {
acc[currentVersion] = chain(operations[currentVersion] || [])
.filter(operation => operation['x-github'].enabledForGitHubApps)
.orderBy('category')
.value()
Expand Down
6 changes: 1 addition & 5 deletions middleware/contextualizers/rest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
const rest = require('../../lib/rest')
const { getVersionedPathWithLanguage } = require('../../lib/path-utils')
const { getOldVersionFromNewVersion } = require('../../lib/old-versions-utils')

module.exports = async function (req, res, next) {
req.context.rest = rest

// TODO need to update this to the new versions in coordination with the updater scripts
const currentOldVersion = getOldVersionFromNewVersion(req.context.currentVersion)

// link to include in `Works with GitHub Apps` notes
// e.g. /ja/rest/reference/apps or /en/enterprise/2.20/user/rest/reference/apps
req.context.restGitHubAppsLink = getVersionedPathWithLanguage(
Expand All @@ -28,7 +24,7 @@ module.exports = async function (req, res, next) {
// ignore empty strings or bare `/`
if (!category || category.length < 2) return next()

const operationsForCurrentProduct = req.context.rest.operations[currentOldVersion] || []
const operationsForCurrentProduct = req.context.rest.operations[req.context.currentVersion] || []

// find all operations with a category matching the current path
req.context.currentRestOperations = operationsForCurrentProduct.filter(operation => operation.category === category)
Expand Down
2 changes: 1 addition & 1 deletion tests/links-and-images/developer-links-and-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('page rendering', () => {
languageCode
)

context.operationsForCurrentProduct = context.rest.operations[currentOldVersion] || []
context.operationsForCurrentProduct = context.rest.operations[pageVersion] || []

if (relevantPermalink.href.includes('rest/reference/')) {
const docsPath = relevantPermalink.href
Expand Down

0 comments on commit f22a82f

Please sign in to comment.