setting.go 15 KB

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