clone_wiki.ts 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
import * as vscode from 'vscode';
import { tokenService } from '../services/token_service';
import { GitLabRemoteSourceProvider } from '../gitlab/clone/gitlab_remote_source_provider';
import { VS_COMMANDS } from '../command_names';
import { showQuickPick } from '../utils/show_quickpick';

interface RemoteSourceItem {
  label: string;
  url: string[];
  description: string;
}

async function pickRemoteProvider(): Promise<GitLabRemoteSourceProvider | undefined> {
  const instanceUrls = tokenService.getInstanceUrls();
  const instanceItems = instanceUrls.map(u => ({
    label: `$(project) ${u}`,
    instance: u,
  }));
  if (instanceItems.length === 0) {
    throw new Error('no GitLab instance found');
  }
  let selectedInstanceUrl;
  if (instanceItems.length === 1) {
    [selectedInstanceUrl] = instanceItems;
  } else {
    selectedInstanceUrl = await vscode.window.showQuickPick(instanceItems, {
      ignoreFocusOut: true,
      placeHolder: 'Select GitLab instance',
    });
  }
  if (!selectedInstanceUrl) {
    return undefined;
  }
  return new GitLabRemoteSourceProvider(selectedInstanceUrl.instance);
}

async function pickRemoteWikiSource(
  provider: GitLabRemoteSourceProvider,
): Promise<RemoteSourceItem | undefined> {
  const wikiPick = vscode.window.createQuickPick<RemoteSourceItem>();
  wikiPick.ignoreFocusOut = true;
  wikiPick.placeholder = 'Select GitLab project';
  const getSourceItemsForQuery = async (query?: string) => {
    const sources = await provider.getRemoteWikiSources(query);
    return sources.map(s => ({
      label: s.name,
      url: s.url as string[],
      description: s.description || '',
    }));
  };
  wikiPick.onDidChangeValue(async value => {
    wikiPick.items = await getSourceItemsForQuery(value);
  });
  wikiPick.items = await getSourceItemsForQuery();

  const selectedSource = await showQuickPick(wikiPick);
  return selectedSource;
}

export async function cloneWiki(): Promise<void> {
  const provider = await pickRemoteProvider();
  if (!provider) {
    return;
  }

  const selectedSource = await pickRemoteWikiSource(provider);
  if (!selectedSource) {
    return;
  }

  const selectedUrl = await vscode.window.showQuickPick(selectedSource.url, {
    ignoreFocusOut: true,
    placeHolder: 'Select URL to clone from',
  });

  if (!selectedUrl) {
    return;
  }

  await vscode.commands.executeCommand(VS_COMMANDS.GIT_CLONE, selectedUrl);
}