Files
Gogs/internal/route/repo/pull.go

787 lines
20 KiB
Go
Raw Normal View History

2014-03-24 18:25:15 +08:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
2015-09-02 09:26:56 -04:00
"container/list"
2015-09-01 19:07:02 -04:00
"path"
2015-08-08 22:43:14 +08:00
"strings"
"github.com/unknwon/com"
log "unknwon.dev/clog/v2"
2015-09-01 19:07:02 -04:00
2018-05-27 08:53:48 +08:00
"github.com/gogs/git-module"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
"gogs.io/gogs/internal/tool"
2014-03-24 18:25:15 +08:00
)
2014-06-22 23:11:12 -04:00
const (
2017-04-05 09:17:21 -04:00
FORK = "repo/pulls/fork"
COMPARE_PULL = "repo/pulls/compare"
PULL_COMMITS = "repo/pulls/commits"
PULL_FILES = "repo/pulls/files"
PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
PULL_REQUEST_TITLE_TEMPLATE_KEY = "PullRequestTitleTemplate"
)
var (
PullRequestTemplateCandidates = []string{
"PULL_REQUEST.md",
".gogs/PULL_REQUEST.md",
".github/PULL_REQUEST.md",
}
PullRequestTitleTemplateCandidates = []string{
"PULL_REQUEST_TITLE.md",
".gogs/PULL_REQUEST_TITLE.md",
".github/PULL_REQUEST_TITLE.md",
}
2014-06-22 23:11:12 -04:00
)
func parseBaseRepository(c *context.Context) *db.Repository {
baseRepo, err := db.GetRepositoryByID(c.ParamsInt64(":repoid"))
2015-08-08 17:10:34 +08:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
2015-08-08 17:10:34 +08:00
return nil
}
2015-09-01 11:57:02 -04:00
2017-06-03 07:26:09 -04:00
if !baseRepo.CanBeForked() || !baseRepo.HasAccess(c.User.ID) {
c.NotFound()
2015-09-01 11:57:02 -04:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["repo_name"] = baseRepo.Name
c.Data["description"] = baseRepo.Description
c.Data["IsPrivate"] = baseRepo.IsPrivate
2015-08-08 17:10:34 +08:00
2017-02-25 03:35:26 -05:00
if err = baseRepo.GetOwner(); err != nil {
c.ServerError("GetOwner", err)
2015-08-08 17:10:34 +08:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name
2015-08-08 17:10:34 +08:00
2017-06-03 07:26:09 -04:00
if err := c.User.GetOrganizations(true); err != nil {
c.ServerError("GetOrganizations", err)
2015-08-08 17:10:34 +08:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["Orgs"] = c.User.Orgs
2015-08-08 17:10:34 +08:00
2017-02-25 03:35:26 -05:00
return baseRepo
2015-08-08 17:10:34 +08:00
}
2017-06-03 07:26:09 -04:00
func Fork(c *context.Context) {
c.Data["Title"] = c.Tr("new_fork")
2015-08-08 17:10:34 +08:00
2017-06-03 07:26:09 -04:00
parseBaseRepository(c)
if c.Written() {
2015-08-08 17:10:34 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = c.User
c.Success(FORK)
2015-08-08 17:10:34 +08:00
}
2017-06-03 07:26:09 -04:00
func ForkPost(c *context.Context, f form.CreateRepo) {
c.Data["Title"] = c.Tr("new_fork")
2015-08-08 17:10:34 +08:00
2017-06-03 07:26:09 -04:00
baseRepo := parseBaseRepository(c)
if c.Written() {
2015-08-08 17:10:34 +08:00
return
}
2017-06-03 07:26:09 -04:00
ctxUser := checkContextUser(c, f.UserID)
if c.Written() {
2015-08-08 17:10:34 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["ContextUser"] = ctxUser
2015-08-08 17:10:34 +08:00
2017-06-03 07:26:09 -04:00
if c.HasError() {
c.Success(FORK)
2015-08-08 17:10:34 +08:00
return
}
repo, has, err := db.HasForkedRepo(ctxUser.ID, baseRepo.ID)
if err != nil {
c.ServerError("HasForkedRepo", err)
return
} else if has {
2017-06-03 07:26:09 -04:00
c.Redirect(repo.Link())
2015-08-08 17:24:10 +08:00
return
}
2015-08-08 17:10:34 +08:00
// Check ownership of organization.
2017-06-03 07:26:09 -04:00
if ctxUser.IsOrganization() && !ctxUser.IsOwnedBy(c.User.ID) {
c.Error(403)
2017-02-25 03:35:26 -05:00
return
2015-08-08 17:10:34 +08:00
}
// Cannot fork to same owner
2017-02-25 03:35:26 -05:00
if ctxUser.ID == baseRepo.OwnerID {
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("repo.settings.cannot_fork_to_same_owner"), FORK, &f)
return
}
repo, err = db.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
2015-08-08 17:10:34 +08:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Data["Err_RepoName"] = true
2015-08-08 17:10:34 +08:00
switch {
case errors.IsReachLimitOfRepo(err):
c.RenderWithErr(c.Tr("repo.form.reach_limit_of_creation", c.User.RepoCreationNum()), FORK, &f)
case db.IsErrRepoAlreadyExist(err):
2017-06-03 07:26:09 -04:00
c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), FORK, &f)
case db.IsErrNameReserved(err):
c.RenderWithErr(c.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), FORK, &f)
case db.IsErrNamePatternNotAllowed(err):
c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), FORK, &f)
2015-08-08 17:10:34 +08:00
default:
c.ServerError("ForkPost", err)
2015-08-08 17:10:34 +08:00
}
return
}
2017-02-25 03:35:26 -05:00
log.Trace("Repository forked from '%s' -> '%s'", baseRepo.FullName(), repo.FullName())
2017-06-03 07:26:09 -04:00
c.Redirect(repo.Link())
2015-08-08 17:10:34 +08:00
}
func checkPullInfo(c *context.Context) *db.Issue {
issue, err := db.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
2015-09-02 04:08:05 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
2015-09-02 04:08:05 -04:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["Title"] = issue.Title
c.Data["Issue"] = issue
2015-09-02 04:08:05 -04:00
if !issue.IsPull {
2017-06-03 07:26:09 -04:00
c.Handle(404, "ViewPullCommits", nil)
2015-09-02 04:08:05 -04:00
return nil
}
2017-06-03 07:26:09 -04:00
if c.IsLogged {
2015-09-02 04:08:05 -04:00
// Update issue-user.
2017-06-03 07:26:09 -04:00
if err = issue.ReadBy(c.User.ID); err != nil {
c.ServerError("ReadBy", err)
2015-09-02 04:08:05 -04:00
return nil
}
}
return issue
2015-09-02 04:08:05 -04:00
}
func PrepareMergedViewPullInfo(c *context.Context, issue *db.Issue) {
pull := issue.PullRequest
2017-06-03 07:26:09 -04:00
c.Data["HasMerged"] = true
c.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
2015-09-02 09:26:56 -04:00
var err error
2017-06-03 07:26:09 -04:00
c.Data["NumCommits"], err = c.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("Repo.GitRepo.CommitsCountBetween", err)
2015-09-02 09:26:56 -04:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["NumFiles"], err = c.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("Repo.GitRepo.FilesCountBetween", err)
2015-09-02 09:26:56 -04:00
return
}
}
func PrepareViewPullInfo(c *context.Context, issue *db.Issue) *git.PullRequestInfo {
2017-06-03 07:26:09 -04:00
repo := c.Repo.Repository
pull := issue.PullRequest
2015-09-02 04:08:05 -04:00
2017-06-03 07:26:09 -04:00
c.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
2015-09-02 04:08:05 -04:00
var (
headGitRepo *git.Repository
err error
)
if pull.HeadRepo != nil {
2015-11-26 17:33:45 -05:00
headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
if err != nil {
c.ServerError("OpenRepository", err)
return nil
}
2015-09-02 04:08:05 -04:00
}
if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
2017-06-03 07:26:09 -04:00
c.Data["IsPullReuqestBroken"] = true
c.Data["HeadTarget"] = "deleted"
c.Data["NumCommits"] = 0
c.Data["NumFiles"] = 0
2015-09-02 09:26:56 -04:00
return nil
}
prInfo, err := headGitRepo.GetPullRequestInfo(db.RepoPath(repo.Owner.Name, repo.Name),
pull.BaseBranch, pull.HeadBranch)
2015-09-02 04:08:05 -04:00
if err != nil {
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
2017-06-03 07:26:09 -04:00
c.Data["IsPullReuqestBroken"] = true
c.Data["BaseTarget"] = "deleted"
c.Data["NumCommits"] = 0
c.Data["NumFiles"] = 0
return nil
}
c.ServerError("GetPullRequestInfo", err)
2015-09-02 04:08:05 -04:00
return nil
}
2017-06-03 07:26:09 -04:00
c.Data["NumCommits"] = prInfo.Commits.Len()
c.Data["NumFiles"] = prInfo.NumFiles
2015-09-02 04:08:05 -04:00
return prInfo
}
2017-06-03 07:26:09 -04:00
func ViewPullCommits(c *context.Context) {
c.Data["PageIsPullList"] = true
c.Data["PageIsPullCommits"] = true
2015-09-02 04:08:05 -04:00
2017-06-03 07:26:09 -04:00
issue := checkPullInfo(c)
if c.Written() {
2015-09-02 04:08:05 -04:00
return
}
pull := issue.PullRequest
if pull.HeadRepo != nil {
2017-06-03 07:26:09 -04:00
c.Data["Username"] = pull.HeadUserName
c.Data["Reponame"] = pull.HeadRepo.Name
}
2015-09-02 04:08:05 -04:00
2015-09-02 09:26:56 -04:00
var commits *list.List
if pull.HasMerged {
2017-06-03 07:26:09 -04:00
PrepareMergedViewPullInfo(c, issue)
if c.Written() {
2015-09-02 09:26:56 -04:00
return
}
2017-06-03 07:26:09 -04:00
startCommit, err := c.Repo.GitRepo.GetCommit(pull.MergeBase)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("Repo.GitRepo.GetCommit", err)
2015-09-02 09:26:56 -04:00
return
}
2017-06-03 07:26:09 -04:00
endCommit, err := c.Repo.GitRepo.GetCommit(pull.MergedCommitID)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("Repo.GitRepo.GetCommit", err)
2015-09-02 09:26:56 -04:00
return
}
2017-06-03 07:26:09 -04:00
commits, err = c.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("Repo.GitRepo.CommitsBetween", err)
2015-09-02 09:26:56 -04:00
return
}
} else {
2017-06-03 07:26:09 -04:00
prInfo := PrepareViewPullInfo(c, issue)
if c.Written() {
2015-09-02 09:26:56 -04:00
return
} else if prInfo == nil {
2017-11-18 00:38:46 -05:00
c.NotFound()
2015-09-02 09:26:56 -04:00
return
}
commits = prInfo.Commits
2015-09-02 04:08:05 -04:00
}
commits = db.ValidateCommitsWithEmails(commits)
2017-06-03 07:26:09 -04:00
c.Data["Commits"] = commits
c.Data["CommitsCount"] = commits.Len()
2015-09-02 09:26:56 -04:00
c.Success(PULL_COMMITS)
2015-09-02 04:08:05 -04:00
}
2017-06-03 07:26:09 -04:00
func ViewPullFiles(c *context.Context) {
c.Data["PageIsPullList"] = true
c.Data["PageIsPullFiles"] = true
2015-09-02 04:08:05 -04:00
2017-06-03 07:26:09 -04:00
issue := checkPullInfo(c)
if c.Written() {
2015-09-02 04:08:05 -04:00
return
}
pull := issue.PullRequest
2015-09-02 04:08:05 -04:00
2015-09-02 09:26:56 -04:00
var (
diffRepoPath string
startCommitID string
endCommitID string
gitRepo *git.Repository
)
if pull.HasMerged {
2017-06-03 07:26:09 -04:00
PrepareMergedViewPullInfo(c, issue)
if c.Written() {
2015-09-02 09:26:56 -04:00
return
}
2015-09-02 04:08:05 -04:00
2017-06-03 07:26:09 -04:00
diffRepoPath = c.Repo.GitRepo.Path
2015-09-02 09:26:56 -04:00
startCommitID = pull.MergeBase
endCommitID = pull.MergedCommitID
2017-06-03 07:26:09 -04:00
gitRepo = c.Repo.GitRepo
2015-09-02 09:26:56 -04:00
} else {
2017-06-03 07:26:09 -04:00
prInfo := PrepareViewPullInfo(c, issue)
if c.Written() {
2015-09-02 09:26:56 -04:00
return
} else if prInfo == nil {
2017-06-03 07:26:09 -04:00
c.Handle(404, "ViewPullFiles", nil)
2015-09-02 09:26:56 -04:00
return
}
2015-09-02 04:08:05 -04:00
headRepoPath := db.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
2015-09-02 04:08:05 -04:00
2015-09-02 09:26:56 -04:00
headGitRepo, err := git.OpenRepository(headRepoPath)
if err != nil {
c.ServerError("OpenRepository", err)
2015-09-02 09:26:56 -04:00
return
}
headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
2015-09-02 09:26:56 -04:00
if err != nil {
c.ServerError("GetBranchCommitID", err)
2015-09-02 09:26:56 -04:00
return
}
diffRepoPath = headRepoPath
startCommitID = prInfo.MergeBase
endCommitID = headCommitID
gitRepo = headGitRepo
2015-09-02 04:08:05 -04:00
}
diff, err := db.GetDiffRange(diffRepoPath,
startCommitID, endCommitID, conf.Git.MaxGitDiffLines,
conf.Git.MaxGitDiffLineCharacters, conf.Git.MaxGitDiffFiles)
2015-09-02 04:08:05 -04:00
if err != nil {
c.ServerError("GetDiffRange", err)
2015-09-02 04:08:05 -04:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Diff"] = diff
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
2015-09-02 04:08:05 -04:00
2015-09-02 09:26:56 -04:00
commit, err := gitRepo.GetCommit(endCommitID)
2015-09-02 04:08:05 -04:00
if err != nil {
c.ServerError("GetCommit", err)
2015-09-02 04:08:05 -04:00
return
}
2017-06-03 07:26:09 -04:00
setEditorconfigIfExists(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsSplitStyle"] = c.Query("style") == "split"
c.Data["IsImageFile"] = commit.IsImageFile
2015-09-02 04:08:05 -04:00
// It is possible head repo has been deleted for merged pull requests
if pull.HeadRepo != nil {
2017-06-03 07:26:09 -04:00
c.Data["Username"] = pull.HeadUserName
c.Data["Reponame"] = pull.HeadRepo.Name
headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", endCommitID)
c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", startCommitID)
c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", endCommitID)
}
2017-06-03 07:26:09 -04:00
c.Data["RequireHighlightJS"] = true
c.Success(PULL_FILES)
2015-09-02 04:08:05 -04:00
}
2015-08-31 16:24:28 +09:00
2017-06-03 07:26:09 -04:00
func MergePullRequest(c *context.Context) {
issue := checkPullInfo(c)
if c.Written() {
2015-09-02 09:26:56 -04:00
return
}
if issue.IsClosed {
c.NotFound()
2015-09-02 09:26:56 -04:00
return
}
pr, err := db.GetPullRequestByIssueID(issue.ID)
2015-09-02 09:26:56 -04:00
if err != nil {
c.NotFoundOrServerError("GetPullRequestByIssueID", db.IsErrPullRequestNotExist, err)
2015-09-02 09:26:56 -04:00
return
}
2015-10-18 17:18:45 -04:00
if !pr.CanAutoMerge() || pr.HasMerged {
c.NotFound()
2015-09-02 09:26:56 -04:00
return
}
pr.Issue = issue
2017-06-03 07:26:09 -04:00
pr.Issue.Repo = c.Repo.Repository
if err = pr.Merge(c.User, c.Repo.GitRepo, db.MergeStyle(c.Query("merge_style")), c.Query("commit_description")); err != nil {
c.ServerError("Merge", err)
2015-09-02 09:26:56 -04:00
return
}
log.Trace("Pull request merged: %d", pr.ID)
2017-06-03 07:26:09 -04:00
c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
2015-09-02 09:26:56 -04:00
}
func ParseCompareInfo(c *context.Context) (*db.User, *db.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
2017-06-03 07:26:09 -04:00
baseRepo := c.Repo.Repository
// Get compared branches information
// format: <base branch>...[<head repo>:]<head branch>
// base<-head: master...head:feature
// same repo: master...feature
2017-06-03 07:26:09 -04:00
infos := strings.Split(c.Params("*"), "...")
2015-08-08 22:43:14 +08:00
if len(infos) != 2 {
log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
c.NotFound()
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
2015-08-08 22:43:14 +08:00
}
baseBranch := infos[0]
2017-06-03 07:26:09 -04:00
c.Data["BaseBranch"] = baseBranch
2015-08-08 22:43:14 +08:00
var (
headUser *db.User
headBranch string
isSameRepo bool
err error
)
// If there is no head repository, it means pull request between same repository.
2015-08-08 22:43:14 +08:00
headInfos := strings.Split(infos[1], ":")
if len(headInfos) == 1 {
isSameRepo = true
2017-06-03 07:26:09 -04:00
headUser = c.Repo.Owner
headBranch = headInfos[0]
2015-08-08 22:43:14 +08:00
} else if len(headInfos) == 2 {
headUser, err = db.GetUserByName(headInfos[0])
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return nil, nil, nil, nil, "", ""
2015-09-01 19:07:02 -04:00
}
headBranch = headInfos[1]
isSameRepo = headUser.ID == baseRepo.OwnerID
} else {
c.NotFound()
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
}
2017-06-03 07:26:09 -04:00
c.Data["HeadUser"] = headUser
c.Data["HeadBranch"] = headBranch
c.Repo.PullRequest.SameRepo = isSameRepo
2015-09-01 19:07:02 -04:00
// Check if base branch is valid.
2017-06-03 07:26:09 -04:00
if !c.Repo.GitRepo.IsBranchExist(baseBranch) {
c.NotFound()
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
}
2015-08-08 22:43:14 +08:00
var (
headRepo *db.Repository
headGitRepo *git.Repository
)
// In case user included redundant head user name for comparison in same repository,
// no need to check the fork relation.
if !isSameRepo {
var has bool
headRepo, has, err = db.HasForkedRepo(headUser.ID, baseRepo.ID)
if err != nil {
c.ServerError("HasForkedRepo", err)
return nil, nil, nil, nil, "", ""
} else if !has {
log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID)
c.NotFound()
return nil, nil, nil, nil, "", ""
}
2015-08-08 22:43:14 +08:00
headGitRepo, err = git.OpenRepository(db.RepoPath(headUser.Name, headRepo.Name))
if err != nil {
c.ServerError("OpenRepository", err)
return nil, nil, nil, nil, "", ""
}
} else {
2017-06-03 07:26:09 -04:00
headRepo = c.Repo.Repository
headGitRepo = c.Repo.GitRepo
2015-09-01 19:07:02 -04:00
}
2017-06-03 07:26:09 -04:00
if !c.User.IsWriterOfRepo(headRepo) && !c.User.IsAdmin {
log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have write access or site admin", baseRepo.ID)
c.NotFound()
return nil, nil, nil, nil, "", ""
}
2015-09-01 19:07:02 -04:00
// Check if head branch is valid.
if !headGitRepo.IsBranchExist(headBranch) {
c.NotFound()
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
2015-08-08 22:43:14 +08:00
}
2015-09-01 19:07:02 -04:00
2015-08-08 22:43:14 +08:00
headBranches, err := headGitRepo.GetBranches()
if err != nil {
c.ServerError("GetBranches", err)
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
2015-08-08 22:43:14 +08:00
}
2017-06-03 07:26:09 -04:00
c.Data["HeadBranches"] = headBranches
2015-08-08 22:43:14 +08:00
prInfo, err := headGitRepo.GetPullRequestInfo(db.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
2015-09-01 19:07:02 -04:00
if err != nil {
if git.IsErrNoMergeBase(err) {
c.Data["IsNoMergeBase"] = true
c.Success(COMPARE_PULL)
} else {
c.ServerError("GetPullRequestInfo", err)
}
2015-09-01 19:07:02 -04:00
return nil, nil, nil, nil, "", ""
}
2017-06-03 07:26:09 -04:00
c.Data["BeforeCommitID"] = prInfo.MergeBase
2015-09-01 19:07:02 -04:00
return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
}
func PrepareCompareDiff(
c *context.Context,
headUser *db.User,
headRepo *db.Repository,
2015-09-01 19:07:02 -04:00
headGitRepo *git.Repository,
prInfo *git.PullRequestInfo,
baseBranch, headBranch string) bool {
2015-09-01 19:07:02 -04:00
var (
repo = c.Repo.Repository
2015-09-01 19:07:02 -04:00
err error
)
// Get diff information.
c.Data["CommitRepoLink"] = headRepo.Link()
2015-09-01 19:07:02 -04:00
headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
2015-09-01 19:07:02 -04:00
if err != nil {
c.ServerError("GetBranchCommitID", err)
return false
2015-09-01 19:07:02 -04:00
}
c.Data["AfterCommitID"] = headCommitID
2015-09-01 19:07:02 -04:00
if headCommitID == prInfo.MergeBase {
c.Data["IsNothingToCompare"] = true
return true
}
diff, err := db.GetDiffRange(db.RepoPath(headUser.Name, headRepo.Name),
prInfo.MergeBase, headCommitID, conf.Git.MaxGitDiffLines,
conf.Git.MaxGitDiffLineCharacters, conf.Git.MaxGitDiffFiles)
2015-09-01 19:07:02 -04:00
if err != nil {
c.ServerError("GetDiffRange", err)
return false
2015-09-01 19:07:02 -04:00
}
c.Data["Diff"] = diff
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
2015-09-01 19:07:02 -04:00
headCommit, err := headGitRepo.GetCommit(headCommitID)
if err != nil {
c.ServerError("GetCommit", err)
return false
2015-09-01 19:07:02 -04:00
}
prInfo.Commits = db.ValidateCommitsWithEmails(prInfo.Commits)
c.Data["Commits"] = prInfo.Commits
c.Data["CommitCount"] = prInfo.Commits.Len()
c.Data["Username"] = headUser.Name
c.Data["Reponame"] = headRepo.Name
c.Data["IsImageFile"] = headCommit.IsImageFile
2015-09-02 04:08:05 -04:00
headTarget := path.Join(headUser.Name, repo.Name)
c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", headCommitID)
c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(headTarget, "raw", headCommitID)
return false
2015-09-01 19:07:02 -04:00
}
2017-06-03 07:26:09 -04:00
func CompareAndPullRequest(c *context.Context) {
c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
c.Data["PageIsComparePull"] = true
c.Data["IsDiffCompare"] = true
c.Data["RequireHighlightJS"] = true
setTemplateIfExists(c, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
renderAttachmentSettings(c)
2015-09-01 19:07:02 -04:00
2017-06-03 07:26:09 -04:00
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
if c.Written() {
2015-09-01 19:07:02 -04:00
return
}
pr, err := db.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch)
if err != nil {
if !db.IsErrPullRequestNotExist(err) {
c.ServerError("GetUnmergedPullRequest", err)
return
}
} else {
2017-06-03 07:26:09 -04:00
c.Data["HasPullRequest"] = true
c.Data["PullRequest"] = pr
c.Success(COMPARE_PULL)
return
}
2015-09-01 19:07:02 -04:00
2017-06-03 07:26:09 -04:00
nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
if c.Written() {
2015-08-31 16:24:28 +09:00
return
}
if !nothingToCompare {
// Setup information for new form.
2017-06-03 07:26:09 -04:00
RetrieveRepoMetas(c, c.Repo.Repository)
if c.Written() {
return
}
}
2017-06-03 07:26:09 -04:00
setEditorconfigIfExists(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
c.Data["IsSplitStyle"] = c.Query("style") == "split"
setTemplateIfExists(c, PULL_REQUEST_TITLE_TEMPLATE_KEY, PullRequestTitleTemplateCandidates)
if c.Data[PULL_REQUEST_TITLE_TEMPLATE_KEY] != nil {
customTitle := c.Data[PULL_REQUEST_TITLE_TEMPLATE_KEY].(string)
r := strings.NewReplacer("{{headBranch}}", headBranch, "{{baseBranch}}", baseBranch)
c.Data["title"] = r.Replace(customTitle)
}
c.Success(COMPARE_PULL)
2015-08-08 22:43:14 +08:00
}
2017-06-03 07:26:09 -04:00
func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
c.Data["PageIsComparePull"] = true
c.Data["IsDiffCompare"] = true
c.Data["RequireHighlightJS"] = true
renderAttachmentSettings(c)
2015-09-01 19:07:02 -04:00
var (
2017-06-03 07:26:09 -04:00
repo = c.Repo.Repository
2015-09-01 19:07:02 -04:00
attachments []string
)
2017-06-03 07:26:09 -04:00
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
if c.Written() {
2015-09-01 19:07:02 -04:00
return
}
2017-06-03 07:26:09 -04:00
labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f)
if c.Written() {
2015-09-01 19:07:02 -04:00
return
}
if conf.AttachmentEnabled {
attachments = f.Files
2015-09-01 19:07:02 -04:00
}
2017-06-03 07:26:09 -04:00
if c.HasError() {
form.Assign(f, c.Data)
// This stage is already stop creating new pull request, so it does not matter if it has
// something to compare or not.
2017-06-03 07:26:09 -04:00
PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
if c.Written() {
return
}
c.Success(COMPARE_PULL)
2015-09-01 19:07:02 -04:00
return
}
patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
if err != nil {
c.ServerError("GetPatch", err)
return
}
pullIssue := &db.Issue{
2015-09-01 19:07:02 -04:00
RepoID: repo.ID,
2015-09-03 03:49:50 -04:00
Index: repo.NextIssueIndex(),
Title: f.Title,
2017-06-03 07:26:09 -04:00
PosterID: c.User.ID,
Poster: c.User,
2015-09-01 19:07:02 -04:00
MilestoneID: milestoneID,
AssigneeID: assigneeID,
IsPull: true,
Content: f.Content,
2015-09-01 19:07:02 -04:00
}
pullRequest := &db.PullRequest{
2015-09-01 19:26:39 -04:00
HeadRepoID: headRepo.ID,
BaseRepoID: repo.ID,
HeadUserName: headUser.Name,
HeadBranch: headBranch,
2015-09-01 19:26:39 -04:00
BaseBranch: baseBranch,
HeadRepo: headRepo,
BaseRepo: repo,
2015-09-01 19:26:39 -04:00
MergeBase: prInfo.MergeBase,
Type: db.PULL_REQUEST_GOGS,
}
// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
// instead of 500.
if err := db.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
c.ServerError("NewPullRequest", err)
2015-09-01 19:07:02 -04:00
return
} else if err := pullRequest.PushToBaseRepo(); err != nil {
c.ServerError("PushToBaseRepo", err)
return
}
log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
2017-06-03 07:26:09 -04:00
c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
2014-03-24 18:25:15 +08:00
}
func parseOwnerAndRepo(c *context.Context) (*db.User, *db.Repository) {
owner, err := db.GetUserByName(c.Params(":username"))
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
return nil, nil
}
repo, err := db.GetRepositoryByName(owner.ID, c.Params(":reponame"))
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
return nil, nil
}
return owner, repo
}
2017-06-03 07:26:09 -04:00
func TriggerTask(c *context.Context) {
pusherID := c.QueryInt64("pusher")
branch := c.Query("branch")
secret := c.Query("secret")
if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
2017-06-03 07:26:09 -04:00
c.Error(404)
log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
2015-12-14 17:06:54 -05:00
return
}
2017-06-03 07:26:09 -04:00
owner, repo := parseOwnerAndRepo(c)
if c.Written() {
return
}
if secret != tool.MD5(owner.Salt) {
2017-06-03 07:26:09 -04:00
c.Error(404)
2015-12-14 17:06:54 -05:00
log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
return
}
pusher, err := db.GetUserByID(pusherID)
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetUserByID", errors.IsUserNotExist, err)
return
}
log.Trace("TriggerTask '%s/%s' by '%s'", repo.Name, branch, pusher.Name)
go db.HookQueue.Add(repo.ID)
go db.AddTestPullRequestTask(pusher, repo.ID, branch, true)
2017-06-03 07:26:09 -04:00
c.Status(202)
}