You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pull_review.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. pull_model "code.gitea.io/gitea/models/pull"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/json"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/web"
  16. "code.gitea.io/gitea/services/context"
  17. "code.gitea.io/gitea/services/context/upload"
  18. "code.gitea.io/gitea/services/forms"
  19. pull_service "code.gitea.io/gitea/services/pull"
  20. user_service "code.gitea.io/gitea/services/user"
  21. )
  22. const (
  23. tplDiffConversation base.TplName = "repo/diff/conversation"
  24. tplConversationOutdated base.TplName = "repo/diff/conversation_outdated"
  25. tplTimelineConversation base.TplName = "repo/issue/view_content/conversation"
  26. tplNewComment base.TplName = "repo/diff/new_comment"
  27. )
  28. // RenderNewCodeCommentForm will render the form for creating a new review comment
  29. func RenderNewCodeCommentForm(ctx *context.Context) {
  30. issue := GetActionIssue(ctx)
  31. if ctx.Written() {
  32. return
  33. }
  34. if !issue.IsPull {
  35. return
  36. }
  37. currentReview, err := issues_model.GetCurrentReview(ctx, ctx.Doer, issue)
  38. if err != nil && !issues_model.IsErrReviewNotExist(err) {
  39. ctx.ServerError("GetCurrentReview", err)
  40. return
  41. }
  42. ctx.Data["PageIsPullFiles"] = true
  43. ctx.Data["Issue"] = issue
  44. ctx.Data["CurrentReview"] = currentReview
  45. pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(issue.PullRequest.GetGitRefName())
  46. if err != nil {
  47. ctx.ServerError("GetRefCommitID", err)
  48. return
  49. }
  50. ctx.Data["AfterCommitID"] = pullHeadCommitID
  51. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  52. upload.AddUploadContext(ctx, "comment")
  53. ctx.HTML(http.StatusOK, tplNewComment)
  54. }
  55. // CreateCodeComment will create a code comment including an pending review if required
  56. func CreateCodeComment(ctx *context.Context) {
  57. form := web.GetForm(ctx).(*forms.CodeCommentForm)
  58. issue := GetActionIssue(ctx)
  59. if ctx.Written() {
  60. return
  61. }
  62. if !issue.IsPull {
  63. return
  64. }
  65. if ctx.HasError() {
  66. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  67. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  68. return
  69. }
  70. signedLine := form.Line
  71. if form.Side == "previous" {
  72. signedLine *= -1
  73. }
  74. var attachments []string
  75. if setting.Attachment.Enabled {
  76. attachments = form.Files
  77. }
  78. comment, err := pull_service.CreateCodeComment(ctx,
  79. ctx.Doer,
  80. ctx.Repo.GitRepo,
  81. issue,
  82. signedLine,
  83. form.Content,
  84. form.TreePath,
  85. !form.SingleReview,
  86. form.Reply,
  87. form.LatestCommitID,
  88. attachments,
  89. )
  90. if err != nil {
  91. ctx.ServerError("CreateCodeComment", err)
  92. return
  93. }
  94. if comment == nil {
  95. log.Trace("Comment not created: %-v #%d[%d]", ctx.Repo.Repository, issue.Index, issue.ID)
  96. ctx.Redirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  97. return
  98. }
  99. log.Trace("Comment created: %-v #%d[%d] Comment[%d]", ctx.Repo.Repository, issue.Index, issue.ID, comment.ID)
  100. renderConversation(ctx, comment, form.Origin)
  101. }
  102. // UpdateResolveConversation add or remove an Conversation resolved mark
  103. func UpdateResolveConversation(ctx *context.Context) {
  104. origin := ctx.FormString("origin")
  105. action := ctx.FormString("action")
  106. commentID := ctx.FormInt64("comment_id")
  107. comment, err := issues_model.GetCommentByID(ctx, commentID)
  108. if err != nil {
  109. ctx.ServerError("GetIssueByID", err)
  110. return
  111. }
  112. if err = comment.LoadIssue(ctx); err != nil {
  113. ctx.ServerError("comment.LoadIssue", err)
  114. return
  115. }
  116. if comment.Issue.RepoID != ctx.Repo.Repository.ID {
  117. ctx.NotFound("comment's repoID is incorrect", errors.New("comment's repoID is incorrect"))
  118. return
  119. }
  120. var permResult bool
  121. if permResult, err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer); err != nil {
  122. ctx.ServerError("CanMarkConversation", err)
  123. return
  124. }
  125. if !permResult {
  126. ctx.Error(http.StatusForbidden)
  127. return
  128. }
  129. if !comment.Issue.IsPull {
  130. ctx.Error(http.StatusBadRequest)
  131. return
  132. }
  133. if action == "Resolve" || action == "UnResolve" {
  134. err = issues_model.MarkConversation(ctx, comment, ctx.Doer, action == "Resolve")
  135. if err != nil {
  136. ctx.ServerError("MarkConversation", err)
  137. return
  138. }
  139. } else {
  140. ctx.Error(http.StatusBadRequest)
  141. return
  142. }
  143. renderConversation(ctx, comment, origin)
  144. }
  145. func renderConversation(ctx *context.Context, comment *issues_model.Comment, origin string) {
  146. ctx.Data["PageIsPullFiles"] = origin == "diff"
  147. showOutdatedComments := origin == "timeline" || ctx.Data["ShowOutdatedComments"].(bool)
  148. comments, err := issues_model.FetchCodeCommentsByLine(ctx, comment.Issue, ctx.Doer, comment.TreePath, comment.Line, showOutdatedComments)
  149. if err != nil {
  150. ctx.ServerError("FetchCodeCommentsByLine", err)
  151. return
  152. }
  153. if len(comments) == 0 {
  154. // if the comments are empty (deleted, outdated, etc), it's better to tell the users that it is outdated
  155. ctx.HTML(http.StatusOK, tplConversationOutdated)
  156. return
  157. }
  158. if err := comments.LoadAttachments(ctx); err != nil {
  159. ctx.ServerError("LoadAttachments", err)
  160. return
  161. }
  162. ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
  163. upload.AddUploadContext(ctx, "comment")
  164. ctx.Data["comments"] = comments
  165. if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, comment.Issue, ctx.Doer); err != nil {
  166. ctx.ServerError("CanMarkConversation", err)
  167. return
  168. }
  169. ctx.Data["Issue"] = comment.Issue
  170. if err = comment.Issue.LoadPullRequest(ctx); err != nil {
  171. ctx.ServerError("comment.Issue.LoadPullRequest", err)
  172. return
  173. }
  174. pullHeadCommitID, err := ctx.Repo.GitRepo.GetRefCommitID(comment.Issue.PullRequest.GetGitRefName())
  175. if err != nil {
  176. ctx.ServerError("GetRefCommitID", err)
  177. return
  178. }
  179. ctx.Data["AfterCommitID"] = pullHeadCommitID
  180. ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
  181. return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
  182. }
  183. if origin == "diff" {
  184. ctx.HTML(http.StatusOK, tplDiffConversation)
  185. } else if origin == "timeline" {
  186. ctx.HTML(http.StatusOK, tplTimelineConversation)
  187. } else {
  188. ctx.Error(http.StatusBadRequest, "Unknown origin: "+origin)
  189. }
  190. }
  191. // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
  192. func SubmitReview(ctx *context.Context) {
  193. form := web.GetForm(ctx).(*forms.SubmitReviewForm)
  194. issue := GetActionIssue(ctx)
  195. if ctx.Written() {
  196. return
  197. }
  198. if !issue.IsPull {
  199. return
  200. }
  201. if ctx.HasError() {
  202. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  203. ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  204. return
  205. }
  206. reviewType := form.ReviewType()
  207. switch reviewType {
  208. case issues_model.ReviewTypeUnknown:
  209. ctx.ServerError("ReviewType", fmt.Errorf("unknown ReviewType: %s", form.Type))
  210. return
  211. // can not approve/reject your own PR
  212. case issues_model.ReviewTypeApprove, issues_model.ReviewTypeReject:
  213. if issue.IsPoster(ctx.Doer.ID) {
  214. var translated string
  215. if reviewType == issues_model.ReviewTypeApprove {
  216. translated = ctx.Locale.TrString("repo.issues.review.self.approval")
  217. } else {
  218. translated = ctx.Locale.TrString("repo.issues.review.self.rejection")
  219. }
  220. ctx.Flash.Error(translated)
  221. ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  222. return
  223. }
  224. }
  225. var attachments []string
  226. if setting.Attachment.Enabled {
  227. attachments = form.Files
  228. }
  229. _, comm, err := pull_service.SubmitReview(ctx, ctx.Doer, ctx.Repo.GitRepo, issue, reviewType, form.Content, form.CommitID, attachments)
  230. if err != nil {
  231. if issues_model.IsContentEmptyErr(err) {
  232. ctx.Flash.Error(ctx.Tr("repo.issues.review.content.empty"))
  233. ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d/files", ctx.Repo.RepoLink, issue.Index))
  234. } else {
  235. ctx.ServerError("SubmitReview", err)
  236. }
  237. return
  238. }
  239. ctx.JSONRedirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, issue.Index, comm.HashTag()))
  240. }
  241. // DismissReview dismissing stale review by repo admin
  242. func DismissReview(ctx *context.Context) {
  243. form := web.GetForm(ctx).(*forms.DismissReviewForm)
  244. comm, err := pull_service.DismissReview(ctx, form.ReviewID, ctx.Repo.Repository.ID, form.Message, ctx.Doer, true, true)
  245. if err != nil {
  246. if pull_service.IsErrDismissRequestOnClosedPR(err) {
  247. ctx.Status(http.StatusForbidden)
  248. return
  249. }
  250. ctx.ServerError("pull_service.DismissReview", err)
  251. return
  252. }
  253. ctx.Redirect(fmt.Sprintf("%s/pulls/%d#%s", ctx.Repo.RepoLink, comm.Issue.Index, comm.HashTag()))
  254. }
  255. // viewedFilesUpdate Struct to parse the body of a request to update the reviewed files of a PR
  256. // If you want to implement an API to update the review, simply move this struct into modules.
  257. type viewedFilesUpdate struct {
  258. Files map[string]bool `json:"files"`
  259. HeadCommitSHA string `json:"headCommitSHA"`
  260. }
  261. func UpdateViewedFiles(ctx *context.Context) {
  262. // Find corresponding PR
  263. issue, ok := getPullInfo(ctx)
  264. if !ok {
  265. return
  266. }
  267. pull := issue.PullRequest
  268. var data *viewedFilesUpdate
  269. err := json.NewDecoder(ctx.Req.Body).Decode(&data)
  270. if err != nil {
  271. log.Warn("Attempted to update a review but could not parse request body: %v", err)
  272. ctx.Resp.WriteHeader(http.StatusBadRequest)
  273. return
  274. }
  275. // Expect the review to have been now if no head commit was supplied
  276. if data.HeadCommitSHA == "" {
  277. data.HeadCommitSHA = pull.HeadCommitID
  278. }
  279. updatedFiles := make(map[string]pull_model.ViewedState, len(data.Files))
  280. for file, viewed := range data.Files {
  281. // Only unviewed and viewed are possible, has-changed can not be set from the outside
  282. state := pull_model.Unviewed
  283. if viewed {
  284. state = pull_model.Viewed
  285. }
  286. updatedFiles[file] = state
  287. }
  288. if err := pull_model.UpdateReviewState(ctx, ctx.Doer.ID, pull.ID, data.HeadCommitSHA, updatedFiles); err != nil {
  289. ctx.ServerError("UpdateReview", err)
  290. }
  291. }