@@ -245,91 +245,73 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
245
245
246
246
// CheckFileProtection check file Protection
247
247
func CheckFileProtection (oldCommitID , newCommitID string , patterns []glob.Glob , limit int , env []string , repo * git.Repository ) ([]string , error ) {
248
- // 1. If there are no patterns short-circuit and just return nil
249
248
if len (patterns ) == 0 {
250
249
return nil , nil
251
250
}
252
-
253
- // 2. Prep the pipe
254
- stdoutReader , stdoutWriter , err := os .Pipe ()
251
+ affectedFiles , err := getAffectedFiles (oldCommitID , newCommitID , env , repo )
255
252
if err != nil {
256
- log .Error ("Unable to create os.Pipe for %s" , repo .Path )
257
253
return nil , err
258
254
}
259
- defer func () {
260
- _ = stdoutReader .Close ()
261
- _ = stdoutWriter .Close ()
262
- }()
263
-
264
255
changedProtectedFiles := make ([]string , 0 , limit )
265
-
266
- // 3. Run `git diff --name-only` to get the names of the changed files
267
- err = git .NewCommand ("diff" , "--name-only" , oldCommitID , newCommitID ).
268
- RunInDirTimeoutEnvFullPipelineFunc (env , - 1 , repo .Path ,
269
- stdoutWriter , nil , nil ,
270
- func (ctx context.Context , cancel context.CancelFunc ) error {
271
- // Close the writer end of the pipe to begin processing
272
- _ = stdoutWriter .Close ()
273
- defer func () {
274
- // Close the reader on return to terminate the git command if necessary
275
- _ = stdoutReader .Close ()
276
- }()
277
-
278
- // Now scan the output from the command
279
- scanner := bufio .NewScanner (stdoutReader )
280
- for scanner .Scan () {
281
- path := strings .TrimSpace (scanner .Text ())
282
- if len (path ) == 0 {
283
- continue
284
- }
285
- lpath := strings .ToLower (path )
286
- for _ , pat := range patterns {
287
- if pat .Match (lpath ) {
288
- changedProtectedFiles = append (changedProtectedFiles , path )
289
- break
290
- }
291
- }
292
- if len (changedProtectedFiles ) >= limit {
293
- break
294
- }
295
- }
296
-
297
- if len (changedProtectedFiles ) > 0 {
298
- return models.ErrFilePathProtected {
299
- Path : changedProtectedFiles [0 ],
300
- }
301
- }
302
- return scanner .Err ()
303
- })
304
- // 4. log real errors if there are any...
305
- if err != nil && ! models .IsErrFilePathProtected (err ) {
306
- log .Error ("Unable to check file protection for commits from %s to %s in %s: %v" , oldCommitID , newCommitID , repo .Path , err )
256
+ for _ , affectedFile := range affectedFiles {
257
+ lpath := strings .ToLower (affectedFile )
258
+ for _ , pat := range patterns {
259
+ if pat .Match (lpath ) {
260
+ changedProtectedFiles = append (changedProtectedFiles , lpath )
261
+ break
262
+ }
263
+ }
264
+ if len (changedProtectedFiles ) >= limit {
265
+ break
266
+ }
267
+ }
268
+ if len (changedProtectedFiles ) > 0 {
269
+ err = models.ErrFilePathProtected {
270
+ Path : changedProtectedFiles [0 ],
271
+ }
307
272
}
308
-
309
273
return changedProtectedFiles , err
310
274
}
311
275
312
276
// CheckUnprotectedFiles check if the commit only touches unprotected files
313
277
func CheckUnprotectedFiles (oldCommitID , newCommitID string , patterns []glob.Glob , env []string , repo * git.Repository ) (bool , error ) {
314
- // 1. If there are no patterns short-circuit and just return false
315
278
if len (patterns ) == 0 {
316
279
return false , nil
317
280
}
281
+ affectedFiles , err := getAffectedFiles (oldCommitID , newCommitID , env , repo )
282
+ if err != nil {
283
+ return false , err
284
+ }
285
+ for _ , affectedFile := range affectedFiles {
286
+ lpath := strings .ToLower (affectedFile )
287
+ unprotected := false
288
+ for _ , pat := range patterns {
289
+ if pat .Match (lpath ) {
290
+ unprotected = true
291
+ break
292
+ }
293
+ }
294
+ if ! unprotected {
295
+ return false , nil
296
+ }
297
+ }
298
+ return true , nil
299
+ }
318
300
319
- // 2. Prep the pipe
301
+ func getAffectedFiles ( oldCommitID , newCommitID string , env [] string , repo * git. Repository ) ([] string , error ) {
320
302
stdoutReader , stdoutWriter , err := os .Pipe ()
321
303
if err != nil {
322
304
log .Error ("Unable to create os.Pipe for %s" , repo .Path )
323
- return false , err
305
+ return nil , err
324
306
}
325
307
defer func () {
326
308
_ = stdoutReader .Close ()
327
309
_ = stdoutWriter .Close ()
328
310
}()
329
311
330
- unprotectedFilesOnly := true
312
+ affectedFiles := make ([] string , 0 , 32 )
331
313
332
- // 3. Run `git diff --name-only` to get the names of the changed files
314
+ // Run `git diff --name-only` to get the names of the changed files
333
315
err = git .NewCommand ("diff" , "--name-only" , oldCommitID , newCommitID ).
334
316
RunInDirTimeoutEnvFullPipelineFunc (env , - 1 , repo .Path ,
335
317
stdoutWriter , nil , nil ,
@@ -340,36 +322,22 @@ func CheckUnprotectedFiles(oldCommitID, newCommitID string, patterns []glob.Glob
340
322
// Close the reader on return to terminate the git command if necessary
341
323
_ = stdoutReader .Close ()
342
324
}()
343
-
344
325
// Now scan the output from the command
345
326
scanner := bufio .NewScanner (stdoutReader )
346
327
for scanner .Scan () {
347
328
path := strings .TrimSpace (scanner .Text ())
348
329
if len (path ) == 0 {
349
330
continue
350
331
}
351
- lpath := strings .ToLower (path )
352
- unprotected := false
353
- for _ , pat := range patterns {
354
- if pat .Match (lpath ) {
355
- unprotected = true
356
- break
357
- }
358
- }
359
- if ! unprotected {
360
- unprotectedFilesOnly = false
361
- break
362
- }
332
+ affectedFiles = append (affectedFiles , path )
363
333
}
364
-
365
334
return scanner .Err ()
366
335
})
367
- // 4. log errors if there are any...
368
336
if err != nil {
369
- log .Error ("Unable to check file protection for commits from %s to %s in %s: %v" , oldCommitID , newCommitID , repo .Path , err )
337
+ log .Error ("Unable to get affected files for commits from %s to %s in %s: %v" , oldCommitID , newCommitID , repo .Path , err )
370
338
}
371
339
372
- return unprotectedFilesOnly , err
340
+ return affectedFiles , err
373
341
}
374
342
375
343
// checkPullFilesProtection check if pr changed protected files and save results
0 commit comments