Skip to content

Commit

Permalink
feat: Clone repo using username/repoName or repo id
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Feb 27, 2024
1 parent 707b806 commit d50f572
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,25 @@ export const exitWithError = (message: string) => {
log(``);
process.exit(1);
};

export function isValidUuid(uuid: string) {
const REGEX =
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
return typeof uuid === 'string' && REGEX.test(uuid);
}

export function setGitRemoteOrigin(repo: Repo) {
try {
const remoteUrl = process.argv[3];
if (!remoteUrl) return;

const repoId = `${remoteUrl.replace(/.*:\/\//, '')}`;
if (isValidUuid(repoId)) return;

const repoPath = gitdir.split(path.sep).slice(0, -2).join(path.sep);
const currentDir = process.cwd();
process.chdir(repoPath);
execSync(`git remote set-url origin proland://${repo.id}`);
process.chdir(currentDir);
} catch (error) {}
}
2 changes: 2 additions & 0 deletions src/lib/remoteHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
setCacheDirty,
unsetCacheDirty,
waitFor,
setGitRemoteOrigin,
} from './common';
import {
trackRepositoryCloneEvent,
Expand Down Expand Up @@ -253,6 +254,7 @@ const spawnPipedGitCommand = async (
process.exit(1);
}
} else if (isCloningRepo) {
setGitRemoteOrigin(repo);
await trackRepositoryCloneEvent(wallet, {
repo_name: repo.name,
repo_id: repo.id,
Expand Down
42 changes: 32 additions & 10 deletions src/lib/warpHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
defaultCacheOptions,
type LogLevel,
} from 'warp-contracts/mjs';
import { getWallet, getWarpContractTxId, waitFor } from './common';
import type { Repo } from '../types';
import { getWallet, getWarpContractTxId, isValidUuid, waitFor } from './common';
import type { Repo, User } from '../types';
import path from 'path';

export const getWarpCacheOptions = (cachePath: string) => {
Expand All @@ -26,14 +26,36 @@ const getWarp = (destPath?: string, logLevel?: LogLevel) => {

export async function getRepo(id: string, destpath?: string) {
let pl = getWarp(destpath).contract(getWarpContractTxId());
// let warp throw error if it can't retrieve the repository
const response = await pl.viewState({
function: 'getRepository',
payload: {
id,
},
});
return response.result as Repo;
if (isValidUuid(id)) {
// let warp throw error if it can't retrieve the repository
const response = await pl.viewState({
function: 'getRepository',
payload: {
id,
},
});
return response.result as Repo;
} else {
const [username, repoName] = id.split('/');
if (!username || !repoName) return;

const state = (await pl.readState()).cachedValue.state as {
users: { [key: string]: User };
};
const userAddress = Object.entries(state.users).find(
([_, user]) => user.username === username
)?.[0];
if (!userAddress) return;

const ownerReposResponse = await pl.viewState({
function: 'getRepositoriesByOwner',
payload: { owner: userAddress },
});

const repos = ownerReposResponse?.result as Repo[];
const repo = repos.find((repo) => repo.name === repoName);
return repo;
}
}

export async function updateWarpRepo(
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export type User = {
fullname?: string;
username?: string;
isUserNameArNS?: boolean;
avatar?: string;
bio?: string;
location?: string;
twitter?: string;
email?: string;
website?: string;
readmeTxId?: string;
};

export type Repo = {
id: string;
name: string;
Expand Down

0 comments on commit d50f572

Please sign in to comment.