-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathgitExtensionIntegration.ts
More file actions
82 lines (66 loc) · 2.67 KB
/
gitExtensionIntegration.ts
File metadata and controls
82 lines (66 loc) · 2.67 KB
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
82
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { RemoteSource, RemoteSourceProvider } from './@types/git';
import { AuthProvider } from './common/authentication';
import { OctokitCommon } from './github/common';
import { CredentialStore, GitHub } from './github/credentials';
import { isEnterprise } from './github/utils';
interface Repository {
readonly full_name: string;
readonly description: string | null;
readonly clone_url: string;
readonly ssh_url: string;
}
function repoResponseAsRemoteSource(raw: OctokitCommon.SearchReposResponseItem): RemoteSource {
return {
name: `$(github) ${raw.full_name}`,
description: raw.description || undefined,
url: raw.url,
};
}
function asRemoteSource(raw: Repository): RemoteSource {
return {
name: `$(github) ${raw.full_name}`,
description: raw.description || undefined,
url: raw.clone_url,
};
}
export class GithubRemoteSourceProvider implements RemoteSourceProvider {
readonly name: string = 'GitHub';
readonly icon = 'github';
readonly supportsQuery = true;
private userReposCache: RemoteSource[] = [];
constructor(private readonly credentialStore: CredentialStore, private readonly authProviderId: AuthProvider = AuthProvider.github) {
if (isEnterprise(authProviderId)) {
this.name = 'GitHub Enterprise';
}
}
async getRemoteSources(query?: string): Promise<RemoteSource[]> {
const hub = await this.credentialStore.getHubOrLogin(this.authProviderId);
if (!hub) {
throw new Error('Could not fetch repositories from GitHub.');
}
const [fromUser, fromQuery] = await Promise.all([
this.getUserRemoteSources(hub, query),
this.getQueryRemoteSources(hub, query),
]);
const userRepos = new Set(fromUser.map(r => r.name));
return [...fromUser, ...fromQuery.filter(r => !userRepos.has(r.name))];
}
private async getUserRemoteSources(hub: GitHub, query?: string): Promise<RemoteSource[]> {
if (!query) {
const res = await hub.octokit.call(hub.octokit.api.repos.listForAuthenticatedUser, { sort: 'pushed', per_page: 100 });
this.userReposCache = res.data.map(asRemoteSource);
}
return this.userReposCache;
}
private async getQueryRemoteSources(hub: GitHub, query?: string): Promise<RemoteSource[]> {
if (!query) {
return [];
}
const raw = await hub.octokit.call(hub.octokit.api.search.repos, { q: query, sort: 'updated' });
return raw.data.items.map(repoResponseAsRemoteSource);
}
}