Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
fix: multiple routes in get a git reference section
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 17, 2018
1 parent 4a6f367 commit b7a9800
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/endpoint/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = getEndpoint

const existsSync = require('fs').existsSync
const {join: joinPath, relative: relativePath} = require('path')
const parseUrl = require('url').parse

const cheerio = require('cheerio')
Expand All @@ -11,6 +13,7 @@ const htmlToJson = require('./html-to-json')
async function getEndpoint (url) {
const [pageUrl, id] = url.split('#')
const {path: pagePath} = parseUrl(url)
const pageOverridePath = joinPath(__dirname, 'overrides', `${pagePath}${id}.json`)
const pageEndpointPath = `${pagePath}${id}.html`
let endpointHtml

Expand All @@ -27,6 +30,16 @@ async function getEndpoint (url) {
.get()
.join('\n')

// add override to cached HTML page so we can review changes and update
// the override if necessary
if (existsSync(pageOverridePath)) {
endpointHtml = `<!--
custom override at ${relativePath(process.cwd(), pageOverridePath)}
Make sure to review it on updates to this endpoint’s documentation
-->
${endpointHtml}`
}
cache.writeHtml(pageEndpointPath, endpointHtml)
}

Expand Down
46 changes: 45 additions & 1 deletion lib/endpoint/html-to-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = htmlToJson

const readFileSync = require('fs').readFileSync
const joinPath = require('path').join

const _ = require('lodash')
const cheerio = require('cheerio')
const markdownTable = require('markdown-table')
Expand All @@ -8,6 +11,14 @@ const elementToBlock = require('./element-to-block')
const htmlContainsEndpoint = require('./html-contains-endpoint')

function htmlToJson (html) {
const override = findOverride(html)
if (override) {
override.forEach(endpoint => {
endpoint.isOverride = true
})
return override
}

if (!htmlContainsEndpoint(html)) {
return
}
Expand Down Expand Up @@ -47,6 +58,15 @@ function htmlToJson (html) {
return state.results
}

function findOverride (html) {
if (!html.startsWith('<!--')) {
return
}

const overridePath = html.match(/custom override at (.*)/).pop()
return JSON.parse(readFileSync(joinPath(process.cwd(), overridePath), 'utf8'))
}

/**
*
*/
Expand All @@ -61,7 +81,19 @@ function findRoutes (state) {
result.method = method
result.path = path
state.blocks.splice(state.blocks.indexOf(routeBlock), 1)
} else {
return
}

const hasAuthenticatedUserRoute = !!routeBlocks.slice(1).find(block => {
const prevBlock = state.blocks[state.blocks.indexOf(block) - 1]
return prevBlock.type === 'description' && /^\/user\//.test(block.path)
})
const hasExampleRoutes = !!routeBlocks.slice(1).find(block => {
const prevBlock = state.blocks[state.blocks.indexOf(block) - 1]
return prevBlock.type === 'description' && /\bexample\b/i.test(prevBlock.text)
})

if (hasAuthenticatedUserRoute) {
state.results = routeBlocks.map((routeBlock) => {
const routeResult = _.merge({
method: routeBlock.method,
Expand All @@ -79,6 +111,18 @@ function findRoutes (state) {

return routeResult
})

return
}

if (hasExampleRoutes) {
routeBlocks.slice(1).forEach(block => {
block.type = 'description'
block.text = '```\n' + [block.method, block.path].join(' ') + '\n```'

delete block.method
delete block.path
})
}
}

Expand Down
17 changes: 17 additions & 0 deletions lib/endpoint/overrides/v3/git/refs/get-a-reference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"name": "Get a Reference",
"enabledForApps": true,
"method": "GET",
"path": "/repos/:owner/:repo/git/refs/:ref",
"description": "Returns a branch or tag reference. Other than the [REST API](https://developer.github.com/v3/git/refs/#get-a-reference) it always returns a single reference. If the REST API returns with an array then the method responds with an error.",
"params": [
{
"name": "ref",
"type": "string",
"description": "Must be formatted as `heads/branch`, not just `branch`",
"required": true
}
]
}
]

0 comments on commit b7a9800

Please # to comment.