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.

298 lines
8.1 KiB

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
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/go-xorm/xorm"
  10. )
  11. // IssueAssignees saves all issue assignees
  12. type IssueAssignees struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. AssigneeID int64 `xorm:"INDEX"`
  15. IssueID int64 `xorm:"INDEX"`
  16. }
  17. // This loads all assignees of an issue
  18. func (issue *Issue) loadAssignees(e Engine) (err error) {
  19. // Reset maybe preexisting assignees
  20. issue.Assignees = []*User{}
  21. err = e.Table("`user`").
  22. Join("INNER", "issue_assignees", "assignee_id = `user`.id").
  23. Where("issue_assignees.issue_id = ?", issue.ID).
  24. Find(&issue.Assignees)
  25. if err != nil {
  26. return err
  27. }
  28. // Check if we have at least one assignee and if yes put it in as `Assignee`
  29. if len(issue.Assignees) > 0 {
  30. issue.Assignee = issue.Assignees[0]
  31. }
  32. return
  33. }
  34. // GetAssigneesByIssue returns everyone assigned to that issue
  35. func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) {
  36. return getAssigneesByIssue(x, issue)
  37. }
  38. func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error) {
  39. err = issue.loadAssignees(e)
  40. if err != nil {
  41. return assignees, err
  42. }
  43. return issue.Assignees, nil
  44. }
  45. // IsUserAssignedToIssue returns true when the user is assigned to the issue
  46. func IsUserAssignedToIssue(issue *Issue, user *User) (isAssigned bool, err error) {
  47. isAssigned, err = x.Exist(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
  48. return
  49. }
  50. // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
  51. func DeleteNotPassedAssignee(issue *Issue, doer *User, assignees []*User) (err error) {
  52. var found bool
  53. for _, assignee := range issue.Assignees {
  54. found = false
  55. for _, alreadyAssignee := range assignees {
  56. if assignee.ID == alreadyAssignee.ID {
  57. found = true
  58. break
  59. }
  60. }
  61. if !found {
  62. // This function also does comments and hooks, which is why we call it seperatly instead of directly removing the assignees here
  63. if err := UpdateAssignee(issue, doer, assignee.ID); err != nil {
  64. return err
  65. }
  66. }
  67. }
  68. return nil
  69. }
  70. // MakeAssigneeList concats a string with all names of the assignees. Useful for logs.
  71. func MakeAssigneeList(issue *Issue) (assigneeList string, err error) {
  72. err = issue.loadAssignees(x)
  73. if err != nil {
  74. return "", err
  75. }
  76. for in, assignee := range issue.Assignees {
  77. assigneeList += assignee.Name
  78. if len(issue.Assignees) > (in + 1) {
  79. assigneeList += ", "
  80. }
  81. }
  82. return
  83. }
  84. // ClearAssigneeByUserID deletes all assignments of an user
  85. func clearAssigneeByUserID(sess *xorm.Session, userID int64) (err error) {
  86. _, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
  87. return
  88. }
  89. // AddAssigneeIfNotAssigned adds an assignee only if he isn't aleady assigned to the issue
  90. func AddAssigneeIfNotAssigned(issue *Issue, doer *User, assigneeID int64) (err error) {
  91. // Check if the user is already assigned
  92. isAssigned, err := IsUserAssignedToIssue(issue, &User{ID: assigneeID})
  93. if err != nil {
  94. return err
  95. }
  96. if !isAssigned {
  97. return issue.ChangeAssignee(doer, assigneeID)
  98. }
  99. return nil
  100. }
  101. // UpdateAssignee deletes or adds an assignee to an issue
  102. func UpdateAssignee(issue *Issue, doer *User, assigneeID int64) (err error) {
  103. return issue.ChangeAssignee(doer, assigneeID)
  104. }
  105. // ChangeAssignee changes the Assignee of this issue.
  106. func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
  107. sess := x.NewSession()
  108. defer sess.Close()
  109. if err := sess.Begin(); err != nil {
  110. return err
  111. }
  112. if err := issue.changeAssignee(sess, doer, assigneeID, false); err != nil {
  113. return err
  114. }
  115. if err := sess.Commit(); err != nil {
  116. return err
  117. }
  118. go HookQueue.Add(issue.RepoID)
  119. return nil
  120. }
  121. func (issue *Issue) changeAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (err error) {
  122. // Update the assignee
  123. removed, err := updateIssueAssignee(sess, issue, assigneeID)
  124. if err != nil {
  125. return fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  126. }
  127. // Repo infos
  128. if err = issue.loadRepo(sess); err != nil {
  129. return fmt.Errorf("loadRepo: %v", err)
  130. }
  131. // Comment
  132. if _, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed); err != nil {
  133. return fmt.Errorf("createAssigneeComment: %v", err)
  134. }
  135. // if pull request is in the middle of creation - don't call webhook
  136. if isCreate {
  137. return nil
  138. }
  139. if issue.IsPull {
  140. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypePullRequests)
  141. if err = issue.loadPullRequest(sess); err != nil {
  142. return fmt.Errorf("loadPullRequest: %v", err)
  143. }
  144. issue.PullRequest.Issue = issue
  145. apiPullRequest := &api.PullRequestPayload{
  146. Index: issue.Index,
  147. PullRequest: issue.PullRequest.apiFormat(sess),
  148. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  149. Sender: doer.APIFormat(),
  150. }
  151. if removed {
  152. apiPullRequest.Action = api.HookIssueUnassigned
  153. } else {
  154. apiPullRequest.Action = api.HookIssueAssigned
  155. }
  156. if err := prepareWebhooks(sess, issue.Repo, HookEventPullRequest, apiPullRequest); err != nil {
  157. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  158. return nil
  159. }
  160. } else {
  161. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypeIssues)
  162. apiIssue := &api.IssuePayload{
  163. Index: issue.Index,
  164. Issue: issue.apiFormat(sess),
  165. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  166. Sender: doer.APIFormat(),
  167. }
  168. if removed {
  169. apiIssue.Action = api.HookIssueUnassigned
  170. } else {
  171. apiIssue.Action = api.HookIssueAssigned
  172. }
  173. if err := prepareWebhooks(sess, issue.Repo, HookEventIssues, apiIssue); err != nil {
  174. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  175. return nil
  176. }
  177. }
  178. return nil
  179. }
  180. // UpdateAPIAssignee is a helper function to add or delete one or multiple issue assignee(s)
  181. // Deleting is done the GitHub way (quote from their api documentation):
  182. // https://developer.github.com/v3/issues/#edit-an-issue
  183. // "assignees" (array): Logins for Users to assign to this issue.
  184. // Pass one or more user logins to replace the set of assignees on this Issue.
  185. // Send an empty array ([]) to clear all assignees from the Issue.
  186. func UpdateAPIAssignee(issue *Issue, oneAssignee string, multipleAssignees []string, doer *User) (err error) {
  187. var allNewAssignees []*User
  188. // Keep the old assignee thingy for compatibility reasons
  189. if oneAssignee != "" {
  190. // Prevent double adding assignees
  191. var isDouble bool
  192. for _, assignee := range multipleAssignees {
  193. if assignee == oneAssignee {
  194. isDouble = true
  195. break
  196. }
  197. }
  198. if !isDouble {
  199. multipleAssignees = append(multipleAssignees, oneAssignee)
  200. }
  201. }
  202. // Loop through all assignees to add them
  203. for _, assigneeName := range multipleAssignees {
  204. assignee, err := GetUserByName(assigneeName)
  205. if err != nil {
  206. return err
  207. }
  208. allNewAssignees = append(allNewAssignees, assignee)
  209. }
  210. // Delete all old assignees not passed
  211. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  212. return err
  213. }
  214. // Add all new assignees
  215. // Update the assignee. The function will check if the user exists, is already
  216. // assigned (which he shouldn't as we deleted all assignees before) and
  217. // has access to the repo.
  218. for _, assignee := range allNewAssignees {
  219. // Extra method to prevent double adding (which would result in removing)
  220. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  221. if err != nil {
  222. return err
  223. }
  224. }
  225. return
  226. }
  227. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  228. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  229. // Keeping the old assigning method for compatibility reasons
  230. if oneAssignee != "" {
  231. // Prevent double adding assignees
  232. var isDouble bool
  233. for _, assignee := range multipleAssignees {
  234. if assignee == oneAssignee {
  235. isDouble = true
  236. break
  237. }
  238. }
  239. if !isDouble {
  240. multipleAssignees = append(multipleAssignees, oneAssignee)
  241. }
  242. }
  243. // Get the IDs of all assignees
  244. assigneeIDs = GetUserIDsByNames(multipleAssignees)
  245. return
  246. }