1
1
'use strict'
2
2
3
+ const request = require ( 'request' )
3
4
const LRU = require ( 'lru-cache' )
4
5
const retry = require ( 'async' ) . retry
6
+ const debug = require ( 'debug' ) ( 'node_repo' )
5
7
6
8
const githubClient = require ( './github-client' )
7
9
const resolveLabels = require ( './node-labels' ) . resolveLabels
10
+ const { Owners } = require ( './node-owners' )
11
+ const { createPrComment } = require ( './github-comment' )
8
12
const existingLabelsCache = new LRU ( { max : 1 , maxAge : 1000 * 60 * 60 } )
9
13
10
14
const fiveSeconds = 5 * 1000
11
15
12
- function deferredResolveLabelsThenUpdatePr ( options ) {
13
- const timeoutMillis = ( options . timeoutInSec || 0 ) * 1000
14
- setTimeout ( resolveLabelsThenUpdatePr , timeoutMillis , options )
15
- }
16
-
17
- function resolveLabelsThenUpdatePr ( options ) {
16
+ function getFilepathsChanged ( options , handleFilepaths ) {
18
17
options . logger . debug ( 'Fetching PR files for labelling' )
19
18
20
- const getFiles = ( cb ) => {
19
+ const getPRFiles = ( cb ) => {
21
20
githubClient . pullRequests . getFiles ( {
22
21
owner : options . owner ,
23
22
repo : options . repo ,
24
23
number : options . prId
25
24
} , cb )
26
25
}
27
26
28
- retry ( { times : 5 , interval : fiveSeconds } , getFiles , ( err , res ) => {
27
+ retry ( { times : 5 , interval : fiveSeconds } , getPRFiles , ( err , res ) => {
29
28
if ( err ) {
30
29
return options . logger . error ( err , 'Error retrieving files from GitHub' )
31
30
}
32
-
33
31
const filepathsChanged = res . data . map ( ( fileMeta ) => fileMeta . filename )
34
- const resolvedLabels = resolveLabels ( filepathsChanged , options . baseBranch )
35
32
36
- fetchExistingThenUpdatePr ( options , resolvedLabels )
33
+ handleFilepaths ( options , filepathsChanged )
37
34
} )
38
35
}
39
36
37
+ function deferredResolveOwnersThenPingPr ( options ) {
38
+ const timeoutMillis = ( options . timeoutInSec || 0 ) * 1000
39
+ setTimeout ( ( ) => getFilepathsChanged ( options , resolveOwnersThenPingPr ) , timeoutMillis , options )
40
+ }
41
+
42
+ function resolveOwnersThenPingPr ( options , filepathsChanged ) {
43
+ const { owner, repo } = options
44
+ githubClient . repos . get ( {
45
+ owner,
46
+ repo
47
+ } , ( err , res ) => {
48
+ if ( err ) {
49
+ return options . logger . error ( err , 'Error retrieving repository data' )
50
+ }
51
+ const data = res . data || { }
52
+ if ( ! data [ 'default_branch' ] ) {
53
+ return options . logger . error ( err , 'Couldn\' determine default branch' )
54
+ }
55
+
56
+ const { default_branch : defaultBranch } = data
57
+
58
+ const url = `https://raw.githubusercontent.com/${ owner } /${ repo } /${ defaultBranch } /OWNERS.yml`
59
+ debug ( `Fetching OWNERS on ${ url } ` )
60
+ request ( url , ( err , res , body ) => {
61
+ if ( err || ! res || res . statusCode >= 400 ) {
62
+ return options . logger . error ( err , 'Error retrieving OWNERS' )
63
+ }
64
+ const owners = Owners . fromYaml ( body )
65
+ const selectedOwners = owners . getOwnersForPaths ( filepathsChanged )
66
+ console . log ( selectedOwners )
67
+ if ( selectedOwners . length > 0 ) {
68
+ pingOwners ( options , selectedOwners )
69
+ }
70
+ console . log ( 'done' )
71
+ } )
72
+ } )
73
+ }
74
+
75
+ function deferredResolveLabelsThenUpdatePr ( options ) {
76
+ const timeoutMillis = ( options . timeoutInSec || 0 ) * 1000
77
+ setTimeout ( ( ) => getFilepathsChanged ( options , resolveLabelsThenUpdatePr ) , timeoutMillis , options )
78
+ }
79
+
80
+ function resolveLabelsThenUpdatePr ( options , filepathsChanged ) {
81
+ const resolvedLabels = resolveLabels ( filepathsChanged , options . baseBranch )
82
+
83
+ fetchExistingThenUpdatePr ( options , resolvedLabels )
84
+ }
85
+
40
86
function fetchExistingThenUpdatePr ( options , labels ) {
41
87
fetchExistingLabels ( options , ( err , existingLabels ) => {
42
88
if ( err ) {
@@ -53,6 +99,21 @@ function fetchExistingThenUpdatePr (options, labels) {
53
99
} )
54
100
}
55
101
102
+ function pingOwners ( options , owners ) {
103
+ createPrComment ( {
104
+ owner : options . owner ,
105
+ repo : options . repo ,
106
+ number : options . prId ,
107
+ logger : options . logger
108
+ } , `Review requested:\n\n${ owners . map ( i => `- [ ] ${ i } ` ) . join ( '\n' ) } ` , ( err ) => {
109
+ if ( err ) {
110
+ return options . logger . error ( err , 'Error while pinging owners' )
111
+ }
112
+
113
+ options . logger . info ( 'Pinged owners: ' + owners )
114
+ } )
115
+ }
116
+
56
117
function updatePrWithLabels ( options , labels ) {
57
118
// no need to request github if we didn't resolve any labels
58
119
if ( ! labels . length ) {
@@ -192,6 +253,7 @@ exports.getBotPrLabels = getBotPrLabels
192
253
exports . removeLabelFromPR = removeLabelFromPR
193
254
exports . fetchExistingThenUpdatePr = fetchExistingThenUpdatePr
194
255
exports . resolveLabelsThenUpdatePr = deferredResolveLabelsThenUpdatePr
256
+ exports . resolveOwnersThenPingPr = deferredResolveOwnersThenPingPr
195
257
196
258
// exposed for testability
197
259
exports . _fetchExistingLabels = fetchExistingLabels
0 commit comments