2018-01-05 11:56:52 +01:00
|
|
|
// Copyright 2018 The Gitea Authors.
|
|
|
|
|
// Copyright 2014 The Gogs Authors.
|
|
|
|
|
// All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-03-24 18:25:15 +08:00
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-07 22:52:59 +08:00
|
|
|
"errors"
|
2016-10-12 10:28:51 -03:00
|
|
|
"fmt"
|
2021-11-16 18:18:25 +00:00
|
|
|
"html"
|
2019-12-16 07:20:25 +01:00
|
|
|
"net/http"
|
2021-11-16 18:18:25 +00:00
|
|
|
"strconv"
|
2015-08-08 22:43:14 +08:00
|
|
|
"strings"
|
2020-01-17 07:03:40 +01:00
|
|
|
"time"
|
2015-08-08 22:43:14 +08:00
|
|
|
|
2022-08-25 10:31:57 +08:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2021-09-24 19:32:56 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-11 18:09:36 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2022-06-11 16:44:20 +02:00
|
|
|
pull_model "code.gitea.io/gitea/models/pull"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2026-01-15 09:25:14 +11:00
|
|
|
"code.gitea.io/gitea/modules/commitstatus"
|
2024-02-25 06:34:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/emoji"
|
2025-04-29 10:51:32 +08:00
|
|
|
"code.gitea.io/gitea/modules/fileicon"
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2025-09-15 23:33:12 -07:00
|
|
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2025-09-14 02:01:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/glob"
|
2025-06-05 01:09:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2022-09-02 15:58:49 +08:00
|
|
|
issue_template "code.gitea.io/gitea/modules/issue/template"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2026-01-03 10:36:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-12-22 23:33:19 +08:00
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2026-01-15 09:25:14 +11:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2019-01-21 12:45:32 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-02-22 13:08:48 +00:00
|
|
|
"code.gitea.io/gitea/routers/utils"
|
2024-09-12 06:53:40 +03:00
|
|
|
shared_user "code.gitea.io/gitea/routers/web/shared/user"
|
2025-10-20 04:46:37 -06:00
|
|
|
actions_service "code.gitea.io/gitea/services/actions"
|
2022-03-31 16:53:08 +02:00
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2022-06-11 16:44:20 +02:00
|
|
|
"code.gitea.io/gitea/services/automerge"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
|
"code.gitea.io/gitea/services/context/upload"
|
2021-04-06 20:44:05 +01:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2025-12-25 17:51:30 -08:00
|
|
|
git_service "code.gitea.io/gitea/services/git"
|
2019-09-06 10:20:09 +08:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-09-27 08:22:36 +08:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
2019-10-26 14:54:11 +08:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2024-03-04 09:16:03 +01:00
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2014-03-24 18:25:15 +08:00
|
|
|
)
|
|
|
|
|
|
2014-06-22 23:11:12 -04:00
|
|
|
const (
|
2024-12-22 23:33:19 +08:00
|
|
|
tplCompareDiff templates.TplName = "repo/diff/compare"
|
|
|
|
|
tplPullCommits templates.TplName = "repo/pulls/commits"
|
|
|
|
|
tplPullFiles templates.TplName = "repo/pulls/files"
|
2016-02-17 20:21:31 -02:00
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
pullRequestTemplateKey = "PullRequestTemplate"
|
2016-02-17 20:21:31 -02:00
|
|
|
)
|
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
var pullRequestTemplateCandidates = []string{
|
|
|
|
|
"PULL_REQUEST_TEMPLATE.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
"PULL_REQUEST_TEMPLATE.yaml",
|
|
|
|
|
"PULL_REQUEST_TEMPLATE.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
"pull_request_template.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
"pull_request_template.yaml",
|
|
|
|
|
"pull_request_template.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
".gitea/PULL_REQUEST_TEMPLATE.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
".gitea/PULL_REQUEST_TEMPLATE.yaml",
|
|
|
|
|
".gitea/PULL_REQUEST_TEMPLATE.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
".gitea/pull_request_template.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
".gitea/pull_request_template.yaml",
|
|
|
|
|
".gitea/pull_request_template.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
".github/PULL_REQUEST_TEMPLATE.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
".github/PULL_REQUEST_TEMPLATE.yaml",
|
|
|
|
|
".github/PULL_REQUEST_TEMPLATE.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
".github/pull_request_template.md",
|
2022-09-02 15:58:49 +08:00
|
|
|
".github/pull_request_template.yaml",
|
|
|
|
|
".github/pull_request_template.yml",
|
2022-01-20 18:46:10 +01:00
|
|
|
}
|
2014-06-22 23:11:12 -04:00
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
func getRepository(ctx *context.Context, repoID int64) *repo_model.Repository {
|
2022-12-03 10:48:26 +08:00
|
|
|
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
|
2015-08-08 17:10:34 +08:00
|
|
|
if err != nil {
|
2021-12-10 09:27:50 +08:00
|
|
|
if repo_model.IsErrRepoNotExist(err) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2015-08-08 17:10:34 +08:00
|
|
|
} else {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetRepositoryByID", err)
|
2015-08-08 17:10:34 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2015-09-01 11:57:02 -04:00
|
|
|
|
2026-03-29 11:21:14 +02:00
|
|
|
perm, err := access_model.GetDoerRepoPermission(ctx, repo, ctx.Doer)
|
2018-11-28 19:26:14 +08:00
|
|
|
if err != nil {
|
2026-03-29 11:21:14 +02:00
|
|
|
ctx.ServerError("GetDoerRepoPermission", err)
|
2018-11-28 19:26:14 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 03:57:58 +08:00
|
|
|
if !perm.CanRead(unit.TypeCode) {
|
2019-11-11 09:15:29 -06:00
|
|
|
log.Trace("Permission Denied: User %-v cannot read %-v of repo %-v\n"+
|
|
|
|
|
"User in repo has Permissions: %-+v",
|
2022-03-22 08:03:22 +01:00
|
|
|
ctx.Doer,
|
2021-11-10 03:57:58 +08:00
|
|
|
unit.TypeCode,
|
2019-11-11 09:15:29 -06:00
|
|
|
ctx.Repo,
|
|
|
|
|
perm)
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2019-11-11 09:15:29 -06:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return repo
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 11:43:18 +08:00
|
|
|
func getPullInfo(ctx *context.Context) (issue *issues_model.Issue, ok bool) {
|
2024-12-24 21:47:45 +08:00
|
|
|
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
|
2015-09-02 04:08:05 -04:00
|
|
|
if err != nil {
|
2022-06-13 17:37:59 +08:00
|
|
|
if issues_model.IsErrIssueNotExist(err) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(err)
|
2015-09-02 04:08:05 -04:00
|
|
|
} else {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetIssueByIndex", err)
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPoster(ctx); err != nil {
|
2018-12-13 23:55:43 +08:00
|
|
|
ctx.ServerError("LoadPoster", err)
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2018-12-13 23:55:43 +08:00
|
|
|
}
|
2022-04-08 17:11:15 +08:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-10-23 12:54:13 -05:00
|
|
|
ctx.ServerError("LoadRepo", err)
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2019-10-23 12:54:13 -05:00
|
|
|
}
|
2024-02-25 06:34:51 +08:00
|
|
|
ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, emoji.ReplaceAliases(issue.Title))
|
2015-10-18 19:30:39 -04:00
|
|
|
ctx.Data["Issue"] = issue
|
2015-09-02 04:08:05 -04:00
|
|
|
|
2015-10-18 19:30:39 -04:00
|
|
|
if !issue.IsPull {
|
2025-10-19 00:37:50 +08:00
|
|
|
ctx.Redirect(issue.Link())
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2018-12-13 23:55:43 +08:00
|
|
|
ctx.ServerError("LoadPullRequest", err)
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2018-12-13 23:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.PullRequest.LoadHeadRepo(ctx); err != nil {
|
2020-03-03 06:31:55 +08:00
|
|
|
ctx.ServerError("LoadHeadRepo", err)
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.IsSigned {
|
|
|
|
|
// Update issue-user.
|
2022-08-25 10:31:57 +08:00
|
|
|
if err = activities_model.SetIssueReadBy(ctx, issue.ID, ctx.Doer.ID); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("ReadBy", err)
|
2023-08-07 11:43:18 +08:00
|
|
|
return nil, false
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 11:43:18 +08:00
|
|
|
return issue, true
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if ctx.Repo.Owner.Name == pull.MustHeadUserName(ctx) {
|
2017-10-05 02:35:01 +09:00
|
|
|
ctx.Data["HeadTarget"] = pull.HeadBranch
|
|
|
|
|
} else if pull.HeadRepo == nil {
|
2022-11-19 09:12:33 +01:00
|
|
|
ctx.Data["HeadTarget"] = pull.MustHeadUserName(ctx) + ":" + pull.HeadBranch
|
2017-10-05 02:35:01 +09:00
|
|
|
} else {
|
2022-11-19 09:12:33 +01:00
|
|
|
ctx.Data["HeadTarget"] = pull.MustHeadUserName(ctx) + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch
|
2017-10-05 02:35:01 +09:00
|
|
|
}
|
|
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
2024-09-24 12:14:57 +08:00
|
|
|
headBranchLink := ""
|
|
|
|
|
if pull.Flow == issues_model.PullRequestFlowGithub {
|
2024-10-10 08:25:46 +08:00
|
|
|
b, err := git_model.GetBranch(ctx, pull.HeadRepoID, pull.HeadBranch)
|
2024-09-24 12:14:57 +08:00
|
|
|
switch {
|
|
|
|
|
case err == nil:
|
|
|
|
|
if !b.IsDeleted {
|
|
|
|
|
headBranchLink = pull.GetHeadBranchLink(ctx)
|
|
|
|
|
}
|
|
|
|
|
case !git_model.IsErrBranchNotExist(err):
|
|
|
|
|
log.Error("GetBranch: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ctx.Data["HeadBranchLink"] = headBranchLink
|
2023-10-11 06:24:07 +02:00
|
|
|
ctx.Data["BaseBranchLink"] = pull.GetBaseBranchLink(ctx)
|
2017-10-05 02:35:01 +09:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:00:28 +08:00
|
|
|
// GetPullDiffStats get Pull Requests diff stats
|
|
|
|
|
func GetPullDiffStats(ctx *context.Context) {
|
2025-04-18 22:56:50 +08:00
|
|
|
// FIXME: this getPullInfo seems to be a duplicate call with other route handlers
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-08-16 10:19:09 -07:00
|
|
|
pull := issue.PullRequest
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2023-07-03 09:00:28 +08:00
|
|
|
mergeBaseCommitID := GetMergedBaseCommitID(ctx, issue)
|
2023-08-07 11:43:18 +08:00
|
|
|
if mergeBaseCommitID == "" {
|
2025-04-18 22:56:50 +08:00
|
|
|
return // no merge base, do nothing, do not stop the route handler, see below
|
2023-07-03 09:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 22:56:50 +08:00
|
|
|
// do not report 500 server error to end users if error occurs, otherwise a PR missing ref won't be able to view.
|
2025-07-16 21:33:33 +08:00
|
|
|
headCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
2023-07-03 09:00:28 +08:00
|
|
|
if err != nil {
|
2025-04-18 22:56:50 +08:00
|
|
|
log.Error("Failed to GetRefCommitID: %v, repo: %v", err, ctx.Repo.Repository.FullName())
|
2023-07-03 09:00:28 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-09-26 10:14:20 -07:00
|
|
|
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, mergeBaseCommitID, headCommitID)
|
2023-07-03 09:00:28 +08:00
|
|
|
if err != nil {
|
2025-04-18 22:56:50 +08:00
|
|
|
log.Error("Failed to GetDiffShortStat: %v, repo: %v", err, ctx.Repo.Repository.FullName())
|
2023-07-03 09:00:28 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-08 17:36:08 +08:00
|
|
|
ctx.Data["DiffShortStat"] = diffShortStat
|
2023-07-03 09:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetMergedBaseCommitID(ctx *context.Context, issue *issues_model.Issue) string {
|
|
|
|
|
pull := issue.PullRequest
|
2017-10-05 02:35:01 +09:00
|
|
|
|
2021-12-23 09:32:29 +01:00
|
|
|
var baseCommit string
|
|
|
|
|
// Some migrated PR won't have any Base SHA and lose history, try to get one
|
|
|
|
|
if pull.MergeBase == "" {
|
|
|
|
|
var commitSHA, parentCommit string
|
|
|
|
|
// If there is a head or a patch file, and it is readable, grab info
|
2025-07-16 21:33:33 +08:00
|
|
|
commitSHA, err := ctx.Repo.GitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
2021-12-23 09:32:29 +01:00
|
|
|
if err != nil {
|
|
|
|
|
// Head File does not exist, try the patch
|
|
|
|
|
commitSHA, err = ctx.Repo.GitRepo.ReadPatchCommit(pull.Index)
|
|
|
|
|
if err == nil {
|
|
|
|
|
// Recreate pull head in files for next time
|
2025-09-19 08:04:18 -07:00
|
|
|
if err := gitrepo.UpdateRef(ctx, ctx.Repo.Repository, pull.GetGitHeadRefName(), commitSHA); err != nil {
|
2021-12-23 09:32:29 +01:00
|
|
|
log.Error("Could not write head file", err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// There is no history available
|
|
|
|
|
log.Trace("No history file available for PR %d", pull.Index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if commitSHA != "" {
|
|
|
|
|
// Get immediate parent of the first commit in the patch, grab history back
|
2026-01-19 07:10:33 +08:00
|
|
|
parentCommit, _, err = gitrepo.RunCmdString(ctx, ctx.Repo.Repository,
|
2025-10-07 02:06:51 -07:00
|
|
|
gitcmd.NewCommand("rev-list", "-1", "--skip=1").AddDynamicArguments(commitSHA))
|
2021-12-23 09:32:29 +01:00
|
|
|
if err == nil {
|
|
|
|
|
parentCommit = strings.TrimSpace(parentCommit)
|
|
|
|
|
}
|
|
|
|
|
// Special case on Git < 2.25 that doesn't fail on immediate empty history
|
|
|
|
|
if err != nil || parentCommit == "" {
|
|
|
|
|
log.Info("No known parent commit for PR %d, error: %v", pull.Index, err)
|
|
|
|
|
// bring at least partial history if it can work
|
|
|
|
|
parentCommit = commitSHA
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
baseCommit = parentCommit
|
|
|
|
|
} else {
|
|
|
|
|
// Keep an empty history or original commit
|
|
|
|
|
baseCommit = pull.MergeBase
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:00:28 +08:00
|
|
|
return baseCommit
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-25 17:51:30 -08:00
|
|
|
func preparePullViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git_service.CompareInfo {
|
2024-12-07 20:39:35 -08:00
|
|
|
if !issue.IsPull {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if issue.PullRequest.HasMerged {
|
|
|
|
|
return prepareMergedViewPullInfo(ctx, issue)
|
|
|
|
|
}
|
|
|
|
|
return prepareViewPullInfo(ctx, issue)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prepareMergedViewPullInfo show meta information for a merged pull request view page
|
2025-12-25 17:51:30 -08:00
|
|
|
func prepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git_service.CompareInfo {
|
2023-07-03 09:00:28 +08:00
|
|
|
pull := issue.PullRequest
|
|
|
|
|
|
|
|
|
|
setMergeTarget(ctx, pull)
|
|
|
|
|
ctx.Data["HasMerged"] = true
|
|
|
|
|
|
|
|
|
|
baseCommit := GetMergedBaseCommitID(ctx, issue)
|
|
|
|
|
|
2025-12-25 17:51:30 -08:00
|
|
|
compareInfo, err := git_service.GetCompareInfo(ctx, ctx.Repo.Repository, ctx.Repo.Repository, ctx.Repo.GitRepo,
|
|
|
|
|
git.RefName(baseCommit), git.RefName(pull.GetGitHeadRefName()), false, false)
|
2015-09-02 09:26:56 -04:00
|
|
|
if err != nil {
|
2026-02-24 10:25:34 +06:00
|
|
|
if gitcmd.IsStdErrorNotValidObjectName(err) || strings.Contains(err.Error(), "unknown revision or path not in the working tree") {
|
2018-08-01 05:00:35 +02:00
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
2020-03-03 06:31:55 +08:00
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
2018-01-19 08:18:51 +02:00
|
|
|
ctx.Data["NumCommits"] = 0
|
|
|
|
|
ctx.Data["NumFiles"] = 0
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 22:29:29 +02:00
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
2018-01-19 08:18:51 +02:00
|
|
|
return nil
|
2015-09-02 09:26:56 -04:00
|
|
|
}
|
2021-08-09 20:08:51 +02:00
|
|
|
ctx.Data["NumCommits"] = len(compareInfo.Commits)
|
2019-06-12 01:32:08 +02:00
|
|
|
ctx.Data["NumFiles"] = compareInfo.NumFiles
|
2020-12-18 13:37:55 +01:00
|
|
|
|
2021-08-09 20:08:51 +02:00
|
|
|
if len(compareInfo.Commits) != 0 {
|
|
|
|
|
sha := compareInfo.Commits[0].ID.String()
|
2025-05-27 03:00:22 +08:00
|
|
|
commitStatuses, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, sha, db.ListOptionsAll)
|
2020-12-18 13:37:55 +01:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetLatestCommitStatus", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-07-28 23:11:40 +08:00
|
|
|
if !ctx.Repo.CanRead(unit.TypeActions) {
|
|
|
|
|
git_model.CommitStatusesHideActionsURL(ctx, commitStatuses)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 13:37:55 +01:00
|
|
|
if len(commitStatuses) != 0 {
|
|
|
|
|
ctx.Data["LatestCommitStatuses"] = commitStatuses
|
2022-06-12 23:51:54 +08:00
|
|
|
ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(commitStatuses)
|
2020-12-18 13:37:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 01:32:08 +02:00
|
|
|
return compareInfo
|
2015-09-02 09:26:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-20 04:46:37 -06:00
|
|
|
type pullCommitStatusCheckData struct {
|
|
|
|
|
MissingRequiredChecks []string // list of missing required checks
|
|
|
|
|
IsContextRequired func(string) bool // function to check whether a context is required
|
|
|
|
|
RequireApprovalRunCount int // number of workflow runs that require approval
|
|
|
|
|
CanApprove bool // whether the user can approve workflow runs
|
|
|
|
|
ApproveLink string // link to approve all checks
|
2026-01-15 09:25:14 +11:00
|
|
|
RequiredChecksState commitstatus.CommitStatusState
|
|
|
|
|
LatestCommitStatus *git_model.CommitStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *pullCommitStatusCheckData) CommitStatusCheckPrompt(locale translation.Locale) string {
|
|
|
|
|
if d.RequiredChecksState.IsPending() || len(d.MissingRequiredChecks) > 0 {
|
|
|
|
|
return locale.TrString("repo.pulls.status_checking")
|
|
|
|
|
} else if d.RequiredChecksState.IsSuccess() {
|
|
|
|
|
if d.LatestCommitStatus != nil && d.LatestCommitStatus.State.IsFailure() {
|
|
|
|
|
return locale.TrString("repo.pulls.status_checks_failure_optional")
|
|
|
|
|
}
|
|
|
|
|
return locale.TrString("repo.pulls.status_checks_success")
|
|
|
|
|
} else if d.RequiredChecksState.IsWarning() {
|
|
|
|
|
return locale.TrString("repo.pulls.status_checks_warning")
|
|
|
|
|
} else if d.RequiredChecksState.IsFailure() {
|
|
|
|
|
return locale.TrString("repo.pulls.status_checks_failure_required")
|
|
|
|
|
} else if d.RequiredChecksState.IsError() {
|
|
|
|
|
return locale.TrString("repo.pulls.status_checks_error")
|
|
|
|
|
}
|
|
|
|
|
return locale.TrString("repo.pulls.status_checking")
|
2025-10-20 04:46:37 -06:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 05:57:08 +00:00
|
|
|
func getViewPullHeadBranchInfo(ctx *context.Context, pull *issues_model.PullRequest, baseGitRepo *git.Repository) (headCommitID string, headCommitExists bool, err error) {
|
|
|
|
|
if pull.HeadRepo == nil {
|
|
|
|
|
return "", false, nil
|
|
|
|
|
}
|
|
|
|
|
headGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pull.HeadRepo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false, util.Iif(errors.Is(err, util.ErrNotExist), nil, err)
|
|
|
|
|
}
|
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
|
|
if pull.Flow == issues_model.PullRequestFlowGithub {
|
|
|
|
|
headCommitExists, _ = git_model.IsBranchExist(ctx, pull.HeadRepo.ID, pull.HeadBranch)
|
|
|
|
|
} else {
|
|
|
|
|
headCommitExists = gitrepo.IsReferenceExist(ctx, pull.BaseRepo, pull.GetGitHeadRefName())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if headCommitExists {
|
|
|
|
|
if pull.Flow != issues_model.PullRequestFlowGithub {
|
|
|
|
|
headCommitID, err = baseGitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
|
|
|
|
} else {
|
|
|
|
|
headCommitID, err = headGitRepo.GetBranchCommitID(pull.HeadBranch)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", false, util.Iif(errors.Is(err, util.ErrNotExist), nil, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return headCommitID, headCommitExists, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 20:39:35 -08:00
|
|
|
// prepareViewPullInfo show meta information for a pull request preview page
|
2025-12-25 17:51:30 -08:00
|
|
|
func prepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git_service.CompareInfo {
|
2022-02-11 08:02:53 +00:00
|
|
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
|
|
|
|
|
2015-09-02 04:08:05 -04:00
|
|
|
repo := ctx.Repo.Repository
|
2016-08-16 10:19:09 -07:00
|
|
|
pull := issue.PullRequest
|
2015-09-02 04:08:05 -04:00
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pull.LoadHeadRepo(ctx); err != nil {
|
2020-03-03 06:31:55 +08:00
|
|
|
ctx.ServerError("LoadHeadRepo", err)
|
2015-10-24 03:36:47 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pull.LoadBaseRepo(ctx); err != nil {
|
2020-03-03 06:31:55 +08:00
|
|
|
ctx.ServerError("LoadBaseRepo", err)
|
2020-01-07 17:06:14 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 02:35:01 +09:00
|
|
|
setMergeTarget(ctx, pull)
|
|
|
|
|
|
2023-01-16 16:00:22 +08:00
|
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, pull.BaseBranch)
|
|
|
|
|
if err != nil {
|
2020-01-17 07:03:40 +01:00
|
|
|
ctx.ServerError("LoadProtectedBranch", err)
|
2019-09-18 13:39:45 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2023-01-16 16:00:22 +08:00
|
|
|
ctx.Data["EnableStatusCheck"] = pb != nil && pb.EnableStatusCheck
|
2019-09-18 13:39:45 +08:00
|
|
|
|
2022-01-19 23:26:57 +00:00
|
|
|
var baseGitRepo *git.Repository
|
|
|
|
|
if pull.BaseRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
|
|
|
|
|
baseGitRepo = ctx.Repo.GitRepo
|
|
|
|
|
} else {
|
2024-01-28 04:09:51 +08:00
|
|
|
baseGitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo)
|
2022-01-19 23:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
defer baseGitRepo.Close()
|
2020-01-07 17:06:14 +00:00
|
|
|
}
|
2020-03-05 18:51:21 +00:00
|
|
|
|
2026-01-15 09:25:14 +11:00
|
|
|
statusCheckData := &pullCommitStatusCheckData{}
|
|
|
|
|
|
2025-10-25 10:08:25 -07:00
|
|
|
if exist, _ := git_model.IsBranchExist(ctx, pull.BaseRepo.ID, pull.BaseBranch); !exist {
|
2024-11-21 09:04:18 -08:00
|
|
|
ctx.Data["BaseBranchNotExist"] = true
|
2020-03-05 18:51:21 +00:00
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
|
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
|
|
|
|
ctx.Data["HeadTarget"] = pull.HeadBranch
|
2020-03-31 14:42:44 +01:00
|
|
|
|
2025-07-16 21:33:33 +08:00
|
|
|
sha, err := baseGitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
2020-03-31 14:42:44 +01:00
|
|
|
if err != nil {
|
2025-07-16 21:33:33 +08:00
|
|
|
ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitHeadRefName()), err)
|
2020-03-31 14:42:44 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-05-27 03:00:22 +08:00
|
|
|
commitStatuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll)
|
2020-03-31 14:42:44 +01:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetLatestCommitStatus", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-07-28 23:11:40 +08:00
|
|
|
if !ctx.Repo.CanRead(unit.TypeActions) {
|
|
|
|
|
git_model.CommitStatusesHideActionsURL(ctx, commitStatuses)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 09:25:14 +11:00
|
|
|
statusCheckData.LatestCommitStatus = git_model.CalcCommitStatus(commitStatuses)
|
2020-03-31 14:42:44 +01:00
|
|
|
if len(commitStatuses) > 0 {
|
|
|
|
|
ctx.Data["LatestCommitStatuses"] = commitStatuses
|
2026-01-15 09:25:14 +11:00
|
|
|
ctx.Data["LatestCommitStatus"] = statusCheckData.LatestCommitStatus
|
2020-03-31 14:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-25 17:51:30 -08:00
|
|
|
compareInfo, err := git_service.GetCompareInfo(ctx, pull.BaseRepo, pull.BaseRepo, baseGitRepo,
|
|
|
|
|
git.RefName(pull.MergeBase), git.RefName(pull.GetGitHeadRefName()), false, false)
|
2020-03-31 14:42:44 +01:00
|
|
|
if err != nil {
|
2026-02-24 10:25:34 +06:00
|
|
|
if gitcmd.IsStdErrorNotValidObjectName(err) {
|
2020-03-31 14:42:44 +01:00
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
|
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
|
|
|
|
ctx.Data["NumCommits"] = 0
|
|
|
|
|
ctx.Data["NumFiles"] = 0
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 20:08:51 +02:00
|
|
|
ctx.Data["NumCommits"] = len(compareInfo.Commits)
|
2020-03-31 14:42:44 +01:00
|
|
|
ctx.Data["NumFiles"] = compareInfo.NumFiles
|
|
|
|
|
return compareInfo
|
2020-03-05 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 05:57:08 +00:00
|
|
|
headBranchSha, headBranchExist, err := getViewPullHeadBranchInfo(ctx, pull, baseGitRepo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("getViewPullHeadBranchInfo", err)
|
|
|
|
|
return nil
|
2020-01-07 17:06:14 +00:00
|
|
|
}
|
2019-06-30 15:57:59 +08:00
|
|
|
|
2020-01-25 10:48:22 +08:00
|
|
|
if headBranchExist {
|
2022-01-19 23:26:57 +00:00
|
|
|
var err error
|
2022-04-28 13:48:48 +02:00
|
|
|
ctx.Data["UpdateAllowed"], ctx.Data["UpdateByRebaseAllowed"], err = pull_service.IsUserAllowedToUpdate(ctx, pull, ctx.Doer)
|
2020-01-25 10:48:22 +08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("IsUserAllowedToUpdate", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-01-19 23:26:57 +00:00
|
|
|
ctx.Data["GetCommitMessages"] = pull_service.GetSquashMergeCommitMessages(ctx, pull)
|
2022-08-03 12:56:59 +08:00
|
|
|
} else {
|
|
|
|
|
ctx.Data["GetCommitMessages"] = ""
|
2020-01-25 10:48:22 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 21:33:33 +08:00
|
|
|
sha, err := baseGitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
2020-01-07 17:06:14 +00:00
|
|
|
if err != nil {
|
2020-06-08 19:07:41 +01:00
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
|
|
|
|
if pull.IsSameRepo() {
|
|
|
|
|
ctx.Data["HeadTarget"] = pull.HeadBranch
|
|
|
|
|
} else if pull.HeadRepo == nil {
|
2023-08-04 07:07:15 +09:00
|
|
|
ctx.Data["HeadTarget"] = ctx.Locale.Tr("repo.pull.deleted_branch", pull.HeadBranch)
|
2020-06-08 19:07:41 +01:00
|
|
|
} else {
|
|
|
|
|
ctx.Data["HeadTarget"] = pull.HeadRepo.OwnerName + ":" + pull.HeadBranch
|
|
|
|
|
}
|
|
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
|
|
|
|
ctx.Data["NumCommits"] = 0
|
|
|
|
|
ctx.Data["NumFiles"] = 0
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-07-16 21:33:33 +08:00
|
|
|
ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitHeadRefName()), err)
|
2020-01-07 17:06:14 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 04:46:37 -06:00
|
|
|
ctx.Data["StatusCheckData"] = statusCheckData
|
2026-01-15 09:25:14 +11:00
|
|
|
statusCheckData.ApproveLink = fmt.Sprintf("%s/actions/approve-all-checks?commit_id=%s", repo.Link(), sha)
|
2025-10-20 04:46:37 -06:00
|
|
|
|
2025-05-27 03:00:22 +08:00
|
|
|
commitStatuses, err := git_model.GetLatestCommitStatus(ctx, repo.ID, sha, db.ListOptionsAll)
|
2020-01-07 17:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetLatestCommitStatus", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-07-28 23:11:40 +08:00
|
|
|
if !ctx.Repo.CanRead(unit.TypeActions) {
|
|
|
|
|
git_model.CommitStatusesHideActionsURL(ctx, commitStatuses)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 04:46:37 -06:00
|
|
|
runs, err := actions_service.GetRunsFromCommitStatuses(ctx, commitStatuses)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetRunsFromCommitStatuses", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
for _, run := range runs {
|
|
|
|
|
if run.NeedApproval {
|
|
|
|
|
statusCheckData.RequireApprovalRunCount++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if statusCheckData.RequireApprovalRunCount > 0 {
|
|
|
|
|
statusCheckData.CanApprove = ctx.Repo.CanWrite(unit.TypeActions)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 09:25:14 +11:00
|
|
|
statusCheckData.LatestCommitStatus = git_model.CalcCommitStatus(commitStatuses)
|
2020-01-07 17:06:14 +00:00
|
|
|
if len(commitStatuses) > 0 {
|
|
|
|
|
ctx.Data["LatestCommitStatuses"] = commitStatuses
|
2026-01-15 09:25:14 +11:00
|
|
|
ctx.Data["LatestCommitStatus"] = statusCheckData.LatestCommitStatus
|
2020-01-07 17:06:14 +00:00
|
|
|
}
|
2019-09-18 13:39:45 +08:00
|
|
|
|
2023-01-16 16:00:22 +08:00
|
|
|
if pb != nil && pb.EnableStatusCheck {
|
2024-02-19 10:57:08 +01:00
|
|
|
var missingRequiredChecks []string
|
|
|
|
|
for _, requiredContext := range pb.StatusCheckContexts {
|
|
|
|
|
contextFound := false
|
|
|
|
|
matchesRequiredContext := createRequiredContextMatcher(requiredContext)
|
|
|
|
|
for _, presentStatus := range commitStatuses {
|
|
|
|
|
if matchesRequiredContext(presentStatus.Context) {
|
|
|
|
|
contextFound = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !contextFound {
|
|
|
|
|
missingRequiredChecks = append(missingRequiredChecks, requiredContext)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-20 04:46:37 -06:00
|
|
|
statusCheckData.MissingRequiredChecks = missingRequiredChecks
|
2024-02-19 10:57:08 +01:00
|
|
|
|
2025-10-20 04:46:37 -06:00
|
|
|
statusCheckData.IsContextRequired = func(context string) bool {
|
2023-01-16 16:00:22 +08:00
|
|
|
for _, c := range pb.StatusCheckContexts {
|
2023-12-22 21:29:50 +08:00
|
|
|
if c == context {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if gp, err := glob.Compile(c); err != nil {
|
|
|
|
|
// All newly created status_check_contexts are checked to ensure they are valid glob expressions before being stored in the database.
|
|
|
|
|
// But some old status_check_context created before glob was introduced may be invalid glob expressions.
|
|
|
|
|
// So log the error here for debugging.
|
|
|
|
|
log.Error("compile glob %q: %v", c, err)
|
|
|
|
|
} else if gp.Match(context) {
|
2020-01-07 17:06:14 +00:00
|
|
|
return true
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-07 17:06:14 +00:00
|
|
|
return false
|
2019-06-30 15:57:59 +08:00
|
|
|
}
|
2026-01-15 09:25:14 +11:00
|
|
|
statusCheckData.RequiredChecksState = pull_service.MergeRequiredContextsCommitStatus(commitStatuses, pb.StatusCheckContexts)
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-07 17:06:14 +00:00
|
|
|
ctx.Data["HeadBranchMovedOn"] = headBranchSha != sha
|
|
|
|
|
ctx.Data["HeadBranchCommitID"] = headBranchSha
|
|
|
|
|
ctx.Data["PullHeadCommitID"] = sha
|
|
|
|
|
|
2023-03-03 21:16:58 +08:00
|
|
|
if pull.HeadRepo == nil || !headBranchExist || (!pull.Issue.IsClosed && (headBranchSha != sha)) {
|
2018-08-01 05:00:35 +02:00
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
2020-03-03 06:31:55 +08:00
|
|
|
if pull.IsSameRepo() {
|
|
|
|
|
ctx.Data["HeadTarget"] = pull.HeadBranch
|
2020-06-08 19:07:41 +01:00
|
|
|
} else if pull.HeadRepo == nil {
|
2023-08-04 07:07:15 +09:00
|
|
|
ctx.Data["HeadTarget"] = ctx.Locale.Tr("repo.pull.deleted_branch", pull.HeadBranch)
|
2020-03-03 06:31:55 +08:00
|
|
|
} else {
|
2020-06-08 19:07:41 +01:00
|
|
|
ctx.Data["HeadTarget"] = pull.HeadRepo.OwnerName + ":" + pull.HeadBranch
|
2020-03-03 06:31:55 +08:00
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-25 17:51:30 -08:00
|
|
|
compareInfo, err := git_service.GetCompareInfo(ctx, pull.BaseRepo, pull.BaseRepo, baseGitRepo,
|
|
|
|
|
git.RefNameFromBranch(pull.BaseBranch), git.RefName(pull.GetGitHeadRefName()), false, false)
|
2015-09-02 04:08:05 -04:00
|
|
|
if err != nil {
|
2026-02-24 10:25:34 +06:00
|
|
|
if gitcmd.IsStdErrorNotValidObjectName(err) {
|
2018-08-01 05:00:35 +02:00
|
|
|
ctx.Data["IsPullRequestBroken"] = true
|
2020-03-03 06:31:55 +08:00
|
|
|
ctx.Data["BaseTarget"] = pull.BaseBranch
|
2016-07-23 18:35:16 +08:00
|
|
|
ctx.Data["NumCommits"] = 0
|
|
|
|
|
ctx.Data["NumFiles"] = 0
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-07 22:29:29 +02:00
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
2015-09-02 04:08:05 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
2018-08-13 21:04:39 +02:00
|
|
|
|
2021-07-29 04:32:48 +02:00
|
|
|
if compareInfo.HeadCommitID == compareInfo.MergeBase {
|
|
|
|
|
ctx.Data["IsNothingToCompare"] = true
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 06:24:07 +02:00
|
|
|
if pull.IsWorkInProgress(ctx) {
|
2018-08-13 21:04:39 +02:00
|
|
|
ctx.Data["IsPullWorkInProgress"] = true
|
2022-11-19 09:12:33 +01:00
|
|
|
ctx.Data["WorkInProgressPrefix"] = pull.GetWorkInProgressPrefix(ctx)
|
2018-08-13 21:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 19:54:49 +08:00
|
|
|
if pull.IsFilesConflicted() {
|
|
|
|
|
ctx.Data["IsPullFilesConflicted"] = true
|
|
|
|
|
ctx.Data["ConflictedFiles"] = pull.ConflictedFiles
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 20:08:51 +02:00
|
|
|
ctx.Data["NumCommits"] = len(compareInfo.Commits)
|
2019-06-12 01:32:08 +02:00
|
|
|
ctx.Data["NumFiles"] = compareInfo.NumFiles
|
|
|
|
|
return compareInfo
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-19 10:57:08 +01:00
|
|
|
func createRequiredContextMatcher(requiredContext string) func(string) bool {
|
|
|
|
|
if gp, err := glob.Compile(requiredContext); err == nil {
|
|
|
|
|
return func(contextToCheck string) bool {
|
|
|
|
|
return gp.Match(contextToCheck)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return func(contextToCheck string) bool {
|
|
|
|
|
return requiredContext == contextToCheck
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 21:18:12 +02:00
|
|
|
type pullCommitList struct {
|
|
|
|
|
Commits []pull_service.CommitInfo `json:"commits"`
|
|
|
|
|
LastReviewCommitSha string `json:"last_review_commit_sha"`
|
2024-02-15 05:48:45 +08:00
|
|
|
Locale map[string]any `json:"locale"`
|
2023-07-28 21:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPullCommits get all commits for given pull request
|
|
|
|
|
func GetPullCommits(ctx *context.Context) {
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2023-07-28 21:18:12 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp := &pullCommitList{}
|
|
|
|
|
|
2025-07-31 11:43:54 +08:00
|
|
|
commits, lastReviewCommitSha, err := pull_service.GetPullCommits(ctx, ctx.Repo.GitRepo, ctx.Doer, issue)
|
2023-07-28 21:18:12 +02:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the needed locale
|
2024-02-15 05:48:45 +08:00
|
|
|
resp.Locale = map[string]any{
|
2023-07-28 21:18:12 +02:00
|
|
|
"lang": ctx.Locale.Language(),
|
|
|
|
|
"show_all_commits": ctx.Tr("repo.pulls.show_all_commits"),
|
|
|
|
|
"stats_num_commits": ctx.TrN(len(commits), "repo.activity.git_stats_commit_1", "repo.activity.git_stats_commit_n", len(commits)),
|
|
|
|
|
"show_changes_since_your_last_review": ctx.Tr("repo.pulls.show_changes_since_your_last_review"),
|
|
|
|
|
"select_commit_hold_shift_for_range": ctx.Tr("repo.pulls.select_commit_hold_shift_for_range"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.Commits = commits
|
|
|
|
|
resp.LastReviewCommitSha = lastReviewCommitSha
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
// ViewPullCommits show commits for a pull request
|
2016-03-11 11:56:52 -05:00
|
|
|
func ViewPullCommits(ctx *context.Context) {
|
2016-08-14 03:32:24 -07:00
|
|
|
ctx.Data["PageIsPullList"] = true
|
2015-09-02 04:08:05 -04:00
|
|
|
ctx.Data["PageIsPullCommits"] = true
|
|
|
|
|
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2015-09-02 04:08:05 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 20:39:35 -08:00
|
|
|
prInfo := preparePullViewPullInfo(ctx, issue)
|
2019-12-18 13:37:44 -05:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
} else if prInfo == nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2019-12-18 13:37:44 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Data["Username"] = ctx.Repo.Owner.Name
|
|
|
|
|
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
|
2021-08-09 20:08:51 +02:00
|
|
|
|
2025-02-14 00:05:55 -08:00
|
|
|
commits, err := processGitCommits(ctx, prInfo.Commits)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("processGitCommits", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
ctx.Data["Commits"] = commits
|
2021-08-09 20:08:51 +02:00
|
|
|
ctx.Data["CommitCount"] = len(commits)
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2023-07-10 09:05:59 +02:00
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
|
|
|
|
|
ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.Doer.ID)
|
|
|
|
|
|
2023-07-21 19:20:04 +08:00
|
|
|
// For PR commits page
|
|
|
|
|
PrepareBranchList(ctx)
|
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-16 07:20:25 +01:00
|
|
|
getBranchData(ctx, issue)
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplPullCommits)
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 10:23:10 -07:00
|
|
|
func indexCommit(commits []*git.Commit, commitID string) *git.Commit {
|
|
|
|
|
for i := range commits {
|
|
|
|
|
if commits[i].ID.String() == commitID {
|
|
|
|
|
return commits[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
// ViewPullFiles render pull request changed files list page
|
2025-08-03 10:23:10 -07:00
|
|
|
func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
|
2026-05-19 20:45:18 +02:00
|
|
|
var err error
|
|
|
|
|
|
2016-08-14 03:32:24 -07:00
|
|
|
ctx.Data["PageIsPullList"] = true
|
2015-09-02 04:08:05 -04:00
|
|
|
ctx.Data["PageIsPullFiles"] = true
|
|
|
|
|
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2015-09-02 04:08:05 -04:00
|
|
|
return
|
|
|
|
|
}
|
2016-08-16 10:19:09 -07:00
|
|
|
pull := issue.PullRequest
|
2015-09-02 04:08:05 -04:00
|
|
|
|
2025-08-03 10:23:10 -07:00
|
|
|
gitRepo := ctx.Repo.GitRepo
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2024-12-07 20:39:35 -08:00
|
|
|
prInfo := preparePullViewPullInfo(ctx, issue)
|
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
} else if prInfo == nil {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2024-12-07 20:39:35 -08:00
|
|
|
return
|
2019-12-18 13:37:44 -05:00
|
|
|
}
|
2015-09-02 04:08:05 -04:00
|
|
|
|
2025-07-16 21:33:33 +08:00
|
|
|
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitHeadRefName())
|
2019-12-18 13:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetRefCommitID", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2025-08-03 10:23:10 -07:00
|
|
|
isSingleCommit := beforeCommitID == "" && afterCommitID != ""
|
2026-05-19 20:45:18 +02:00
|
|
|
// FIXME: when afterCommitID==headCommitID, isSingleCommit and isShowAllCommits can be both true, which doesn't seem right
|
2025-08-03 10:23:10 -07:00
|
|
|
isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prInfo.MergeBase) && (afterCommitID == "" || afterCommitID == headCommitID)
|
2026-05-19 20:45:18 +02:00
|
|
|
|
|
|
|
|
ctx.Data["IsShowingOnlySingleCommit"] = isSingleCommit
|
2025-08-03 10:23:10 -07:00
|
|
|
ctx.Data["IsShowingAllCommits"] = isShowAllCommits
|
2023-07-28 21:18:12 +02:00
|
|
|
|
2026-05-19 20:45:18 +02:00
|
|
|
// "commits list" is half-open, half-closed: (base, head]
|
|
|
|
|
// * base commit is not in the list
|
|
|
|
|
// * if the PR is empty, the list is also empty (head commit is not in the list)
|
|
|
|
|
|
|
|
|
|
afterCommitID = util.IfZero(afterCommitID, headCommitID)
|
2025-08-03 10:23:10 -07:00
|
|
|
afterCommit := indexCommit(prInfo.Commits, afterCommitID)
|
2026-05-19 20:45:18 +02:00
|
|
|
if afterCommit == nil && afterCommitID == headCommitID {
|
|
|
|
|
afterCommit, err = gitRepo.GetCommit(afterCommitID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetCommit(afterCommitID)", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-03 10:23:10 -07:00
|
|
|
if afterCommit == nil {
|
2026-05-19 20:45:18 +02:00
|
|
|
ctx.NotFound(nil)
|
2025-08-03 10:23:10 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var beforeCommit *git.Commit
|
2026-05-19 20:45:18 +02:00
|
|
|
if isSingleCommit {
|
|
|
|
|
beforeCommit, err = afterCommit.Parent(0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("afterCommit.Parent", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
beforeCommitID = beforeCommit.ID.String()
|
|
|
|
|
} else {
|
|
|
|
|
beforeCommitID = util.IfZero(beforeCommitID, prInfo.MergeBase)
|
|
|
|
|
beforeCommit = indexCommit(prInfo.Commits, beforeCommitID)
|
|
|
|
|
if beforeCommit == nil && beforeCommitID == prInfo.MergeBase {
|
2025-08-03 10:23:10 -07:00
|
|
|
// mergebase commit is not in the list of the pull request commits
|
|
|
|
|
beforeCommit, err = gitRepo.GetCommit(beforeCommitID)
|
|
|
|
|
if err != nil {
|
2026-05-19 20:45:18 +02:00
|
|
|
ctx.ServerError("GetCommit(beforeCommitID)", err)
|
2025-08-03 10:23:10 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-19 20:45:18 +02:00
|
|
|
}
|
|
|
|
|
if beforeCommit == nil {
|
|
|
|
|
ctx.NotFound(nil)
|
|
|
|
|
return
|
2023-07-28 21:18:12 +02:00
|
|
|
}
|
2017-06-21 01:25:38 +03:00
|
|
|
|
2019-12-18 13:37:44 -05:00
|
|
|
ctx.Data["Username"] = ctx.Repo.Owner.Name
|
|
|
|
|
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
|
2025-08-03 10:23:10 -07:00
|
|
|
ctx.Data["MergeBase"] = prInfo.MergeBase
|
|
|
|
|
ctx.Data["AfterCommitID"] = afterCommitID
|
|
|
|
|
ctx.Data["BeforeCommitID"] = beforeCommitID
|
2021-11-21 16:51:08 +00:00
|
|
|
|
|
|
|
|
maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
|
|
|
|
|
files := ctx.FormStrings("files")
|
2025-08-03 10:23:10 -07:00
|
|
|
fileOnly := ctx.FormBool("file-only")
|
2021-11-21 16:51:08 +00:00
|
|
|
if fileOnly && (len(files) == 2 || len(files) == 1) {
|
|
|
|
|
maxLines, maxFiles = -1, -1
|
|
|
|
|
}
|
2023-07-28 21:18:12 +02:00
|
|
|
|
2022-05-07 20:28:10 +02:00
|
|
|
diffOptions := &gitdiff.DiffOptions{
|
2025-08-03 10:23:10 -07:00
|
|
|
BeforeCommitID: beforeCommitID,
|
|
|
|
|
AfterCommitID: afterCommitID,
|
2022-05-07 20:28:10 +02:00
|
|
|
SkipTo: ctx.FormString("skip-to"),
|
|
|
|
|
MaxLines: maxLines,
|
|
|
|
|
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
|
|
|
|
MaxFiles: maxFiles,
|
|
|
|
|
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 23:28:34 +08:00
|
|
|
diff, err := gitdiff.GetDiffForRender(ctx, ctx.Repo.RepoLink, gitRepo, diffOptions, files...)
|
2025-03-08 17:36:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetDiff", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-28 21:18:12 +02:00
|
|
|
|
|
|
|
|
// if we're not logged in or only a single commit (or commit range) is shown we
|
|
|
|
|
// have to load only the diff and not get the viewed information
|
|
|
|
|
// as the viewed information is designed to be loaded only on latest PR
|
|
|
|
|
// diff and if you're signed in.
|
2025-04-15 22:35:22 +08:00
|
|
|
var reviewState *pull_model.ReviewState
|
2025-11-03 13:34:52 -08:00
|
|
|
var numViewedFiles int
|
2025-08-03 10:23:10 -07:00
|
|
|
if ctx.IsSigned && isShowAllCommits {
|
2025-04-15 22:35:22 +08:00
|
|
|
reviewState, err = gitdiff.SyncUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions)
|
2025-03-08 17:36:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("SyncUserSpecificDiff", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-03 13:34:52 -08:00
|
|
|
if reviewState != nil {
|
|
|
|
|
numViewedFiles = reviewState.GetViewedFileCount()
|
|
|
|
|
}
|
2022-05-07 20:28:10 +02:00
|
|
|
}
|
2025-03-08 17:36:08 +08:00
|
|
|
|
2025-09-26 10:14:20 -07:00
|
|
|
diffShortStat, err := gitdiff.GetDiffShortStat(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, beforeCommitID, afterCommitID)
|
2015-09-02 04:08:05 -04:00
|
|
|
if err != nil {
|
2025-03-08 17:36:08 +08:00
|
|
|
ctx.ServerError("GetDiffShortStat", err)
|
2015-09-02 04:08:05 -04:00
|
|
|
return
|
|
|
|
|
}
|
2025-03-08 17:36:08 +08:00
|
|
|
ctx.Data["DiffShortStat"] = diffShortStat
|
2025-11-03 13:34:52 -08:00
|
|
|
ctx.Data["NumViewedFiles"] = numViewedFiles
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.PageData["prReview"] = map[string]any{
|
2025-03-08 17:36:08 +08:00
|
|
|
"numberOfFiles": diffShortStat.NumFiles,
|
2025-11-03 13:34:52 -08:00
|
|
|
"numberOfViewedFiles": numViewedFiles,
|
2022-05-07 20:28:10 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-21 18:08:12 +02:00
|
|
|
if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil {
|
2018-08-06 07:43:22 +03:00
|
|
|
ctx.ServerError("LoadComments", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 22:49:58 -08:00
|
|
|
allComments := issues_model.CommentList{}
|
2024-02-25 07:00:55 +01:00
|
|
|
for _, file := range diff.Files {
|
|
|
|
|
for _, section := range file.Sections {
|
|
|
|
|
for _, line := range section.Lines {
|
2025-02-13 22:49:58 -08:00
|
|
|
allComments = append(allComments, line.Comments...)
|
2024-02-25 07:00:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-13 22:49:58 -08:00
|
|
|
if err := allComments.LoadAttachments(ctx); err != nil {
|
|
|
|
|
ctx.ServerError("LoadAttachments", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-02-25 07:00:55 +01:00
|
|
|
|
2023-01-16 16:00:22 +08:00
|
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pull.BaseRepoID, pull.BaseBranch)
|
|
|
|
|
if err != nil {
|
2020-10-14 02:50:57 +08:00
|
|
|
ctx.ServerError("LoadProtectedBranch", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 16:00:22 +08:00
|
|
|
if pb != nil {
|
|
|
|
|
glob := pb.GetProtectedFilePatterns()
|
2020-10-14 02:50:57 +08:00
|
|
|
if len(glob) != 0 {
|
|
|
|
|
for _, file := range diff.Files {
|
2023-01-16 16:00:22 +08:00
|
|
|
file.IsProtected = pb.IsProtectedFile(glob, file.Name)
|
2020-10-14 02:50:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 16:58:25 -08:00
|
|
|
if !fileOnly {
|
|
|
|
|
// note: use mergeBase is set to false because we already have the merge base from the pull request info
|
2025-08-03 10:23:10 -07:00
|
|
|
diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, beforeCommitID, afterCommitID)
|
2025-02-27 16:58:25 -08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetDiffTree", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-04-15 22:35:22 +08:00
|
|
|
var filesViewedState map[string]pull_model.ViewedState
|
|
|
|
|
if reviewState != nil {
|
|
|
|
|
filesViewedState = reviewState.UpdatedFiles
|
2025-02-27 16:58:25 -08:00
|
|
|
}
|
2025-04-29 10:51:32 +08:00
|
|
|
|
|
|
|
|
renderedIconPool := fileicon.NewRenderedIconPool()
|
|
|
|
|
ctx.PageData["DiffFileTree"] = transformDiffTreeForWeb(renderedIconPool, diffTree, filesViewedState)
|
|
|
|
|
ctx.PageData["FolderIcon"] = fileicon.RenderEntryIconHTML(renderedIconPool, fileicon.EntryInfoFolder())
|
|
|
|
|
ctx.PageData["FolderOpenIcon"] = fileicon.RenderEntryIconHTML(renderedIconPool, fileicon.EntryInfoFolderOpen())
|
|
|
|
|
ctx.Data["FileIconPoolHTML"] = renderedIconPool.RenderToHTML()
|
2025-02-27 16:58:25 -08:00
|
|
|
}
|
|
|
|
|
|
2015-09-02 04:08:05 -04:00
|
|
|
ctx.Data["Diff"] = diff
|
2025-10-19 13:19:12 +03:00
|
|
|
ctx.Data["DiffBlobExcerptData"] = &gitdiff.DiffBlobExcerptData{
|
|
|
|
|
BaseLink: ctx.Repo.RepoLink + "/blob_excerpt",
|
|
|
|
|
PullIssueIndex: pull.Index,
|
2026-01-09 12:37:16 +08:00
|
|
|
DiffStyle: GetDiffViewStyle(ctx),
|
2025-10-19 13:19:12 +03:00
|
|
|
AfterCommitID: afterCommitID,
|
|
|
|
|
}
|
2025-03-08 17:36:08 +08:00
|
|
|
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
|
2015-09-02 04:08:05 -04:00
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if ctx.IsSigned && ctx.Doer != nil {
|
2023-09-29 14:12:54 +02:00
|
|
|
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
|
2020-04-18 21:50:25 +08:00
|
|
|
ctx.ServerError("CanMarkConversation", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 10:23:10 -07:00
|
|
|
setCompareContext(ctx, beforeCommit, afterCommit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
2019-09-16 11:03:22 +02:00
|
|
|
|
2023-04-07 08:11:02 +08:00
|
|
|
assigneeUsers, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetRepoAssignees", err)
|
2018-08-06 07:43:22 +03:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-07 21:37:37 +01:00
|
|
|
ctx.Data["Assignees"] = shared_user.MakeSelfOnTop(ctx.Doer, assigneeUsers)
|
2022-05-07 05:35:12 +00:00
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
currentReview, err := issues_model.GetCurrentReview(ctx, ctx.Doer, issue)
|
|
|
|
|
if err != nil && !issues_model.IsErrReviewNotExist(err) {
|
2018-08-06 07:43:22 +03:00
|
|
|
ctx.ServerError("GetCurrentReview", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-05-07 05:35:12 +00:00
|
|
|
numPendingCodeComments := int64(0)
|
|
|
|
|
if currentReview != nil {
|
2023-09-29 14:12:54 +02:00
|
|
|
numPendingCodeComments, err = issues_model.CountComments(ctx, &issues_model.FindCommentsOptions{
|
2022-06-13 17:37:59 +08:00
|
|
|
Type: issues_model.CommentTypeCode,
|
2022-05-07 05:35:12 +00:00
|
|
|
ReviewID: currentReview.ID,
|
|
|
|
|
IssueID: issue.ID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("CountComments", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ctx.Data["CurrentReview"] = currentReview
|
|
|
|
|
ctx.Data["PendingCodeCommentNumber"] = numPendingCodeComments
|
|
|
|
|
|
2019-12-16 07:20:25 +01:00
|
|
|
getBranchData(ctx, issue)
|
2022-03-22 08:03:22 +01:00
|
|
|
ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.Doer.ID)
|
2020-04-04 13:39:48 +08:00
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
|
2021-06-15 03:12:33 +02:00
|
|
|
|
|
|
|
|
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
|
2023-07-21 19:20:04 +08:00
|
|
|
// For files changed page
|
|
|
|
|
PrepareBranchList(ctx)
|
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-15 03:12:33 +02:00
|
|
|
upload.AddUploadContext(ctx, "comment")
|
|
|
|
|
|
2024-03-04 09:16:03 +01:00
|
|
|
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
|
|
|
|
|
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
|
|
|
|
|
}
|
2025-08-03 10:23:10 -07:00
|
|
|
if isShowAllCommits && pull.Flow == issues_model.PullRequestFlowGithub {
|
2024-03-27 10:34:10 +08:00
|
|
|
if err := pull.LoadHeadRepo(ctx); err != nil {
|
|
|
|
|
ctx.ServerError("LoadHeadRepo", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pull.HeadRepo != nil {
|
2024-04-24 13:26:50 +08:00
|
|
|
if !pull.HasMerged && ctx.Doer != nil {
|
2026-04-29 10:07:09 -07:00
|
|
|
headPerm, err := access_model.GetDoerRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
|
2024-04-24 13:26:50 +08:00
|
|
|
if err != nil {
|
2026-03-29 11:21:14 +02:00
|
|
|
ctx.ServerError("GetDoerRepoPermission", err)
|
2024-04-24 13:26:50 +08:00
|
|
|
return
|
|
|
|
|
}
|
2024-03-27 10:34:10 +08:00
|
|
|
|
2026-04-29 10:07:09 -07:00
|
|
|
if issues_model.CanMaintainerWriteToBranch(ctx, headPerm, pull.HeadBranch, ctx.Doer) {
|
2024-04-24 13:26:50 +08:00
|
|
|
ctx.Data["CanEditFile"] = true
|
|
|
|
|
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
|
|
|
|
|
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
|
|
|
|
|
ctx.Data["HeadBranchName"] = pull.HeadBranch
|
|
|
|
|
ctx.Data["BackToLink"] = setting.AppSubURL + ctx.Req.URL.RequestURI()
|
|
|
|
|
}
|
2024-03-27 10:34:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 09:16:03 +01:00
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplPullFiles)
|
2015-09-02 04:08:05 -04:00
|
|
|
}
|
2015-08-31 16:24:28 +09:00
|
|
|
|
2023-07-28 21:18:12 +02:00
|
|
|
func ViewPullFilesForSingleCommit(ctx *context.Context) {
|
2025-08-03 10:23:10 -07:00
|
|
|
// it doesn't support showing files from mergebase to the special commit
|
|
|
|
|
// otherwise it will be ambiguous
|
|
|
|
|
viewPullFiles(ctx, "", ctx.PathParam("sha"))
|
2023-07-28 21:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ViewPullFilesForRange(ctx *context.Context) {
|
2025-08-03 10:23:10 -07:00
|
|
|
viewPullFiles(ctx, ctx.PathParam("shaFrom"), ctx.PathParam("shaTo"))
|
2023-07-28 21:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ViewPullFilesForAllCommitsOfPr(ctx *context.Context) {
|
2025-08-03 10:23:10 -07:00
|
|
|
viewPullFiles(ctx, "", "")
|
2023-07-28 21:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 04:55:22 +08:00
|
|
|
// UpdatePullRequest merge PR's baseBranch into headBranch
|
2020-01-17 07:03:40 +01:00
|
|
|
func UpdatePullRequest(ctx *context.Context) {
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if issue.IsClosed {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if issue.PullRequest.HasMerged {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(nil)
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 22:03:45 +08:00
|
|
|
rebase := ctx.FormString("style") == "rebase"
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.PullRequest.LoadBaseRepo(ctx); err != nil {
|
2021-01-15 04:27:22 +08:00
|
|
|
ctx.ServerError("LoadBaseRepo", err)
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.PullRequest.LoadHeadRepo(ctx); err != nil {
|
2021-01-15 04:27:22 +08:00
|
|
|
ctx.ServerError("LoadHeadRepo", err)
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 13:48:48 +02:00
|
|
|
allowedUpdateByMerge, allowedUpdateByRebase, err := pull_service.IsUserAllowedToUpdate(ctx, issue.PullRequest, ctx.Doer)
|
2020-01-17 07:03:40 +01:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("IsUserAllowedToMerge", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToDo: add check if maintainers are allowed to change branch ... (need migration & co)
|
2021-08-31 22:03:45 +08:00
|
|
|
if (!allowedUpdateByMerge && !rebase) || (rebase && !allowedUpdateByRebase) {
|
2020-01-17 07:03:40 +01:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.update_not_allowed"))
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(issue.Link())
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// default merge commit message
|
|
|
|
|
message := fmt.Sprintf("Merge branch '%s' into %s", issue.PullRequest.BaseBranch, issue.PullRequest.HeadBranch)
|
|
|
|
|
|
2025-06-05 01:09:08 +08:00
|
|
|
// The update process should not be cancelled by the user
|
|
|
|
|
// so we set the context to be a background context
|
|
|
|
|
if err = pull_service.Update(graceful.GetManager().ShutdownContext(), issue.PullRequest, ctx.Doer, message, rebase); err != nil {
|
2024-12-20 10:05:29 -08:00
|
|
|
if pull_service.IsErrMergeConflicts(err) {
|
|
|
|
|
conflictError := err.(pull_service.ErrMergeConflicts)
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2020-10-21 00:50:10 +01:00
|
|
|
"Message": ctx.Tr("repo.pulls.merge_conflict"),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.merge_conflict_summary"),
|
|
|
|
|
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("UpdatePullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(flashError)
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(issue.Link())
|
2020-01-17 07:03:40 +01:00
|
|
|
return
|
2024-12-20 10:05:29 -08:00
|
|
|
} else if pull_service.IsErrRebaseConflicts(err) {
|
|
|
|
|
conflictError := err.(pull_service.ErrRebaseConflicts)
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2021-09-05 10:30:40 +01:00
|
|
|
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
|
|
|
|
|
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("UpdatePullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(flashError)
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(issue.Link())
|
2021-09-05 10:30:40 +01:00
|
|
|
return
|
2020-01-17 07:03:40 +01:00
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(err.Error())
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(issue.Link())
|
2020-08-04 04:50:29 +08:00
|
|
|
return
|
2020-01-17 07:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.pulls.update_branch_success"))
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Redirect(issue.Link())
|
2020-01-17 07:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
// MergePullRequest response for merging pull request
|
2021-01-26 23:36:53 +08:00
|
|
|
func MergePullRequest(ctx *context.Context) {
|
2021-04-06 20:44:05 +01:00
|
|
|
form := web.GetForm(ctx).(*forms.MergePullRequestForm)
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2015-09-02 09:26:56 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 23:55:43 +08:00
|
|
|
pr := issue.PullRequest
|
2022-03-31 16:53:08 +02:00
|
|
|
pr.Issue = issue
|
|
|
|
|
pr.Issue.Repo = ctx.Repo.Repository
|
2023-02-21 22:42:07 +08:00
|
|
|
|
|
|
|
|
manuallyMerged := repo_model.MergeStyle(form.Do) == repo_model.MergeStyleManuallyMerged
|
|
|
|
|
|
|
|
|
|
mergeCheckType := pull_service.MergeCheckTypeGeneral
|
|
|
|
|
if form.MergeWhenChecksSucceed {
|
|
|
|
|
mergeCheckType = pull_service.MergeCheckTypeAuto
|
|
|
|
|
}
|
|
|
|
|
if manuallyMerged {
|
|
|
|
|
mergeCheckType = pull_service.MergeCheckTypeManually
|
|
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2022-05-03 21:46:28 +02:00
|
|
|
// start with merging by checking
|
2024-05-09 01:11:43 +09:00
|
|
|
if err := pull_service.CheckPullMergeable(ctx, ctx.Doer, &ctx.Repo.Permission, pr, mergeCheckType, form.ForceMerge); err != nil {
|
2023-02-03 23:11:48 +00:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, pull_service.ErrIsClosed):
|
2022-03-31 16:53:08 +02:00
|
|
|
if issue.IsPull {
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.is_closed"))
|
2022-03-31 16:53:08 +02:00
|
|
|
} else {
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.issues.closed_title"))
|
2022-03-31 16:53:08 +02:00
|
|
|
}
|
2025-04-24 21:26:57 +02:00
|
|
|
case errors.Is(err, pull_service.ErrNoPermissionToMerge):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.update_not_allowed"))
|
2023-02-03 23:11:48 +00:00
|
|
|
case errors.Is(err, pull_service.ErrHasMerged):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.has_merged"))
|
2023-02-03 23:11:48 +00:00
|
|
|
case errors.Is(err, pull_service.ErrIsWorkInProgress):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.no_merge_wip"))
|
2024-05-09 01:11:43 +09:00
|
|
|
case errors.Is(err, pull_service.ErrNotMergeableState):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.no_merge_not_ready"))
|
2025-04-24 21:26:57 +02:00
|
|
|
case errors.Is(err, pull_service.ErrNotReadyToMerge):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.no_merge_not_ready"))
|
2023-02-03 23:11:48 +00:00
|
|
|
case asymkey_service.IsErrWontSign(err):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(err.Error()) // has no translation ...
|
2023-02-03 23:11:48 +00:00
|
|
|
case errors.Is(err, pull_service.ErrDependenciesLeft):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.issues.dependency.pr_close_blocked"))
|
2023-02-03 23:11:48 +00:00
|
|
|
default:
|
2022-03-31 16:53:08 +02:00
|
|
|
ctx.ServerError("WebCheck", err)
|
|
|
|
|
}
|
2023-02-03 23:11:48 +00:00
|
|
|
|
2020-01-27 10:26:53 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 11:41:23 +08:00
|
|
|
// handle manually-merged mark
|
2023-02-21 22:42:07 +08:00
|
|
|
if manuallyMerged {
|
2023-10-14 10:37:24 +02:00
|
|
|
if err := pull_service.MergedManually(ctx, pr, ctx.Doer, ctx.Repo.GitRepo, form.MergeCommitID); err != nil {
|
2023-02-03 23:11:48 +00:00
|
|
|
switch {
|
2024-12-20 10:05:29 -08:00
|
|
|
case pull_service.IsErrInvalidMergeStyle(err):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.invalid_merge_option"))
|
2023-02-03 23:11:48 +00:00
|
|
|
case strings.Contains(err.Error(), "Wrong commit ID"):
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.wrong_commit_id"))
|
2023-02-03 23:11:48 +00:00
|
|
|
default:
|
2022-05-03 21:46:28 +02:00
|
|
|
ctx.ServerError("MergedManually", err)
|
2021-03-04 11:41:23 +08:00
|
|
|
}
|
2024-01-14 23:00:47 +01:00
|
|
|
|
|
|
|
|
return
|
2021-03-04 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2021-03-04 11:41:23 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 20:32:45 +08:00
|
|
|
message := strings.TrimSpace(form.MergeTitleField)
|
|
|
|
|
if len(message) == 0 {
|
|
|
|
|
var err error
|
2022-12-29 20:40:20 +08:00
|
|
|
message, _, err = pull_service.GetDefaultMergeMessage(ctx, ctx.Repo.GitRepo, pr, repo_model.MergeStyle(form.Do))
|
2022-05-08 20:32:45 +08:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetDefaultMergeMessage", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
form.MergeMessageField = strings.TrimSpace(form.MergeMessageField)
|
|
|
|
|
if len(form.MergeMessageField) > 0 {
|
|
|
|
|
message += "\n\n" + form.MergeMessageField
|
2018-07-17 23:23:58 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-03 10:36:21 +08:00
|
|
|
// There is always a checkbox on the UI (the DeleteBranchAfterMerge is nil if the checkbox is not checked),
|
|
|
|
|
// just use the user's choice, don't use pull_service.ShouldDeleteBranchAfterMerge to decide
|
|
|
|
|
deleteBranchAfterMerge := optional.FromPtr(form.DeleteBranchAfterMerge).Value()
|
2025-10-21 22:06:56 -07:00
|
|
|
|
2022-06-11 16:44:20 +02:00
|
|
|
if form.MergeWhenChecksSucceed {
|
|
|
|
|
// delete all scheduled auto merges
|
|
|
|
|
_ = pull_model.DeleteScheduledAutoMerge(ctx, pr.ID)
|
|
|
|
|
// schedule auto merge
|
2025-10-21 22:06:56 -07:00
|
|
|
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, deleteBranchAfterMerge)
|
2022-06-11 16:44:20 +02:00
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("ScheduleAutoMerge", err)
|
|
|
|
|
return
|
|
|
|
|
} else if scheduled {
|
|
|
|
|
// nothing more to do ...
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.pulls.auto_merge_newly_scheduled"))
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, pr.Index))
|
2022-06-11 16:44:20 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 21:56:08 -07:00
|
|
|
if err := pull_service.Merge(ctx, pr, ctx.Doer, repo_model.MergeStyle(form.Do), form.HeadCommitID, message, false); err != nil {
|
2024-12-20 10:05:29 -08:00
|
|
|
if pull_service.IsErrInvalidMergeStyle(err) {
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.invalid_merge_option"))
|
2024-12-20 10:05:29 -08:00
|
|
|
} else if pull_service.IsErrMergeConflicts(err) {
|
|
|
|
|
conflictError := err.(pull_service.ErrMergeConflicts)
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2020-10-21 00:50:10 +01:00
|
|
|
"Message": ctx.Tr("repo.editor.merge_conflict"),
|
|
|
|
|
"Summary": ctx.Tr("repo.editor.merge_conflict_summary"),
|
|
|
|
|
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("MergePullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(flashError)
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2024-12-20 10:05:29 -08:00
|
|
|
} else if pull_service.IsErrRebaseConflicts(err) {
|
|
|
|
|
conflictError := err.(pull_service.ErrRebaseConflicts)
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2020-10-22 04:59:12 +09:00
|
|
|
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.SanitizeFlashErrorString(conflictError.CommitSHA)),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
|
2020-10-21 00:50:10 +01:00
|
|
|
"Details": utils.SanitizeFlashErrorString(conflictError.StdErr) + "<br>" + utils.SanitizeFlashErrorString(conflictError.StdOut),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("MergePullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(flashError)
|
2024-02-21 19:40:46 +08:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2024-12-20 10:05:29 -08:00
|
|
|
} else if pull_service.IsErrMergeUnrelatedHistories(err) {
|
2019-11-10 08:42:51 +00:00
|
|
|
log.Debug("MergeUnrelatedHistories error: %v", err)
|
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.unrelated_histories"))
|
2024-02-21 19:40:46 +08:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2020-03-28 04:13:18 +00:00
|
|
|
} else if git.IsErrPushOutOfDate(err) {
|
2019-11-10 08:42:51 +00:00
|
|
|
log.Debug("MergePushOutOfDate error: %v", err)
|
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.merge_out_of_date"))
|
2024-02-21 19:40:46 +08:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2024-12-20 10:05:29 -08:00
|
|
|
} else if pull_service.IsErrSHADoesNotMatch(err) {
|
2021-12-20 00:32:54 +00:00
|
|
|
log.Debug("MergeHeadOutOfDate error: %v", err)
|
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.head_out_of_date"))
|
2024-02-21 19:40:46 +08:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2020-03-28 04:13:18 +00:00
|
|
|
} else if git.IsErrPushRejected(err) {
|
2020-02-22 13:08:48 +00:00
|
|
|
log.Debug("MergePushRejected error: %v", err)
|
2020-03-28 04:13:18 +00:00
|
|
|
pushrejErr := err.(*git.ErrPushRejected)
|
2020-02-22 13:08:48 +00:00
|
|
|
message := pushrejErr.Message
|
|
|
|
|
if len(message) == 0 {
|
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
|
|
|
|
|
} else {
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2020-10-21 00:50:10 +01:00
|
|
|
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
|
|
|
|
|
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("MergePullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Error(flashError)
|
2020-02-22 13:08:48 +00:00
|
|
|
}
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2022-05-03 21:46:28 +02:00
|
|
|
} else {
|
|
|
|
|
ctx.ServerError("Merge", err)
|
2018-01-05 20:56:50 +02:00
|
|
|
}
|
2015-09-02 09:26:56 -04:00
|
|
|
return
|
|
|
|
|
}
|
2022-05-03 21:46:28 +02:00
|
|
|
log.Trace("Pull request merged: %d", pr.ID)
|
2015-09-02 09:26:56 -04:00
|
|
|
|
2023-09-16 16:39:12 +02:00
|
|
|
if err := stopTimerIfAvailable(ctx, ctx.Doer, issue); err != nil {
|
2024-01-14 23:00:47 +01:00
|
|
|
ctx.ServerError("stopTimerIfAvailable", err)
|
2019-02-05 12:38:11 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-02 09:26:56 -04:00
|
|
|
log.Trace("Pull request merged: %d", pr.ID)
|
2021-07-13 01:26:25 +02:00
|
|
|
|
2025-10-21 22:06:56 -07:00
|
|
|
if deleteBranchAfterMerge {
|
|
|
|
|
deleteBranchAfterMergeAndFlashMessage(ctx, pr.ID)
|
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-03 19:59:48 -08:00
|
|
|
}
|
2025-10-21 22:06:56 -07:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
|
|
|
|
}
|
2024-12-03 19:59:48 -08:00
|
|
|
|
2025-10-21 22:06:56 -07:00
|
|
|
func deleteBranchAfterMergeAndFlashMessage(ctx *context.Context, prID int64) {
|
|
|
|
|
var fullBranchName string
|
|
|
|
|
err := repo_service.DeleteBranchAfterMerge(ctx, ctx.Doer, prID, &fullBranchName)
|
2025-11-13 07:03:13 +08:00
|
|
|
if errors.Is(err, util.ErrPermissionDenied) || errors.Is(err, util.ErrNotExist) {
|
|
|
|
|
// no need to show error to end users if no permission or branch not exist
|
|
|
|
|
log.Debug("DeleteBranchAfterMerge (ignore unnecessary error): %v", err)
|
|
|
|
|
return
|
|
|
|
|
} else if errTr := util.ErrorAsTranslatable(err); errTr != nil {
|
2025-10-21 22:06:56 -07:00
|
|
|
ctx.Flash.Error(errTr.Translate(ctx.Locale))
|
2024-12-03 19:59:48 -08:00
|
|
|
return
|
2025-10-21 22:06:56 -07:00
|
|
|
} else if err == nil {
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
|
2024-12-03 19:59:48 -08:00
|
|
|
return
|
|
|
|
|
}
|
2025-10-21 22:06:56 -07:00
|
|
|
// catch unknown errors
|
|
|
|
|
ctx.ServerError("DeleteBranchAfterMerge", err)
|
2022-06-11 16:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CancelAutoMergePullRequest cancels a scheduled pr
|
|
|
|
|
func CancelAutoMergePullRequest(ctx *context.Context) {
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2022-06-11 16:44:20 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 13:47:06 -08:00
|
|
|
exist, autoMerge, err := pull_model.GetScheduledMergeByPullID(ctx, issue.PullRequest.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("GetScheduledMergeByPullID", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !exist {
|
|
|
|
|
ctx.NotFound(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.Doer.ID != autoMerge.DoerID {
|
|
|
|
|
allowed, err := pull_service.IsUserAllowedToMerge(ctx, issue.PullRequest, ctx.Repo.Permission, ctx.Doer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("IsUserAllowedToMerge", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !allowed {
|
|
|
|
|
ctx.HTTPError(http.StatusForbidden, "user has no permission to cancel the scheduled auto merge")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-11 16:44:20 +02:00
|
|
|
if err := automerge.RemoveScheduledAutoMerge(ctx, ctx.Doer, issue.PullRequest); err != nil {
|
|
|
|
|
if db.IsErrNotExist(err) {
|
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.pulls.auto_merge_not_scheduled"))
|
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.ServerError("RemoveScheduledAutoMerge", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.pulls.auto_merge_canceled_schedule"))
|
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/pulls/%d", ctx.Repo.RepoLink, issue.Index))
|
2015-09-02 09:26:56 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:39:12 +02:00
|
|
|
func stopTimerIfAvailable(ctx *context.Context, user *user_model.User, issue *issues_model.Issue) error {
|
2025-06-25 08:22:58 +09:00
|
|
|
_, err := issues_model.FinishIssueStopwatch(ctx, user, issue)
|
|
|
|
|
return err
|
2019-02-05 12:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-24 22:28:02 +08:00
|
|
|
func PullsNewRedirect(ctx *context.Context) {
|
|
|
|
|
branch := ctx.PathParam("*")
|
|
|
|
|
redirectRepo := ctx.Repo.Repository
|
|
|
|
|
repo := ctx.Repo.Repository
|
|
|
|
|
if repo.IsFork {
|
|
|
|
|
if err := repo.GetBaseRepo(ctx); err != nil {
|
|
|
|
|
ctx.ServerError("GetBaseRepo", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
redirectRepo = repo.BaseRepo
|
|
|
|
|
branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
|
|
|
|
|
}
|
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/compare/%s...%s?expand=1", redirectRepo.Link(), util.PathEscapeSegments(redirectRepo.DefaultBranch), util.PathEscapeSegments(branch)))
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:04:31 +08:00
|
|
|
// CompareAndPullRequestPost response for creating pull request
|
2021-01-26 23:36:53 +08:00
|
|
|
func CompareAndPullRequestPost(ctx *context.Context) {
|
2021-04-06 20:44:05 +01:00
|
|
|
form := web.GetForm(ctx).(*forms.CreateIssueForm)
|
2015-09-01 19:07:02 -04:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
|
|
|
|
|
ctx.Data["PageIsComparePull"] = true
|
|
|
|
|
ctx.Data["IsDiffCompare"] = true
|
2018-08-13 21:04:39 +02:00
|
|
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
2020-10-05 07:49:33 +02:00
|
|
|
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
|
|
|
|
|
upload.AddUploadContext(ctx, "comment")
|
2021-11-10 03:57:58 +08:00
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(unit.TypePullRequests)
|
2015-09-01 19:07:02 -04:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
repo = ctx.Repo.Repository
|
|
|
|
|
attachments []string
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
ci := ParseCompareInfo(ctx)
|
2015-09-01 19:07:02 -04:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-08 21:08:36 +02:00
|
|
|
if ctx.Data["IsNoMergeBase"] == true {
|
|
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.no_common_history"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-09-01 19:07:02 -04:00
|
|
|
|
2024-11-11 04:07:54 +08:00
|
|
|
validateRet := ValidateRepoMetasForNewIssue(ctx, *form, true)
|
2015-09-01 19:07:02 -04:00
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 12:48:31 +08:00
|
|
|
labelIDs, assigneeIDs, milestoneID, projectID := validateRet.LabelIDs, validateRet.AssigneeIDs, validateRet.MilestoneID, validateRet.ProjectID
|
|
|
|
|
|
2020-08-18 12:23:45 +08:00
|
|
|
if setting.Attachment.Enabled {
|
2016-08-11 05:48:08 -07:00
|
|
|
attachments = form.Files
|
2015-09-01 19:07:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2023-06-19 16:25:36 +08:00
|
|
|
ctx.JSONError(ctx.GetErrMsg())
|
2015-09-01 19:07:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 12:45:32 +01:00
|
|
|
if util.IsEmptyString(form.Title) {
|
2023-06-19 16:25:36 +08:00
|
|
|
ctx.JSONError(ctx.Tr("repo.issues.new.title_empty"))
|
2019-01-21 12:45:32 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 13:21:04 -08:00
|
|
|
// Check if a pull request already exists with the same head and base branch.
|
2025-12-25 17:51:30 -08:00
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, repo.ID, ci.HeadRef.ShortName(), ci.BaseRef.ShortName(), issues_model.PullRequestFlowGithub)
|
2025-12-17 13:21:04 -08:00
|
|
|
if err != nil && !issues_model.IsErrPullRequestNotExist(err) {
|
|
|
|
|
ctx.ServerError("GetUnmergedPullRequest", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if pr != nil {
|
|
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.new.already_existed"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 15:58:49 +08:00
|
|
|
content := form.Content
|
|
|
|
|
if filename := ctx.Req.Form.Get("template-file"); filename != "" {
|
|
|
|
|
if template, err := issue_template.UnmarshalFromRepo(ctx.Repo.GitRepo, ctx.Repo.Repository.DefaultBranch, filename); err == nil {
|
|
|
|
|
content = issue_template.RenderToMarkdown(template, ctx.Req.Form)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
pullIssue := &issues_model.Issue{
|
2015-09-01 19:07:02 -04:00
|
|
|
RepoID: repo.ID,
|
2021-11-16 18:18:25 +00:00
|
|
|
Repo: repo,
|
2016-08-14 03:32:24 -07:00
|
|
|
Title: form.Title,
|
2022-03-22 08:03:22 +01:00
|
|
|
PosterID: ctx.Doer.ID,
|
|
|
|
|
Poster: ctx.Doer,
|
2015-09-01 19:07:02 -04:00
|
|
|
MilestoneID: milestoneID,
|
|
|
|
|
IsPull: true,
|
2022-09-02 15:58:49 +08:00
|
|
|
Content: content,
|
2015-09-01 19:07:02 -04:00
|
|
|
}
|
2022-06-13 17:37:59 +08:00
|
|
|
pullRequest := &issues_model.PullRequest{
|
2022-04-28 17:45:33 +02:00
|
|
|
HeadRepoID: ci.HeadRepo.ID,
|
|
|
|
|
BaseRepoID: repo.ID,
|
2025-12-25 17:51:30 -08:00
|
|
|
HeadBranch: ci.HeadRef.ShortName(),
|
|
|
|
|
BaseBranch: ci.BaseRef.ShortName(),
|
2022-04-28 17:45:33 +02:00
|
|
|
HeadRepo: ci.HeadRepo,
|
|
|
|
|
BaseRepo: repo,
|
2025-12-25 17:51:30 -08:00
|
|
|
MergeBase: ci.MergeBase,
|
2022-06-13 17:37:59 +08:00
|
|
|
Type: issues_model.PullRequestGitea,
|
2022-04-28 17:45:33 +02:00
|
|
|
AllowMaintainerEdit: form.AllowMaintainerEdit,
|
2016-02-24 13:56:54 +01:00
|
|
|
}
|
2016-08-15 14:04:44 -07:00
|
|
|
// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
|
|
|
|
|
// instead of 500.
|
2024-11-09 12:48:31 +08:00
|
|
|
prOpts := &pull_service.NewPullRequestOptions{
|
|
|
|
|
Repo: repo,
|
|
|
|
|
Issue: pullIssue,
|
|
|
|
|
LabelIDs: labelIDs,
|
|
|
|
|
AttachmentUUIDs: attachments,
|
|
|
|
|
PullRequest: pullRequest,
|
|
|
|
|
AssigneeIDs: assigneeIDs,
|
|
|
|
|
Reviewers: validateRet.Reviewers,
|
|
|
|
|
TeamReviewers: validateRet.TeamReviewers,
|
2026-01-04 08:45:36 -08:00
|
|
|
ProjectID: projectID,
|
2024-11-09 12:48:31 +08:00
|
|
|
}
|
|
|
|
|
if err := pull_service.NewPullRequest(ctx, prOpts); err != nil {
|
2024-08-17 01:04:54 +08:00
|
|
|
switch {
|
|
|
|
|
case repo_model.IsErrUserDoesNotHaveAccessToRepo(err):
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
|
2024-08-17 01:04:54 +08:00
|
|
|
case git.IsErrPushRejected(err):
|
2020-06-08 19:07:41 +01:00
|
|
|
pushrejErr := err.(*git.ErrPushRejected)
|
|
|
|
|
message := pushrejErr.Message
|
|
|
|
|
if len(message) == 0 {
|
2023-06-19 16:25:36 +08:00
|
|
|
ctx.JSONError(ctx.Tr("repo.pulls.push_rejected_no_message"))
|
|
|
|
|
return
|
2020-06-08 19:07:41 +01:00
|
|
|
}
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2023-06-19 16:25:36 +08:00
|
|
|
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.push_rejected_summary"),
|
|
|
|
|
"Details": utils.SanitizeFlashErrorString(pushrejErr.Message),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-02 23:05:07 +08:00
|
|
|
ctx.JSONError(flashError)
|
2024-08-17 01:04:54 +08:00
|
|
|
case errors.Is(err, user_model.ErrBlockedUser):
|
2024-03-04 09:16:03 +01:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
|
|
|
|
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.new.blocked_user"),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.JSONError(flashError)
|
2024-08-17 01:04:54 +08:00
|
|
|
case errors.Is(err, issues_model.ErrMustCollaborator):
|
2024-07-29 11:21:22 +09:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
|
|
|
|
"Message": ctx.Tr("repo.pulls.push_rejected"),
|
|
|
|
|
"Summary": ctx.Tr("repo.pulls.new.must_collaborator"),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("CompareAndPullRequest.HTMLString", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.JSONError(flashError)
|
2024-08-17 01:04:54 +08:00
|
|
|
default:
|
|
|
|
|
// It's an unexpected error.
|
|
|
|
|
// If it happens, we should add another case to handle it.
|
|
|
|
|
log.Error("Unexpected error of NewPullRequest: %T %s", err, err)
|
|
|
|
|
ctx.ServerError("CompareAndPullRequest", err)
|
2018-05-09 18:29:04 +02:00
|
|
|
}
|
2015-09-01 19:07:02 -04:00
|
|
|
return
|
2015-12-10 11:18:56 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-24 13:56:54 +01:00
|
|
|
log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
|
2023-06-19 16:25:36 +08:00
|
|
|
ctx.JSONRedirect(pullIssue.Link())
|
2014-03-24 18:25:15 +08:00
|
|
|
}
|
2015-10-24 03:36:47 -04:00
|
|
|
|
2017-06-21 04:00:03 +03:00
|
|
|
// CleanUpPullRequest responses for delete merged branch when PR has been merged
|
2025-10-21 22:06:56 -07:00
|
|
|
// Used by "DeleteBranchLink" for "delete branch" button
|
2017-06-21 04:00:03 +03:00
|
|
|
func CleanUpPullRequest(ctx *context.Context) {
|
2023-08-07 11:43:18 +08:00
|
|
|
issue, ok := getPullInfo(ctx)
|
|
|
|
|
if !ok {
|
2017-06-21 04:00:03 +03:00
|
|
|
return
|
|
|
|
|
}
|
2025-10-21 22:06:56 -07:00
|
|
|
deleteBranchAfterMergeAndFlashMessage(ctx, issue.PullRequest.ID)
|
|
|
|
|
if ctx.Written() {
|
2017-06-21 04:00:03 +03:00
|
|
|
return
|
|
|
|
|
}
|
2025-10-21 22:06:56 -07:00
|
|
|
ctx.JSONRedirect(issue.Link())
|
2017-06-21 04:00:03 +03:00
|
|
|
}
|
2018-01-05 11:56:52 +01:00
|
|
|
|
|
|
|
|
// DownloadPullDiff render a pull's raw diff
|
|
|
|
|
func DownloadPullDiff(ctx *context.Context) {
|
2019-12-13 22:21:06 +00:00
|
|
|
DownloadPullDiffOrPatch(ctx, false)
|
2018-01-05 11:56:52 +01:00
|
|
|
}
|
2018-01-07 14:10:20 +01:00
|
|
|
|
|
|
|
|
// DownloadPullPatch render a pull's raw patch
|
|
|
|
|
func DownloadPullPatch(ctx *context.Context) {
|
2019-12-13 22:21:06 +00:00
|
|
|
DownloadPullDiffOrPatch(ctx, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DownloadPullDiffOrPatch render a pull's raw diff or patch
|
|
|
|
|
func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) {
|
2024-12-24 21:47:45 +08:00
|
|
|
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
|
2018-01-07 14:10:20 +01:00
|
|
|
if err != nil {
|
2022-06-13 17:37:59 +08:00
|
|
|
if issues_model.IsErrPullRequestNotExist(err) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(err)
|
2018-01-07 14:10:20 +01:00
|
|
|
} else {
|
2021-10-05 21:41:48 +07:00
|
|
|
ctx.ServerError("GetPullRequestByIndex", err)
|
2018-01-07 14:10:20 +01:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 23:09:49 +02:00
|
|
|
binary := ctx.FormBool("binary")
|
2018-01-07 14:10:20 +01:00
|
|
|
|
2022-01-19 23:26:57 +00:00
|
|
|
if err := pull_service.DownloadDiffOrPatch(ctx, pr, ctx, patch, binary); err != nil {
|
2019-12-13 22:21:06 +00:00
|
|
|
ctx.ServerError("DownloadDiffOrPatch", err)
|
2018-01-07 14:10:20 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-16 07:20:25 +01:00
|
|
|
|
|
|
|
|
// UpdatePullRequestTarget change pull request's target branch
|
|
|
|
|
func UpdatePullRequestTarget(ctx *context.Context) {
|
|
|
|
|
issue := GetActionIssue(ctx)
|
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-07-06 02:52:12 +08:00
|
|
|
pr := issue.PullRequest
|
2019-12-16 07:20:25 +01:00
|
|
|
if !issue.IsPull {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNotFound)
|
2019-12-16 07:20:25 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if !ctx.IsSigned || (!issue.IsPoster(ctx.Doer.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2019-12-16 07:20:25 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 09:42:15 +08:00
|
|
|
targetBranch := ctx.FormTrim("target_branch")
|
2019-12-16 07:20:25 +01:00
|
|
|
if len(targetBranch) == 0 {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusNoContent)
|
2019-12-16 07:20:25 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if err := pull_service.ChangeTargetBranch(ctx, pr, ctx.Doer, targetBranch); err != nil {
|
2025-10-03 15:16:17 -07:00
|
|
|
switch {
|
|
|
|
|
case git_model.IsErrBranchNotExist(err):
|
|
|
|
|
errorMessage := ctx.Tr("form.target_branch_not_exist")
|
|
|
|
|
|
|
|
|
|
ctx.Flash.Error(errorMessage)
|
|
|
|
|
ctx.JSON(http.StatusBadRequest, map[string]any{
|
|
|
|
|
"error": err.Error(),
|
|
|
|
|
"user_error": errorMessage,
|
|
|
|
|
})
|
|
|
|
|
case issues_model.IsErrPullRequestAlreadyExists(err):
|
2022-06-13 17:37:59 +08:00
|
|
|
err := err.(issues_model.ErrPullRequestAlreadyExists)
|
2019-12-16 07:20:25 +01:00
|
|
|
|
|
|
|
|
RepoRelPath := ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
|
2022-07-12 17:32:37 -04:00
|
|
|
errorMessage := ctx.Tr("repo.pulls.has_pull_request", html.EscapeString(ctx.Repo.RepoLink+"/pulls/"+strconv.FormatInt(err.IssueID, 10)), html.EscapeString(RepoRelPath), err.IssueID) // FIXME: Creates url inside locale string
|
2019-12-16 07:20:25 +01:00
|
|
|
|
|
|
|
|
ctx.Flash.Error(errorMessage)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusConflict, map[string]any{
|
2019-12-16 07:20:25 +01:00
|
|
|
"error": err.Error(),
|
|
|
|
|
"user_error": errorMessage,
|
|
|
|
|
})
|
2025-10-03 15:16:17 -07:00
|
|
|
case issues_model.IsErrIssueIsClosed(err):
|
2019-12-16 07:20:25 +01:00
|
|
|
errorMessage := ctx.Tr("repo.pulls.is_closed")
|
|
|
|
|
|
|
|
|
|
ctx.Flash.Error(errorMessage)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusConflict, map[string]any{
|
2019-12-16 07:20:25 +01:00
|
|
|
"error": err.Error(),
|
|
|
|
|
"user_error": errorMessage,
|
|
|
|
|
})
|
2025-10-03 15:16:17 -07:00
|
|
|
case pull_service.IsErrPullRequestHasMerged(err):
|
2019-12-16 07:20:25 +01:00
|
|
|
errorMessage := ctx.Tr("repo.pulls.has_merged")
|
|
|
|
|
|
|
|
|
|
ctx.Flash.Error(errorMessage)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusConflict, map[string]any{
|
2019-12-16 07:20:25 +01:00
|
|
|
"error": err.Error(),
|
|
|
|
|
"user_error": errorMessage,
|
|
|
|
|
})
|
2025-10-03 15:16:17 -07:00
|
|
|
case git_model.IsErrBranchesEqual(err):
|
2019-12-16 07:20:25 +01:00
|
|
|
errorMessage := ctx.Tr("repo.pulls.nothing_to_compare")
|
|
|
|
|
|
|
|
|
|
ctx.Flash.Error(errorMessage)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusBadRequest, map[string]any{
|
2019-12-16 07:20:25 +01:00
|
|
|
"error": err.Error(),
|
|
|
|
|
"user_error": errorMessage,
|
|
|
|
|
})
|
2025-10-03 15:16:17 -07:00
|
|
|
default:
|
2019-12-16 07:20:25 +01:00
|
|
|
ctx.ServerError("UpdatePullRequestTarget", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.PullRequestChangeTargetBranch(ctx, ctx.Doer, pr, targetBranch)
|
2019-12-16 07:20:25 +01:00
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
2019-12-16 07:20:25 +01:00
|
|
|
"base_branch": pr.BaseBranch,
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-04-28 17:45:33 +02:00
|
|
|
|
|
|
|
|
// SetAllowEdits allow edits from maintainers to PRs
|
|
|
|
|
func SetAllowEdits(ctx *context.Context) {
|
|
|
|
|
form := web.GetForm(ctx).(*forms.UpdateAllowEditsForm)
|
|
|
|
|
|
2024-12-24 21:47:45 +08:00
|
|
|
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
|
2022-04-28 17:45:33 +02:00
|
|
|
if err != nil {
|
2022-06-13 17:37:59 +08:00
|
|
|
if issues_model.IsErrPullRequestNotExist(err) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.NotFound(err)
|
2022-04-28 17:45:33 +02:00
|
|
|
} else {
|
|
|
|
|
ctx.ServerError("GetPullRequestByIndex", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := pull_service.SetAllowEdits(ctx, ctx.Doer, pr, form.AllowMaintainerEdit); err != nil {
|
2024-09-09 22:23:07 -04:00
|
|
|
if errors.Is(err, pull_service.ErrUserHasNoPermissionForAction) {
|
2025-02-17 14:13:17 +08:00
|
|
|
ctx.HTTPError(http.StatusForbidden)
|
2022-04-28 17:45:33 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.ServerError("SetAllowEdits", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
2022-04-28 17:45:33 +02:00
|
|
|
"allow_maintainer_edit": pr.AllowMaintainerEdit,
|
|
|
|
|
})
|
|
|
|
|
}
|