aboutsummaryrefslogtreecommitdiff
path: root/routers/repo
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-09-02 09:26:56 -0400
committerUnknwon <u@gogs.io>2015-09-02 09:26:56 -0400
commit953bb06857f59f5c6987f68a08a94cf5a885b456 (patch)
tree196a45fa4e7f8808b35790981eb0e417bbfa1a61 /routers/repo
parent6ea28f2a4759c5192811b12de054e7ad62f080f6 (diff)
basic PR feature
Diffstat (limited to 'routers/repo')
-rw-r--r--routers/repo/issue.go10
-rw-r--r--routers/repo/pull.go182
2 files changed, 159 insertions, 33 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index d51354a4..63198687 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -466,7 +466,12 @@ func ViewIssue(ctx *middleware.Context) {
// Get more information if it's a pull request.
if issue.IsPull {
- PrepareViewPullInfo(ctx, issue)
+ if issue.HasMerged {
+ ctx.Data["DisableStatusChange"] = issue.HasMerged
+ PrepareMergedViewPullInfo(ctx, issue)
+ } else {
+ PrepareViewPullInfo(ctx, issue)
+ }
if ctx.Written() {
return
}
@@ -730,7 +735,8 @@ func NewComment(ctx *middleware.Context, form auth.CreateCommentForm) {
// Check if issue owner/poster changes the status of issue.
if (ctx.Repo.IsOwner() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id))) &&
- (form.Status == "reopen" || form.Status == "close") {
+ (form.Status == "reopen" || form.Status == "close") &&
+ !(issue.IsPull && issue.HasMerged) {
issue.Repo = ctx.Repo.Repository
if err = issue.ChangeStatus(ctx.User, form.Status == "close"); err != nil {
ctx.Handle(500, "ChangeStatus", err)
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index 149c2f92..b55d928b 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -5,6 +5,7 @@
package repo
import (
+ "container/list"
"path"
"strings"
@@ -167,6 +168,26 @@ func checkPullInfo(ctx *middleware.Context) *models.Issue {
return pull
}
+func PrepareMergedViewPullInfo(ctx *middleware.Context, pull *models.Issue) {
+ ctx.Data["HasMerged"] = true
+
+ var err error
+
+ ctx.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBarcnh
+ ctx.Data["BaseTarget"] = ctx.Repo.Owner.Name + "/" + pull.BaseBranch
+
+ ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
+ if err != nil {
+ ctx.Handle(500, "Repo.GitRepo.CommitsCountBetween", err)
+ return
+ }
+ ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
+ if err != nil {
+ ctx.Handle(500, "Repo.GitRepo.FilesCountBetween", err)
+ return
+ }
+}
+
func PrepareViewPullInfo(ctx *middleware.Context, pull *models.Issue) *git.PullRequestInfo {
repo := ctx.Repo.Repository
@@ -185,6 +206,14 @@ func PrepareViewPullInfo(ctx *middleware.Context, pull *models.Issue) *git.PullR
return nil
}
+ if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBarcnh) {
+ ctx.Data["IsPullReuqestBroken"] = true
+ ctx.Data["HeadTarget"] = "deleted"
+ ctx.Data["NumCommits"] = 0
+ ctx.Data["NumFiles"] = 0
+ return nil
+ }
+
prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
pull.BaseBranch, pull.HeadBarcnh)
if err != nil {
@@ -203,17 +232,46 @@ func ViewPullCommits(ctx *middleware.Context) {
if ctx.Written() {
return
}
+ ctx.Data["Username"] = pull.HeadUserName
+ ctx.Data["Reponame"] = pull.HeadRepo.Name
- prInfo := PrepareViewPullInfo(ctx, pull)
- if ctx.Written() {
- return
+ var commits *list.List
+ if pull.HasMerged {
+ PrepareMergedViewPullInfo(ctx, pull)
+ if ctx.Written() {
+ return
+ }
+ startCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergeBase)
+ if err != nil {
+ ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
+ return
+ }
+ endCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
+ if err != nil {
+ ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
+ return
+ }
+ commits, err = ctx.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
+ if err != nil {
+ ctx.Handle(500, "Repo.GitRepo.CommitsBetween", err)
+ return
+ }
+
+ } else {
+ prInfo := PrepareViewPullInfo(ctx, pull)
+ if ctx.Written() {
+ return
+ } else if prInfo == nil {
+ ctx.Handle(404, "ViewPullCommits", nil)
+ return
+ }
+ commits = prInfo.Commits
}
- prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
- ctx.Data["Commits"] = prInfo.Commits
- ctx.Data["CommitCount"] = prInfo.Commits.Len()
- ctx.Data["Username"] = pull.HeadUserName
- ctx.Data["Reponame"] = pull.HeadRepo.Name
+ commits = models.ValidateCommitsWithEmails(commits)
+ ctx.Data["Commits"] = commits
+ ctx.Data["CommitCount"] = commits.Len()
+
ctx.HTML(200, PULL_COMMITS)
}
@@ -225,27 +283,54 @@ func ViewPullFiles(ctx *middleware.Context) {
return
}
- prInfo := PrepareViewPullInfo(ctx, pull)
- if ctx.Written() {
- return
- }
+ var (
+ diffRepoPath string
+ startCommitID string
+ endCommitID string
+ gitRepo *git.Repository
+ )
+
+ if pull.HasMerged {
+ PrepareMergedViewPullInfo(ctx, pull)
+ if ctx.Written() {
+ return
+ }
- headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
+ diffRepoPath = ctx.Repo.GitRepo.Path
+ startCommitID = pull.MergeBase
+ endCommitID = pull.MergedCommitID
+ gitRepo = ctx.Repo.GitRepo
+ } else {
+ prInfo := PrepareViewPullInfo(ctx, pull)
+ if ctx.Written() {
+ return
+ } else if prInfo == nil {
+ ctx.Handle(404, "ViewPullFiles", nil)
+ return
+ }
- headGitRepo, err := git.OpenRepository(headRepoPath)
- if err != nil {
- ctx.Handle(500, "OpenRepository", err)
- return
- }
+ headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
- headCommitID, err := headGitRepo.GetCommitIdOfBranch(pull.HeadBarcnh)
- if err != nil {
- ctx.Handle(500, "GetCommitIdOfBranch", err)
- return
+ headGitRepo, err := git.OpenRepository(headRepoPath)
+ if err != nil {
+ ctx.Handle(500, "OpenRepository", err)
+ return
+ }
+
+ headCommitID, err := headGitRepo.GetCommitIdOfBranch(pull.HeadBarcnh)
+ if err != nil {
+ ctx.Handle(500, "GetCommitIdOfBranch", err)
+ return
+ }
+
+ diffRepoPath = headRepoPath
+ startCommitID = prInfo.MergeBase
+ endCommitID = headCommitID
+ gitRepo = headGitRepo
}
- diff, err := models.GetDiffRange(headRepoPath,
- prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines)
+ diff, err := models.GetDiffRange(diffRepoPath,
+ startCommitID, endCommitID, setting.Git.MaxGitDiffLines)
if err != nil {
ctx.Handle(500, "GetDiffRange", err)
return
@@ -253,7 +338,7 @@ func ViewPullFiles(ctx *middleware.Context) {
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
- headCommit, err := headGitRepo.GetCommit(headCommitID)
+ commit, err := gitRepo.GetCommit(endCommitID)
if err != nil {
ctx.Handle(500, "GetCommit", err)
return
@@ -262,14 +347,49 @@ func ViewPullFiles(ctx *middleware.Context) {
headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
ctx.Data["Username"] = pull.HeadUserName
ctx.Data["Reponame"] = pull.HeadRepo.Name
- ctx.Data["IsImageFile"] = headCommit.IsImageFile
- ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", headCommitID)
- ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
- ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", headCommitID)
+ ctx.Data["IsImageFile"] = commit.IsImageFile
+ ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
+ ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
+ ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
ctx.HTML(200, PULL_FILES)
}
+func MergePullRequest(ctx *middleware.Context) {
+ pull := checkPullInfo(ctx)
+ if ctx.Written() {
+ return
+ }
+ if pull.IsClosed {
+ ctx.Handle(404, "MergePullRequest", nil)
+ return
+ }
+
+ pr, err := models.GetPullRequestByPullID(pull.ID)
+ if err != nil {
+ if models.IsErrPullRequestNotExist(err) {
+ ctx.Handle(404, "GetPullRequestByPullID", nil)
+ } else {
+ ctx.Handle(500, "GetPullRequestByPullID", err)
+ }
+ return
+ }
+
+ if !pr.CanAutoMerge || pr.HasMerged {
+ ctx.Handle(404, "MergePullRequest", nil)
+ return
+ }
+
+ pr.Pull = pull
+ if err = pr.Merge(ctx.Repo.GitRepo); err != nil {
+ ctx.Handle(500, "GetPullRequestByPullID", err)
+ return
+ }
+
+ log.Trace("Pull request merged: %d", pr.ID)
+ ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.PullIndex))
+}
+
func ParseCompareInfo(ctx *middleware.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
// Get compare branch information.
infos := strings.Split(ctx.Params("*"), "...")
@@ -416,10 +536,10 @@ func CompareAndPullRequest(ctx *middleware.Context) {
return
}
- pr, err := models.GetPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
+ pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
if err != nil {
if !models.IsErrPullRequestNotExist(err) {
- ctx.Handle(500, "HasPullRequest", err)
+ ctx.Handle(500, "GetUnmergedPullRequest", err)
return
}
} else {