setting.go 14 KB

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