setting.go 14 KB

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