mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 02:46:04 +01:00 
			
		
		
		
	Fix pull view when head repository or head branch missed and close related pull requests when delete head repository or head branch (#9927)
* fix pull view when head repository or head branch missed and close related pull requests when delete branch * fix pull view broken when head repository deleted * close pull requests when head repositories deleted * Add tests for broken pull request head repository or branch * fix typo * ignore special error when close pull request Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
		| @@ -355,3 +355,79 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) { | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| type errlist []error | ||||
|  | ||||
| func (errs errlist) Error() string { | ||||
| 	if len(errs) > 0 { | ||||
| 		var buf strings.Builder | ||||
| 		for i, err := range errs { | ||||
| 			if i > 0 { | ||||
| 				buf.WriteString(", ") | ||||
| 			} | ||||
| 			buf.WriteString(err.Error()) | ||||
| 		} | ||||
| 		return buf.String() | ||||
| 	} | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| // CloseBranchPulls close all the pull requests who's head branch is the branch | ||||
| func CloseBranchPulls(doer *models.User, repoID int64, branch string) error { | ||||
| 	prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	prs2, err := models.GetUnmergedPullRequestsByBaseInfo(repoID, branch) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	prs = append(prs, prs2...) | ||||
| 	if err := models.PullRequestList(prs).LoadAttributes(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	var errs errlist | ||||
| 	for _, pr := range prs { | ||||
| 		if err = issue_service.ChangeStatus(pr.Issue, doer, true); err != nil && !models.IsErrIssueWasClosed(err) { | ||||
| 			errs = append(errs, err) | ||||
| 		} | ||||
| 	} | ||||
| 	if len(errs) > 0 { | ||||
| 		return errs | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // CloseRepoBranchesPulls close all pull requests which head branches are in the given repository | ||||
| func CloseRepoBranchesPulls(doer *models.User, repo *models.Repository) error { | ||||
| 	branches, err := git.GetBranchesByPath(repo.RepoPath()) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	var errs errlist | ||||
| 	for _, branch := range branches { | ||||
| 		prs, err := models.GetUnmergedPullRequestsByHeadInfo(repo.ID, branch.Name) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		if err = models.PullRequestList(prs).LoadAttributes(); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		for _, pr := range prs { | ||||
| 			if err = issue_service.ChangeStatus(pr.Issue, doer, true); err != nil && !models.IsErrIssueWasClosed(err) { | ||||
| 				errs = append(errs, err) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if len(errs) > 0 { | ||||
| 		return errs | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user