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.

356 lines
9.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 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>
4 years ago
10 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
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
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>
4 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>
4 years ago
9 years ago
9 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 org
  6. import (
  7. "net/http"
  8. "path"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/routers/utils"
  16. "github.com/unknwon/com"
  17. )
  18. const (
  19. // tplTeams template path for teams list page
  20. tplTeams base.TplName = "org/team/teams"
  21. // tplTeamNew template path for create new team page
  22. tplTeamNew base.TplName = "org/team/new"
  23. // tplTeamMembers template path for showing team members page
  24. tplTeamMembers base.TplName = "org/team/members"
  25. // tplTeamRepositories template path for showing team repositories page
  26. tplTeamRepositories base.TplName = "org/team/repositories"
  27. )
  28. // Teams render teams list page
  29. func Teams(ctx *context.Context) {
  30. org := ctx.Org.Organization
  31. ctx.Data["Title"] = org.FullName
  32. ctx.Data["PageIsOrgTeams"] = true
  33. for _, t := range org.Teams {
  34. if err := t.GetMembers(&models.SearchMembersOptions{}); err != nil {
  35. ctx.ServerError("GetMembers", err)
  36. return
  37. }
  38. }
  39. ctx.Data["Teams"] = org.Teams
  40. ctx.HTML(200, tplTeams)
  41. }
  42. // TeamsAction response for join, leave, remove, add operations to team
  43. func TeamsAction(ctx *context.Context) {
  44. uid := com.StrTo(ctx.Query("uid")).MustInt64()
  45. if uid == 0 {
  46. ctx.Redirect(ctx.Org.OrgLink + "/teams")
  47. return
  48. }
  49. page := ctx.Query("page")
  50. var err error
  51. switch ctx.Params(":action") {
  52. case "join":
  53. if !ctx.Org.IsOwner {
  54. ctx.Error(404)
  55. return
  56. }
  57. err = ctx.Org.Team.AddMember(ctx.User.ID)
  58. case "leave":
  59. err = ctx.Org.Team.RemoveMember(ctx.User.ID)
  60. case "remove":
  61. if !ctx.Org.IsOwner {
  62. ctx.Error(404)
  63. return
  64. }
  65. err = ctx.Org.Team.RemoveMember(uid)
  66. page = "team"
  67. case "add":
  68. if !ctx.Org.IsOwner {
  69. ctx.Error(404)
  70. return
  71. }
  72. uname := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.Query("uname")))
  73. var u *models.User
  74. u, err = models.GetUserByName(uname)
  75. if err != nil {
  76. if models.IsErrUserNotExist(err) {
  77. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  78. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
  79. } else {
  80. ctx.ServerError(" GetUserByName", err)
  81. }
  82. return
  83. }
  84. if u.IsOrganization() {
  85. ctx.Flash.Error(ctx.Tr("form.cannot_add_org_to_team"))
  86. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
  87. return
  88. }
  89. if ctx.Org.Team.IsMember(u.ID) {
  90. ctx.Flash.Error(ctx.Tr("org.teams.add_duplicate_users"))
  91. } else {
  92. err = ctx.Org.Team.AddMember(u.ID)
  93. }
  94. page = "team"
  95. }
  96. if err != nil {
  97. if models.IsErrLastOrgOwner(err) {
  98. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  99. } else {
  100. log.Error("Action(%s): %v", ctx.Params(":action"), err)
  101. ctx.JSON(200, map[string]interface{}{
  102. "ok": false,
  103. "err": err.Error(),
  104. })
  105. return
  106. }
  107. }
  108. switch page {
  109. case "team":
  110. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
  111. case "home":
  112. ctx.Redirect(ctx.Org.Organization.HomeLink())
  113. default:
  114. ctx.Redirect(ctx.Org.OrgLink + "/teams")
  115. }
  116. }
  117. // TeamsRepoAction operate team's repository
  118. func TeamsRepoAction(ctx *context.Context) {
  119. if !ctx.Org.IsOwner {
  120. ctx.Error(404)
  121. return
  122. }
  123. var err error
  124. action := ctx.Params(":action")
  125. switch action {
  126. case "add":
  127. repoName := path.Base(ctx.Query("repo_name"))
  128. var repo *models.Repository
  129. repo, err = models.GetRepositoryByName(ctx.Org.Organization.ID, repoName)
  130. if err != nil {
  131. if models.IsErrRepoNotExist(err) {
  132. ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo"))
  133. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  134. return
  135. }
  136. ctx.ServerError("GetRepositoryByName", err)
  137. return
  138. }
  139. err = ctx.Org.Team.AddRepository(repo)
  140. case "remove":
  141. err = ctx.Org.Team.RemoveRepository(com.StrTo(ctx.Query("repoid")).MustInt64())
  142. case "addall":
  143. err = ctx.Org.Team.AddAllRepositories()
  144. case "removeall":
  145. err = ctx.Org.Team.RemoveAllRepositories()
  146. }
  147. if err != nil {
  148. log.Error("Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err)
  149. ctx.ServerError("TeamsRepoAction", err)
  150. return
  151. }
  152. if action == "addall" || action == "removeall" {
  153. ctx.JSON(200, map[string]interface{}{
  154. "redirect": ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories",
  155. })
  156. return
  157. }
  158. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  159. }
  160. // NewTeam render create new team page
  161. func NewTeam(ctx *context.Context) {
  162. ctx.Data["Title"] = ctx.Org.Organization.FullName
  163. ctx.Data["PageIsOrgTeams"] = true
  164. ctx.Data["PageIsOrgTeamsNew"] = true
  165. ctx.Data["Team"] = &models.Team{}
  166. ctx.Data["Units"] = models.Units
  167. ctx.HTML(200, tplTeamNew)
  168. }
  169. // NewTeamPost response for create new team
  170. func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
  171. ctx.Data["Title"] = ctx.Org.Organization.FullName
  172. ctx.Data["PageIsOrgTeams"] = true
  173. ctx.Data["PageIsOrgTeamsNew"] = true
  174. ctx.Data["Units"] = models.Units
  175. var includesAllRepositories = (form.RepoAccess == "all")
  176. t := &models.Team{
  177. OrgID: ctx.Org.Organization.ID,
  178. Name: form.TeamName,
  179. Description: form.Description,
  180. Authorize: models.ParseAccessMode(form.Permission),
  181. IncludesAllRepositories: includesAllRepositories,
  182. CanCreateOrgRepo: form.CanCreateOrgRepo,
  183. }
  184. if t.Authorize < models.AccessModeOwner {
  185. var units = make([]*models.TeamUnit, 0, len(form.Units))
  186. for _, tp := range form.Units {
  187. units = append(units, &models.TeamUnit{
  188. OrgID: ctx.Org.Organization.ID,
  189. Type: tp,
  190. })
  191. }
  192. t.Units = units
  193. }
  194. ctx.Data["Team"] = t
  195. if ctx.HasError() {
  196. ctx.HTML(200, tplTeamNew)
  197. return
  198. }
  199. if t.Authorize < models.AccessModeAdmin && len(form.Units) == 0 {
  200. ctx.RenderWithErr(ctx.Tr("form.team_no_units_error"), tplTeamNew, &form)
  201. return
  202. }
  203. if err := models.NewTeam(t); err != nil {
  204. ctx.Data["Err_TeamName"] = true
  205. switch {
  206. case models.IsErrTeamAlreadyExist(err):
  207. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
  208. default:
  209. ctx.ServerError("NewTeam", err)
  210. }
  211. return
  212. }
  213. log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name)
  214. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  215. }
  216. // TeamMembers render team members page
  217. func TeamMembers(ctx *context.Context) {
  218. ctx.Data["Title"] = ctx.Org.Team.Name
  219. ctx.Data["PageIsOrgTeams"] = true
  220. ctx.Data["PageIsOrgTeamMembers"] = true
  221. if err := ctx.Org.Team.GetMembers(&models.SearchMembersOptions{}); err != nil {
  222. ctx.ServerError("GetMembers", err)
  223. return
  224. }
  225. ctx.HTML(200, tplTeamMembers)
  226. }
  227. // TeamRepositories show the repositories of team
  228. func TeamRepositories(ctx *context.Context) {
  229. ctx.Data["Title"] = ctx.Org.Team.Name
  230. ctx.Data["PageIsOrgTeams"] = true
  231. ctx.Data["PageIsOrgTeamRepos"] = true
  232. if err := ctx.Org.Team.GetRepositories(&models.SearchTeamOptions{}); err != nil {
  233. ctx.ServerError("GetRepositories", err)
  234. return
  235. }
  236. ctx.HTML(200, tplTeamRepositories)
  237. }
  238. // EditTeam render team edit page
  239. func EditTeam(ctx *context.Context) {
  240. ctx.Data["Title"] = ctx.Org.Organization.FullName
  241. ctx.Data["PageIsOrgTeams"] = true
  242. ctx.Data["team_name"] = ctx.Org.Team.Name
  243. ctx.Data["desc"] = ctx.Org.Team.Description
  244. ctx.Data["Units"] = models.Units
  245. ctx.HTML(200, tplTeamNew)
  246. }
  247. // EditTeamPost response for modify team information
  248. func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
  249. t := ctx.Org.Team
  250. ctx.Data["Title"] = ctx.Org.Organization.FullName
  251. ctx.Data["PageIsOrgTeams"] = true
  252. ctx.Data["Team"] = t
  253. ctx.Data["Units"] = models.Units
  254. isAuthChanged := false
  255. isIncludeAllChanged := false
  256. var includesAllRepositories = (form.RepoAccess == "all")
  257. if !t.IsOwnerTeam() {
  258. // Validate permission level.
  259. auth := models.ParseAccessMode(form.Permission)
  260. t.Name = form.TeamName
  261. if t.Authorize != auth {
  262. isAuthChanged = true
  263. t.Authorize = auth
  264. }
  265. if t.IncludesAllRepositories != includesAllRepositories {
  266. isIncludeAllChanged = true
  267. t.IncludesAllRepositories = includesAllRepositories
  268. }
  269. }
  270. t.Description = form.Description
  271. if t.Authorize < models.AccessModeOwner {
  272. var units = make([]models.TeamUnit, 0, len(form.Units))
  273. for _, tp := range form.Units {
  274. units = append(units, models.TeamUnit{
  275. OrgID: t.OrgID,
  276. TeamID: t.ID,
  277. Type: tp,
  278. })
  279. }
  280. err := models.UpdateTeamUnits(t, units)
  281. if err != nil {
  282. ctx.Error(http.StatusInternalServerError, "LoadIssue", err.Error())
  283. return
  284. }
  285. }
  286. t.CanCreateOrgRepo = form.CanCreateOrgRepo
  287. if ctx.HasError() {
  288. ctx.HTML(200, tplTeamNew)
  289. return
  290. }
  291. if t.Authorize < models.AccessModeAdmin && len(form.Units) == 0 {
  292. ctx.RenderWithErr(ctx.Tr("form.team_no_units_error"), tplTeamNew, &form)
  293. return
  294. }
  295. if err := models.UpdateTeam(t, isAuthChanged, isIncludeAllChanged); err != nil {
  296. ctx.Data["Err_TeamName"] = true
  297. switch {
  298. case models.IsErrTeamAlreadyExist(err):
  299. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
  300. default:
  301. ctx.ServerError("UpdateTeam", err)
  302. }
  303. return
  304. }
  305. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  306. }
  307. // DeleteTeam response for the delete team request
  308. func DeleteTeam(ctx *context.Context) {
  309. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  310. ctx.Flash.Error("DeleteTeam: " + err.Error())
  311. } else {
  312. ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
  313. }
  314. ctx.JSON(200, map[string]interface{}{
  315. "redirect": ctx.Org.OrgLink + "/teams",
  316. })
  317. }