From 6d6cf70f38801ee770de862cbbe776e1b641895a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 28 Jul 2023 14:37:48 +0800 Subject: [PATCH 1/2] introduce pull request commit view files page --- routers/web/repo/pull.go | 73 ++++++++++++++++++++++++++------ routers/web/repo/pull_review.go | 2 +- routers/web/web.go | 1 + templates/repo/commits_list.tmpl | 2 + 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 237e53413f767..4efa99933b8bf 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -738,24 +738,59 @@ func ViewPullCommits(ctx *context.Context) { ctx.HTML(http.StatusOK, tplPullCommits) } +func ViewPullCommitFiles(ctx *context.Context) { + ctx.Data["PageIsPullCommits"] = true + issue := checkPullInfo(ctx) + if ctx.Written() { + return + } + var prInfo *git.CompareInfo + if issue.PullRequest.HasMerged { + prInfo = PrepareMergedViewPullInfo(ctx, issue) + } else { + prInfo = PrepareViewPullInfo(ctx, issue) + } + + if ctx.Written() { + return + } else if prInfo == nil { + ctx.NotFound("ViewPullCommits", nil) + return + } + + var index int = -1 + for i, commit := range prInfo.Commits { + if commit.ID.String() == ctx.Params("sha") { + index = i + break + } + } + if index == -1 { + ctx.NotFound("ViewPullCommitFiles", nil) + return + } + + var startCommitID string + if index == len(prInfo.Commits)-1 { + startCommitID = issue.PullRequest.MergeBase + } else { + startCommitID = prInfo.Commits[index+1].ID.String() + } + + baseViewPullCommitFiles(ctx, issue.PullRequest, startCommitID, ctx.Params("sha")) +} + // ViewPullFiles render pull request changed files list page func ViewPullFiles(ctx *context.Context) { - ctx.Data["PageIsPullList"] = true ctx.Data["PageIsPullFiles"] = true - + var startCommitID string + var endCommitID string + var prInfo *git.CompareInfo issue := checkPullInfo(ctx) if ctx.Written() { return } pull := issue.PullRequest - - var ( - startCommitID string - endCommitID string - gitRepo = ctx.Repo.GitRepo - ) - - var prInfo *git.CompareInfo if pull.HasMerged { prInfo = PrepareMergedViewPullInfo(ctx, issue) } else { @@ -769,7 +804,7 @@ func ViewPullFiles(ctx *context.Context) { return } - headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName()) + headCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(pull.GetGitRefName()) if err != nil { ctx.ServerError("GetRefCommitID", err) return @@ -777,6 +812,18 @@ func ViewPullFiles(ctx *context.Context) { startCommitID = prInfo.MergeBase endCommitID = headCommitID + baseViewPullCommitFiles(ctx, pull, startCommitID, endCommitID) +} + +func showOutdatedComments(ctx *context.Context) bool { + show, ok := ctx.Data["ShowOutdatedComments"].(bool) + return ok && show +} + +func baseViewPullCommitFiles(ctx *context.Context, pull *issues_model.PullRequest, startCommitID, endCommitID string) { + ctx.Data["PageIsPullList"] = true + + gitRepo := ctx.Repo.GitRepo ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name @@ -801,6 +848,8 @@ func ViewPullFiles(ctx *context.Context) { var methodWithError string var diff *gitdiff.Diff + var err error + issue := pull.Issue if !ctx.IsSigned { diff, err = gitdiff.GetDiff(gitRepo, diffOptions, files...) methodWithError = "GetDiff" @@ -818,7 +867,7 @@ func ViewPullFiles(ctx *context.Context) { "numberOfViewedFiles": diff.NumViewedFiles, } - if err = diff.LoadComments(ctx, issue, ctx.Doer, ctx.Data["ShowOutdatedComments"].(bool)); err != nil { + if err = diff.LoadComments(ctx, issue, ctx.Doer, showOutdatedComments(ctx)); err != nil { ctx.ServerError("LoadComments", err) return } diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go index c2271750c4d1d..77deb7583e6e5 100644 --- a/routers/web/repo/pull_review.go +++ b/routers/web/repo/pull_review.go @@ -160,7 +160,7 @@ func UpdateResolveConversation(ctx *context.Context) { } func renderConversation(ctx *context.Context, comment *issues_model.Comment) { - comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, ctx.Data["ShowOutdatedComments"].(bool)) + comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, showOutdatedComments(ctx)) if err != nil { ctx.ServerError("FetchCodeCommentsByLine", err) return diff --git a/routers/web/web.go b/routers/web/web.go index 0b519614453a7..578f7e6baf529 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1280,6 +1280,7 @@ func registerRoutes(m *web.Route) { m.Get(".diff", repo.DownloadPullDiff) m.Get(".patch", repo.DownloadPullPatch) m.Get("/commits", context.RepoRef(), repo.SetWhitespaceBehavior, repo.GetPullDiffStats, repo.ViewPullCommits) + m.Get("/commits/{sha}", context.RepoRef(), repo.SetWhitespaceBehavior, repo.GetPullDiffStats, repo.ViewPullCommitFiles) m.Post("/merge", context.RepoMustNotBeArchived(), web.Bind(forms.MergePullRequestForm{}), repo.MergePullRequest) m.Post("/cancel_auto_merge", context.RepoMustNotBeArchived(), repo.CancelAutoMergePullRequest) m.Post("/update", repo.UpdatePullRequest) diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index ef9d0654566f5..640bd5addaab4 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -43,6 +43,8 @@ {{end}} {{if $.PageIsWiki}} + {{else if $.PageIsPullCommits}} + {{else if $.Reponame}} {{else}} From c8dcf513060a322b86be052ee1773f7ad4193e33 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 28 Jul 2023 14:42:49 +0800 Subject: [PATCH 2/2] Fix lint --- routers/web/repo/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 4efa99933b8bf..66040cbc6ec13 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -758,7 +758,7 @@ func ViewPullCommitFiles(ctx *context.Context) { return } - var index int = -1 + index := -1 for i, commit := range prInfo.Commits { if commit.ID.String() == ctx.Params("sha") { index = i