From 2d83f54f328f4432ccad1428836a46f44b64f14b Mon Sep 17 00:00:00 2001 From: Anil Vishnoi Date: Fri, 13 Dec 2024 19:49:33 -0800 Subject: [PATCH] Fix PathService selection issue Fix document upload for scenarios where docs needs to be mounted from host in container environment. Signed-off-by: Anil Vishnoi --- src/app/api/envConfig/route.ts | 2 +- src/app/api/native/clone-repo/route.ts | 3 +- src/app/api/native/git/branches/route.ts | 29 +++++++++-------- src/app/api/native/upload/route.ts | 31 ++++++++++++++----- src/app/api/tree/route.ts | 7 ++--- src/components/Dashboard/Native/dashboard.tsx | 6 ++-- src/components/PathService/PathService.tsx | 20 ++---------- 7 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/app/api/envConfig/route.ts b/src/app/api/envConfig/route.ts index 39515ea6..9381ed2b 100644 --- a/src/app/api/envConfig/route.ts +++ b/src/app/api/envConfig/route.ts @@ -17,7 +17,7 @@ export async function GET() { DEPLOYMENT_TYPE: process.env.IL_UI_DEPLOYMENT || '', ENABLE_DEV_MODE: process.env.IL_ENABLE_DEV_MODE || 'false', EXPERIMENTAL_FEATURES: process.env.NEXT_PUBLIC_EXPERIMENTAL_FEATURES || '', - TAXONOMY_REPO_DIR: process.env.NEXT_PUBLIC_TAXONOMY_REPO_DIR || '' + TAXONOMY_ROOT_DIR: process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || '' }; return NextResponse.json(envConfig); diff --git a/src/app/api/native/clone-repo/route.ts b/src/app/api/native/clone-repo/route.ts index cf6a3cd2..5f4ab02a 100644 --- a/src/app/api/native/clone-repo/route.ts +++ b/src/app/api/native/clone-repo/route.ts @@ -27,8 +27,7 @@ export async function POST() { http, dir: taxonomyDirectoryPath, url: TAXONOMY_REPO_URL, - singleBranch: true, - depth: 1 + singleBranch: true }); // Include the full path in the response for client display diff --git a/src/app/api/native/git/branches/route.ts b/src/app/api/native/git/branches/route.ts index 088a7191..95b4fd9a 100644 --- a/src/app/api/native/git/branches/route.ts +++ b/src/app/api/native/git/branches/route.ts @@ -6,8 +6,8 @@ import path from 'path'; // Get the repository path from the environment variable const LOCAL_TAXONOMY_ROOT_DIR = process.env.NEXT_PUBLIC_LOCAL_TAXONOMY_ROOT_DIR || `${process.env.HOME}/.instructlab-ui`; -const REMOTE_TAXONOMY_REPO_DIR = process.env.NEXT_PUBLIC_TAXONOMY_REPO_DIR || ''; -const REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_REPO_CONTAINER_MOUNT_DIR || '/tmp/.instructlab-ui/taxonomy'; +const REMOTE_TAXONOMY_ROOT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || ''; +const REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR = '/tmp/.instructlab-ui'; interface Diffs { file: string; @@ -69,22 +69,25 @@ export async function POST(req: NextRequest) { } if (action === 'publish') { - let remoteTaxonomyRepoDir: string = ''; + let remoteTaxonomyRepoDirFinal: string = ''; // Check if directory pointed by remoteTaxonomyRepoDir exists and not empty - if (fs.existsSync(REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR) && fs.readdirSync(REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR).length !== 0) { - remoteTaxonomyRepoDir = REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR; + const remoteTaxonomyRepoContainerMountDir = path.join(REMOTE_TAXONOMY_REPO_CONTAINER_MOUNT_DIR, '/taxonomy'); + const remoteTaxonomyRepoDir = path.join(REMOTE_TAXONOMY_ROOT_DIR, '/taxonomy'); + if (fs.existsSync(remoteTaxonomyRepoContainerMountDir) && fs.readdirSync(remoteTaxonomyRepoContainerMountDir).length !== 0) { + remoteTaxonomyRepoDirFinal = remoteTaxonomyRepoContainerMountDir; + console.log('Remote taxonomy repository is mounted at:', remoteTaxonomyRepoDirFinal); } else { - if (fs.existsSync(REMOTE_TAXONOMY_REPO_DIR) && fs.readdirSync(REMOTE_TAXONOMY_REPO_DIR).length !== 0) { - remoteTaxonomyRepoDir = REMOTE_TAXONOMY_REPO_DIR; + if (fs.existsSync(remoteTaxonomyRepoDir) && fs.readdirSync(remoteTaxonomyRepoDir).length !== 0) { + remoteTaxonomyRepoDirFinal = remoteTaxonomyRepoDir; } } - if (remoteTaxonomyRepoDir === '') { + if (remoteTaxonomyRepoDirFinal === '') { return NextResponse.json({ error: 'Remote taxonomy repository path does not exist.' }, { status: 400 }); } - console.log('Remote taxonomy repository path:', remoteTaxonomyRepoDir); + console.log('Remote taxonomy repository path:', remoteTaxonomyRepoDirFinal); - return handlePublish(branchName, LOCAL_TAXONOMY_DIR, remoteTaxonomyRepoDir); + return handlePublish(branchName, LOCAL_TAXONOMY_DIR, remoteTaxonomyRepoDirFinal); } return NextResponse.json({ error: 'Invalid action specified' }, { status: 400 }); } @@ -210,7 +213,7 @@ async function handlePublish(branchName: string, localTaxonomyDir: string, remot return NextResponse.json({ error: 'Invalid contribution name for publish' }, { status: 400 }); } - console.log(`Publishing contribution from ${branchName} to remote taxonomy repo at ${remoteTaxonomyDir}`); + console.log(`Publishing contribution from ${branchName} to remote taxonomy repo at ${REMOTE_TAXONOMY_ROOT_DIR}/taxonomy`); const changes = await findDiff(branchName, localTaxonomyDir); // Check if there are any changes to publish, create a new branch at remoteTaxonomyDir and copy all the files listed in the changes array to the new branch and create a commit @@ -268,8 +271,8 @@ async function handlePublish(branchName: string, localTaxonomyDir: string, remot email: authorEmail } }); - console.log(`Successfully published contribution from ${branchName} to remote taxonomy repo at ${remoteTaxonomyDir}`); - return NextResponse.json({ message: `Successfully published contribution to ${REMOTE_TAXONOMY_REPO_DIR}.` }, { status: 200 }); + console.log(`Successfully published contribution from ${branchName} to remote taxonomy repo at ${REMOTE_TAXONOMY_ROOT_DIR}/taxonomy.`); + return NextResponse.json({ message: `Successfully published contribution to ${REMOTE_TAXONOMY_ROOT_DIR}/taxonomy.` }, { status: 200 }); } else { return NextResponse.json({ message: `No changes to publish from contribution ${branchName}.` }, { status: 200 }); } diff --git a/src/app/api/native/upload/route.ts b/src/app/api/native/upload/route.ts index b67a452f..41445898 100644 --- a/src/app/api/native/upload/route.ts +++ b/src/app/api/native/upload/route.ts @@ -6,7 +6,8 @@ import http from 'isomorphic-git/http/node'; import path from 'path'; import fs from 'fs'; -const LOCAL_TAXONOMY_DOCS_ROOT_DIR = process.env.NEXT_PUBLIC_LOCAL_TAXONOMY_ROOT_DIR || `${process.env.HOME}/.instructlab-ui`; +const TAXONOMY_DOCS_ROOT_DIR = process.env.NEXT_PUBLIC_TAXONOMY_ROOT_DIR || ''; +const TAXONOMY_DOCS_CONTAINER_MOUNT_DIR = '/tmp/.instructlab-ui'; const TAXONOMY_KNOWLEDGE_DOCS_REPO_URL = 'https://github.com/instructlab-public/taxonomy-knowledge-docs.git'; export async function POST(req: NextRequest) { @@ -30,8 +31,9 @@ export async function POST(req: NextRequest) { }); // Write the files to the repository + const docsRepoUrlTmp = path.join(docsRepoUrl, '/'); for (const file of filesWithTimestamp) { - const filePath = path.join(docsRepoUrl, file.fileName); + const filePath = path.join(docsRepoUrlTmp, file.fileName); fs.writeFileSync(filePath, file.fileContent); } @@ -51,9 +53,10 @@ export async function POST(req: NextRequest) { .join(', ')}\n\nSigned-off-by: ui@instructlab.ai` }); + const origTaxonomyDocsRepoDir = path.join(TAXONOMY_DOCS_ROOT_DIR, '/taxonomy-knowledge-docs'); return NextResponse.json( { - repoUrl: docsRepoUrl, + repoUrl: origTaxonomyDocsRepoDir, commitSha, documentNames: filesWithTimestamp.map((file: { fileName: string }) => file.fileName), prUrl: '' @@ -67,8 +70,23 @@ export async function POST(req: NextRequest) { } async function cloneTaxonomyDocsRepo() { - const taxonomyDocsDirectoryPath = path.join(LOCAL_TAXONOMY_DOCS_ROOT_DIR, '/taxonomy-knowledge-docs'); - console.log(`Cloning taxonomy docs repository to ${taxonomyDocsDirectoryPath}...`); + // Check the location of the taxonomy repository and create the taxonomy-docs-repository parallel to that. + let remoteTaxonomyRepoDirFinal: string = ''; + // Check if directory pointed by remoteTaxonomyRepoDir exists and not empty + const remoteTaxonomyRepoContainerMountDir = path.join(TAXONOMY_DOCS_CONTAINER_MOUNT_DIR, '/taxonomy'); + const remoteTaxonomyRepoDir = path.join(TAXONOMY_DOCS_ROOT_DIR, '/taxonomy'); + if (fs.existsSync(remoteTaxonomyRepoContainerMountDir) && fs.readdirSync(remoteTaxonomyRepoContainerMountDir).length !== 0) { + remoteTaxonomyRepoDirFinal = TAXONOMY_DOCS_CONTAINER_MOUNT_DIR; + } else { + if (fs.existsSync(remoteTaxonomyRepoDir) && fs.readdirSync(remoteTaxonomyRepoDir).length !== 0) { + remoteTaxonomyRepoDirFinal = TAXONOMY_DOCS_ROOT_DIR; + } + } + if (remoteTaxonomyRepoDirFinal === '') { + return null; + } + + const taxonomyDocsDirectoryPath = path.join(remoteTaxonomyRepoDirFinal, '/taxonomy-knowledge-docs'); if (fs.existsSync(taxonomyDocsDirectoryPath)) { console.log(`Using existing taxonomy knowledge docs repository at ${taxonomyDocsDirectoryPath}.`); @@ -83,8 +101,7 @@ async function cloneTaxonomyDocsRepo() { http, dir: taxonomyDocsDirectoryPath, url: TAXONOMY_KNOWLEDGE_DOCS_REPO_URL, - singleBranch: true, - depth: 1 + singleBranch: true }); // Include the full path in the response for client display diff --git a/src/app/api/tree/route.ts b/src/app/api/tree/route.ts index f0f5327c..7af32894 100644 --- a/src/app/api/tree/route.ts +++ b/src/app/api/tree/route.ts @@ -2,17 +2,14 @@ import axios from 'axios'; import { NextRequest, NextResponse } from 'next/server'; -const ENABLE_DEV_MODE = process.env.IL_ENABLE_DEV_MODE || 'false'; +const PATH_SERVICE_URL = process.env.IL_PATH_SERVICE_URL || 'http://pathservice:4000/tree/'; export async function POST(req: NextRequest) { const body = await req.json(); const { root_path, dir_name } = body; try { - let apiBaseUrl = 'http://pathservice:4000/tree/'; - if (ENABLE_DEV_MODE === 'true') { - apiBaseUrl = 'http://localhost:4000/tree/'; - } + const apiBaseUrl = PATH_SERVICE_URL; const response = await axios.get(apiBaseUrl + root_path, { params: { dir_name: dir_name } }); diff --git a/src/components/Dashboard/Native/dashboard.tsx b/src/components/Dashboard/Native/dashboard.tsx index 7d041261..a53d79c5 100644 --- a/src/components/Dashboard/Native/dashboard.tsx +++ b/src/components/Dashboard/Native/dashboard.tsx @@ -26,6 +26,7 @@ import { Content } from '@patternfly/react-core/dist/esm/components/Content/Cont import { Popover } from '@patternfly/react-core/dist/esm/components/Popover/Popover'; import { ExternalLinkAltIcon } from '@patternfly/react-icons/dist/esm/icons/external-link-alt-icon'; import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons/dist/esm/icons/outlined-question-circle-icon'; +import path from 'path'; const InstructLabLogo: React.FC = () => InstructLab Logo; @@ -52,7 +53,8 @@ const DashboardNative: React.FunctionComponent = () => { const getEnvVariables = async () => { const res = await fetch('/api/envConfig'); const envConfig = await res.json(); - setTaxonomyRepoDir(envConfig.TAXONOMY_REPO_DIR); + const taxonomyRepoDir = path.join(envConfig.TAXONOMY_ROOT_DIR + '/taxonomy'); + setTaxonomyRepoDir(taxonomyRepoDir); }; getEnvVariables(); @@ -420,7 +422,7 @@ const DashboardNative: React.FunctionComponent = () => { onClose={() => setIsPublishModalOpen(false)} actions={[ ,