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.

742 lines
20 KiB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
9 years ago
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Change target branch for pull request (#6488) * Adds functionality to change target branch of created pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use const instead of var in JavaScript additions Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Check if branches are equal and if PR already exists before changing target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Make sure to check all commits Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Print error messages for user as error flash message Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Disallow changing target branch of closed or merged pull requests Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Resolve conflicts after merge of upstream/master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change order of branch select fields Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes duplicate check Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use ctx.Tr for translations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Recompile JS Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use correct translation namespace Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove redundant if condition Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves most change branch logic into pull service Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Completes comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Add Ref to ChangesPayload for logging changed target branches instead of creating a new struct Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Revert changes to go.mod Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Directly use createComment method Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return 404 if pull request is not found. Move written check up Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Remove variable declaration Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return client errors on change pull request target errors Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error in commit.HasPreviousCommit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds blank line Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Test patch before persisting new target branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update patch before testing (not working) Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes patch calls when changeing pull request target Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unneeded check for base name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ChangeTargetBranch completely to pull service. Update patch status. Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Set webhook mode after errors were validated Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update PR in one transaction Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Move logic for check if head is equal with branch to pull model Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment and simplify return Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjust CreateComment method call Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
4 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "io"
  9. "strings"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. )
  15. // PullRequestType defines pull request type
  16. type PullRequestType int
  17. // Enumerate all the pull request types
  18. const (
  19. PullRequestGitea PullRequestType = iota
  20. PullRequestGit
  21. )
  22. // PullRequestStatus defines pull request status
  23. type PullRequestStatus int
  24. // Enumerate all the pull request status
  25. const (
  26. PullRequestStatusConflict PullRequestStatus = iota
  27. PullRequestStatusChecking
  28. PullRequestStatusMergeable
  29. PullRequestStatusManuallyMerged
  30. PullRequestStatusError
  31. )
  32. // PullRequest represents relation between pull request and repositories.
  33. type PullRequest struct {
  34. ID int64 `xorm:"pk autoincr"`
  35. Type PullRequestType
  36. Status PullRequestStatus
  37. ConflictedFiles []string `xorm:"TEXT JSON"`
  38. IssueID int64 `xorm:"INDEX"`
  39. Issue *Issue `xorm:"-"`
  40. Index int64
  41. HeadRepoID int64 `xorm:"INDEX"`
  42. HeadRepo *Repository `xorm:"-"`
  43. BaseRepoID int64 `xorm:"INDEX"`
  44. BaseRepo *Repository `xorm:"-"`
  45. HeadBranch string
  46. BaseBranch string
  47. ProtectedBranch *ProtectedBranch `xorm:"-"`
  48. MergeBase string `xorm:"VARCHAR(40)"`
  49. HasMerged bool `xorm:"INDEX"`
  50. MergedCommitID string `xorm:"VARCHAR(40)"`
  51. MergerID int64 `xorm:"INDEX"`
  52. Merger *User `xorm:"-"`
  53. MergedUnix timeutil.TimeStamp `xorm:"updated INDEX"`
  54. }
  55. // MustHeadUserName returns the HeadRepo's username if failed return blank
  56. func (pr *PullRequest) MustHeadUserName() string {
  57. if err := pr.LoadHeadRepo(); err != nil {
  58. log.Error("LoadHeadRepo: %v", err)
  59. return ""
  60. }
  61. return pr.HeadRepo.OwnerName
  62. }
  63. // Note: don't try to get Issue because will end up recursive querying.
  64. func (pr *PullRequest) loadAttributes(e Engine) (err error) {
  65. if pr.HasMerged && pr.Merger == nil {
  66. pr.Merger, err = getUserByID(e, pr.MergerID)
  67. if IsErrUserNotExist(err) {
  68. pr.MergerID = -1
  69. pr.Merger = NewGhostUser()
  70. } else if err != nil {
  71. return fmt.Errorf("getUserByID [%d]: %v", pr.MergerID, err)
  72. }
  73. }
  74. return nil
  75. }
  76. // LoadAttributes loads pull request attributes from database
  77. func (pr *PullRequest) LoadAttributes() error {
  78. return pr.loadAttributes(x)
  79. }
  80. // LoadBaseRepo loads pull request base repository from database
  81. func (pr *PullRequest) LoadBaseRepo() error {
  82. if pr.BaseRepo == nil {
  83. if pr.HeadRepoID == pr.BaseRepoID && pr.HeadRepo != nil {
  84. pr.BaseRepo = pr.HeadRepo
  85. return nil
  86. }
  87. var repo Repository
  88. if has, err := x.ID(pr.BaseRepoID).Get(&repo); err != nil {
  89. return err
  90. } else if !has {
  91. return ErrRepoNotExist{ID: pr.BaseRepoID}
  92. }
  93. pr.BaseRepo = &repo
  94. }
  95. return nil
  96. }
  97. // LoadHeadRepo loads pull request head repository from database
  98. func (pr *PullRequest) LoadHeadRepo() error {
  99. if pr.HeadRepo == nil {
  100. if pr.HeadRepoID == pr.BaseRepoID && pr.BaseRepo != nil {
  101. pr.HeadRepo = pr.BaseRepo
  102. return nil
  103. }
  104. var repo Repository
  105. if has, err := x.ID(pr.HeadRepoID).Get(&repo); err != nil {
  106. return err
  107. } else if !has {
  108. return ErrRepoNotExist{ID: pr.HeadRepoID}
  109. }
  110. pr.HeadRepo = &repo
  111. }
  112. return nil
  113. }
  114. // LoadIssue loads issue information from database
  115. func (pr *PullRequest) LoadIssue() (err error) {
  116. return pr.loadIssue(x)
  117. }
  118. func (pr *PullRequest) loadIssue(e Engine) (err error) {
  119. if pr.Issue != nil {
  120. return nil
  121. }
  122. pr.Issue, err = getIssueByID(e, pr.IssueID)
  123. if err == nil {
  124. pr.Issue.PullRequest = pr
  125. }
  126. return err
  127. }
  128. // LoadProtectedBranch loads the protected branch of the base branch
  129. func (pr *PullRequest) LoadProtectedBranch() (err error) {
  130. return pr.loadProtectedBranch(x)
  131. }
  132. func (pr *PullRequest) loadProtectedBranch(e Engine) (err error) {
  133. if pr.BaseRepo == nil {
  134. if pr.BaseRepoID == 0 {
  135. return nil
  136. }
  137. pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
  138. if err != nil {
  139. return
  140. }
  141. }
  142. pr.ProtectedBranch, err = getProtectedBranchBy(e, pr.BaseRepo.ID, pr.BaseBranch)
  143. return
  144. }
  145. // GetDefaultMergeMessage returns default message used when merging pull request
  146. func (pr *PullRequest) GetDefaultMergeMessage() string {
  147. if pr.HeadRepo == nil {
  148. var err error
  149. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  150. if err != nil {
  151. log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
  152. return ""
  153. }
  154. }
  155. if err := pr.LoadIssue(); err != nil {
  156. log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err)
  157. return ""
  158. }
  159. return fmt.Sprintf("Merge pull request '%s' (#%d) from %s/%s into %s", pr.Issue.Title, pr.Issue.Index, pr.MustHeadUserName(), pr.HeadBranch, pr.BaseBranch)
  160. }
  161. // GetCommitMessages returns the commit messages between head and merge base (if there is one)
  162. func (pr *PullRequest) GetCommitMessages() string {
  163. if err := pr.LoadIssue(); err != nil {
  164. log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err)
  165. return ""
  166. }
  167. if err := pr.Issue.LoadPoster(); err != nil {
  168. log.Error("Cannot load poster %d for pr id %d, index %d Error: %v", pr.Issue.PosterID, pr.ID, pr.Index, err)
  169. return ""
  170. }
  171. if pr.HeadRepo == nil {
  172. var err error
  173. pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
  174. if err != nil {
  175. log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
  176. return ""
  177. }
  178. }
  179. gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  180. if err != nil {
  181. log.Error("Unable to open head repository: Error: %v", err)
  182. return ""
  183. }
  184. defer gitRepo.Close()
  185. headCommit, err := gitRepo.GetBranchCommit(pr.HeadBranch)
  186. if err != nil {
  187. log.Error("Unable to get head commit: %s Error: %v", pr.HeadBranch, err)
  188. return ""
  189. }
  190. mergeBase, err := gitRepo.GetCommit(pr.MergeBase)
  191. if err != nil {
  192. log.Error("Unable to get merge base commit: %s Error: %v", pr.MergeBase, err)
  193. return ""
  194. }
  195. limit := setting.Repository.PullRequest.DefaultMergeMessageCommitsLimit
  196. list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0)
  197. if err != nil {
  198. log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
  199. return ""
  200. }
  201. maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
  202. posterSig := pr.Issue.Poster.NewGitSig().String()
  203. authorsMap := map[string]bool{}
  204. authors := make([]string, 0, list.Len())
  205. stringBuilder := strings.Builder{}
  206. element := list.Front()
  207. for element != nil {
  208. commit := element.Value.(*git.Commit)
  209. if maxSize < 0 || stringBuilder.Len() < maxSize {
  210. toWrite := []byte(commit.CommitMessage)
  211. if len(toWrite) > maxSize-stringBuilder.Len() && maxSize > -1 {
  212. toWrite = append(toWrite[:maxSize-stringBuilder.Len()], "..."...)
  213. }
  214. if _, err := stringBuilder.Write(toWrite); err != nil {
  215. log.Error("Unable to write commit message Error: %v", err)
  216. return ""
  217. }
  218. if _, err := stringBuilder.WriteRune('\n'); err != nil {
  219. log.Error("Unable to write commit message Error: %v", err)
  220. return ""
  221. }
  222. }
  223. authorString := commit.Author.String()
  224. if !authorsMap[authorString] && authorString != posterSig {
  225. authors = append(authors, authorString)
  226. authorsMap[authorString] = true
  227. }
  228. element = element.Next()
  229. }
  230. // Consider collecting the remaining authors
  231. if limit >= 0 && setting.Repository.PullRequest.DefaultMergeMessageAllAuthors {
  232. skip := limit
  233. limit = 30
  234. for {
  235. list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
  236. if err != nil {
  237. log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
  238. return ""
  239. }
  240. if list.Len() == 0 {
  241. break
  242. }
  243. element := list.Front()
  244. for element != nil {
  245. commit := element.Value.(*git.Commit)
  246. authorString := commit.Author.String()
  247. if !authorsMap[authorString] && authorString != posterSig {
  248. authors = append(authors, authorString)
  249. authorsMap[authorString] = true
  250. }
  251. element = element.Next()
  252. }
  253. }
  254. }
  255. if len(authors) > 0 {
  256. if _, err := stringBuilder.WriteRune('\n'); err != nil {
  257. log.Error("Unable to write to string builder Error: %v", err)
  258. return ""
  259. }
  260. }
  261. for _, author := range authors {
  262. if _, err := stringBuilder.Write([]byte("Co-authored-by: ")); err != nil {
  263. log.Error("Unable to write to string builder Error: %v", err)
  264. return ""
  265. }
  266. if _, err := stringBuilder.Write([]byte(author)); err != nil {
  267. log.Error("Unable to write to string builder Error: %v", err)
  268. return ""
  269. }
  270. if _, err := stringBuilder.WriteRune('\n'); err != nil {
  271. log.Error("Unable to write to string builder Error: %v", err)
  272. return ""
  273. }
  274. }
  275. return stringBuilder.String()
  276. }
  277. // GetApprovers returns the approvers of the pull request
  278. func (pr *PullRequest) GetApprovers() string {
  279. stringBuilder := strings.Builder{}
  280. if err := pr.getReviewedByLines(&stringBuilder); err != nil {
  281. log.Error("Unable to getReviewedByLines: Error: %v", err)
  282. return ""
  283. }
  284. return stringBuilder.String()
  285. }
  286. func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
  287. maxReviewers := setting.Repository.PullRequest.DefaultMergeMessageMaxApprovers
  288. if maxReviewers == 0 {
  289. return nil
  290. }
  291. sess := x.NewSession()
  292. defer sess.Close()
  293. if err := sess.Begin(); err != nil {
  294. return err
  295. }
  296. // Note: This doesn't page as we only expect a very limited number of reviews
  297. reviews, err := findReviews(sess, FindReviewOptions{
  298. Type: ReviewTypeApprove,
  299. IssueID: pr.IssueID,
  300. OfficialOnly: setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly,
  301. })
  302. if err != nil {
  303. log.Error("Unable to FindReviews for PR ID %d: %v", pr.ID, err)
  304. return err
  305. }
  306. reviewersWritten := 0
  307. for _, review := range reviews {
  308. if maxReviewers > 0 && reviewersWritten > maxReviewers {
  309. break
  310. }
  311. if err := review.loadReviewer(sess); err != nil && !IsErrUserNotExist(err) {
  312. log.Error("Unable to LoadReviewer[%d] for PR ID %d : %v", review.ReviewerID, pr.ID, err)
  313. return err
  314. } else if review.Reviewer == nil {
  315. continue
  316. }
  317. if _, err := writer.Write([]byte("Reviewed-by: ")); err != nil {
  318. return err
  319. }
  320. if _, err := writer.Write([]byte(review.Reviewer.NewGitSig().String())); err != nil {
  321. return err
  322. }
  323. if _, err := writer.Write([]byte{'\n'}); err != nil {
  324. return err
  325. }
  326. reviewersWritten++
  327. }
  328. return sess.Commit()
  329. }
  330. // GetDefaultSquashMessage returns default message used when squash and merging pull request
  331. func (pr *PullRequest) GetDefaultSquashMessage() string {
  332. if err := pr.LoadIssue(); err != nil {
  333. log.Error("LoadIssue: %v", err)
  334. return ""
  335. }
  336. return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)
  337. }
  338. // GetGitRefName returns git ref for hidden pull request branch
  339. func (pr *PullRequest) GetGitRefName() string {
  340. return fmt.Sprintf("refs/pull/%d/head", pr.Index)
  341. }
  342. func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
  343. pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
  344. if err != nil && !IsErrRepoNotExist(err) {
  345. return fmt.Errorf("getRepositoryByID(head): %v", err)
  346. }
  347. return nil
  348. }
  349. // GetHeadRepo loads the head repository
  350. func (pr *PullRequest) GetHeadRepo() error {
  351. return pr.getHeadRepo(x)
  352. }
  353. // GetBaseRepo loads the target repository
  354. func (pr *PullRequest) GetBaseRepo() (err error) {
  355. if pr.BaseRepo != nil {
  356. return nil
  357. }
  358. pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
  359. if err != nil {
  360. return fmt.Errorf("GetRepositoryByID(base): %v", err)
  361. }
  362. return nil
  363. }
  364. // IsChecking returns true if this pull request is still checking conflict.
  365. func (pr *PullRequest) IsChecking() bool {
  366. return pr.Status == PullRequestStatusChecking
  367. }
  368. // CanAutoMerge returns true if this pull request can be merged automatically.
  369. func (pr *PullRequest) CanAutoMerge() bool {
  370. return pr.Status == PullRequestStatusMergeable
  371. }
  372. // GetLastCommitStatus returns the last commit status for this pull request.
  373. func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
  374. if err = pr.GetHeadRepo(); err != nil {
  375. return nil, err
  376. }
  377. if pr.HeadRepo == nil {
  378. return nil, ErrPullRequestHeadRepoMissing{pr.ID, pr.HeadRepoID}
  379. }
  380. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  381. if err != nil {
  382. return nil, err
  383. }
  384. defer headGitRepo.Close()
  385. lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
  386. if err != nil {
  387. return nil, err
  388. }
  389. err = pr.LoadBaseRepo()
  390. if err != nil {
  391. return nil, err
  392. }
  393. statusList, err := GetLatestCommitStatus(pr.BaseRepo, lastCommitID, 0)
  394. if err != nil {
  395. return nil, err
  396. }
  397. return CalcCommitStatus(statusList), nil
  398. }
  399. // MergeStyle represents the approach to merge commits into base branch.
  400. type MergeStyle string
  401. const (
  402. // MergeStyleMerge create merge commit
  403. MergeStyleMerge MergeStyle = "merge"
  404. // MergeStyleRebase rebase before merging
  405. MergeStyleRebase MergeStyle = "rebase"
  406. // MergeStyleRebaseMerge rebase before merging with merge commit (--no-ff)
  407. MergeStyleRebaseMerge MergeStyle = "rebase-merge"
  408. // MergeStyleSquash squash commits into single commit before merging
  409. MergeStyleSquash MergeStyle = "squash"
  410. )
  411. // SetMerged sets a pull request to merged and closes the corresponding issue
  412. func (pr *PullRequest) SetMerged() (err error) {
  413. if pr.HasMerged {
  414. return fmt.Errorf("PullRequest[%d] already merged", pr.Index)
  415. }
  416. if pr.MergedCommitID == "" || pr.MergedUnix == 0 || pr.Merger == nil {
  417. return fmt.Errorf("Unable to merge PullRequest[%d], some required fields are empty", pr.Index)
  418. }
  419. pr.HasMerged = true
  420. sess := x.NewSession()
  421. defer sess.Close()
  422. if err = sess.Begin(); err != nil {
  423. return err
  424. }
  425. if err = pr.loadIssue(sess); err != nil {
  426. return err
  427. }
  428. if err = pr.Issue.loadRepo(sess); err != nil {
  429. return err
  430. }
  431. if err = pr.Issue.Repo.getOwner(sess); err != nil {
  432. return err
  433. }
  434. if _, err = pr.Issue.changeStatus(sess, pr.Merger, true); err != nil {
  435. return fmt.Errorf("Issue.changeStatus: %v", err)
  436. }
  437. if _, err = sess.ID(pr.ID).Cols("has_merged, status, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
  438. return fmt.Errorf("update pull request: %v", err)
  439. }
  440. if err = sess.Commit(); err != nil {
  441. return fmt.Errorf("Commit: %v", err)
  442. }
  443. return nil
  444. }
  445. // NewPullRequest creates new pull request with labels for repository.
  446. func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
  447. // Retry several times in case INSERT fails due to duplicate key for (repo_id, index); see #7887
  448. i := 0
  449. for {
  450. if err = newPullRequestAttempt(repo, pull, labelIDs, uuids, pr); err == nil {
  451. return nil
  452. }
  453. if !IsErrNewIssueInsert(err) {
  454. return err
  455. }
  456. if i++; i == issueMaxDupIndexAttempts {
  457. break
  458. }
  459. log.Error("NewPullRequest: error attempting to insert the new issue; will retry. Original error: %v", err)
  460. }
  461. return fmt.Errorf("NewPullRequest: too many errors attempting to insert the new issue. Last error was: %v", err)
  462. }
  463. func newPullRequestAttempt(repo *Repository, pull *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
  464. sess := x.NewSession()
  465. defer sess.Close()
  466. if err = sess.Begin(); err != nil {
  467. return err
  468. }
  469. if err = newIssue(sess, pull.Poster, NewIssueOptions{
  470. Repo: repo,
  471. Issue: pull,
  472. LabelIDs: labelIDs,
  473. Attachments: uuids,
  474. IsPull: true,
  475. }); err != nil {
  476. if IsErrUserDoesNotHaveAccessToRepo(err) || IsErrNewIssueInsert(err) {
  477. return err
  478. }
  479. return fmt.Errorf("newIssue: %v", err)
  480. }
  481. pr.Index = pull.Index
  482. pr.BaseRepo = repo
  483. pr.IssueID = pull.ID
  484. if _, err = sess.Insert(pr); err != nil {
  485. return fmt.Errorf("insert pull repo: %v", err)
  486. }
  487. if err = sess.Commit(); err != nil {
  488. return fmt.Errorf("Commit: %v", err)
  489. }
  490. return nil
  491. }
  492. // GetUnmergedPullRequest returns a pull request that is open and has not been merged
  493. // by given head/base and repo/branch.
  494. func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch string) (*PullRequest, error) {
  495. pr := new(PullRequest)
  496. has, err := x.
  497. Where("head_repo_id=? AND head_branch=? AND base_repo_id=? AND base_branch=? AND has_merged=? AND issue.is_closed=?",
  498. headRepoID, headBranch, baseRepoID, baseBranch, false, false).
  499. Join("INNER", "issue", "issue.id=pull_request.issue_id").
  500. Get(pr)
  501. if err != nil {
  502. return nil, err
  503. } else if !has {
  504. return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
  505. }
  506. return pr, nil
  507. }
  508. // GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status)
  509. // by given head information (repo and branch).
  510. func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
  511. pr := new(PullRequest)
  512. has, err := x.
  513. Where("head_repo_id = ? AND head_branch = ?", repoID, branch).
  514. OrderBy("id DESC").
  515. Get(pr)
  516. if !has {
  517. return nil, err
  518. }
  519. return pr, err
  520. }
  521. // GetPullRequestByIndex returns a pull request by the given index
  522. func GetPullRequestByIndex(repoID int64, index int64) (*PullRequest, error) {
  523. pr := &PullRequest{
  524. BaseRepoID: repoID,
  525. Index: index,
  526. }
  527. has, err := x.Get(pr)
  528. if err != nil {
  529. return nil, err
  530. } else if !has {
  531. return nil, ErrPullRequestNotExist{0, 0, 0, repoID, "", ""}
  532. }
  533. if err = pr.LoadAttributes(); err != nil {
  534. return nil, err
  535. }
  536. if err = pr.LoadIssue(); err != nil {
  537. return nil, err
  538. }
  539. return pr, nil
  540. }
  541. func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
  542. pr := new(PullRequest)
  543. has, err := e.ID(id).Get(pr)
  544. if err != nil {
  545. return nil, err
  546. } else if !has {
  547. return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
  548. }
  549. return pr, pr.loadAttributes(e)
  550. }
  551. // GetPullRequestByID returns a pull request by given ID.
  552. func GetPullRequestByID(id int64) (*PullRequest, error) {
  553. return getPullRequestByID(x, id)
  554. }
  555. func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
  556. pr := &PullRequest{
  557. IssueID: issueID,
  558. }
  559. has, err := e.Get(pr)
  560. if err != nil {
  561. return nil, err
  562. } else if !has {
  563. return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
  564. }
  565. return pr, pr.loadAttributes(e)
  566. }
  567. // GetPullRequestByIssueID returns pull request by given issue ID.
  568. func GetPullRequestByIssueID(issueID int64) (*PullRequest, error) {
  569. return getPullRequestByIssueID(x, issueID)
  570. }
  571. // Update updates all fields of pull request.
  572. func (pr *PullRequest) Update() error {
  573. _, err := x.ID(pr.ID).AllCols().Update(pr)
  574. return err
  575. }
  576. // UpdateCols updates specific fields of pull request.
  577. func (pr *PullRequest) UpdateCols(cols ...string) error {
  578. _, err := x.ID(pr.ID).Cols(cols...).Update(pr)
  579. return err
  580. }
  581. // IsWorkInProgress determine if the Pull Request is a Work In Progress by its title
  582. func (pr *PullRequest) IsWorkInProgress() bool {
  583. if err := pr.LoadIssue(); err != nil {
  584. log.Error("LoadIssue: %v", err)
  585. return false
  586. }
  587. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  588. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  589. return true
  590. }
  591. }
  592. return false
  593. }
  594. // IsFilesConflicted determines if the Pull Request has changes conflicting with the target branch.
  595. func (pr *PullRequest) IsFilesConflicted() bool {
  596. return len(pr.ConflictedFiles) > 0
  597. }
  598. // GetWorkInProgressPrefix returns the prefix used to mark the pull request as a work in progress.
  599. // It returns an empty string when none were found
  600. func (pr *PullRequest) GetWorkInProgressPrefix() string {
  601. if err := pr.LoadIssue(); err != nil {
  602. log.Error("LoadIssue: %v", err)
  603. return ""
  604. }
  605. for _, prefix := range setting.Repository.PullRequest.WorkInProgressPrefixes {
  606. if strings.HasPrefix(strings.ToUpper(pr.Issue.Title), prefix) {
  607. return pr.Issue.Title[0:len(prefix)]
  608. }
  609. }
  610. return ""
  611. }
  612. // IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
  613. func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
  614. var err error
  615. if err = pr.GetBaseRepo(); err != nil {
  616. return false, err
  617. }
  618. baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
  619. if err != nil {
  620. return false, err
  621. }
  622. baseCommit, err := baseGitRepo.GetBranchCommit(branchName)
  623. if err != nil {
  624. return false, err
  625. }
  626. if err = pr.GetHeadRepo(); err != nil {
  627. return false, err
  628. }
  629. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  630. if err != nil {
  631. return false, err
  632. }
  633. headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
  634. if err != nil {
  635. return false, err
  636. }
  637. return baseCommit.HasPreviousCommit(headCommit.ID)
  638. }