From 47aa553279837bfdfb4b569f2fa5bff2fd8aa815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=A2=A6=E9=87=91?= <735416909@qq.com> Date: Thu, 31 May 2018 14:45:27 +0800 Subject: [PATCH] fix#5252, double click to submit pull request will make 2 same pr. --- models/error.go | 14 ++++++++++++++ models/pull.go | 14 ++++++++++++++ routes/repo/pull.go | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/models/error.go b/models/error.go index 63e06f6e0..e442926fe 100644 --- a/models/error.go +++ b/models/error.go @@ -414,6 +414,20 @@ func (err ErrPullRequestNotExist) Error() string { err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch) } +type ErrPullRequestExist struct { + ID int64 + IssueID int64 + HeadRepoID int64 + BaseRepoID int64 + HeadBarcnh string + BaseBranch string +} + +func (err ErrPullRequestExist) Error() string { + return fmt.Sprintf("pull request already exist [id: %d, issue_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]", + err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch) +} + // _________ __ // \_ ___ \ ____ _____ _____ ____ _____/ |_ // / \ \/ / _ \ / \ / \_/ __ \ / \ __\ diff --git a/models/pull.go b/models/pull.go index b4524f163..49948c7f9 100644 --- a/models/pull.go +++ b/models/pull.go @@ -525,6 +525,20 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch return pr, nil } +// CheckPullRequestExist returns if the pull request has been create before create a new pull request +func CheckPullRequestExist(headRepoID, baseRepoID int64, headBranch, baseBranch string) (bool, error) { + pr := new(PullRequest) + has, err := x.Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?", + headRepoID, headBranch, baseRepoID, baseBranch, false, false). + Join("INNER", "issue", "issue.id=pull_request.issue_id").Get(pr) + if err != nil { + return false, err + } else if has { + return true, ErrPullRequestExist{pr.ID, pr.IssueID, headRepoID, baseRepoID, headBranch, baseBranch} + } + return false, nil +} + // GetUnmergedPullRequestsByHeadInfo returnss all pull requests that are open and has not been merged // by given head information (repo and branch). func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequest, error) { diff --git a/routes/repo/pull.go b/routes/repo/pull.go index 049cf3686..187a781d4 100644 --- a/routes/repo/pull.go +++ b/routes/repo/pull.go @@ -655,6 +655,14 @@ func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) { return } + // check one people send the pr at the same time + has , err := models.CheckPullRequestExist(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch) + + if has || err != nil { + c.ServerError("CheckPullRequestExist",err) + return + } + labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f) if c.Written() { return