Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to fetch RAD Lab modules data from git repo #184

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions radlab-ui/webapp/src/pages/api/github/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getGitHubModulesData, getSecretKeyValue } from "@/utils/api"
import { envOrFail } from "@/utils/env"
import { withAuth } from "@/utils/middleware"
import { AuthedNextApiHandler } from "@/utils/types"
import { NextApiResponse } from "next"

const GIT_TOKEN_SECRET_KEY_NAME = envOrFail(
"GIT_TOKEN_SECRET_KEY_NAME",
process.env.GIT_TOKEN_SECRET_KEY_NAME,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the whole app fail to start if this is not provided? Is this optional (as in we by default pull from GoogleCloudPlatform/radlab)? If so, can we move the envOrFail into the handler itself (getModuleDataFromGithub).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code can be moved inside handler function since app will not fail to start if not provided.
Code modified accordingly.

const getModuleDataFromGitHub = async (
req: AuthedNextApiHandler,
res: NextApiResponse,
) => {
const { path } = req.body
const secret = await getSecretKeyValue(GIT_TOKEN_SECRET_KEY_NAME)
try {
const modules = await getGitHubModulesData(path, secret)
return res.status(200).json(modules)
} catch (error: any) {
return res.status(error.response.status).json({ message: error.message })
}
}

const handler = async (req: AuthedNextApiHandler, res: NextApiResponse) => {
try {
if (req.method === "POST") return getModuleDataFromGitHub(req, res)
} catch (error) {
return res.status(500).json({ message: "Internal Server Error" })
}
}

export default withAuth(handler)
16 changes: 16 additions & 0 deletions radlab-ui/webapp/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,19 @@ export const getGitHubVariables = async (moduleName: string, token: string) => {
throw error
}
}

export const getGitHubModulesData = async (path: string, token: string) => {
try {
const result = await axios({
method: "GET",
url: `${NEXT_PUBLIC_GIT_API_URL}/contents${path}?ref=${NEXT_PUBLIC_GIT_BRANCH}`,
headers: {
Authorization: `Bearer ${token}`,
},
})
return result.data || null
} catch (error) {
console.error(error)
throw error
}
}
Loading