Skip to content

Commit 1f066dc

Browse files
committed
[prebuilds] speed up query for active branches
using a couple of graphql queries instead of many rest requests. ordering by change date. limiting to 30 items.
1 parent 0af0a0c commit 1f066dc

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import { injectable, inject } from 'inversify';
88

99
import { User, Repository } from "@gitpod/gitpod-protocol"
10-
import { GitHubRestApi } from "./api";
10+
import { GitHubGraphQlEndpoint, GitHubRestApi } from "./api";
1111
import { RepositoryProvider } from '../repohost/repository-provider';
1212
import { parseRepoUrl } from '../repohost/repo-url';
1313
import { Branch, CommitInfo } from '@gitpod/gitpod-protocol/src/protocol';
1414

1515
@injectable()
1616
export class GithubRepositoryProvider implements RepositoryProvider {
1717
@inject(GitHubRestApi) protected readonly github: GitHubRestApi;
18+
@inject(GitHubGraphQlEndpoint) protected readonly githubQueryApi: GitHubGraphQlEndpoint;
1819

1920
async getRepo(user: User, owner: string, repo: string): Promise<Repository> {
2021
const repository = await this.github.getRepository(user, { owner, repo });
@@ -33,7 +34,71 @@ export class GithubRepositoryProvider implements RepositoryProvider {
3334
}
3435

3536
async getBranches(user: User, owner: string, repo: string): Promise<Branch[]> {
36-
const branches = await this.github.getBranches(user, { repo, owner });
37+
const branches: Branch[] = [];
38+
let endCursor: string | undefined;
39+
let hasNextPage: boolean = true;
40+
41+
while (hasNextPage) {
42+
const result: any = await this.githubQueryApi.runQuery(user, `
43+
query {
44+
repository(name: "${repo}", owner: "${owner}") {
45+
refs(refPrefix: "refs/heads/", orderBy: {field: TAG_COMMIT_DATE, direction: ASC}, first: 100 ${endCursor ? `, after: "${endCursor}"` : ""}) {
46+
nodes {
47+
name
48+
target {
49+
... on Commit {
50+
oid
51+
history(first: 1) {
52+
nodes {
53+
messageHeadline
54+
committedDate
55+
oid
56+
authoredDate
57+
tree {
58+
id
59+
}
60+
treeUrl
61+
author {
62+
avatarUrl
63+
name
64+
date
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
pageInfo {
72+
endCursor
73+
hasNextPage
74+
hasPreviousPage
75+
startCursor
76+
}
77+
totalCount
78+
}
79+
}
80+
}
81+
`);
82+
83+
endCursor = result.data.repository?.refs?.pageInfo?.endCursor;
84+
hasNextPage = result.data.repository?.refs?.pageInfo?.hasNextPage;
85+
86+
const nodes = result.data.repository?.refs?.nodes;
87+
for (const node of (nodes || [])) {
88+
89+
branches.push({
90+
name: node.name,
91+
commit: {
92+
sha: node.target.oid,
93+
commitMessage: node.target.history.nodes[0].messageHeadline,
94+
author: node.target.history.nodes[0].author.name,
95+
authorAvatarUrl: node.target.history.nodes[0].author.avatarUrl,
96+
authorDate: node.target.history.nodes[0].author.date,
97+
},
98+
htmlUrl: node.target.history.nodes[0].treeUrl.replace(node.target.oid, node.name)
99+
});
100+
}
101+
}
37102
return branches;
38103
}
39104

components/server/src/projects/projects-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export class ProjectsService {
7878
changeUrl: "changeUrl", // todo: compute in repositoryProvider
7979
});
8080
}
81-
return result;
81+
result.sort((a, b) => (b.changeDate || "").localeCompare(a.changeDate || ""));
82+
return result.slice(0, 30);
8283
}
8384

8485
async createProject({ name, cloneUrl, teamId, appInstallationId }: CreateProjectParams): Promise<Project> {

0 commit comments

Comments
 (0)