Skip to content

Commit

Permalink
refactor: allow Drips Projects URL as input in list editor (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtourkos committed Jun 3, 2024
1 parent 3119972 commit 00e60c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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.');
}

0 comments on commit 00e60c2

Please sign in to comment.