A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

288 lines
8.6 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 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
3 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. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "xorm.io/xorm"
  16. )
  17. // CommitStatus holds a single Status of a single Commit
  18. type CommitStatus struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. Index int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  21. RepoID int64 `xorm:"INDEX UNIQUE(repo_sha_index)"`
  22. Repo *Repository `xorm:"-"`
  23. State api.CommitStatusState `xorm:"VARCHAR(7) NOT NULL"`
  24. SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
  25. TargetURL string `xorm:"TEXT"`
  26. Description string `xorm:"TEXT"`
  27. ContextHash string `xorm:"char(40) index"`
  28. Context string `xorm:"TEXT"`
  29. Creator *User `xorm:"-"`
  30. CreatorID int64
  31. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. }
  34. func (status *CommitStatus) loadAttributes(e Engine) (err error) {
  35. if status.Repo == nil {
  36. status.Repo, err = getRepositoryByID(e, status.RepoID)
  37. if err != nil {
  38. return fmt.Errorf("getRepositoryByID [%d]: %v", status.RepoID, err)
  39. }
  40. }
  41. if status.Creator == nil && status.CreatorID > 0 {
  42. status.Creator, err = getUserByID(e, status.CreatorID)
  43. if err != nil {
  44. return fmt.Errorf("getUserByID [%d]: %v", status.CreatorID, err)
  45. }
  46. }
  47. return nil
  48. }
  49. // APIURL returns the absolute APIURL to this commit-status.
  50. func (status *CommitStatus) APIURL() string {
  51. _ = status.loadAttributes(x)
  52. return fmt.Sprintf("%sapi/v1/repos/%s/statuses/%s",
  53. setting.AppURL, status.Repo.FullName(), status.SHA)
  54. }
  55. // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
  56. func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
  57. var lastStatus *CommitStatus
  58. var state api.CommitStatusState
  59. for _, status := range statuses {
  60. if status.State.NoBetterThan(state) {
  61. state = status.State
  62. lastStatus = status
  63. }
  64. }
  65. if lastStatus == nil {
  66. if len(statuses) > 0 {
  67. lastStatus = statuses[0]
  68. } else {
  69. lastStatus = &CommitStatus{}
  70. }
  71. }
  72. return lastStatus
  73. }
  74. // CommitStatusOptions holds the options for query commit statuses
  75. type CommitStatusOptions struct {
  76. ListOptions
  77. State string
  78. SortType string
  79. }
  80. // GetCommitStatuses returns all statuses for a given commit.
  81. func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
  82. if opts.Page <= 0 {
  83. opts.Page = 1
  84. }
  85. if opts.PageSize <= 0 {
  86. opts.Page = ItemsPerPage
  87. }
  88. countSession := listCommitStatusesStatement(repo, sha, opts)
  89. countSession = opts.setSessionPagination(countSession)
  90. maxResults, err := countSession.Count(new(CommitStatus))
  91. if err != nil {
  92. log.Error("Count PRs: %v", err)
  93. return nil, maxResults, err
  94. }
  95. statuses := make([]*CommitStatus, 0, opts.PageSize)
  96. findSession := listCommitStatusesStatement(repo, sha, opts)
  97. findSession = opts.setSessionPagination(findSession)
  98. sortCommitStatusesSession(findSession, opts.SortType)
  99. return statuses, maxResults, findSession.Find(&statuses)
  100. }
  101. func listCommitStatusesStatement(repo *Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
  102. sess := x.Where("repo_id = ?", repo.ID).And("sha = ?", sha)
  103. switch opts.State {
  104. case "pending", "success", "error", "failure", "warning":
  105. sess.And("state = ?", opts.State)
  106. }
  107. return sess
  108. }
  109. func sortCommitStatusesSession(sess *xorm.Session, sortType string) {
  110. switch sortType {
  111. case "oldest":
  112. sess.Asc("created_unix")
  113. case "recentupdate":
  114. sess.Desc("updated_unix")
  115. case "leastupdate":
  116. sess.Asc("updated_unix")
  117. case "leastindex":
  118. sess.Desc("index")
  119. case "highestindex":
  120. sess.Asc("index")
  121. default:
  122. sess.Desc("created_unix")
  123. }
  124. }
  125. // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
  126. func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) {
  127. return getLatestCommitStatus(x, repoID, sha, listOptions)
  128. }
  129. func getLatestCommitStatus(e Engine, repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) {
  130. ids := make([]int64, 0, 10)
  131. sess := e.Table(&CommitStatus{}).
  132. Where("repo_id = ?", repoID).And("sha = ?", sha).
  133. Select("max( id ) as id").
  134. GroupBy("context_hash").OrderBy("max( id ) desc")
  135. sess = listOptions.setSessionPagination(sess)
  136. err := sess.Find(&ids)
  137. if err != nil {
  138. return nil, err
  139. }
  140. statuses := make([]*CommitStatus, 0, len(ids))
  141. if len(ids) == 0 {
  142. return statuses, nil
  143. }
  144. return statuses, x.In("id", ids).Find(&statuses)
  145. }
  146. // FindRepoRecentCommitStatusContexts returns repository's recent commit status contexts
  147. func FindRepoRecentCommitStatusContexts(repoID int64, before time.Duration) ([]string, error) {
  148. start := timeutil.TimeStampNow().AddDuration(-before)
  149. ids := make([]int64, 0, 10)
  150. if err := x.Table("commit_status").
  151. Where("repo_id = ?", repoID).
  152. And("updated_unix >= ?", start).
  153. Select("max( id ) as id").
  154. GroupBy("context_hash").OrderBy("max( id ) desc").
  155. Find(&ids); err != nil {
  156. return nil, err
  157. }
  158. contexts := make([]string, 0, len(ids))
  159. if len(ids) == 0 {
  160. return contexts, nil
  161. }
  162. return contexts, x.Select("context").Table("commit_status").In("id", ids).Find(&contexts)
  163. }
  164. // NewCommitStatusOptions holds options for creating a CommitStatus
  165. type NewCommitStatusOptions struct {
  166. Repo *Repository
  167. Creator *User
  168. SHA string
  169. CommitStatus *CommitStatus
  170. }
  171. // NewCommitStatus save commit statuses into database
  172. func NewCommitStatus(opts NewCommitStatusOptions) error {
  173. if opts.Repo == nil {
  174. return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
  175. }
  176. repoPath := opts.Repo.RepoPath()
  177. if opts.Creator == nil {
  178. return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
  179. }
  180. sess := x.NewSession()
  181. defer sess.Close()
  182. if err := sess.Begin(); err != nil {
  183. return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
  184. }
  185. opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
  186. opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
  187. opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
  188. opts.CommitStatus.SHA = opts.SHA
  189. opts.CommitStatus.CreatorID = opts.Creator.ID
  190. opts.CommitStatus.RepoID = opts.Repo.ID
  191. // Get the next Status Index
  192. var nextIndex int64
  193. lastCommitStatus := &CommitStatus{
  194. SHA: opts.SHA,
  195. RepoID: opts.Repo.ID,
  196. }
  197. has, err := sess.Desc("index").Limit(1).Get(lastCommitStatus)
  198. if err != nil {
  199. if err := sess.Rollback(); err != nil {
  200. log.Error("NewCommitStatus: sess.Rollback: %v", err)
  201. }
  202. return fmt.Errorf("NewCommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  203. }
  204. if has {
  205. log.Debug("NewCommitStatus[%s, %s]: found", repoPath, opts.SHA)
  206. nextIndex = lastCommitStatus.Index
  207. }
  208. opts.CommitStatus.Index = nextIndex + 1
  209. log.Debug("NewCommitStatus[%s, %s]: %d", repoPath, opts.SHA, opts.CommitStatus.Index)
  210. opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context)
  211. // Insert new CommitStatus
  212. if _, err = sess.Insert(opts.CommitStatus); err != nil {
  213. if err := sess.Rollback(); err != nil {
  214. log.Error("Insert CommitStatus: sess.Rollback: %v", err)
  215. }
  216. return fmt.Errorf("Insert CommitStatus[%s, %s]: %v", repoPath, opts.SHA, err)
  217. }
  218. return sess.Commit()
  219. }
  220. // SignCommitWithStatuses represents a commit with validation of signature and status state.
  221. type SignCommitWithStatuses struct {
  222. Status *CommitStatus
  223. Statuses []*CommitStatus
  224. *SignCommit
  225. }
  226. // ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
  227. func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
  228. var (
  229. newCommits = list.New()
  230. e = oldCommits.Front()
  231. )
  232. for e != nil {
  233. c := e.Value.(SignCommit)
  234. commit := SignCommitWithStatuses{
  235. SignCommit: &c,
  236. }
  237. statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
  238. if err != nil {
  239. log.Error("GetLatestCommitStatus: %v", err)
  240. } else {
  241. commit.Statuses = statuses
  242. commit.Status = CalcCommitStatus(statuses)
  243. }
  244. newCommits.PushBack(commit)
  245. e = e.Next()
  246. }
  247. return newCommits
  248. }
  249. // hashCommitStatusContext hash context
  250. func hashCommitStatusContext(context string) string {
  251. return fmt.Sprintf("%x", sha1.Sum([]byte(context)))
  252. }