Skip to content

Commit e971a66

Browse files
authored
Improve GithubRepositoryProvider.getUserRepos – EXP-407 (#18468)
* Improve GithubRepositoryProvider.getUserRepos The graphql query is extend to mimic what you would see in Top Repositories box visiting github.com. * fixup consider private repos as well * fixup missing spread
1 parent a660f80 commit e971a66

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,10 @@ class TestGithubContextRepositoryProvider {
7878
"f5b041513bfab914b5fbf7ae55788d9835004d76",
7979
]);
8080
}
81+
82+
@test public async testGetUserRepos() {
83+
const result = await this.provider.getUserRepos(this.user);
84+
expect(result).to.include("https://github.com/gitpod-io/gitpod");
85+
}
8186
}
8287
module.exports = new TestGithubContextRepositoryProvider(); // Only to circumvent no usage warning :-/

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,28 +172,55 @@ export class GithubRepositoryProvider implements RepositoryProvider {
172172
}
173173

174174
async getUserRepos(user: User): Promise<string[]> {
175-
// Hint: Use this to get richer results:
176-
// node {
177-
// nameWithOwner
178-
// shortDescriptionHTML(limit: 120)
179-
// url
180-
// }
181175
const result: any = await this.githubQueryApi.runQuery(
182176
user,
183177
`
184-
query {
178+
fragment Repos on RepositoryConnection {
179+
nodes {
180+
url
181+
}
182+
}
183+
184+
query topRepositories {
185185
viewer {
186-
repositoriesContributedTo(includeUserRepositories: true, first: 100) {
187-
edges {
188-
node {
189-
url
190-
}
191-
}
192-
}
186+
contributedTo: repositoriesContributedTo(
187+
first: 100
188+
orderBy: {field: PUSHED_AT, direction: DESC}
189+
includeUserRepositories: true
190+
contributionTypes: [COMMIT]
191+
) {
192+
...Repos
193+
}
194+
original: repositories(
195+
first: 100
196+
ownerAffiliations: OWNER
197+
isFork: false
198+
isLocked: false
199+
orderBy: {field: UPDATED_AT, direction: DESC}
200+
) {
201+
...Repos
202+
}
203+
forked: repositories(
204+
first: 100
205+
ownerAffiliations: OWNER
206+
isFork: true
207+
isLocked: false
208+
orderBy: {field: UPDATED_AT, direction: DESC}
209+
) {
210+
...Repos
211+
}
193212
}
194-
}`,
213+
}`,
195214
);
196-
return (result.data.viewer?.repositoriesContributedTo?.edges || []).map((edge: any) => edge.node.url);
215+
216+
const urls = [];
217+
for (const type of ["contributedTo", "original", "forked"]) {
218+
const nodes = result.data.viewer[type]?.nodes;
219+
if (nodes) {
220+
urls.push(...nodes.map((n: any) => n?.url).filter((u: any) => typeof u === "string"));
221+
}
222+
}
223+
return urls;
197224
}
198225

199226
async hasReadAccess(user: User, owner: string, repo: string): Promise<boolean> {

0 commit comments

Comments
 (0)