@@ -9,63 +9,25 @@ const graphql = require("./graphql.js");
9
9
const Octokat = require ( "octokat" ) ;
10
10
const octo = new Octokat ( { token : config . ghToken } ) ;
11
11
12
- async function fetchLabelPage ( org , repo , acc = { edges : [ ] } , cursor = null ) {
13
- console . warn ( "Fetching labels for " + repo ) ;
14
- let res ;
15
- try {
16
- res = await graphql ( `
17
- query {
18
- repository(owner:"${ org } ",name:"${ repo } ") {
19
- labels(first:10 ${ cursor ? 'after:"' + cursor + '"' : '' } ) {
20
- edges {
21
- node {
22
- name
23
- color
24
- }
25
- }
26
- pageInfo {
27
- endCursor
28
- hasNextPage
29
- }
30
- }
31
- }
32
-
33
- }` ) ;
34
- } catch ( err ) {
35
- // istanbul ignore next
36
- console . error ( "query failed " + JSON . stringify ( err ) ) ;
37
- }
38
- // istanbul ignore else
39
- if ( res && res . repository ) {
40
- const labels = { edges : acc . edges . concat ( res . repository . labels . edges ) } ;
41
- if ( res . repository . labels . pageInfo . hasNextPage ) {
42
- return fetchLabelPage ( org , repo , labels , res . repository . labels . pageInfo . endCursor ) ;
43
- } else {
44
- return { "repo" : { "owner" : org , "name" : repo } , labels} ;
45
- }
46
- } else {
47
- console . error ( "Fetching label for " + repo + " at cursor " + cursor + " failed with " + JSON . stringify ( res ) + ", not retrying" ) ;
48
- return { "repo" : { "owner" : org , "name" : repo } , "labels" : acc } ;
49
- //return fetchLabelPage(org, repo, acc, cursor);
50
- }
51
- }
52
-
53
- async function fetchRepoPage ( org , acc = [ ] , cursor = null ) {
54
- let res ;
55
- try {
56
- res = await graphql ( `
57
- query {
58
- organization(login:"${ org } ") {
59
- repositories(first:10 ${ cursor ? 'after:"' + cursor + '"' : '' } ) {
60
- edges {
61
- node {
62
- id, name, owner { login } , isArchived, homepageUrl, description, isPrivate, createdAt
63
- labels(first:10) {
64
- edges {
65
- node {
66
- name
67
- color
68
- }
12
+ const repoQuery = `
13
+ query ($org: String!, $cursor: String) {
14
+ organization(login: $org) {
15
+ repositories(first: 10, after: $cursor) {
16
+ nodes {
17
+ id
18
+ name
19
+ owner {
20
+ login
21
+ }
22
+ isArchived
23
+ homepageUrl
24
+ description
25
+ isPrivate
26
+ createdAt
27
+ labels(first: 100) {
28
+ nodes {
29
+ name
30
+ color
69
31
}
70
32
pageInfo {
71
33
endCursor
@@ -103,66 +65,70 @@ async function fetchRepoPage(org, acc = [], cursor = null) {
103
65
text
104
66
}
105
67
}
106
- codeOfConduct { body }
68
+ codeOfConduct {
69
+ body
70
+ }
107
71
readme: object(expression: "HEAD:README.md") {
108
72
... on Blob {
109
73
text
110
74
}
111
75
}
112
76
}
113
- cursor
114
- }
115
- pageInfo {
116
- endCursor
117
- hasNextPage
77
+ pageInfo {
78
+ endCursor
79
+ hasNextPage
80
+ }
118
81
}
119
82
}
120
83
}
121
- rateLimit {
122
- limit
123
- cost
124
- remaining
125
- resetAt
126
- }
127
- }` ) ;
128
- } catch ( err ) {
129
- // istanbul ignore next
130
- console . error ( err ) ;
131
- }
132
- // Fetch labels if they are paginated
133
- // istanbul ignore else
134
- if ( res && res . organization ) {
135
- console . error ( "GitHub rate limit: " + JSON . stringify ( res . rateLimit ) ) ;
136
- return Promise . all (
137
- res . organization . repositories . edges
138
- . filter ( e => e . node . labels . pageInfo . hasNextPage )
139
- . map ( e => fetchLabelPage ( e . node . owner . login , e . node . name , e . node . labels , e . node . labels . pageInfo . endCursor ) )
140
- ) . then ( ( labelsPerRepos ) => {
141
- const data = acc . concat ( res . organization . repositories . edges . map ( e => e . node ) ) ;
142
- labelsPerRepos . forEach ( ( { repo, labels} ) => {
143
- data . find ( r => r . owner . login == repo . owner && r . name == repo . name ) . labels = labels ;
144
- } ) ;
145
- // Clean up labels data structure
146
- data . forEach ( r => {
147
- if ( r . labels && r . labels . edges ) {
148
- r . labels = r . labels . edges . map ( e => e . node ) ;
84
+ ` ;
85
+
86
+ const labelQuery = `
87
+ query ($org: String!, $repo: String!, $cursor: String!) {
88
+ repository(owner: $org, name: $repo) {
89
+ labels(first: 100, after: $cursor) {
90
+ nodes {
91
+ name
92
+ color
93
+ }
94
+ pageInfo {
95
+ endCursor
96
+ hasNextPage
149
97
}
150
- } ) ;
151
- if ( res . organization . repositories . pageInfo . hasNextPage ) {
152
- return fetchRepoPage ( org , data , res . organization . repositories . pageInfo . endCursor ) ;
153
- } else {
154
- return data ;
155
98
}
156
- } ) ;
157
- } else {
158
- console . error ( "Fetching repo results at cursor " + cursor + " failed, retrying" ) ;
159
- return fetchRepoPage ( org , acc , cursor ) ;
99
+ }
100
+ }
101
+ ` ;
102
+
103
+ async function * listRepos ( org ) {
104
+ for ( let cursor = null ; ; ) {
105
+ const res = await graphql ( repoQuery , { org, cursor} ) ;
106
+ for ( const repo of res . organization . repositories . nodes ) {
107
+ const labels = repo . labels . nodes ;
108
+ // Fetch more labels if they are paginated
109
+ for ( let pageInfo = repo . labels . pageInfo ; pageInfo . hasNextPage ; ) {
110
+ const res = await graphql ( labelQuery , {
111
+ org,
112
+ repo : repo . name ,
113
+ cursor : pageInfo . endCursor
114
+ } ) ;
115
+ labels . push ( ...res . repository . labels . nodes ) ;
116
+ pageInfo = res . repository . labels . pageInfo ;
117
+ }
118
+ repo . labels = labels ;
119
+ yield repo ;
120
+ }
121
+ if ( res . organization . repositories . pageInfo . hasNextPage ) {
122
+ cursor = res . organization . repositories . pageInfo . endCursor ;
123
+ } else {
124
+ break ;
125
+ }
160
126
}
161
127
}
162
128
163
- async function fetchRepoHooks ( org , repo ) {
129
+ async function listRepoHooks ( org , repo ) {
164
130
const hooks = await octo . repos ( `${ org } /${ repo } ` ) . hooks . fetch ( ) ;
165
131
return hooks . items ;
166
132
}
167
133
168
- module . exports = { fetchRepoPage , fetchRepoHooks } ;
134
+ module . exports = { listRepos , listRepoHooks } ;
0 commit comments