Skip to content

Commit 51f2a25

Browse files
committed
Extract getAffectedFiles method to remove duplicated code
1 parent df66315 commit 51f2a25

File tree

1 file changed

+43
-75
lines changed

1 file changed

+43
-75
lines changed

services/pull/patch.go

Lines changed: 43 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -245,91 +245,73 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
245245

246246
// CheckFileProtection check file Protection
247247
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
249248
if len(patterns) == 0 {
250249
return nil, nil
251250
}
252-
253-
// 2. Prep the pipe
254-
stdoutReader, stdoutWriter, err := os.Pipe()
251+
affectedFiles, err := getAffectedFiles(oldCommitID, newCommitID, env, repo)
255252
if err != nil {
256-
log.Error("Unable to create os.Pipe for %s", repo.Path)
257253
return nil, err
258254
}
259-
defer func() {
260-
_ = stdoutReader.Close()
261-
_ = stdoutWriter.Close()
262-
}()
263-
264255
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+
}
307272
}
308-
309273
return changedProtectedFiles, err
310274
}
311275

312276
// CheckUnprotectedFiles check if the commit only touches unprotected files
313277
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
315278
if len(patterns) == 0 {
316279
return false, nil
317280
}
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+
}
318300

319-
// 2. Prep the pipe
301+
func getAffectedFiles(oldCommitID, newCommitID string, env []string, repo *git.Repository) ([]string, error) {
320302
stdoutReader, stdoutWriter, err := os.Pipe()
321303
if err != nil {
322304
log.Error("Unable to create os.Pipe for %s", repo.Path)
323-
return false, err
305+
return nil, err
324306
}
325307
defer func() {
326308
_ = stdoutReader.Close()
327309
_ = stdoutWriter.Close()
328310
}()
329311

330-
unprotectedFilesOnly := true
312+
affectedFiles := make([]string, 0, 32)
331313

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
333315
err = git.NewCommand("diff", "--name-only", oldCommitID, newCommitID).
334316
RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
335317
stdoutWriter, nil, nil,
@@ -340,36 +322,22 @@ func CheckUnprotectedFiles(oldCommitID, newCommitID string, patterns []glob.Glob
340322
// Close the reader on return to terminate the git command if necessary
341323
_ = stdoutReader.Close()
342324
}()
343-
344325
// Now scan the output from the command
345326
scanner := bufio.NewScanner(stdoutReader)
346327
for scanner.Scan() {
347328
path := strings.TrimSpace(scanner.Text())
348329
if len(path) == 0 {
349330
continue
350331
}
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)
363333
}
364-
365334
return scanner.Err()
366335
})
367-
// 4. log errors if there are any...
368336
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)
370338
}
371339

372-
return unprotectedFilesOnly, err
340+
return affectedFiles, err
373341
}
374342

375343
// checkPullFilesProtection check if pr changed protected files and save results

0 commit comments

Comments
 (0)