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.

343 lines
9.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
9 years ago
8 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
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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
9 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
9 years ago
8 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
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/timeutil"
  16. "github.com/unknwon/com"
  17. "xorm.io/builder"
  18. )
  19. // ActionType represents the type of an action.
  20. type ActionType int
  21. // Possible action types.
  22. const (
  23. ActionCreateRepo ActionType = iota + 1 // 1
  24. ActionRenameRepo // 2
  25. ActionStarRepo // 3
  26. ActionWatchRepo // 4
  27. ActionCommitRepo // 5
  28. ActionCreateIssue // 6
  29. ActionCreatePullRequest // 7
  30. ActionTransferRepo // 8
  31. ActionPushTag // 9
  32. ActionCommentIssue // 10
  33. ActionMergePullRequest // 11
  34. ActionCloseIssue // 12
  35. ActionReopenIssue // 13
  36. ActionClosePullRequest // 14
  37. ActionReopenPullRequest // 15
  38. ActionDeleteTag // 16
  39. ActionDeleteBranch // 17
  40. ActionMirrorSyncPush // 18
  41. ActionMirrorSyncCreate // 19
  42. ActionMirrorSyncDelete // 20
  43. ActionApprovePullRequest // 21
  44. ActionRejectPullRequest // 22
  45. ActionCommentPull // 23
  46. )
  47. // Action represents user operation type and other information to
  48. // repository. It implemented interface base.Actioner so that can be
  49. // used in template render.
  50. type Action struct {
  51. ID int64 `xorm:"pk autoincr"`
  52. UserID int64 `xorm:"INDEX"` // Receiver user id.
  53. OpType ActionType
  54. ActUserID int64 `xorm:"INDEX"` // Action user id.
  55. ActUser *User `xorm:"-"`
  56. RepoID int64 `xorm:"INDEX"`
  57. Repo *Repository `xorm:"-"`
  58. CommentID int64 `xorm:"INDEX"`
  59. Comment *Comment `xorm:"-"`
  60. IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
  61. RefName string
  62. IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
  63. Content string `xorm:"TEXT"`
  64. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  65. }
  66. // GetOpType gets the ActionType of this action.
  67. func (a *Action) GetOpType() ActionType {
  68. return a.OpType
  69. }
  70. func (a *Action) loadActUser() {
  71. if a.ActUser != nil {
  72. return
  73. }
  74. var err error
  75. a.ActUser, err = GetUserByID(a.ActUserID)
  76. if err == nil {
  77. return
  78. } else if IsErrUserNotExist(err) {
  79. a.ActUser = NewGhostUser()
  80. } else {
  81. log.Error("GetUserByID(%d): %v", a.ActUserID, err)
  82. }
  83. }
  84. func (a *Action) loadRepo() {
  85. if a.Repo != nil {
  86. return
  87. }
  88. var err error
  89. a.Repo, err = GetRepositoryByID(a.RepoID)
  90. if err != nil {
  91. log.Error("GetRepositoryByID(%d): %v", a.RepoID, err)
  92. }
  93. }
  94. // GetActFullName gets the action's user full name.
  95. func (a *Action) GetActFullName() string {
  96. a.loadActUser()
  97. return a.ActUser.FullName
  98. }
  99. // GetActUserName gets the action's user name.
  100. func (a *Action) GetActUserName() string {
  101. a.loadActUser()
  102. return a.ActUser.Name
  103. }
  104. // ShortActUserName gets the action's user name trimmed to max 20
  105. // chars.
  106. func (a *Action) ShortActUserName() string {
  107. return base.EllipsisString(a.GetActUserName(), 20)
  108. }
  109. // GetDisplayName gets the action's display name based on DEFAULT_SHOW_FULL_NAME
  110. func (a *Action) GetDisplayName() string {
  111. if setting.UI.DefaultShowFullName {
  112. return a.GetActFullName()
  113. }
  114. return a.ShortActUserName()
  115. }
  116. // GetDisplayNameTitle gets the action's display name used for the title (tooltip) based on DEFAULT_SHOW_FULL_NAME
  117. func (a *Action) GetDisplayNameTitle() string {
  118. if setting.UI.DefaultShowFullName {
  119. return a.ShortActUserName()
  120. }
  121. return a.GetActFullName()
  122. }
  123. // GetActAvatar the action's user's avatar link
  124. func (a *Action) GetActAvatar() string {
  125. a.loadActUser()
  126. return a.ActUser.RelAvatarLink()
  127. }
  128. // GetRepoUserName returns the name of the action repository owner.
  129. func (a *Action) GetRepoUserName() string {
  130. a.loadRepo()
  131. return a.Repo.OwnerName
  132. }
  133. // ShortRepoUserName returns the name of the action repository owner
  134. // trimmed to max 20 chars.
  135. func (a *Action) ShortRepoUserName() string {
  136. return base.EllipsisString(a.GetRepoUserName(), 20)
  137. }
  138. // GetRepoName returns the name of the action repository.
  139. func (a *Action) GetRepoName() string {
  140. a.loadRepo()
  141. return a.Repo.Name
  142. }
  143. // ShortRepoName returns the name of the action repository
  144. // trimmed to max 33 chars.
  145. func (a *Action) ShortRepoName() string {
  146. return base.EllipsisString(a.GetRepoName(), 33)
  147. }
  148. // GetRepoPath returns the virtual path to the action repository.
  149. func (a *Action) GetRepoPath() string {
  150. return path.Join(a.GetRepoUserName(), a.GetRepoName())
  151. }
  152. // ShortRepoPath returns the virtual path to the action repository
  153. // trimmed to max 20 + 1 + 33 chars.
  154. func (a *Action) ShortRepoPath() string {
  155. return path.Join(a.ShortRepoUserName(), a.ShortRepoName())
  156. }
  157. // GetRepoLink returns relative link to action repository.
  158. func (a *Action) GetRepoLink() string {
  159. if len(setting.AppSubURL) > 0 {
  160. return path.Join(setting.AppSubURL, a.GetRepoPath())
  161. }
  162. return "/" + a.GetRepoPath()
  163. }
  164. // GetRepositoryFromMatch returns a *Repository from a username and repo strings
  165. func GetRepositoryFromMatch(ownerName string, repoName string) (*Repository, error) {
  166. var err error
  167. refRepo, err := GetRepositoryByOwnerAndName(ownerName, repoName)
  168. if err != nil {
  169. if IsErrRepoNotExist(err) {
  170. log.Warn("Repository referenced in commit but does not exist: %v", err)
  171. return nil, err
  172. }
  173. log.Error("GetRepositoryByOwnerAndName: %v", err)
  174. return nil, err
  175. }
  176. return refRepo, nil
  177. }
  178. // GetCommentLink returns link to action comment.
  179. func (a *Action) GetCommentLink() string {
  180. return a.getCommentLink(x)
  181. }
  182. func (a *Action) getCommentLink(e Engine) string {
  183. if a == nil {
  184. return "#"
  185. }
  186. if a.Comment == nil && a.CommentID != 0 {
  187. a.Comment, _ = GetCommentByID(a.CommentID)
  188. }
  189. if a.Comment != nil {
  190. return a.Comment.HTMLURL()
  191. }
  192. if len(a.GetIssueInfos()) == 0 {
  193. return "#"
  194. }
  195. //Return link to issue
  196. issueIDString := a.GetIssueInfos()[0]
  197. issueID, err := strconv.ParseInt(issueIDString, 10, 64)
  198. if err != nil {
  199. return "#"
  200. }
  201. issue, err := getIssueByID(e, issueID)
  202. if err != nil {
  203. return "#"
  204. }
  205. if err = issue.loadRepo(e); err != nil {
  206. return "#"
  207. }
  208. return issue.HTMLURL()
  209. }
  210. // GetBranch returns the action's repository branch.
  211. func (a *Action) GetBranch() string {
  212. return a.RefName
  213. }
  214. // GetContent returns the action's content.
  215. func (a *Action) GetContent() string {
  216. return a.Content
  217. }
  218. // GetCreate returns the action creation time.
  219. func (a *Action) GetCreate() time.Time {
  220. return a.CreatedUnix.AsTime()
  221. }
  222. // GetIssueInfos returns a list of issues associated with
  223. // the action.
  224. func (a *Action) GetIssueInfos() []string {
  225. return strings.SplitN(a.Content, "|", 2)
  226. }
  227. // GetIssueTitle returns the title of first issue associated
  228. // with the action.
  229. func (a *Action) GetIssueTitle() string {
  230. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  231. issue, err := GetIssueByIndex(a.RepoID, index)
  232. if err != nil {
  233. log.Error("GetIssueByIndex: %v", err)
  234. return "500 when get issue"
  235. }
  236. return issue.Title
  237. }
  238. // GetIssueContent returns the content of first issue associated with
  239. // this action.
  240. func (a *Action) GetIssueContent() string {
  241. index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
  242. issue, err := GetIssueByIndex(a.RepoID, index)
  243. if err != nil {
  244. log.Error("GetIssueByIndex: %v", err)
  245. return "500 when get issue"
  246. }
  247. return issue.Content
  248. }
  249. // GetFeedsOptions options for retrieving feeds
  250. type GetFeedsOptions struct {
  251. RequestedUser *User // the user we want activity for
  252. Actor *User // the user viewing the activity
  253. IncludePrivate bool // include private actions
  254. OnlyPerformedBy bool // only actions performed by requested user
  255. IncludeDeleted bool // include deleted actions
  256. }
  257. // GetFeeds returns actions according to the provided options
  258. func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
  259. cond := builder.NewCond()
  260. var repoIDs []int64
  261. var actorID int64
  262. if opts.Actor != nil {
  263. actorID = opts.Actor.ID
  264. }
  265. if opts.RequestedUser.IsOrganization() {
  266. env, err := opts.RequestedUser.AccessibleReposEnv(actorID)
  267. if err != nil {
  268. return nil, fmt.Errorf("AccessibleReposEnv: %v", err)
  269. }
  270. if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
  271. return nil, fmt.Errorf("GetUserRepositories: %v", err)
  272. }
  273. cond = cond.And(builder.In("repo_id", repoIDs))
  274. } else if opts.Actor != nil {
  275. cond = cond.And(builder.In("repo_id", opts.Actor.AccessibleRepoIDsQuery()))
  276. }
  277. cond = cond.And(builder.Eq{"user_id": opts.RequestedUser.ID})
  278. if opts.OnlyPerformedBy {
  279. cond = cond.And(builder.Eq{"act_user_id": opts.RequestedUser.ID})
  280. }
  281. if !opts.IncludePrivate {
  282. cond = cond.And(builder.Eq{"is_private": false})
  283. }
  284. if !opts.IncludeDeleted {
  285. cond = cond.And(builder.Eq{"is_deleted": false})
  286. }
  287. actions := make([]*Action, 0, 20)
  288. if err := x.Limit(20).Desc("id").Where(cond).Find(&actions); err != nil {
  289. return nil, fmt.Errorf("Find: %v", err)
  290. }
  291. if err := ActionList(actions).LoadAttributes(); err != nil {
  292. return nil, fmt.Errorf("LoadAttributes: %v", err)
  293. }
  294. return actions, nil
  295. }