Skip to content

Commit

Permalink
Merge pull request #1235 from lpsinger/schema-stable-refactor
Browse files Browse the repository at this point in the history
Refactor latest stable schema redirect
  • Loading branch information
dakota002 authored Aug 16, 2023
2 parents 6221ea6 + 5815949 commit 04a3904
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
19 changes: 8 additions & 11 deletions app/lib/schema-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,16 @@ export async function getGithubDir(
).data as GitContentDataResponse[]
}

async function getDefaultBranch() {
return (await octokit.rest.repos.get(repoData)).data.default_branch
}

export async function getLatestRelease() {
let latestRelease
try {
latestRelease = (await octokit.rest.repos.getLatestRelease(repoData)).data
return (await octokit.rest.repos.getLatestRelease(repoData)).data.tag_name
} catch (error) {
if (error instanceof RequestError) {
if (error.status != 404) {
throw error
}
latestRelease = { tag_name: 'main' }
} else {
throw error
}
if (error instanceof RequestError && error.status === 404)
return await getDefaultBranch()
throw error
}
return latestRelease
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import { redirect } from '@remix-run/node'
import { getLatestRelease } from '~/lib/schema-data'

export async function loader({ params: { '*': path } }: DataFunctionArgs) {
const latestRelease = await getLatestRelease()
return redirect(`/docs/schema/${latestRelease.tag_name}/${path}`)
return redirect(`/docs/schema/${await getLatestRelease()}/${path}`)
}
13 changes: 2 additions & 11 deletions app/routes/schema.stable.$.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
import { Octokit } from '@octokit/rest'
import { type DataFunctionArgs, redirect } from '@remix-run/node'

const githubData = {
owner: 'nasa-gcn',
repo: 'gcn-schema',
}
const octokit = new Octokit()
import { getLatestRelease } from '~/lib/schema-data'

export async function loader({ params: { '*': path } }: DataFunctionArgs) {
const latestRelease = (await octokit.rest.repos.getLatestRelease(githubData))
.data
if (!latestRelease) throw new Response(null, { status: 404 })

return redirect(
`https://raw.githubusercontent.com/nasa-gcn/gcn-schema/${latestRelease.tag_name}/${path}`
`https://raw.githubusercontent.com/nasa-gcn/gcn-schema/${await getLatestRelease()}/${path}`
)
}

0 comments on commit 04a3904

Please sign in to comment.