1
+ var fetch = require ( 'node-fetch' ) ;
2
+ var fs = require ( 'fs' ) ;
3
+ var path = require ( 'path' ) ;
4
+
5
+ var files = { } ;
6
+
7
+ function readFileForMethod ( method ) {
8
+ return new Promise ( function ( resolve , reject ) {
9
+ if ( files [ method ] ) {
10
+ return setTimeout ( resolve . bind ( null , files [ method ] ) ) ;
11
+ }
12
+ fs . readFile ( path . resolve ( __dirname , method + '.graphql' ) , function ( err , file ) {
13
+ if ( err ) {
14
+ return reject ( err ) ;
15
+ }
16
+ files [ method ] = file . toString ( ) . replace ( '\n' , '' ) ;
17
+ resolve ( files [ method ] ) ;
18
+ } ) ;
19
+ } ) ;
20
+ }
21
+
22
+ function queryGraphQL ( method , vars , key ) {
23
+ return readFileForMethod ( method ) . then ( function ( query ) {
24
+ var body = JSON . stringify ( { query : query , variables : vars } ) ;
25
+ return fetch ( 'https://api.github.com/graphql' , {
26
+ method : 'POST' ,
27
+ headers : {
28
+ Authorization : 'bearer ' + key
29
+ } ,
30
+ body : body
31
+ } ) . then ( res => res . json ( ) ) . then ( function ( result ) {
32
+ if ( result . errors && result . errors . length > 0 ) {
33
+ result . errors . forEach ( console . error ) ;
34
+ throw new Error ( result . error [ 0 ] ) ;
35
+ }
36
+ return result ;
37
+ } ) ;
38
+ } ) ;
39
+ }
40
+
41
+ function hasProperties ( obj ) {
42
+ for ( var key in obj ) {
43
+ return true ;
44
+ }
45
+ return false ;
46
+ }
47
+
48
+ function GithubGraphQLWrapper ( key , owner , repo ) {
49
+ this . _key = key ;
50
+ this . _repo = repo ;
51
+ this . _owner = owner ;
52
+ this . _commits = [ ] ;
53
+ this . _issues = [ ] ;
54
+ this . _mergedPRs = [ ] ;
55
+ this . _closedIssues = [ ] ;
56
+ this . _lastRelease = null ;
57
+ this . _commitAfterRef = null ;
58
+ this . _reachedLastIssue = false ;
59
+ this . _reachedLastPr = false ;
60
+ this . _afterPRRef = null ;
61
+ this . _afterIssueRef = null ;
62
+ }
63
+
64
+ GithubGraphQLWrapper . prototype = {
65
+ constructor : GithubGraphQLWrapper ,
66
+ fetchLastGithubRelease : function fetchLastGithubRelease ( ) {
67
+ return queryGraphQL ( 'GetMasterCommits' , {
68
+ repo : this . _repo ,
69
+ owner : this . _owner ,
70
+ includeLastRelease : true ,
71
+ after : this . _commitAfterRef
72
+ } , this . _key ) . then ( result => {
73
+ var data = result . data . repository ;
74
+ var parentRelease = data . parent && data . parent . releases . nodes . length && data . parent . releases . nodes [ 0 ] ;
75
+ var lastRelease = data . releases . nodes . length > 0 ? data . releases . nodes [ 0 ] : parentRelease ;
76
+ var history = data . ref . target . history ;
77
+ this . _lastRelease = lastRelease ;
78
+ this . _commits = this . _commits . concat ( history . nodes ) ;
79
+ this . _commitAfterRef = history . pageInfo . endCursor ;
80
+ return this ;
81
+ } ) ;
82
+ } ,
83
+ fetchCommitsToLastRelease : function ( ) {
84
+ return queryGraphQL ( 'GetMasterCommits' , {
85
+ repo : this . _repo ,
86
+ owner : this . _owner ,
87
+ includeLastRelease : false ,
88
+ after : this . _commitAfterRef
89
+ } , this . _key ) . then ( result => {
90
+ var data = result . data . repository ;
91
+ var history = data . ref . target . history ;
92
+ this . _commitAfterRef = data . ref . target . history . pageInfo . endCursor ;
93
+ this . _commits = this . _commits . concat ( data . ref . target . history . nodes ) ;
94
+ var commitOids = this . _commits . map ( c => c . oid ) ;
95
+ if ( commitOids . indexOf ( this . _lastRelease . tag . target . oid ) == - 1 && history . pageInfo . hasNextPage ) {
96
+ return this . fetchCommitsToLastRelease ( ) ;
97
+ }
98
+ this . _commits . splice ( commitOids . indexOf ( this . _lastRelease . tag . target . oid ) ) ;
99
+ return this ;
100
+ } ) ;
101
+ } ,
102
+ fetchPRsAndIssues : function ( ) {
103
+ return queryGraphQL ( 'GetClosedIssuesAndMergedPRs' , {
104
+ repo : this . _repo ,
105
+ owner : this . _owner ,
106
+ includePRs : ! this . _reachedLastPr ,
107
+ includeIssues : ! this . _reachedLastIssue ,
108
+ afterIssues : this . _afterIssueRef ,
109
+ afterPRs : this . _afterPRRef ,
110
+ } , this . _key ) . then ( result => {
111
+ var repository = result . data . repository ;
112
+ var parent = repository . parent ;
113
+ var parentIssues = parent && parent . issues && parent . issues . nodes . length && parent . issues ;
114
+ var localIssues = repository . issues && repository . issues . nodes . length && repository . issues ;
115
+ var issues = localIssues || parentIssues ;
116
+ var parentPRs = parent && parent . pullRequests && parent . pullRequests . nodes . length && parent . pullRequests ;
117
+ var localPRs = repository . pullRequests && repository . pullRequests . nodes . length && repository . pullRequests ;
118
+ var prs = localPRs || parentPRs ;
119
+ if ( issues ) {
120
+ this . _reachedLastIssue = ! issues . pageInfo . hasNextPage ;
121
+ this . _afterIssueRef = issues . pageInfo . endCursor ;
122
+ this . _closedIssues = this . _closedIssues . concat ( issues . nodes ) ;
123
+ }
124
+
125
+ if ( prs ) {
126
+ this . _reachedLastPr = ! prs . pageInfo . hasNextPage ;
127
+ this . _afterPRRef = prs . pageInfo . endCursor ;
128
+ this . _mergedPRs = this . _mergedPRs . concat ( prs . nodes ) ;
129
+ }
130
+ if ( ! this . _reachedLastPr && ! this . _reachedLastIssue ) {
131
+ return this . fetchPRsAndIssues ( ) ;
132
+ }
133
+ } ) . then ( ( ) => {
134
+ this . _closedIssues = this . _closedIssues . map ( issue => {
135
+ issue . timeline = issue . timeline . nodes . filter ( hasProperties ) ;
136
+ return issue ;
137
+ } ) . filter ( issue => issue . timeline . length > 0 ) ;
138
+ this . _mergedPRs . map ( pr => {
139
+ pr . timeline = pr . timeline . nodes . filter ( hasProperties ) ;
140
+ return pr ;
141
+ } ) . filter ( pr => pr . timeline . length > 0 ) ;
142
+ return this ;
143
+ } ) ;
144
+ } ,
145
+ getLastRelease : function ( ) {
146
+ return this . _lastRelease ;
147
+ } ,
148
+ getMergedPRs : function ( ) {
149
+ return this . _mergedPRs ;
150
+ } ,
151
+ getCommits : function ( ) {
152
+ return this . _commits ;
153
+ } ,
154
+ getClosedIssues : function ( ) {
155
+ return this . _closedIssues ;
156
+ } ,
157
+ getOwner : function ( ) {
158
+ return this . _owner ;
159
+ } ,
160
+ getRepo : function ( ) {
161
+ return this . _repo ;
162
+ }
163
+ } ;
164
+
165
+ module . exports = GithubGraphQLWrapper ;
0 commit comments