Skip to content

Commit

Permalink
Fix PathService selection issue
Browse files Browse the repository at this point in the history
Fix document upload for scenarios where docs needs
to be mounted from host in container environment.

Signed-off-by: Anil Vishnoi <[email protected]>
  • Loading branch information
vishnoianil committed Dec 14, 2024
1 parent f8a4da8 commit 2d83f54
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/app/api/envConfig/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/app/api/native/clone-repo/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions src/app/api/native/git/branches/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 });
}
Expand Down
31 changes: 24 additions & 7 deletions src/app/api/native/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -51,9 +53,10 @@ export async function POST(req: NextRequest) {
.join(', ')}\n\nSigned-off-by: [email protected]`
});

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: ''
Expand All @@ -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}.`);
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions src/app/api/tree/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>(apiBaseUrl + root_path, {
params: { dir_name: dir_name }
});
Expand Down
6 changes: 4 additions & 2 deletions src/components/Dashboard/Native/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => <Image src="/InstructLab-LogoFile-RGB-FullColor.svg" alt="InstructLab Logo" width={256} height={256} />;

Expand All @@ -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();

Expand Down Expand Up @@ -420,7 +422,7 @@ const DashboardNative: React.FunctionComponent = () => {
onClose={() => setIsPublishModalOpen(false)}
actions={[
<Button key="confirm" variant="primary" onClick={() => handlePublishContributionConfirm()}>
Publish
Publish {' '}
{isPublishing && <Spinner isInline aria-label="Publishing contribution" />}
</Button>,
<Button key="cancel" variant="secondary" onClick={() => handlePublishContributionCancel()}>
Expand Down
20 changes: 3 additions & 17 deletions src/components/PathService/PathService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
const [items, setItems] = useState<string[]>([]);
const [showDropdown, setShowDropdown] = useState<boolean>(false);
const inputRef = useRef<HTMLInputElement>(null);
const componentRef = useRef<HTMLDivElement>(null); // Add a ref to the entire component
const [validPath, setValidPath] = React.useState<ValidatedOptions>();

const validatePath = () => {
console.log('validating path');
if (inputValue.trim().length > 0) {
setValidPath(ValidatedOptions.success);
return;
Expand Down Expand Up @@ -75,7 +73,9 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
setShowDropdown(false);
}
};

window.addEventListener('keydown', handleEsc);

return () => {
window.removeEventListener('keydown', handleEsc);
};
Expand All @@ -93,20 +93,6 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
validatePath();
}, [inputValue]);

useEffect(() => {
//Dropdown menu closed when field is clicked away from
const handleClickOutside = (event: MouseEvent) => {
if (componentRef.current && !componentRef.current.contains(event.target as Node)) {
setShowDropdown(false);
}
};

document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);

const handleChange = (value: string) => {
setInputValue(value);
};
Expand Down Expand Up @@ -149,7 +135,7 @@ const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handle
};

return (
<div ref={componentRef}>
<div>
<SearchInput
ref={inputRef}
placeholder="Type to find taxonomy path"
Expand Down

0 comments on commit 2d83f54

Please sign in to comment.