Pull request conflict files detection (#5951)

* add conflict detection

* test pull request conflict files

* fix detection files number

* fix comments
This commit is contained in:
Lunny Xiao 2019-02-05 19:54:49 +08:00 committed by techknowlogick
parent 680a57ce92
commit bf4badad1d
4 changed files with 45 additions and 5 deletions

View file

@ -54,9 +54,10 @@ const (
// PullRequest represents relation between pull request and repositories.
type PullRequest struct {
ID int64 `xorm:"pk autoincr"`
Type PullRequestType
Status PullRequestStatus
ID int64 `xorm:"pk autoincr"`
Type PullRequestType
Status PullRequestStatus
ConflictedFiles []string `xorm:"TEXT JSON"`
IssueID int64 `xorm:"INDEX"`
Issue *Issue `xorm:"-"`
@ -843,6 +844,7 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
args = append(args, "--ignore-whitespace")
}
args = append(args, patchPath)
pr.ConflictedFiles = []string{}
_, stderr, err = process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("testPatch (git apply --check): %d", pr.BaseRepo.ID),
[]string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()},
@ -851,8 +853,26 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
for i := range patchConflicts {
if strings.Contains(stderr, patchConflicts[i]) {
log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID)
fmt.Println(stderr)
const prefix = "error: patch failed:"
pr.Status = PullRequestStatusConflict
pr.ConflictedFiles = make([]string, 0, 5)
scanner := bufio.NewScanner(strings.NewReader(stderr))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, prefix) {
pr.ConflictedFiles = append(pr.ConflictedFiles, strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0]))
}
// only list 10 conflicted files
if len(pr.ConflictedFiles) >= 10 {
break
}
}
if len(pr.ConflictedFiles) > 0 {
log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
}
return nil
}
}
@ -1375,7 +1395,7 @@ func (pr *PullRequest) checkAndUpdateStatus() {
// Make sure there is no waiting test to process before leaving the checking status.
if !pullRequestQueue.Exist(pr.ID) {
if err := pr.UpdateCols("status"); err != nil {
if err := pr.UpdateCols("status, conflicted_files"); err != nil {
log.Error(4, "Update[%d]: %v", pr.ID, err)
}
}
@ -1396,6 +1416,11 @@ func (pr *PullRequest) IsWorkInProgress() bool {
return false
}
// IsFilesConflicted determines if the Pull Request has changes conflicting with the target branch.
func (pr *PullRequest) IsFilesConflicted() bool {
return len(pr.ConflictedFiles) > 0
}
// GetWorkInProgressPrefix returns the prefix used to mark the pull request as a work in progress.
// It returns an empty string when none were found
func (pr *PullRequest) GetWorkInProgressPrefix() string {