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.

168 lines
4.2 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 2017 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. "path"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "xorm.io/xorm"
  14. )
  15. // LFSLock represents a git lfs lock of repository.
  16. type LFSLock struct {
  17. ID int64 `xorm:"pk autoincr"`
  18. Repo *Repository `xorm:"-"`
  19. RepoID int64 `xorm:"INDEX NOT NULL"`
  20. Owner *User `xorm:"-"`
  21. OwnerID int64 `xorm:"INDEX NOT NULL"`
  22. Path string `xorm:"TEXT"`
  23. Created time.Time `xorm:"created"`
  24. }
  25. // BeforeInsert is invoked from XORM before inserting an object of this type.
  26. func (l *LFSLock) BeforeInsert() {
  27. l.OwnerID = l.Owner.ID
  28. l.RepoID = l.Repo.ID
  29. l.Path = cleanPath(l.Path)
  30. }
  31. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  32. func (l *LFSLock) AfterLoad(session *xorm.Session) {
  33. var err error
  34. l.Owner, err = getUserByID(session, l.OwnerID)
  35. if err != nil {
  36. log.Error("LFS lock AfterLoad failed OwnerId[%d] not found: %v", l.OwnerID, err)
  37. }
  38. l.Repo, err = getRepositoryByID(session, l.RepoID)
  39. if err != nil {
  40. log.Error("LFS lock AfterLoad failed RepoId[%d] not found: %v", l.RepoID, err)
  41. }
  42. }
  43. func cleanPath(p string) string {
  44. return path.Clean("/" + p)[1:]
  45. }
  46. // APIFormat convert a Release to lfs.LFSLock
  47. func (l *LFSLock) APIFormat() *api.LFSLock {
  48. return &api.LFSLock{
  49. ID: strconv.FormatInt(l.ID, 10),
  50. Path: l.Path,
  51. LockedAt: l.Created.Round(time.Second),
  52. Owner: &api.LFSLockOwner{
  53. Name: l.Owner.DisplayName(),
  54. },
  55. }
  56. }
  57. // CreateLFSLock creates a new lock.
  58. func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
  59. err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)
  60. if err != nil {
  61. return nil, err
  62. }
  63. lock.Path = cleanPath(lock.Path)
  64. l, err := GetLFSLock(lock.Repo, lock.Path)
  65. if err == nil {
  66. return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
  67. }
  68. if !IsErrLFSLockNotExist(err) {
  69. return nil, err
  70. }
  71. _, err = x.InsertOne(lock)
  72. return lock, err
  73. }
  74. // GetLFSLock returns release by given path.
  75. func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
  76. path = cleanPath(path)
  77. rel := &LFSLock{RepoID: repo.ID}
  78. has, err := x.Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if !has {
  83. return nil, ErrLFSLockNotExist{0, repo.ID, path}
  84. }
  85. return rel, nil
  86. }
  87. // GetLFSLockByID returns release by given id.
  88. func GetLFSLockByID(id int64) (*LFSLock, error) {
  89. lock := new(LFSLock)
  90. has, err := x.ID(id).Get(lock)
  91. if err != nil {
  92. return nil, err
  93. } else if !has {
  94. return nil, ErrLFSLockNotExist{id, 0, ""}
  95. }
  96. return lock, nil
  97. }
  98. // GetLFSLockByRepoID returns a list of locks of repository.
  99. func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
  100. sess := x.NewSession()
  101. defer sess.Close()
  102. if page >= 0 && pageSize > 0 {
  103. start := 0
  104. if page > 0 {
  105. start = (page - 1) * pageSize
  106. }
  107. sess.Limit(pageSize, start)
  108. }
  109. lfsLocks := make([]*LFSLock, 0, pageSize)
  110. return lfsLocks, sess.Find(&lfsLocks, &LFSLock{RepoID: repoID})
  111. }
  112. // CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.
  113. func CountLFSLockByRepoID(repoID int64) (int64, error) {
  114. return x.Count(&LFSLock{RepoID: repoID})
  115. }
  116. // DeleteLFSLockByID deletes a lock by given ID.
  117. func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
  118. lock, err := GetLFSLockByID(id)
  119. if err != nil {
  120. return nil, err
  121. }
  122. err = CheckLFSAccessForRepo(u, lock.Repo, AccessModeWrite)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if !force && u.ID != lock.OwnerID {
  127. return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
  128. }
  129. _, err = x.ID(id).Delete(new(LFSLock))
  130. return lock, err
  131. }
  132. //CheckLFSAccessForRepo check needed access mode base on action
  133. func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
  134. if u == nil {
  135. return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
  136. }
  137. perm, err := GetUserRepoPermission(repo, u)
  138. if err != nil {
  139. return err
  140. }
  141. if !perm.CanAccess(mode, UnitTypeCode) {
  142. return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
  143. }
  144. return nil
  145. }