Skip to content

Commit

Permalink
chore: removed the activate componennt and added activate_when_valida…
Browse files Browse the repository at this point in the history
…ted in the payload

Signed-off-by: Abinand P <[email protected]>
  • Loading branch information
Abiji-2020 committed Nov 29, 2024
1 parent bf5efbb commit 0eb1643
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 225 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"lint": "eslint \"source/**/*.{js,ts,tsx}\"",
"lint:fix": "eslint \"source/**/*.{js,ts,tsx}\" --fix",
"test": "prettier --check ./source && vitest run --coverage",
"coverage": "npx vitest run --coverage",
"simple-check": "npx tsx ./source/cli.tsx pdp check -u [email protected] -a create -r task"
},
"files": [
Expand Down
14 changes: 13 additions & 1 deletion source/commands/gitops/create/github.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export const options = zod.object({
alias: 'k',
}),
),
inactive: zod
.boolean()
.optional()
.describe(
option({
description: 'Do not activate the repository When Validated',
alias: 'i',
}),
),
});

type Props = {
Expand All @@ -24,7 +33,10 @@ type Props = {
export default function GitHub({ options }: Props) {
return (
<AuthProvider>
<GitHubComponent authKey={options.key} />
<GitHubComponent
authKey={options.key}
inactivateWhenValidated={options.inactive}
/>
</AuthProvider>
);
}
63 changes: 0 additions & 63 deletions source/components/gitops/Activate.tsx

This file was deleted.

46 changes: 17 additions & 29 deletions source/components/gitops/GitHubComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import SelectProject from './SelectProject.js';
import RepositoryKey from './RepositoryKey.js';
import SSHKey from './SSHKey.js';
import BranchName from './BranchName.js';
import Activate from './Activate.js';
import { Box, Text } from 'ink';
import { configurePermit, GitConfig } from '../../lib/gitops/utils.js';
import { useAuth } from '../AuthProvider.js';
type Props = {
authKey: string | undefined;
inactivateWhenValidated: boolean | undefined;
};
const GitHubComponent: React.FC<Props> = ({ authKey }) => {
const GitHubComponent: React.FC<Props> = ({
authKey,
inactivateWhenValidated,
}) => {
const [error, setError] = useState<string>('');
const [projectKey, setProjectKey] = useState<string>('');
const [doneMessage, setDoneMessage] = useState<string>('');
Expand All @@ -23,6 +26,7 @@ const GitHubComponent: React.FC<Props> = ({ authKey }) => {
privateKey: '',
},
key: '',
activateWhenValidated: !inactivateWhenValidated,
});
const [ApiKey, setApiKey] = useState<string>('');
const [state, setState] = useState<
Expand All @@ -31,7 +35,6 @@ const GitHubComponent: React.FC<Props> = ({ authKey }) => {
| 'sshKey'
| 'branch'
| 'project'
| 'activate'
| 'done'
| 'error'
>('apiKey');
Expand Down Expand Up @@ -138,35 +141,20 @@ const GitHubComponent: React.FC<Props> = ({ authKey }) => {
setState('error');
return;
}
setState('activate');
setState('done');
if (gitConfig.activateWhenValidated) {
setDoneMessage(
'Your GitOps is configured successfully and will be activated once validated',
);
return;
}
setDoneMessage(
'Your GitOps is configured succesffuly. To complete the setup, remember to activate it later',
);
}}
/>
)}
{state === 'activate' && (
<>
<Activate
apiKey={ApiKey}
projectKey={projectKey}
repoKey={gitConfig.key}
onError={errormessage => {
setError(errormessage);
setState('error');
}}
onActivate={isConfigured => {
if (isConfigured) {
setDoneMessage(
'Your GitOps is configured and activated sucessfully',
);
} else {
setDoneMessage(
'Your GitOps is configured successfully. To complete the setup, remember to activate it later.',
);
}
setState('done');
}}
/>
</>
)}

{state === 'done' && (
<Box margin={1}>
<Text color={'green'}>{doneMessage}</Text>
Expand Down
20 changes: 2 additions & 18 deletions source/lib/gitops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type GitConfig = {
privateKey: string;
};
key: string;
activateWhenValidated: boolean;
};

async function configurePermit(
Expand All @@ -62,6 +63,7 @@ async function configurePermit(
private_key: gitconfig.credentials.privateKey,
},
key: gitconfig.key,
activate_when_validated: gitconfig.activateWhenValidated,
};
const response = await apiCall(
endpoint,
Expand Down Expand Up @@ -89,28 +91,10 @@ async function configurePermit(
}
}

async function activateRepo(
apiKey: string,
projectKey: string,
repoId: string,
): Promise<boolean> {
const activateResponse = await apiCall(
`v2/projects/${projectKey}/repos/${repoId}/activate`,
apiKey,
'',
'PUT',
);
if (activateResponse.status === 400) {
throw new Error('Invalid Repo Status');
}
return true;
}

export {
getProjectList,
getRepoList,
generateSSHKey,
configurePermit,
activateRepo,
GitConfig,
};
117 changes: 4 additions & 113 deletions tests/github.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import SelectProject from '../source/components/gitops/SelectProject.js';
import RepositoryKey from '../source/components/gitops/RepositoryKey.js';
import SSHKey from '../source/components/gitops/SSHKey.js';
import BranchName from '../source/components/gitops/BranchName.js';
import Activate from '../source/components/gitops/Activate.js';
import GitHub from '../source/commands/gitops/create/github.js';
import delay from 'delay';
import { vi, describe, it, expect } from 'vitest';
import {
activateRepo,
configurePermit,
generateSSHKey,
getProjectList,
Expand Down Expand Up @@ -100,7 +98,9 @@ describe('Select Project Component', () => {
const accessKey = 'permit_key_'.concat('a'.repeat(97));

// Mock error response
(getProjectList as any).mockRejectedValueOnce(new Error('Failed to fetch projects'));
(getProjectList as any).mockRejectedValueOnce(
new Error('Failed to fetch projects'),
);

const { stdin, lastFrame } = render(
<SelectProject
Expand Down Expand Up @@ -340,109 +340,6 @@ describe('Branch Name component', () => {
});
});

describe('Activate Component', () => {
it('should call onActivate with the correct value', async () => {
const onActivate = vi.fn();
const onError = vi.fn();
const accessToken = 'permit_key_'.concat('a'.repeat(97));
const projectKey = 'proj1';
const gitConfig = {
url: '[email protected]/proj1.git',
main_branch_name: 'main',
credentials: {
auth_type: 'ssh',
username: 'git',
private_key: 'private_key',
},
key: 'repo1',
};
const { stdin, lastFrame } = render(
<Activate
apiKey={accessToken}
projectKey={projectKey}
repoKey="repo1"
onActivate={onActivate}
onError={onError}
/>,
);
const frameString = lastFrame()?.toString() ?? '';
expect(frameString).toMatch(/Do you want to activate the repository?/);
await delay(50);
stdin.write(enter);
await delay(50);
expect(onActivate).toHaveBeenCalledOnce();
expect(onActivate).toHaveBeenCalledWith(true);
});
it("should call onError with 'Invalid Repo Status' for incorrect value", async () => {
const onActivate = vi.fn();
const onError = vi.fn();
const accessToken = 'permit_key_'.concat('a'.repeat(97));
const projectKey = 'proj1';
const gitConfig = {
url: '[email protected]/proj1.git',
main_branch_name: 'main',
credentials: {
auth_type: 'ssh',
username: 'git',
private_key: 'private_key',
},
key: 'repo1',
};
(activateRepo as any).mockRejectedValueOnce(new Error('Invalid Repo Status'));
const { stdin, lastFrame } = render(
<Activate
apiKey={accessToken}
projectKey={projectKey}
repoKey="repo1"
onActivate={onActivate}
onError={onError}
/>,
);
const frameString = lastFrame()?.toString() ?? '';
expect(frameString).toMatch(/Do you want to activate the repository?/);
await delay(50);
stdin.write(enter);
await delay(50);
expect(onError).toHaveBeenCalledOnce();
expect(onError).toHaveBeenCalledWith('Invalid Repo Status');
});

it('activate value false', async () => {
const onActivate = vi.fn();
const onError = vi.fn();
const accessToken = 'permit_key_'.concat('a'.repeat(97));
const projectKey = 'proj1';
const gitConfig = {
url: '[email protected]/proj1.git',
main_branch_name: 'main',
credentials: {
auth_type: 'ssh',
username: 'git',
private_key: 'private_key',
},
key: 'repo1',
};
const { stdin, lastFrame } = render(
<Activate
apiKey={accessToken}
projectKey={projectKey}
repoKey="repo1"
onActivate={onActivate}
onError={onError}
/>,
);
const frameString = lastFrame()?.toString() ?? '';
expect(frameString).toMatch(/Do you want to activate the repository?/);
await delay(50);
stdin.write(arrowDown);
await delay(50);
stdin.write(enter);
await delay(50);
expect(onActivate).toHaveBeenCalledOnce();
expect(onActivate).toHaveBeenCalledWith(false);
});
});

describe('GiHub Complete Flow', () => {
it('should complete the flow', async () => {
const { stdin, lastFrame } = render(
Expand Down Expand Up @@ -477,13 +374,7 @@ describe('GiHub Complete Flow', () => {
stdin.write(enter);
await delay(50);
expect(lastFrame()?.toString()).toMatch(
/Do you want to activate the repository?/,
);
await delay(50);
stdin.write(enter);
await delay(50);
expect(lastFrame()?.toString()).toMatch(
/Your GitOps is configured and activated sucessfully/,
/Your GitOps is configured successfully and will be activated once validated/,
);
});
it('should call without value for the props', async () => {
Expand Down

0 comments on commit 0eb1643

Please sign in to comment.