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

Fix contribution publish for the container deployments #423

Merged
merged 1 commit into from
Dec 14, 2024
Merged
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
41 changes: 33 additions & 8 deletions src/app/api/native/git/branches/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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';

interface Diffs {
file: string;
Expand All @@ -28,15 +30,23 @@ export async function GET() {
const branchCommit = await git.resolveRef({ fs, dir: REPO_DIR, ref: branch });
const commitDetails = await git.readCommit({ fs, dir: REPO_DIR, oid: branchCommit });

const commitMessage = commitDetails.commit.message;

// Check for Signed-off-by line
const signoffMatch = commitMessage.match(/^Signed-off-by: (.+)$/m);
const signoff = signoffMatch ? signoffMatch[1] : null;
const messageStr = commitMessage.split('Signed-off-by');
branchDetails.push({
name: branch,
creationDate: commitDetails.commit.committer.timestamp * 1000 // Convert to milliseconds
creationDate: commitDetails.commit.committer.timestamp * 1000, // Convert to milliseconds
message: messageStr[0].replace(/\n+$/, ''),
author: signoff
});
}

branchDetails.sort((a, b) => b.creationDate - a.creationDate); // Sort by creation date, newest first

console.log('Branches present in native taxonomy:', branchDetails);
console.log('Total branches present in native taxonomy:', branchDetails.length);
return NextResponse.json({ branches: branchDetails }, { status: 200 });
} catch (error) {
console.error('Failed to list branches:', error);
Expand All @@ -47,8 +57,8 @@ export async function GET() {
// Handle POST requests for merge or branch comparison
export async function POST(req: NextRequest) {
const LOCAL_TAXONOMY_DIR = path.join(LOCAL_TAXONOMY_ROOT_DIR, '/taxonomy');
const { branchName, action, remoteTaxonomyRepoDir } = await req.json();
console.log('Received POST request:', { branchName, action, remoteTaxonomyRepoDir });
const { branchName, action } = await req.json();
console.log('Received POST request:', { branchName, action });

if (action === 'delete') {
return handleDelete(branchName, LOCAL_TAXONOMY_DIR);
Expand All @@ -59,6 +69,21 @@ export async function POST(req: NextRequest) {
}

if (action === 'publish') {
let remoteTaxonomyRepoDir: 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;
} else {
if (fs.existsSync(REMOTE_TAXONOMY_REPO_DIR) && fs.readdirSync(REMOTE_TAXONOMY_REPO_DIR).length !== 0) {
remoteTaxonomyRepoDir = REMOTE_TAXONOMY_REPO_DIR;
}
}
if (remoteTaxonomyRepoDir === '') {
return NextResponse.json({ error: 'Remote taxonomy repository path does not exist.' }, { status: 400 });
}

console.log('Remote taxonomy repository path:', remoteTaxonomyRepoDir);

return handlePublish(branchName, LOCAL_TAXONOMY_DIR, remoteTaxonomyRepoDir);
}
return NextResponse.json({ error: 'Invalid action specified' }, { status: 400 });
Expand All @@ -73,7 +98,7 @@ async function handleDelete(branchName: string, localTaxonomyDir: string) {
// Delete the target branch
await git.deleteBranch({ fs, dir: localTaxonomyDir, ref: branchName });

return NextResponse.json({ message: `Successfully deleted branch ${branchName}.` }, { status: 200 });
return NextResponse.json({ message: `Successfully deleted contribution ${branchName}.` }, { status: 200 });
} catch (error) {
console.error(`Failed to delete contribution ${branchName}:`, error);
return NextResponse.json(
Expand Down Expand Up @@ -182,7 +207,7 @@ async function getTopCommitDetails(dir: string, ref: string = 'HEAD') {
async function handlePublish(branchName: string, localTaxonomyDir: string, remoteTaxonomyDir: string) {
try {
if (!branchName || branchName === 'main') {
return NextResponse.json({ error: 'Invalid branch name for publish' }, { status: 400 });
return NextResponse.json({ error: 'Invalid contribution name for publish' }, { status: 400 });
}

console.log(`Publishing contribution from ${branchName} to remote taxonomy repo at ${remoteTaxonomyDir}`);
Expand Down Expand Up @@ -244,9 +269,9 @@ async function handlePublish(branchName: string, localTaxonomyDir: string, remot
}
});
console.log(`Successfully published contribution from ${branchName} to remote taxonomy repo at ${remoteTaxonomyDir}`);
return NextResponse.json({ message: `Successfully published contribution to ${remoteTaxonomyDir}.` }, { status: 200 });
return NextResponse.json({ message: `Successfully published contribution to ${REMOTE_TAXONOMY_REPO_DIR}.` }, { status: 200 });
} else {
return NextResponse.json({ message: `No changes to publish from ${branchName}.` }, { status: 200 });
return NextResponse.json({ message: `No changes to publish from contribution ${branchName}.` }, { status: 200 });
}
} catch (error) {
console.error(`Failed to publish contribution from ${branchName}:`, error);
Expand Down
24 changes: 18 additions & 6 deletions src/app/contribute/knowledge/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
// src/app/contribute/knowledge/page.tsx
import KnowledgeFormNative from '@/components/Contribute/Knowledge/Native';
import * as React from 'react';
import { AppLayout } from '../../../components/AppLayout';
import { KnowledgeFormGithub } from '../../../components/Contribute/Knowledge/Github';
'use client';
import { AppLayout } from '@/components/AppLayout';
import { KnowledgeFormGithub } from '@/components/Contribute/Knowledge/Github/index';
import KnowledgeFormNative from '@/components/Contribute/Knowledge/Native/index';
import { useEffect, useState } from 'react';

const KnowledgeFormPage: React.FC = () => {
return <AppLayout>{process.env.IL_UI_DEPLOYMENT === 'native' ? <KnowledgeFormNative /> : <KnowledgeFormGithub />}</AppLayout>;
const KnowledgeFormPage: React.FunctionComponent = () => {
const [deploymentType, setDeploymentType] = useState<string | undefined>();

useEffect(() => {
const getEnvVariables = async () => {
const res = await fetch('/api/envConfig');
const envConfig = await res.json();
setDeploymentType(envConfig.DEPLOYMENT_TYPE);
};
getEnvVariables();
}, []);

return <AppLayout>{deploymentType === 'native' ? <KnowledgeFormNative /> : <KnowledgeFormGithub />}</AppLayout>;
};

export default KnowledgeFormPage;
24 changes: 18 additions & 6 deletions src/app/contribute/skill/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
// src/app/contribute/skill/page.tsx
import SkillFormNative from '@/components/Contribute/Skill/Native';
import * as React from 'react';
import { AppLayout } from '../../../components/AppLayout';
import { SkillFormGithub } from '../../../components/Contribute/Skill/Github';
'use client';
import { AppLayout } from '@/components/AppLayout';
import { SkillFormGithub } from '@/components/Contribute/Skill/Github/index';
import { SkillFormNative } from '@/components/Contribute/Skill/Native/index';
import { useEffect, useState } from 'react';

const SkillFormPage: React.FC = () => {
return <AppLayout>{process.env.IL_UI_DEPLOYMENT === 'native' ? <SkillFormNative /> : <SkillFormGithub />}</AppLayout>;
const SkillFormPage: React.FunctionComponent = () => {
const [deploymentType, setDeploymentType] = useState<string | undefined>();

useEffect(() => {
const getEnvVariables = async () => {
const res = await fetch('/api/envConfig');
const envConfig = await res.json();
setDeploymentType(envConfig.DEPLOYMENT_TYPE);
};
getEnvVariables();
}, []);

return <AppLayout>{deploymentType === 'native' ? <SkillFormNative /> : <SkillFormGithub />}</AppLayout>;
};

export default SkillFormPage;
9 changes: 5 additions & 4 deletions src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// src/app/page.tsx
// src/app/dashboard/page.tsx
'use client';

import * as React from 'react';
import '@patternfly/react-core/dist/styles/base.css';
import { AppLayout } from '@/components/AppLayout';
import { DashboardGithub } from '@/components/Dashboard/Github/dashboard';
import { DashboardNative } from '@/components/Dashboard/Native/dashboard';
import { useEffect, useState } from 'react';

const Home: React.FunctionComponent = () => {
const [deploymentType, setDeploymentType] = React.useState<string | undefined>();
const [deploymentType, setDeploymentType] = useState<string | undefined>();

React.useEffect(() => {
useEffect(() => {
const getEnvVariables = async () => {
const res = await fetch('/api/envConfig');
const envConfig = await res.json();
Expand Down
2 changes: 1 addition & 1 deletion src/components/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PageSidebar } from '@patternfly/react-core/dist/dynamic/components/Page
import { PageSidebarBody } from '@patternfly/react-core/dist/dynamic/components/Page';
import { SkipToContent } from '@patternfly/react-core/dist/dynamic/components/SkipToContent';
import { Spinner } from '@patternfly/react-core/dist/dynamic/components/Spinner';
import { Bullseye } from '@patternfly/react-core';
import { Bullseye } from '@patternfly/react-core/dist/dynamic/layouts/Bullseye';
import UserMenu from './UserMenu/UserMenu';
import { useSession } from 'next-auth/react';
// import { useTheme } from '../context/ThemeContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/DropdownItem';
import { Icon } from '@patternfly/react-core';
import { Icon } from '@patternfly/react-core/dist/dynamic/components/Icon';
import FileIcon from '@patternfly/react-icons/dist/esm/icons/file-icon';
import { KnowledgeFormData } from '@/types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Dropdown } from '@patternfly/react-core/dist/dynamic/components/Dropdown';
import { DropdownList } from '@patternfly/react-core/dist/dynamic/components/Dropdown';
import { Icon } from '@patternfly/react-core';
import { Icon } from '@patternfly/react-core/dist/dynamic/components/Icon';
import { MenuToggle, MenuToggleElement } from '@patternfly/react-core/dist/dynamic/components/MenuToggle';
import DownloadYaml from '../DownloadYaml/DownloadYaml';
import DownloadAttribution from '../DownloadAttribution/DownloadAttribution';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { KnowledgeFormData, KnowledgeYamlData } from '@/types';
import { KnowledgeSchemaVersion } from '@/types/const';
import { dumpYaml } from '@/utils/yamlConfig';
import { Icon } from '@patternfly/react-core';
import { Icon } from '@patternfly/react-core/dist/dynamic/components/Icon';
import { DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/DropdownItem';
import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Knowledge/Github/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/components/Experimental/ContributeLocal/Knowledge/index.tsx
// src/components/Contribute/Knowledge/Github/index.tsx
'use client';
import React, { useEffect, useMemo, useState } from 'react';
import '../knowledge.css';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Knowledge/Native/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/components/Experimental/ContributeLocal/Knowledge/index.tsx
// src/components/Contribute/Native/Knowledge/index.tsx
'use client';
import React, { useEffect, useMemo, useState } from 'react';
import '../knowledge.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';
import { AttributionData, KnowledgeFormData, KnowledgeYamlData } from '@/types';
import { KnowledgeSchemaVersion } from '@/types/const';
import { dumpYaml } from '@/utils/yamlConfig';
import { Icon } from '@patternfly/react-core';
import { Icon } from '@patternfly/react-core/dist/dynamic/components/Icon';
import FileIcon from '@patternfly/react-icons/dist/dynamic/icons/file-icon';
import EyeIcon from '@patternfly/react-icons/dist/esm/icons/eye-icon';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Contribute/Skill/Github/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/components/Contribute/Skill/index.tsx
// src/components/Contribute/Skill/Github/index.tsx
'use client';
import React, { useEffect, useState } from 'react';
import './skills.css';
Expand Down
6 changes: 3 additions & 3 deletions src/components/Contribute/Skill/Native/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/components/contribute/Skill/index.tsx
// src/components/Contribute/Skill/Native/index.tsx
'use client';
import React, { useEffect, useState } from 'react';
import './skills.css';
Expand Down Expand Up @@ -392,8 +392,8 @@ export const SkillFormNative: React.FunctionComponent<SkillFormProps> = ({ skill
actionGroupAlertContent.success &&
actionGroupAlertContent.url &&
actionGroupAlertContent.url.trim().length > 0 && (
<a href={actionGroupAlertContent.url} target="_blank" rel="noreferrer">
View your new branch
<a href={actionGroupAlertContent.url} rel="noreferrer">
View your skill contribution
</a>
)}
</p>
Expand Down
Loading
Loading