Skip to content

Improve GithubRepositoryProvider.getUserRepos – EXP-407 #18468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ class TestGithubContextRepositoryProvider {
"f5b041513bfab914b5fbf7ae55788d9835004d76",
]);
}

@test public async testGetUserRepos() {
const result = await this.provider.getUserRepos(this.user);
expect(result).to.include("https://github.com/gitpod-io/gitpod");
}
}
module.exports = new TestGithubContextRepositoryProvider(); // Only to circumvent no usage warning :-/
59 changes: 43 additions & 16 deletions components/server/src/github/github-repository-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,55 @@ export class GithubRepositoryProvider implements RepositoryProvider {
}

async getUserRepos(user: User): Promise<string[]> {
// Hint: Use this to get richer results:
// node {
// nameWithOwner
// shortDescriptionHTML(limit: 120)
// url
// }
const result: any = await this.githubQueryApi.runQuery(
user,
`
query {
fragment Repos on RepositoryConnection {
nodes {
url
}
}

query topRepositories {
viewer {
repositoriesContributedTo(includeUserRepositories: true, first: 100) {
edges {
node {
url
}
}
}
contributedTo: repositoriesContributedTo(
first: 100
orderBy: {field: PUSHED_AT, direction: DESC}
includeUserRepositories: true
contributionTypes: [COMMIT]
) {
...Repos
}
original: repositories(
first: 100
ownerAffiliations: OWNER
isFork: false
isLocked: false
orderBy: {field: UPDATED_AT, direction: DESC}
) {
...Repos
}
forked: repositories(
first: 100
ownerAffiliations: OWNER
isFork: true
isLocked: false
orderBy: {field: UPDATED_AT, direction: DESC}
) {
...Repos
}
}
}`,
}`,
);
return (result.data.viewer?.repositoriesContributedTo?.edges || []).map((edge: any) => edge.node.url);

const urls = [];
for (const type of ["contributedTo", "original", "forked"]) {
const nodes = result.data.viewer[type]?.nodes;
if (nodes) {
urls.push(...nodes.map((n: any) => n?.url).filter((u: any) => typeof u === "string"));
}
}
return urls;
}

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