setting.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright 2014 The Gogs 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 repo
  5. import (
  6. "strings"
  7. "time"
  8. "github.com/gogits/git-module"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  19. COLLABORATION base.TplName = "repo/settings/collaboration"
  20. GITHOOKS base.TplName = "repo/settings/githooks"
  21. GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  22. DEPLOY_KEYS base.TplName = "repo/settings/deploy_keys"
  23. )
  24. func Settings(ctx *middleware.Context) {
  25. ctx.Data["Title"] = ctx.Tr("repo.settings")
  26. ctx.Data["PageIsSettingsOptions"] = true
  27. ctx.HTML(200, SETTINGS_OPTIONS)
  28. }
  29. func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
  30. ctx.Data["Title"] = ctx.Tr("repo.settings")
  31. ctx.Data["PageIsSettingsOptions"] = true
  32. repo := ctx.Repo.Repository
  33. switch ctx.Query("action") {
  34. case "update":
  35. if ctx.HasError() {
  36. ctx.HTML(200, SETTINGS_OPTIONS)
  37. return
  38. }
  39. isNameChanged := false
  40. oldRepoName := repo.Name
  41. newRepoName := form.RepoName
  42. // Check if repository name has been changed.
  43. if repo.LowerName != strings.ToLower(newRepoName) {
  44. isNameChanged = true
  45. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  46. ctx.Data["Err_RepoName"] = true
  47. switch {
  48. case models.IsErrRepoAlreadyExist(err):
  49. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form)
  50. case models.IsErrNameReserved(err):
  51. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form)
  52. case models.IsErrNamePatternNotAllowed(err):
  53. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form)
  54. default:
  55. ctx.Handle(500, "ChangeRepositoryName", err)
  56. }
  57. return
  58. }
  59. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  60. }
  61. // In case it's just a case change.
  62. repo.Name = newRepoName
  63. repo.LowerName = strings.ToLower(newRepoName)
  64. if ctx.Repo.GitRepo.IsBranchExist(form.Branch) &&
  65. repo.DefaultBranch != form.Branch {
  66. repo.DefaultBranch = form.Branch
  67. if err := ctx.Repo.GitRepo.SetDefaultBranch(form.Branch); err != nil {
  68. if !git.IsErrUnsupportedVersion(err) {
  69. ctx.Handle(500, "SetDefaultBranch", err)
  70. return
  71. }
  72. }
  73. }
  74. repo.Description = form.Description
  75. repo.Website = form.Website
  76. // Visibility of forked repository is forced sync with base repository.
  77. if repo.IsFork {
  78. form.Private = repo.BaseRepo.IsPrivate
  79. }
  80. visibilityChanged := repo.IsPrivate != form.Private
  81. repo.IsPrivate = form.Private
  82. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  83. ctx.Handle(500, "UpdateRepository", err)
  84. return
  85. }
  86. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  87. if isNameChanged {
  88. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  89. log.Error(4, "RenameRepoAction: %v", err)
  90. }
  91. }
  92. if repo.IsMirror {
  93. if form.Interval > 0 {
  94. ctx.Repo.Mirror.Interval = form.Interval
  95. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  96. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  97. ctx.Handle(500, "UpdateMirror", err)
  98. return
  99. }
  100. }
  101. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  102. ctx.Handle(500, "SaveAddress", err)
  103. return
  104. }
  105. }
  106. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  107. ctx.Redirect(repo.RepoLink() + "/settings")
  108. case "advanced":
  109. repo.EnableWiki = form.EnableWiki
  110. repo.EnableExternalWiki = form.EnableExternalWiki
  111. repo.ExternalWikiURL = form.ExternalWikiURL
  112. repo.EnableIssues = form.EnableIssues
  113. repo.EnableExternalTracker = form.EnableExternalTracker
  114. repo.ExternalTrackerFormat = form.TrackerURLFormat
  115. repo.EnablePulls = form.EnablePulls
  116. if err := models.UpdateRepository(repo, false); err != nil {
  117. ctx.Handle(500, "UpdateRepository", err)
  118. return
  119. }
  120. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  121. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  122. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  123. case "transfer":
  124. if repo.Name != form.RepoName {
  125. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  126. return
  127. }
  128. if ctx.Repo.Owner.IsOrganization() {
  129. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  130. ctx.Error(404)
  131. return
  132. }
  133. }
  134. newOwner := ctx.Query("new_owner_name")
  135. isExist, err := models.IsUserExist(0, newOwner)
  136. if err != nil {
  137. ctx.Handle(500, "IsUserExist", err)
  138. return
  139. } else if !isExist {
  140. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  141. return
  142. }
  143. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  144. if models.IsErrRepoAlreadyExist(err) {
  145. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  146. } else {
  147. ctx.Handle(500, "TransferOwnership", err)
  148. }
  149. return
  150. }
  151. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  152. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  153. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  154. case "delete":
  155. if repo.Name != form.RepoName {
  156. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  157. return
  158. }
  159. if ctx.Repo.Owner.IsOrganization() {
  160. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  161. ctx.Error(404)
  162. return
  163. }
  164. }
  165. if err := models.DeleteRepository(ctx.Repo.Owner.Id, repo.ID); err != nil {
  166. ctx.Handle(500, "DeleteRepository", err)
  167. return
  168. }
  169. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  170. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  171. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  172. }
  173. }
  174. func Collaboration(ctx *middleware.Context) {
  175. ctx.Data["Title"] = ctx.Tr("repo.settings")
  176. ctx.Data["PageIsSettingsCollaboration"] = true
  177. if ctx.Req.Method == "POST" {
  178. name := strings.ToLower(ctx.Query("collaborator"))
  179. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  180. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  181. return
  182. }
  183. u, err := models.GetUserByName(name)
  184. if err != nil {
  185. if models.IsErrUserNotExist(err) {
  186. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  187. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  188. } else {
  189. ctx.Handle(500, "GetUserByName", err)
  190. }
  191. return
  192. }
  193. // Check if user is organization member.
  194. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  195. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  196. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  197. return
  198. }
  199. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  200. ctx.Handle(500, "AddCollaborator", err)
  201. return
  202. }
  203. if setting.Service.EnableNotifyMail {
  204. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  205. ctx.Handle(500, "SendCollaboratorMail", err)
  206. return
  207. }
  208. }
  209. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  210. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  211. return
  212. }
  213. // Delete collaborator.
  214. remove := strings.ToLower(ctx.Query("remove"))
  215. if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
  216. u, err := models.GetUserByName(remove)
  217. if err != nil {
  218. ctx.Handle(500, "GetUserByName", err)
  219. return
  220. }
  221. if err := ctx.Repo.Repository.DeleteCollaborator(u); err != nil {
  222. ctx.Handle(500, "DeleteCollaborator", err)
  223. return
  224. }
  225. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  226. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  227. return
  228. }
  229. users, err := ctx.Repo.Repository.GetCollaborators()
  230. if err != nil {
  231. ctx.Handle(500, "GetCollaborators", err)
  232. return
  233. }
  234. ctx.Data["Collaborators"] = users
  235. ctx.HTML(200, COLLABORATION)
  236. }
  237. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  238. owner, err := models.GetUserByName(ctx.Params(":username"))
  239. if err != nil {
  240. if models.IsErrUserNotExist(err) {
  241. ctx.Handle(404, "GetUserByName", err)
  242. } else {
  243. ctx.Handle(500, "GetUserByName", err)
  244. }
  245. return nil, nil
  246. }
  247. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  248. if err != nil {
  249. if models.IsErrRepoNotExist(err) {
  250. ctx.Handle(404, "GetRepositoryByName", err)
  251. } else {
  252. ctx.Handle(500, "GetRepositoryByName", err)
  253. }
  254. return nil, nil
  255. }
  256. return owner, repo
  257. }
  258. func GitHooks(ctx *middleware.Context) {
  259. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  260. ctx.Data["PageIsSettingsGitHooks"] = true
  261. hooks, err := ctx.Repo.GitRepo.Hooks()
  262. if err != nil {
  263. ctx.Handle(500, "Hooks", err)
  264. return
  265. }
  266. ctx.Data["Hooks"] = hooks
  267. ctx.HTML(200, GITHOOKS)
  268. }
  269. func GitHooksEdit(ctx *middleware.Context) {
  270. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  271. ctx.Data["PageIsSettingsGitHooks"] = true
  272. name := ctx.Params(":name")
  273. hook, err := ctx.Repo.GitRepo.GetHook(name)
  274. if err != nil {
  275. if err == git.ErrNotValidHook {
  276. ctx.Handle(404, "GetHook", err)
  277. } else {
  278. ctx.Handle(500, "GetHook", err)
  279. }
  280. return
  281. }
  282. ctx.Data["Hook"] = hook
  283. ctx.HTML(200, GITHOOK_EDIT)
  284. }
  285. func GitHooksEditPost(ctx *middleware.Context) {
  286. name := ctx.Params(":name")
  287. hook, err := ctx.Repo.GitRepo.GetHook(name)
  288. if err != nil {
  289. if err == git.ErrNotValidHook {
  290. ctx.Handle(404, "GetHook", err)
  291. } else {
  292. ctx.Handle(500, "GetHook", err)
  293. }
  294. return
  295. }
  296. hook.Content = ctx.Query("content")
  297. if err = hook.Update(); err != nil {
  298. ctx.Handle(500, "hook.Update", err)
  299. return
  300. }
  301. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  302. }
  303. func DeployKeys(ctx *middleware.Context) {
  304. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  305. ctx.Data["PageIsSettingsKeys"] = true
  306. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  307. if err != nil {
  308. ctx.Handle(500, "ListDeployKeys", err)
  309. return
  310. }
  311. ctx.Data["Deploykeys"] = keys
  312. ctx.HTML(200, DEPLOY_KEYS)
  313. }
  314. func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  315. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  316. ctx.Data["PageIsSettingsKeys"] = true
  317. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  318. if err != nil {
  319. ctx.Handle(500, "ListDeployKeys", err)
  320. return
  321. }
  322. ctx.Data["Deploykeys"] = keys
  323. if ctx.HasError() {
  324. ctx.HTML(200, DEPLOY_KEYS)
  325. return
  326. }
  327. content, err := models.CheckPublicKeyString(form.Content)
  328. if err != nil {
  329. if models.IsErrKeyUnableVerify(err) {
  330. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  331. } else {
  332. ctx.Data["HasError"] = true
  333. ctx.Data["Err_Content"] = true
  334. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  335. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  336. return
  337. }
  338. }
  339. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  340. if err != nil {
  341. ctx.Data["HasError"] = true
  342. switch {
  343. case models.IsErrKeyAlreadyExist(err):
  344. ctx.Data["Err_Content"] = true
  345. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  346. case models.IsErrKeyNameAlreadyUsed(err):
  347. ctx.Data["Err_Title"] = true
  348. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  349. default:
  350. ctx.Handle(500, "AddDeployKey", err)
  351. }
  352. return
  353. }
  354. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  355. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  356. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  357. }
  358. func DeleteDeployKey(ctx *middleware.Context) {
  359. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  360. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  361. } else {
  362. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  363. }
  364. ctx.JSON(200, map[string]interface{}{
  365. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  366. })
  367. }