This commit is contained in:
João Moreno
2020-11-27 12:22:04 +01:00
parent 8012c255e5
commit 0321ca5d96
5 changed files with 67 additions and 12 deletions

View File

@@ -8,6 +8,12 @@ import { getOctokit } from './auth';
import { Octokit } from '@octokit/rest';
import { publishRepository } from './publish';
function parse(url: string): { owner: string, repo: string } | undefined {
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\.git/i.exec(url)
|| /^git@github\.com:([^/]+)\/([^/]+)\.git/i.exec(url);
return (match && { owner: match[1], repo: match[2] }) ?? undefined;
}
function asRemoteSource(raw: any): RemoteSource {
return {
name: `$(github) ${raw.full_name}`,
@@ -30,11 +36,10 @@ export class GithubRemoteSourceProvider implements RemoteSourceProvider {
const octokit = await getOctokit();
if (query) {
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\.git/i.exec(query)
|| /^git@github\.com:([^/]+)\/([^/]+)\.git/i.exec(query);
const repository = parse(query);
if (match) {
const raw = await octokit.repos.get({ owner: match[1], repo: match[2] });
if (repository) {
const raw = await octokit.repos.get(repository);
return [asRemoteSource(raw.data)];
}
}
@@ -75,6 +80,21 @@ export class GithubRemoteSourceProvider implements RemoteSourceProvider {
return raw.data.items.map(asRemoteSource);
}
async getBranches(url: string): Promise<string[]> {
const repository = parse(url);
if (!repository) {
return [];
}
const octokit = await getOctokit();
const branches = await octokit.repos.listBranches(repository);
const repo = await octokit.repos.get(repository);
const defaultBranch = repo.data.default_branch;
return branches.data.map(b => b.name).sort((a, b) => a === defaultBranch ? -1 : b === defaultBranch ? 1 : 0);
}
publishRepository(repository: Repository): Promise<void> {
return publishRepository(this.gitAPI, repository);
}

View File

@@ -130,6 +130,7 @@ export interface CommitOptions {
signoff?: boolean;
signCommit?: boolean;
empty?: boolean;
noVerify?: boolean;
}
export interface BranchQuery {
@@ -211,6 +212,7 @@ export interface RemoteSourceProvider {
readonly icon?: string; // codicon name
readonly supportsQuery?: boolean;
getRemoteSources(query?: string): ProviderResult<RemoteSource[]>;
getBranches?(url: string): ProviderResult<string[]>;
publishRepository?(repository: Repository): Promise<void>;
}