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.

259 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
  1. // Copyright 2017 Gitea. 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. "container/list"
  7. "crypto/sha1"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. // CommitStatusState holds the state of a Status
  16. // It can be "pending", "success", "error", "failure", and "warning"
  17. type CommitStatusState string
  18. // IsWorseThan returns true if this State is worse than the given State
  19. func (css CommitStatusState) IsWorseThan(css2 CommitStatusState) bool {
  20. switch css {
  21. case CommitStatusError:
  22. return true
  23. case CommitStatusFailure:
  24. return css2 != CommitStatusError
  25. case CommitStatusWarning:
  26. return css2 != CommitStatusError && css2 != CommitStatusFailure
  27. case CommitStatusSuccess:
  28. return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning
  29. default:
  30. return css2 != CommitStatusError && css2 != CommitStatusFailure && css2 != CommitStatusWarning && css2 != CommitStatusSuccess
  31. }
  32. }
  33. const (
  34. // CommitStatusPending is for when the Status is Pending
  35. CommitStatusPending CommitStatusState = "pending"
  36. // CommitStatusSuccess is for when the Status is Success
  37. CommitStatusSuccess CommitStatusState = "success"
  38. // CommitStatusError is for when the Status is Error
  39. CommitStatusError CommitStatusState = "error"
  40. // CommitStatusFailure is for when the Status is Failure
  41. CommitStatusFailure CommitStatusState = "failure"
  42. // CommitStatusWarning is for when the Status is Warning
  43. CommitStatusWarning CommitStatusState = "warning"
  44. )
  45. // CommitStatus holds a single Status of a single Commit
  46. type CommitStatus struct {
  47. ID int64 `xorm:"pk autoincr"`
  48. Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  49. RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  50. Repo *Repository `xorm:"-"`
  51. State CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
  52. SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
  53. TargetURL string `xorm:"TEXT"`
  54. Description string `xorm:"TEXT"`
  55. ContextHash string `xorm:"char(40) index"`
  56. Context string `xorm:"TEXT"`
  57. Creator *User `xorm:"-"`
  58. CreatorID int64
  59. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  60. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  61. }
  62. func (status *CommitStatus) loadRepo(e Engine) (err error) {
  63. if status.Repo == nil {
  64. status.Repo, err = getRepositoryByID(e, status.RepoID)
  65. if err != nil {
  66. return fmt.Errorf("getRepositoryByID [%d]: %v", status.RepoID, err)
  67. }
  68. }
  69. if status.Creator == nil && status.CreatorID > 0 {
  70. status.Creator, err = getUserByID(e, status.CreatorID)
  71. if err != nil {
  72. return fmt.Errorf("getUserByID [%d]: %v", status.CreatorID, err)
  73. }
  74. }
  75. return nil
  76. }
  77. // APIURL returns the absolute APIURL to this commit-status.
  78. func (status *CommitStatus) APIURL() string {
  79. _ = status.loadRepo(x)
  80. return fmt.Sprintf("%sapi/v1/%s/statuses/%s",
  81. setting.AppURL, status.Repo.FullName(), status.SHA)
  82. }
  83. // APIFormat assumes some fields assigned with values:
  84. // Required - Repo, Creator
  85. func (status *CommitStatus) APIFormat() *api.Status {
  86. _ = status.loadRepo(x)
  87. apiStatus := &api.Status{
  88. Created: status.CreatedUnix.AsTime(),
  89. Updated: status.CreatedUnix.AsTime(),
  90. State: api.StatusState(status.State),
  91. TargetURL: status.TargetURL,
  92. Description: status.Description,
  93. ID: status.Index,
  94. URL: status.APIURL(),
  95. Context: status.Context,
  96. }
  97. if status.Creator != nil {
  98. apiStatus.Creator = status.Creator.APIFormat()
  99. }
  100. return apiStatus
  101. }
  102. // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
  103. func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
  104. var lastStatus *CommitStatus
  105. var state CommitStatusState
  106. for _, status := range statuses {
  107. if status.State.IsWorseThan(state) {
  108. state = status.State
  109. lastStatus = status
  110. }
  111. }
  112. if lastStatus == nil {
  113. if len(statuses) > 0 {
  114. lastStatus = statuses[0]
  115. } else {
  116. lastStatus = &CommitStatus{}
  117. }
  118. }
  119. return lastStatus
  120. }
  121. // GetCommitStatuses returns all statuses for a given commit.
  122. func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
  123. statuses := make([]*CommitStatus, 0, 10)
  124. return statuses, x.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
  125. }
  126. // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
  127. func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
  128. ids := make([]int64, 0, 10)
  129. err := x.Limit(10, page*10).
  130. Table(&CommitStatus{}).
  131. Where("repo_id = ?", repo.ID).And("sha = ?", sha).
  132. Select("max( id ) as id").
  133. GroupBy("context_hash").OrderBy("max( id ) desc").Find(&ids)
  134. if err != nil {
  135. return nil, err
  136. }
  137. statuses := make([]*CommitStatus, 0, len(ids))
  138. if len(ids) == 0 {
  139. return statuses, nil
  140. }
  141. return statuses, x.In("id", ids).Find(&statuses)
  142. }
  143. // NewCommitStatusOptions holds options for creating a CommitStatus
  144. type NewCommitStatusOptions struct {
  145. Repo *Repository
  146. Creator *User
  147. SHA string
  148. CommitStatus *CommitStatus
  149. }
  150. // NewCommitStatus save commit statuses into database
  151. func NewCommitStatus(opts NewCommitStatusOptions) error {
  152. if opts.Repo == nil {
  153. return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
  154. }
  155. repoPath := opts.Repo.RepoPath()
  156. if opts.Creator == nil {
  157. return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
  158. }
  159. sess := x.NewSession()
  160. defer sess.Close()
  161. if err := sess.Begin(); err != nil {
  162. return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
  163. }
  164. opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
  165. opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
  166. opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
  167. opts.CommitStatus.SHA = opts.SHA
  168. opts.CommitStatus.CreatorID = opts.Creator.ID
  169. opts.CommitStatus.RepoID = opts.Repo.ID
  170. // Get the next Status Index
  171. var nextIndex int64
  172. lastCommitStatus := &CommitStatus{
  173. SHA: opts.SHA,
  174. RepoID: opts.Repo.ID,
  175. }
  176. has, err := sess.Desc("index").Limit(1).Get(lastCommitStatus)
  177. if err != nil {
  178. if err := sess.Rollback(); err != nil {
  179. log.Error("NewCommitStatus: sess.Rollback: %v", err)
  180. }
  181. return fmt.Errorf("NewCommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  182. }
  183. if has {
  184. log.Debug("NewCommitStatus[%s, %s]: found", repoPath, opts.SHA)
  185. nextIndex = lastCommitStatus.Index
  186. }
  187. opts.CommitStatus.Index = nextIndex + 1
  188. log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index)
  189. opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
  190. // Insert new CommitStatus
  191. if _, err = sess.Insert(opts.CommitStatus); err != nil {
  192. if err := sess.Rollback(); err != nil {
  193. log.Error("Insert CommitStatus: sess.Rollback: %v", err)
  194. }
  195. return fmt.Errorf("Insert CommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  196. }
  197. return sess.Commit()
  198. }
  199. // SignCommitWithStatuses represents a commit with validation of signature and status state.
  200. type SignCommitWithStatuses struct {
  201. Status *CommitStatus
  202. *SignCommit
  203. }
  204. // ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
  205. func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
  206. var (
  207. newCommits = list.New()
  208. e = oldCommits.Front()
  209. )
  210. for e != nil {
  211. c := e.Value.(SignCommit)
  212. commit := SignCommitWithStatuses{
  213. SignCommit: &c,
  214. }
  215. statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0)
  216. if err != nil {
  217. log.Error("GetLatestCommitStatus: %v", err)
  218. } else {
  219. commit.Status = CalcCommitStatus(statuses)
  220. }
  221. newCommits.PushBack(commit)
  222. e = e.Next()
  223. }
  224. return newCommits
  225. }
  226. // hashCommitStatusContext hash context
  227. func hashCommitStatusContext(context string) string {
  228. return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
  229. }