@@ -103,32 +103,64 @@ function isConfig(config: any): config is Config {
103
103
commit_sha : event . after
104
104
} ) ;
105
105
106
- // Calculate which file paths have changed in this push
107
- const { stdout : filesChangedRaw } = await execFile ( 'git' , [ 'diff' , '--name-only' , `${ event . before } ..${ event . after } ` ] ) ;
108
- const queriesChanged = new Set ( filesChangedRaw . split ( '\n' )
109
- . map ( s => s . trim ( ) )
110
- . filter ( s => s . endsWith ( '.ql' ) ) ) ;
111
- console . log ( `${ pluralize ( queriesChanged . size , 'query' ) } updated in this push` ) ;
112
- comment += `${ pluralize ( queriesChanged . size , 'query' ) } changed `
113
- comment += `[between \`${ event . before . substr ( 0 , 7 ) } \` and \`${ event . after . substr ( 0 , 7 ) } \`]`
114
- comment += `(${ event . repository . html_url } /compare/${ event . before } ...${ event . after } ) after push to \`${ event . ref } \`` ;
115
- if ( queriesChanged . size > 0 ) {
116
- comment += ':\n' ;
117
- for ( const query of queriesChanged ) {
118
- console . log ( `- ${ query } ` ) ;
119
- const exists = await access ( query , fs . constants . R_OK ) . then ( ( ) => true , ( ) => false ) ;
120
- comment += `* \`${ query } \`${ exists ? '' : ' *(deleted)*' } \n` ;
106
+ /**
107
+ * File paths changed by the user (if we're not just running all queries)
108
+ *
109
+ * This is used to reduce the number of queries we need to run to only those
110
+ * that currently interest the user.
111
+ */
112
+ const queriesChanged = new Set < string > ( ) ;
113
+ let unableToGetChangedQueries = false ;
114
+
115
+ if ( RUN_ALL ) {
116
+
117
+ /*
118
+ * There are a few different ways in which we may determine which queries
119
+ * are currently interesting to the user, in decreasing usefulness:
120
+ *
121
+ * * If the user just pushed to a branch that currently has an open pull
122
+ * request, the interesting queries are those that are changed in the pull
123
+ * request (and not just those changed in the most recent push).
124
+ * * If there's no active pull request, then what's probably most interesting
125
+ * are the queries that have changed in the last push (i.e. between the
126
+ * previous head and the new head)
127
+ * * If that's not possible (e.g. the push could have created the branch for
128
+ * the first time, and so there is no "previous ref"), then comparing this
129
+ * branch to the default branch of a repo will probably give the most
130
+ * accurate results.
131
+ * * Finally, if all else fails (e.g. the push was the initial push to the
132
+ * default branch of the repo), then we should just run every query we
133
+ * recognize (as if RUN_ALL was true). We do this by setting
134
+ * unableToGetChangedQueries to true.
135
+ */
136
+
137
+ const { stdout : filesChangedRaw } = await execFile ( 'git' , [ 'diff' , '--name-only' , `${ event . before } ..${ event . after } ` ] ) ;
138
+ filesChangedRaw . split ( '\n' )
139
+ . map ( s => s . trim ( ) )
140
+ . filter ( s => s . endsWith ( '.ql' ) )
141
+ . forEach ( s => queriesChanged . add ( s ) ) ;
142
+ console . log ( `${ pluralize ( queriesChanged . size , 'query' ) } updated in this push` ) ;
143
+ comment += `${ pluralize ( queriesChanged . size , 'query' ) } changed `
144
+ comment += `[between \`${ event . before . substr ( 0 , 7 ) } \` and \`${ event . after . substr ( 0 , 7 ) } \`]`
145
+ comment += `(${ event . repository . html_url } /compare/${ event . before } ...${ event . after } ) after push to \`${ event . ref } \`` ;
146
+ if ( queriesChanged . size > 0 ) {
147
+ comment += ':\n' ;
148
+ for ( const query of queriesChanged ) {
149
+ console . log ( `- ${ query } ` ) ;
150
+ const exists = await access ( query , fs . constants . R_OK ) . then ( ( ) => true , ( ) => false ) ;
151
+ comment += `* \`${ query } \`${ exists ? '' : ' *(deleted)*' } \n` ;
152
+ }
153
+ } else {
154
+ comment += '\n' ;
121
155
}
122
- } else {
123
- comment += '\n' ;
124
156
}
125
157
126
158
// Work out which queries to run, based on config and changed & existing queries
127
159
const queriesToRun : string [ ] = [ ] ;
128
160
for ( const query of Object . keys ( config . expectedResults ) ) {
129
161
const exists = await access ( query , fs . constants . R_OK ) . then ( ( ) => true , ( ) => false ) ;
130
162
// Run the query if either it's changed, or runAll is true
131
- if ( exists && ( RUN_ALL || queriesChanged . has ( query ) ) ) {
163
+ if ( exists && ( RUN_ALL || unableToGetChangedQueries || queriesChanged . has ( query ) ) ) {
132
164
queriesToRun . push ( query ) ;
133
165
}
134
166
}
0 commit comments