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

refactor: allow Drips Projects URL as input in list editor #1092

Merged
merged 1 commit into from
Jun 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import List from '$lib/components/icons/List.svelte';
import ExclamationCircle from '$lib/components/icons/ExclamationCircle.svelte';
import { slide } from 'svelte/transition';
import { buildRepositoryURL, isDripsProjectUrl } from '$lib/utils/build-repo-url';

const dispatch = createEventDispatcher<{
addAddress: { accountId: string; address: string };
Expand Down Expand Up @@ -55,7 +56,7 @@
});

$: validInput =
(allowProjects && isSupportedGitUrl(inputValue)) ||
(allowProjects && (isSupportedGitUrl(inputValue) || isDripsProjectUrl(inputValue))) ||
(allowAddresses && (inputValue.endsWith('.eth') || isAddress(inputValue))) ||
(allowDripLists && inputValue.includes(`${BASE_URL}/app/drip-lists/`));

Expand All @@ -75,6 +76,10 @@
}
}

async function addDripsProject(urlValue: string) {
return addProject(buildRepositoryURL(urlValue));
}

async function addProject(urlValue: string) {
let url = urlValue;

Expand Down Expand Up @@ -239,6 +244,8 @@
await addAddress(value);
} else if (value.includes(`${BASE_URL}/app/drip-lists/`)) {
await addDripList(value);
} else if (value.includes(`${BASE_URL}/app/projects/`)) {
await addDripsProject(value);
}
} catch (e) {
if (e instanceof AddItemError) {
Expand Down
38 changes: 38 additions & 0 deletions src/lib/utils/build-repo-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BASE_URL } from './base-url';
import { isValidGitUrl } from './is-valid-git-url';

export function isDripsProjectUrl(value: string): boolean {
return value.includes(`${BASE_URL}/app/projects/`);
}

export function buildRepositoryURL(url: string): string {
const parsedUrl = new URL(url);
const pathSegments = parsedUrl.pathname.split('/').filter((segment) => segment.length > 0);

const forgeIndex = pathSegments.findIndex((segment) =>
['github', 'gitlab', 'bitbucket'].includes(segment),
);

if (forgeIndex !== -1) {
const forge = pathSegments[forgeIndex];
const repoPath = pathSegments.slice(forgeIndex + 1).join('/');

if (!repoPath) {
throw new Error('Repository path is incomplete.');
}

if (forge === 'github') {
const githubUrl = `https://github.com/${repoPath}`;

if (isValidGitUrl(githubUrl)) {
return `https://github.com/${repoPath}`;
}

throw new Error(`Invalid GitHub URL: ${githubUrl}.`);
} else {
throw new Error(`Unsupported forge: ${forge}.`);
}
}

throw new Error('Forge not found in the URL path.');
}
Loading