7
7
import { injectable , inject } from 'inversify' ;
8
8
9
9
import { User , Repository } from "@gitpod/gitpod-protocol"
10
- import { GitHubRestApi } from "./api" ;
10
+ import { GitHubGraphQlEndpoint , GitHubRestApi } from "./api" ;
11
11
import { RepositoryProvider } from '../repohost/repository-provider' ;
12
12
import { parseRepoUrl } from '../repohost/repo-url' ;
13
13
import { Branch , CommitInfo } from '@gitpod/gitpod-protocol/src/protocol' ;
14
14
15
15
@injectable ( )
16
16
export class GithubRepositoryProvider implements RepositoryProvider {
17
17
@inject ( GitHubRestApi ) protected readonly github : GitHubRestApi ;
18
+ @inject ( GitHubGraphQlEndpoint ) protected readonly githubQueryApi : GitHubGraphQlEndpoint ;
18
19
19
20
async getRepo ( user : User , owner : string , repo : string ) : Promise < Repository > {
20
21
const repository = await this . github . getRepository ( user , { owner, repo } ) ;
@@ -33,7 +34,71 @@ export class GithubRepositoryProvider implements RepositoryProvider {
33
34
}
34
35
35
36
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
+ }
37
102
return branches ;
38
103
}
39
104
0 commit comments