aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authordelvh <leon@kske.dev>2022-05-07 20:28:10 +0200
committerGitHub <noreply@github.com>2022-05-07 20:28:10 +0200
commit5ca224a789394d00cc1efb5afcae7b87aa7d1e49 (patch)
treed88bddc7a08449a2c8f2b4e5ae47d3ef067a1cc0 /routers
parent59b30f060a840cde305952ef7bc344fa4101c0d5 (diff)
downloadgitea-5ca224a789394d00cc1efb5afcae7b87aa7d1e49.tar.gz
gitea-5ca224a789394d00cc1efb5afcae7b87aa7d1e49.zip
Allow to mark files in a PR as viewed (#19007)
Users can now mark files in PRs as viewed, resulting in them not being shown again by default when they reopen the PR again.
Diffstat (limited to 'routers')
-rw-r--r--routers/web/repo/pull.go37
-rw-r--r--routers/web/repo/pull_review.go46
-rw-r--r--routers/web/web.go1
3 files changed, 72 insertions, 12 deletions
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go
index 7cedeec10e..a0b3567738 100644
--- a/routers/web/repo/pull.go
+++ b/routers/web/repo/pull.go
@@ -685,22 +685,35 @@ func ViewPullFiles(ctx *context.Context) {
if fileOnly && (len(files) == 2 || len(files) == 1) {
maxLines, maxFiles = -1, -1
}
-
- diff, err := gitdiff.GetDiff(gitRepo,
- &gitdiff.DiffOptions{
- BeforeCommitID: startCommitID,
- AfterCommitID: endCommitID,
- SkipTo: ctx.FormString("skip-to"),
- MaxLines: maxLines,
- MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
- MaxFiles: maxFiles,
- WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
- }, ctx.FormStrings("files")...)
+ diffOptions := &gitdiff.DiffOptions{
+ BeforeCommitID: startCommitID,
+ AfterCommitID: endCommitID,
+ SkipTo: ctx.FormString("skip-to"),
+ MaxLines: maxLines,
+ MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
+ MaxFiles: maxFiles,
+ WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
+ }
+
+ var methodWithError string
+ var diff *gitdiff.Diff
+ if !ctx.IsSigned {
+ diff, err = gitdiff.GetDiff(gitRepo, diffOptions, files...)
+ methodWithError = "GetDiff"
+ } else {
+ diff, err = gitdiff.SyncAndGetUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diffOptions, files...)
+ methodWithError = "SyncAndGetUserSpecificDiff"
+ }
if err != nil {
- ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
+ ctx.ServerError(methodWithError, err)
return
}
+ ctx.PageData["prReview"] = map[string]interface{}{
+ "numberOfFiles": diff.NumFiles,
+ "numberOfViewedFiles": diff.NumViewedFiles,
+ }
+
if err = diff.LoadComments(ctx, issue, ctx.Doer); err != nil {
ctx.ServerError("LoadComments", err)
return
diff --git a/routers/web/repo/pull_review.go b/routers/web/repo/pull_review.go
index 939b0037a0..98272ed48d 100644
--- a/routers/web/repo/pull_review.go
+++ b/routers/web/repo/pull_review.go
@@ -9,8 +9,10 @@ import (
"net/http"
"code.gitea.io/gitea/models"
+ pull_model "code.gitea.io/gitea/models/pull"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web"
@@ -242,3 +244,47 @@ func DismissReview(ctx *context.Context) {
ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, comm.Issue.Index, comm.HashTag()))
}
+
+// viewedFilesUpdate Struct to parse the body of a request to update the reviewed files of a PR
+// If you want to implement an API to update the review, simply move this struct into modules.
+type viewedFilesUpdate struct {
+ Files map[string]bool `json:"files"`
+ HeadCommitSHA string `json:"headCommitSHA"`
+}
+
+func UpdateViewedFiles(ctx *context.Context) {
+ // Find corresponding PR
+ issue := checkPullInfo(ctx)
+ if ctx.Written() {
+ return
+ }
+ pull := issue.PullRequest
+
+ var data *viewedFilesUpdate
+ err := json.NewDecoder(ctx.Req.Body).Decode(&data)
+ if err != nil {
+ log.Warn("Attempted to update a review but could not parse request body: %v", err)
+ ctx.Resp.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ // Expect the review to have been now if no head commit was supplied
+ if data.HeadCommitSHA == "" {
+ data.HeadCommitSHA = pull.HeadCommitID
+ }
+
+ updatedFiles := make(map[string]pull_model.ViewedState, len(data.Files))
+ for file, viewed := range data.Files {
+
+ // Only unviewed and viewed are possible, has-changed can not be set from the outside
+ state := pull_model.Unviewed
+ if viewed {
+ state = pull_model.Viewed
+ }
+ updatedFiles[file] = state
+ }
+
+ if err := pull_model.UpdateReviewState(ctx, ctx.Doer.ID, pull.ID, data.HeadCommitSHA, updatedFiles); err != nil {
+ ctx.ServerError("UpdateReview", err)
+ }
+}
diff --git a/routers/web/web.go b/routers/web/web.go
index dcaad3d2bd..38754025ee 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -849,6 +849,7 @@ func RegisterRoutes(m *web.Route) {
m.Post("/deadline", bindIgnErr(structs.EditDeadlineOption{}), repo.UpdateIssueDeadline)
m.Post("/watch", repo.IssueWatch)
m.Post("/ref", repo.UpdateIssueRef)
+ m.Post("/viewed-files", repo.UpdateViewedFiles)
m.Group("/dependency", func() {
m.Post("/add", repo.AddDependency)
m.Post("/delete", repo.RemoveDependency)