Skip to content

Commit 4f8645a

Browse files
committed
[server] Implement a RepositoryProvider.getUserRepos() method
1 parent 5903d7b commit 4f8645a

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

components/server/src/bitbucket/bitbucket-repository-provider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,9 @@ export class BitbucketRepositoryProvider implements RepositoryProvider {
9898
authorAvatarUrl: commit.author?.user?.links?.avatar?.href,
9999
};
100100
}
101+
102+
async getUserRepos(user: User): Promise<string[]> {
103+
// FIXME(janx): Not implemented yet
104+
return [];
105+
}
101106
}

components/server/src/github/github-repository-provider.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ export class GithubRepositoryProvider implements RepositoryProvider {
106106
const commit = await this.github.getCommit(user, { repo, owner, ref });
107107
return commit;
108108
}
109+
110+
async getUserRepos(user: User): Promise<string[]> {
111+
// Hint: Use this to get richer results:
112+
// node {
113+
// nameWithOwner
114+
// shortDescriptionHTML(limit: 120)
115+
// url
116+
// }
117+
const result: any = await this.githubQueryApi.runQuery(user, `
118+
query {
119+
viewer {
120+
repositoriesContributedTo(includeUserRepositories: true, first: 100) {
121+
edges {
122+
node {
123+
url
124+
}
125+
}
126+
}
127+
}
128+
}`);
129+
return (result.data.viewer?.repositoriesContributedTo?.edges || []).map((edge: any) => edge.node.url)
130+
}
109131
}

components/server/src/gitlab/gitlab-repository-provider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,9 @@ export class GitlabRepositoryProvider implements RepositoryProvider {
9494
authorAvatarUrl: "",
9595
};
9696
}
97+
98+
async getUserRepos(user: User): Promise<string[]> {
99+
// FIXME(janx): Not implemented yet
100+
return [];
101+
}
97102
}

components/server/src/repohost/repository-provider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export interface RepositoryProvider {
1212
getRepo(user: User, owner: string, repo: string): Promise<Repository>;
1313
getBranch(user: User, owner: string, repo: string, branch: string): Promise<Branch>;
1414
getBranches(user: User, owner: string, repo: string): Promise<Branch[]>;
15-
getCommitInfo(user: User, owner: string, repo: string, ref: string): Promise<CommitInfo | undefined>
15+
getCommitInfo(user: User, owner: string, repo: string, ref: string): Promise<CommitInfo | undefined>;
16+
getUserRepos(user: User): Promise<string[]>;
1617
}

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
997997
}
998998

999999
public async getSuggestedContextURLs(ctx: TraceContext): Promise<string[]> {
1000-
this.checkUser("getSuggestedContextURLs");
1000+
const user = this.checkUser("getSuggestedContextURLs");
10011001
const suggestions: string[] = [];
10021002
const logCtx: LogContext = { userId: user.id };
10031003

@@ -1011,10 +1011,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
10111011
log.error(logCtx, 'Could not get example repositories', error);
10121012
}));
10131013

1014-
// User repositories
1014+
// User repositories (from Apps)
10151015
promises.push(this.getAuthProviders(ctx).then(authProviders => Promise.all(authProviders.map(async (p) => {
1016-
// TODO(janx): Refactor this in order not to limit results to app installations & not fetch projects.
1017-
// This should be entirely about proposing great matches for a user, no matter an app is installed.
10181016
try {
10191017
const userRepos = await this.getProviderRepositoriesForUser(ctx, { provider: p.host });
10201018
userRepos.forEach(r => suggestions.push(r.cloneUrl.replace(/\.git$/, '')));
@@ -1025,6 +1023,24 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
10251023
log.error(logCtx, 'Could not get auth providers', error);
10261024
}));
10271025

1026+
// User repositories (from Git hosts directly)
1027+
promises.push(this.getAuthProviders(ctx).then(authProviders => Promise.all(authProviders.map(async (p) => {
1028+
try {
1029+
const hostContext = this.hostContextProvider.get(p.host);
1030+
const services = hostContext?.services;
1031+
if (!services) {
1032+
log.error(logCtx, 'Unsupported repository host: ' + p.host);
1033+
return;
1034+
}
1035+
const userRepos = await services.repositoryProvider.getUserRepos(user);
1036+
userRepos.forEach(r => suggestions.push(r.replace(/\.git$/, '')));
1037+
} catch (error) {
1038+
log.debug(logCtx, 'Could not get user repositories from host ' + p.host, error);
1039+
}
1040+
}))).catch(error => {
1041+
log.error(logCtx, 'Could not get auth providers', error);
1042+
}));
1043+
10281044
// Recent repositories
10291045
promises.push(this.getWorkspaces(ctx, { /* limit: 20 */ }).then(workspaces => {
10301046
workspaces.forEach(ws => {

0 commit comments

Comments
 (0)