PMFlow/extensions/pmflow-ide/src/shared.ts

47 lines
1.3 KiB
TypeScript

export type GitHubRemoteInfo = {
owner: string;
repoName: string;
};
export type GitHubAvailableRepository = {
id: string;
installation_id: string;
installation_account_login: string;
external_repo_id: string;
owner: string;
repo_name: string;
full_name: string;
default_branch: string;
is_private: boolean;
status: string;
};
export function parseGitHubRemote(remoteUrl: string): GitHubRemoteInfo | null {
const raw = String(remoteUrl || "").trim();
if (!raw) return null;
const sshMatch = raw.match(/^git@github\.com:(.+?)\/(.+?)(?:\.git)?$/i);
if (sshMatch) {
return { owner: sshMatch[1], repoName: sshMatch[2] };
}
const httpsMatch = raw.match(/^https?:\/\/github\.com\/(.+?)\/(.+?)(?:\.git)?$/i);
if (httpsMatch) {
return { owner: httpsMatch[1], repoName: httpsMatch[2] };
}
return null;
}
export function matchAuthorizedRepository(
repositories: GitHubAvailableRepository[],
remote: GitHubRemoteInfo
): GitHubAvailableRepository | null {
const owner = remote.owner.toLowerCase();
const repoName = remote.repoName.toLowerCase();
return (
repositories.find(
(item) =>
String(item.owner || "").toLowerCase() === owner &&
String(item.repo_name || "").toLowerCase() === repoName
) || null
);
}