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 all commits
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
34 changes: 34 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,34 @@
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 getModuleDataFromGitHub = async (
req: AuthedNextApiHandler,
res: NextApiResponse,
) => {
const GIT_TOKEN_SECRET_KEY_NAME = envOrFail(
"GIT_TOKEN_SECRET_KEY_NAME",
process.env.GIT_TOKEN_SECRET_KEY_NAME,
)

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)
8 changes: 4 additions & 4 deletions radlab-ui/webapp/src/pages/api/github/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ 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,
)
const getModulesFromGitHub = async (
_req: AuthedNextApiHandler,
res: NextApiResponse,
) => {
const GIT_TOKEN_SECRET_KEY_NAME = envOrFail(
"GIT_TOKEN_SECRET_KEY_NAME",
process.env.GIT_TOKEN_SECRET_KEY_NAME,
)
const secret = await getSecretKeyValue(GIT_TOKEN_SECRET_KEY_NAME)
const modules = await getGitHubModules(secret)
return res.status(200).json(modules)
Expand Down
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