setting.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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/context"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/mailer"
  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 *context.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 *context.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.EnablePrune = form.EnablePrune
  95. ctx.Repo.Mirror.Interval = form.Interval
  96. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  97. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  98. ctx.Handle(500, "UpdateMirror", err)
  99. return
  100. }
  101. }
  102. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  103. ctx.Handle(500, "SaveAddress", err)
  104. return
  105. }
  106. }
  107. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  108. ctx.Redirect(repo.Link() + "/settings")
  109. case "advanced":
  110. repo.EnableWiki = form.EnableWiki
  111. repo.EnableExternalWiki = form.EnableExternalWiki
  112. repo.ExternalWikiURL = form.ExternalWikiURL
  113. repo.EnableIssues = form.EnableIssues
  114. repo.EnableExternalTracker = form.EnableExternalTracker
  115. repo.ExternalTrackerFormat = form.TrackerURLFormat
  116. repo.ExternalTrackerStyle = form.TrackerIssueStyle
  117. repo.EnablePulls = form.EnablePulls
  118. if err := models.UpdateRepository(repo, false); err != nil {
  119. ctx.Handle(500, "UpdateRepository", err)
  120. return
  121. }
  122. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  123. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  124. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  125. case "convert":
  126. if !ctx.Repo.IsOwner() {
  127. ctx.Error(404)
  128. return
  129. }
  130. if repo.Name != form.RepoName {
  131. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  132. return
  133. }
  134. if ctx.Repo.Owner.IsOrganization() {
  135. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  136. ctx.Error(404)
  137. return
  138. }
  139. }
  140. if !repo.IsMirror {
  141. ctx.Error(404)
  142. return
  143. }
  144. repo.IsMirror = false
  145. if _, err := models.CleanUpMigrateInfo(repo, models.RepoPath(ctx.Repo.Owner.Name, repo.Name)); err != nil {
  146. ctx.Handle(500, "CleanUpMigrateInfo", err)
  147. return
  148. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  149. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  150. return
  151. }
  152. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  153. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  154. ctx.Redirect(setting.AppSubUrl + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  155. case "transfer":
  156. if !ctx.Repo.IsOwner() {
  157. ctx.Error(404)
  158. return
  159. }
  160. if repo.Name != form.RepoName {
  161. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  162. return
  163. }
  164. if ctx.Repo.Owner.IsOrganization() {
  165. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  166. ctx.Error(404)
  167. return
  168. }
  169. }
  170. newOwner := ctx.Query("new_owner_name")
  171. isExist, err := models.IsUserExist(0, newOwner)
  172. if err != nil {
  173. ctx.Handle(500, "IsUserExist", err)
  174. return
  175. } else if !isExist {
  176. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  177. return
  178. }
  179. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  180. if models.IsErrRepoAlreadyExist(err) {
  181. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  182. } else {
  183. ctx.Handle(500, "TransferOwnership", err)
  184. }
  185. return
  186. }
  187. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  188. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  189. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  190. case "delete":
  191. if !ctx.Repo.IsOwner() {
  192. ctx.Error(404)
  193. return
  194. }
  195. if repo.Name != form.RepoName {
  196. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  197. return
  198. }
  199. if ctx.Repo.Owner.IsOrganization() {
  200. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  201. ctx.Error(404)
  202. return
  203. }
  204. }
  205. if err := models.DeleteRepository(ctx.Repo.Owner.Id, repo.ID); err != nil {
  206. ctx.Handle(500, "DeleteRepository", err)
  207. return
  208. }
  209. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  210. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  211. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  212. case "delete-wiki":
  213. if !ctx.Repo.IsOwner() {
  214. ctx.Error(404)
  215. return
  216. }
  217. if repo.Name != form.RepoName {
  218. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  219. return
  220. }
  221. if ctx.Repo.Owner.IsOrganization() {
  222. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  223. ctx.Error(404)
  224. return
  225. }
  226. }
  227. repo.DeleteWiki()
  228. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  229. repo.EnableWiki = false
  230. if err := models.UpdateRepository(repo, false); err != nil {
  231. ctx.Handle(500, "UpdateRepository", err)
  232. return
  233. }
  234. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  235. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  236. }
  237. }
  238. func Collaboration(ctx *context.Context) {
  239. ctx.Data["Title"] = ctx.Tr("repo.settings")
  240. ctx.Data["PageIsSettingsCollaboration"] = true
  241. users, err := ctx.Repo.Repository.GetCollaborators()
  242. if err != nil {
  243. ctx.Handle(500, "GetCollaborators", err)
  244. return
  245. }
  246. ctx.Data["Collaborators"] = users
  247. ctx.HTML(200, COLLABORATION)
  248. }
  249. func CollaborationPost(ctx *context.Context) {
  250. name := strings.ToLower(ctx.Query("collaborator"))
  251. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  252. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  253. return
  254. }
  255. u, err := models.GetUserByName(name)
  256. if err != nil {
  257. if models.IsErrUserNotExist(err) {
  258. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  259. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  260. } else {
  261. ctx.Handle(500, "GetUserByName", err)
  262. }
  263. return
  264. }
  265. // Organization is not allowed to be added as a collaborator.
  266. if u.IsOrganization() {
  267. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  268. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  269. return
  270. }
  271. // Check if user is organization member.
  272. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  273. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  274. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  275. return
  276. }
  277. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  278. ctx.Handle(500, "AddCollaborator", err)
  279. return
  280. }
  281. if setting.Service.EnableNotifyMail {
  282. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  283. ctx.Handle(500, "SendCollaboratorMail", err)
  284. return
  285. }
  286. }
  287. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  288. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  289. }
  290. func ChangeCollaborationAccessMode(ctx *context.Context) {
  291. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  292. ctx.QueryInt64("uid"),
  293. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  294. log.Error(4, "ChangeCollaborationAccessMode: %v", err)
  295. }
  296. }
  297. func DeleteCollaboration(ctx *context.Context) {
  298. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  299. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  300. } else {
  301. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  302. }
  303. ctx.JSON(200, map[string]interface{}{
  304. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  305. })
  306. }
  307. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  308. owner, err := models.GetUserByName(ctx.Params(":username"))
  309. if err != nil {
  310. if models.IsErrUserNotExist(err) {
  311. ctx.Handle(404, "GetUserByName", err)
  312. } else {
  313. ctx.Handle(500, "GetUserByName", err)
  314. }
  315. return nil, nil
  316. }
  317. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  318. if err != nil {
  319. if models.IsErrRepoNotExist(err) {
  320. ctx.Handle(404, "GetRepositoryByName", err)
  321. } else {
  322. ctx.Handle(500, "GetRepositoryByName", err)
  323. }
  324. return nil, nil
  325. }
  326. return owner, repo
  327. }
  328. func GitHooks(ctx *context.Context) {
  329. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  330. ctx.Data["PageIsSettingsGitHooks"] = true
  331. hooks, err := ctx.Repo.GitRepo.Hooks()
  332. if err != nil {
  333. ctx.Handle(500, "Hooks", err)
  334. return
  335. }
  336. ctx.Data["Hooks"] = hooks
  337. ctx.HTML(200, GITHOOKS)
  338. }
  339. func GitHooksEdit(ctx *context.Context) {
  340. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  341. ctx.Data["PageIsSettingsGitHooks"] = true
  342. name := ctx.Params(":name")
  343. hook, err := ctx.Repo.GitRepo.GetHook(name)
  344. if err != nil {
  345. if err == git.ErrNotValidHook {
  346. ctx.Handle(404, "GetHook", err)
  347. } else {
  348. ctx.Handle(500, "GetHook", err)
  349. }
  350. return
  351. }
  352. ctx.Data["Hook"] = hook
  353. ctx.HTML(200, GITHOOK_EDIT)
  354. }
  355. func GitHooksEditPost(ctx *context.Context) {
  356. name := ctx.Params(":name")
  357. hook, err := ctx.Repo.GitRepo.GetHook(name)
  358. if err != nil {
  359. if err == git.ErrNotValidHook {
  360. ctx.Handle(404, "GetHook", err)
  361. } else {
  362. ctx.Handle(500, "GetHook", err)
  363. }
  364. return
  365. }
  366. hook.Content = ctx.Query("content")
  367. if err = hook.Update(); err != nil {
  368. ctx.Handle(500, "hook.Update", err)
  369. return
  370. }
  371. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  372. }
  373. func DeployKeys(ctx *context.Context) {
  374. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  375. ctx.Data["PageIsSettingsKeys"] = true
  376. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  377. if err != nil {
  378. ctx.Handle(500, "ListDeployKeys", err)
  379. return
  380. }
  381. ctx.Data["Deploykeys"] = keys
  382. ctx.HTML(200, DEPLOY_KEYS)
  383. }
  384. func DeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  385. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  386. ctx.Data["PageIsSettingsKeys"] = true
  387. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  388. if err != nil {
  389. ctx.Handle(500, "ListDeployKeys", err)
  390. return
  391. }
  392. ctx.Data["Deploykeys"] = keys
  393. if ctx.HasError() {
  394. ctx.HTML(200, DEPLOY_KEYS)
  395. return
  396. }
  397. content, err := models.CheckPublicKeyString(form.Content)
  398. if err != nil {
  399. if models.IsErrKeyUnableVerify(err) {
  400. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  401. } else {
  402. ctx.Data["HasError"] = true
  403. ctx.Data["Err_Content"] = true
  404. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  405. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  406. return
  407. }
  408. }
  409. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  410. if err != nil {
  411. ctx.Data["HasError"] = true
  412. switch {
  413. case models.IsErrKeyAlreadyExist(err):
  414. ctx.Data["Err_Content"] = true
  415. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  416. case models.IsErrKeyNameAlreadyUsed(err):
  417. ctx.Data["Err_Title"] = true
  418. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  419. default:
  420. ctx.Handle(500, "AddDeployKey", err)
  421. }
  422. return
  423. }
  424. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  425. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  426. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  427. }
  428. func DeleteDeployKey(ctx *context.Context) {
  429. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  430. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  431. } else {
  432. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  433. }
  434. ctx.JSON(200, map[string]interface{}{
  435. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  436. })
  437. }