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.

295 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. return sess.Commit()
  116. }
  117. func (issue *Issue) changeAssignee(sess *xorm.Session, doer *User, assigneeID int64, isCreate bool) (err error) {
  118. // Update the assignee
  119. removed, err := updateIssueAssignee(sess, issue, assigneeID)
  120. if err != nil {
  121. return fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  122. }
  123. // Repo infos
  124. if err = issue.loadRepo(sess); err != nil {
  125. return fmt.Errorf("loadRepo: %v", err)
  126. }
  127. // Comment
  128. if _, err = createAssigneeComment(sess, doer, issue.Repo, issue, assigneeID, removed); err != nil {
  129. return fmt.Errorf("createAssigneeComment: %v", err)
  130. }
  131. // if pull request is in the middle of creation - don't call webhook
  132. if isCreate {
  133. return nil
  134. }
  135. if issue.IsPull {
  136. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypePullRequests)
  137. if err = issue.loadPullRequest(sess); err != nil {
  138. return fmt.Errorf("loadPullRequest: %v", err)
  139. }
  140. issue.PullRequest.Issue = issue
  141. apiPullRequest := &api.PullRequestPayload{
  142. Index: issue.Index,
  143. PullRequest: issue.PullRequest.apiFormat(sess),
  144. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  145. Sender: doer.APIFormat(),
  146. }
  147. if removed {
  148. apiPullRequest.Action = api.HookIssueUnassigned
  149. } else {
  150. apiPullRequest.Action = api.HookIssueAssigned
  151. }
  152. if err := prepareWebhooks(sess, issue.Repo, HookEventPullRequest, apiPullRequest); err != nil {
  153. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  154. return nil
  155. }
  156. } else {
  157. mode, _ := accessLevelUnit(sess, doer, issue.Repo, UnitTypeIssues)
  158. apiIssue := &api.IssuePayload{
  159. Index: issue.Index,
  160. Issue: issue.apiFormat(sess),
  161. Repository: issue.Repo.innerAPIFormat(sess, mode, false),
  162. Sender: doer.APIFormat(),
  163. }
  164. if removed {
  165. apiIssue.Action = api.HookIssueUnassigned
  166. } else {
  167. apiIssue.Action = api.HookIssueAssigned
  168. }
  169. if err := prepareWebhooks(sess, issue.Repo, HookEventIssues, apiIssue); err != nil {
  170. log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
  171. return nil
  172. }
  173. }
  174. go HookQueue.Add(issue.RepoID)
  175. return nil
  176. }
  177. // UpdateAPIAssignee is a helper function to add or delete one or multiple issue assignee(s)
  178. // Deleting is done the GitHub way (quote from their api documentation):
  179. // https://developer.github.com/v3/issues/#edit-an-issue
  180. // "assignees" (array): Logins for Users to assign to this issue.
  181. // Pass one or more user logins to replace the set of assignees on this Issue.
  182. // Send an empty array ([]) to clear all assignees from the Issue.
  183. func UpdateAPIAssignee(issue *Issue, oneAssignee string, multipleAssignees []string, doer *User) (err error) {
  184. var allNewAssignees []*User
  185. // Keep the old assignee thingy for compatibility reasons
  186. if oneAssignee != "" {
  187. // Prevent double adding assignees
  188. var isDouble bool
  189. for _, assignee := range multipleAssignees {
  190. if assignee == oneAssignee {
  191. isDouble = true
  192. break
  193. }
  194. }
  195. if !isDouble {
  196. multipleAssignees = append(multipleAssignees, oneAssignee)
  197. }
  198. }
  199. // Loop through all assignees to add them
  200. for _, assigneeName := range multipleAssignees {
  201. assignee, err := GetUserByName(assigneeName)
  202. if err != nil {
  203. return err
  204. }
  205. allNewAssignees = append(allNewAssignees, assignee)
  206. }
  207. // Delete all old assignees not passed
  208. if err = DeleteNotPassedAssignee(issue, doer, allNewAssignees); err != nil {
  209. return err
  210. }
  211. // Add all new assignees
  212. // Update the assignee. The function will check if the user exists, is already
  213. // assigned (which he shouldn't as we deleted all assignees before) and
  214. // has access to the repo.
  215. for _, assignee := range allNewAssignees {
  216. // Extra method to prevent double adding (which would result in removing)
  217. err = AddAssigneeIfNotAssigned(issue, doer, assignee.ID)
  218. if err != nil {
  219. return err
  220. }
  221. }
  222. return
  223. }
  224. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  225. func MakeIDsFromAPIAssigneesToAdd(oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  226. // Keeping the old assigning method for compatibility reasons
  227. if oneAssignee != "" {
  228. // Prevent double adding assignees
  229. var isDouble bool
  230. for _, assignee := range multipleAssignees {
  231. if assignee == oneAssignee {
  232. isDouble = true
  233. break
  234. }
  235. }
  236. if !isDouble {
  237. multipleAssignees = append(multipleAssignees, oneAssignee)
  238. }
  239. }
  240. // Get the IDs of all assignees
  241. assigneeIDs = GetUserIDsByNames(multipleAssignees)
  242. return
  243. }