setting.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. "fmt"
  7. "strings"
  8. "time"
  9. log "gopkg.in/clog.v1"
  10. "github.com/gogits/git-module"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/models/errors"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/context"
  15. "github.com/gogits/gogs/modules/form"
  16. "github.com/gogits/gogs/modules/mailer"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. const (
  20. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  21. SETTINGS_COLLABORATION base.TplName = "repo/settings/collaboration"
  22. SETTINGS_BRANCHES base.TplName = "repo/settings/branches"
  23. SETTINGS_PROTECTED_BRANCH base.TplName = "repo/settings/protected_branch"
  24. SETTINGS_GITHOOKS base.TplName = "repo/settings/githooks"
  25. SETTINGS_GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  26. SETTINGS_DEPLOY_KEYS base.TplName = "repo/settings/deploy_keys"
  27. )
  28. func Settings(ctx *context.Context) {
  29. ctx.Data["Title"] = ctx.Tr("repo.settings")
  30. ctx.Data["PageIsSettingsOptions"] = true
  31. ctx.HTML(200, SETTINGS_OPTIONS)
  32. }
  33. func SettingsPost(ctx *context.Context, f form.RepoSetting) {
  34. ctx.Data["Title"] = ctx.Tr("repo.settings")
  35. ctx.Data["PageIsSettingsOptions"] = true
  36. repo := ctx.Repo.Repository
  37. switch ctx.Query("action") {
  38. case "update":
  39. if ctx.HasError() {
  40. ctx.HTML(200, SETTINGS_OPTIONS)
  41. return
  42. }
  43. isNameChanged := false
  44. oldRepoName := repo.Name
  45. newRepoName := f.RepoName
  46. // Check if repository name has been changed.
  47. if repo.LowerName != strings.ToLower(newRepoName) {
  48. isNameChanged = true
  49. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  50. ctx.Data["Err_RepoName"] = true
  51. switch {
  52. case models.IsErrRepoAlreadyExist(err):
  53. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &f)
  54. case models.IsErrNameReserved(err):
  55. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &f)
  56. case models.IsErrNamePatternNotAllowed(err):
  57. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &f)
  58. default:
  59. ctx.Handle(500, "ChangeRepositoryName", err)
  60. }
  61. return
  62. }
  63. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  64. }
  65. // In case it's just a case change.
  66. repo.Name = newRepoName
  67. repo.LowerName = strings.ToLower(newRepoName)
  68. repo.Description = f.Description
  69. repo.Website = f.Website
  70. // Visibility of forked repository is forced sync with base repository.
  71. if repo.IsFork {
  72. f.Private = repo.BaseRepo.IsPrivate
  73. }
  74. visibilityChanged := repo.IsPrivate != f.Private
  75. repo.IsPrivate = f.Private
  76. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  77. ctx.Handle(500, "UpdateRepository", err)
  78. return
  79. }
  80. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  81. if isNameChanged {
  82. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  83. log.Error(4, "RenameRepoAction: %v", err)
  84. }
  85. }
  86. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  87. ctx.Redirect(repo.Link() + "/settings")
  88. case "mirror":
  89. if !repo.IsMirror {
  90. ctx.Handle(404, "", nil)
  91. return
  92. }
  93. if f.Interval > 0 {
  94. ctx.Repo.Mirror.EnablePrune = f.EnablePrune
  95. ctx.Repo.Mirror.Interval = f.Interval
  96. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(f.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(f.MirrorAddress); err != nil {
  103. ctx.Handle(500, "SaveAddress", err)
  104. return
  105. }
  106. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  107. ctx.Redirect(repo.Link() + "/settings")
  108. case "mirror-sync":
  109. if !repo.IsMirror {
  110. ctx.Handle(404, "", nil)
  111. return
  112. }
  113. go models.MirrorQueue.Add(repo.ID)
  114. ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
  115. ctx.Redirect(repo.Link() + "/settings")
  116. case "advanced":
  117. repo.EnableWiki = f.EnableWiki
  118. repo.AllowPublicWiki = f.AllowPublicWiki
  119. repo.EnableExternalWiki = f.EnableExternalWiki
  120. repo.ExternalWikiURL = f.ExternalWikiURL
  121. repo.EnableIssues = f.EnableIssues
  122. repo.AllowPublicIssues = f.AllowPublicIssues
  123. repo.EnableExternalTracker = f.EnableExternalTracker
  124. repo.ExternalTrackerURL = f.ExternalTrackerURL
  125. repo.ExternalTrackerFormat = f.TrackerURLFormat
  126. repo.ExternalTrackerStyle = f.TrackerIssueStyle
  127. repo.EnablePulls = f.EnablePulls
  128. if err := models.UpdateRepository(repo, false); err != nil {
  129. ctx.Handle(500, "UpdateRepository", err)
  130. return
  131. }
  132. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  133. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  134. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  135. case "convert":
  136. if !ctx.Repo.IsOwner() {
  137. ctx.Error(404)
  138. return
  139. }
  140. if repo.Name != f.RepoName {
  141. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  142. return
  143. }
  144. if ctx.Repo.Owner.IsOrganization() {
  145. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  146. ctx.Error(404)
  147. return
  148. }
  149. }
  150. if !repo.IsMirror {
  151. ctx.Error(404)
  152. return
  153. }
  154. repo.IsMirror = false
  155. if _, err := models.CleanUpMigrateInfo(repo); err != nil {
  156. ctx.Handle(500, "CleanUpMigrateInfo", err)
  157. return
  158. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  159. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  160. return
  161. }
  162. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  163. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  164. ctx.Redirect(setting.AppSubUrl + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  165. case "transfer":
  166. if !ctx.Repo.IsOwner() {
  167. ctx.Error(404)
  168. return
  169. }
  170. if repo.Name != f.RepoName {
  171. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  172. return
  173. }
  174. if ctx.Repo.Owner.IsOrganization() && !ctx.User.IsAdmin {
  175. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  176. ctx.Error(404)
  177. return
  178. }
  179. }
  180. newOwner := ctx.Query("new_owner_name")
  181. isExist, err := models.IsUserExist(0, newOwner)
  182. if err != nil {
  183. ctx.Handle(500, "IsUserExist", err)
  184. return
  185. } else if !isExist {
  186. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  187. return
  188. }
  189. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  190. if models.IsErrRepoAlreadyExist(err) {
  191. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  192. } else {
  193. ctx.Handle(500, "TransferOwnership", err)
  194. }
  195. return
  196. }
  197. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  198. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  199. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  200. case "delete":
  201. if !ctx.Repo.IsOwner() {
  202. ctx.Error(404)
  203. return
  204. }
  205. if repo.Name != f.RepoName {
  206. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  207. return
  208. }
  209. if ctx.Repo.Owner.IsOrganization() && !ctx.User.IsAdmin {
  210. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  211. ctx.Error(404)
  212. return
  213. }
  214. }
  215. if err := models.DeleteRepository(ctx.Repo.Owner.ID, repo.ID); err != nil {
  216. ctx.Handle(500, "DeleteRepository", err)
  217. return
  218. }
  219. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  220. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  221. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  222. case "delete-wiki":
  223. if !ctx.Repo.IsOwner() {
  224. ctx.Error(404)
  225. return
  226. }
  227. if repo.Name != f.RepoName {
  228. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  229. return
  230. }
  231. if ctx.Repo.Owner.IsOrganization() && !ctx.User.IsAdmin {
  232. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  233. ctx.Error(404)
  234. return
  235. }
  236. }
  237. repo.DeleteWiki()
  238. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  239. repo.EnableWiki = false
  240. if err := models.UpdateRepository(repo, false); err != nil {
  241. ctx.Handle(500, "UpdateRepository", err)
  242. return
  243. }
  244. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  245. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  246. default:
  247. ctx.Handle(404, "", nil)
  248. }
  249. }
  250. func SettingsCollaboration(ctx *context.Context) {
  251. ctx.Data["Title"] = ctx.Tr("repo.settings")
  252. ctx.Data["PageIsSettingsCollaboration"] = true
  253. users, err := ctx.Repo.Repository.GetCollaborators()
  254. if err != nil {
  255. ctx.Handle(500, "GetCollaborators", err)
  256. return
  257. }
  258. ctx.Data["Collaborators"] = users
  259. ctx.HTML(200, SETTINGS_COLLABORATION)
  260. }
  261. func SettingsCollaborationPost(ctx *context.Context) {
  262. name := strings.ToLower(ctx.Query("collaborator"))
  263. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  264. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  265. return
  266. }
  267. u, err := models.GetUserByName(name)
  268. if err != nil {
  269. if errors.IsUserNotExist(err) {
  270. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  271. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  272. } else {
  273. ctx.Handle(500, "GetUserByName", err)
  274. }
  275. return
  276. }
  277. // Organization is not allowed to be added as a collaborator
  278. if u.IsOrganization() {
  279. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  280. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  281. return
  282. }
  283. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  284. ctx.Handle(500, "AddCollaborator", err)
  285. return
  286. }
  287. if setting.Service.EnableNotifyMail {
  288. mailer.SendCollaboratorMail(models.NewMailerUser(u), models.NewMailerUser(ctx.User), models.NewMailerRepo(ctx.Repo.Repository))
  289. }
  290. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  291. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  292. }
  293. func ChangeCollaborationAccessMode(ctx *context.Context) {
  294. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  295. ctx.QueryInt64("uid"),
  296. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  297. log.Error(2, "ChangeCollaborationAccessMode: %v", err)
  298. return
  299. }
  300. ctx.Status(204)
  301. }
  302. func DeleteCollaboration(ctx *context.Context) {
  303. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  304. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  305. } else {
  306. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  307. }
  308. ctx.JSON(200, map[string]interface{}{
  309. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  310. })
  311. }
  312. func SettingsBranches(ctx *context.Context) {
  313. ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
  314. ctx.Data["PageIsSettingsBranches"] = true
  315. if ctx.Repo.Repository.IsBare {
  316. ctx.Flash.Info(ctx.Tr("repo.settings.branches_bare"), true)
  317. ctx.HTML(200, SETTINGS_BRANCHES)
  318. return
  319. }
  320. protectBranches, err := models.GetProtectBranchesByRepoID(ctx.Repo.Repository.ID)
  321. if err != nil {
  322. ctx.Handle(500, "GetProtectBranchesByRepoID", err)
  323. return
  324. }
  325. // Filter out deleted branches
  326. branches := make([]string, 0, len(protectBranches))
  327. for i := range protectBranches {
  328. if ctx.Repo.GitRepo.IsBranchExist(protectBranches[i].Name) {
  329. branches = append(branches, protectBranches[i].Name)
  330. }
  331. }
  332. ctx.Data["ProtectBranches"] = branches
  333. ctx.HTML(200, SETTINGS_BRANCHES)
  334. }
  335. func UpdateDefaultBranch(ctx *context.Context) {
  336. branch := ctx.Query("branch")
  337. if ctx.Repo.GitRepo.IsBranchExist(branch) &&
  338. ctx.Repo.Repository.DefaultBranch != branch {
  339. ctx.Repo.Repository.DefaultBranch = branch
  340. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  341. if !git.IsErrUnsupportedVersion(err) {
  342. ctx.Handle(500, "SetDefaultBranch", err)
  343. return
  344. }
  345. }
  346. }
  347. if err := models.UpdateRepository(ctx.Repo.Repository, false); err != nil {
  348. ctx.Handle(500, "UpdateRepository", err)
  349. return
  350. }
  351. ctx.Flash.Success(ctx.Tr("repo.settings.update_default_branch_success"))
  352. ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches")
  353. }
  354. func SettingsProtectedBranch(ctx *context.Context) {
  355. branch := ctx.Params("*")
  356. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  357. ctx.NotFound()
  358. return
  359. }
  360. ctx.Data["Title"] = ctx.Tr("repo.settings.protected_branches") + " - " + branch
  361. ctx.Data["PageIsSettingsBranches"] = true
  362. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  363. if err != nil {
  364. if !models.IsErrBranchNotExist(err) {
  365. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  366. return
  367. }
  368. // No options found, create defaults.
  369. protectBranch = &models.ProtectBranch{
  370. Name: branch,
  371. }
  372. }
  373. if ctx.Repo.Owner.IsOrganization() {
  374. users, err := ctx.Repo.Repository.GetWriters()
  375. if err != nil {
  376. ctx.Handle(500, "Repo.Repository.GetPushers", err)
  377. return
  378. }
  379. ctx.Data["Users"] = users
  380. ctx.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  381. teams, err := ctx.Repo.Owner.TeamsHaveAccessToRepo(ctx.Repo.Repository.ID, models.ACCESS_MODE_WRITE)
  382. if err != nil {
  383. ctx.Handle(500, "Repo.Owner.TeamsHaveAccessToRepo", err)
  384. return
  385. }
  386. ctx.Data["Teams"] = teams
  387. ctx.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  388. }
  389. ctx.Data["Branch"] = protectBranch
  390. ctx.HTML(200, SETTINGS_PROTECTED_BRANCH)
  391. }
  392. func SettingsProtectedBranchPost(ctx *context.Context, f form.ProtectBranch) {
  393. branch := ctx.Params("*")
  394. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  395. ctx.NotFound()
  396. return
  397. }
  398. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  399. if err != nil {
  400. if !models.IsErrBranchNotExist(err) {
  401. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  402. return
  403. }
  404. // No options found, create defaults.
  405. protectBranch = &models.ProtectBranch{
  406. RepoID: ctx.Repo.Repository.ID,
  407. Name: branch,
  408. }
  409. }
  410. protectBranch.Protected = f.Protected
  411. protectBranch.RequirePullRequest = f.RequirePullRequest
  412. protectBranch.EnableWhitelist = f.EnableWhitelist
  413. if ctx.Repo.Owner.IsOrganization() {
  414. err = models.UpdateOrgProtectBranch(ctx.Repo.Repository, protectBranch, f.WhitelistUsers, f.WhitelistTeams)
  415. } else {
  416. err = models.UpdateProtectBranch(protectBranch)
  417. }
  418. if err != nil {
  419. ctx.Handle(500, "UpdateOrgProtectBranch/UpdateProtectBranch", err)
  420. return
  421. }
  422. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success"))
  423. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  424. }
  425. func SettingsGitHooks(ctx *context.Context) {
  426. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  427. ctx.Data["PageIsSettingsGitHooks"] = true
  428. hooks, err := ctx.Repo.GitRepo.Hooks()
  429. if err != nil {
  430. ctx.Handle(500, "Hooks", err)
  431. return
  432. }
  433. ctx.Data["Hooks"] = hooks
  434. ctx.HTML(200, SETTINGS_GITHOOKS)
  435. }
  436. func SettingsGitHooksEdit(ctx *context.Context) {
  437. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  438. ctx.Data["PageIsSettingsGitHooks"] = true
  439. ctx.Data["RequireSimpleMDE"] = true
  440. name := ctx.Params(":name")
  441. hook, err := ctx.Repo.GitRepo.GetHook(name)
  442. if err != nil {
  443. if err == git.ErrNotValidHook {
  444. ctx.Handle(404, "GetHook", err)
  445. } else {
  446. ctx.Handle(500, "GetHook", err)
  447. }
  448. return
  449. }
  450. ctx.Data["Hook"] = hook
  451. ctx.HTML(200, SETTINGS_GITHOOK_EDIT)
  452. }
  453. func SettingsGitHooksEditPost(ctx *context.Context) {
  454. name := ctx.Params(":name")
  455. hook, err := ctx.Repo.GitRepo.GetHook(name)
  456. if err != nil {
  457. if err == git.ErrNotValidHook {
  458. ctx.Handle(404, "GetHook", err)
  459. } else {
  460. ctx.Handle(500, "GetHook", err)
  461. }
  462. return
  463. }
  464. hook.Content = ctx.Query("content")
  465. if err = hook.Update(); err != nil {
  466. ctx.Handle(500, "hook.Update", err)
  467. return
  468. }
  469. ctx.Redirect(ctx.Data["Link"].(string))
  470. }
  471. func SettingsDeployKeys(ctx *context.Context) {
  472. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  473. ctx.Data["PageIsSettingsKeys"] = true
  474. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  475. if err != nil {
  476. ctx.Handle(500, "ListDeployKeys", err)
  477. return
  478. }
  479. ctx.Data["Deploykeys"] = keys
  480. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  481. }
  482. func SettingsDeployKeysPost(ctx *context.Context, f form.AddSSHKey) {
  483. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  484. ctx.Data["PageIsSettingsKeys"] = true
  485. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  486. if err != nil {
  487. ctx.Handle(500, "ListDeployKeys", err)
  488. return
  489. }
  490. ctx.Data["Deploykeys"] = keys
  491. if ctx.HasError() {
  492. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  493. return
  494. }
  495. content, err := models.CheckPublicKeyString(f.Content)
  496. if err != nil {
  497. if models.IsErrKeyUnableVerify(err) {
  498. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  499. } else {
  500. ctx.Data["HasError"] = true
  501. ctx.Data["Err_Content"] = true
  502. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  503. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  504. return
  505. }
  506. }
  507. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, f.Title, content)
  508. if err != nil {
  509. ctx.Data["HasError"] = true
  510. switch {
  511. case models.IsErrKeyAlreadyExist(err):
  512. ctx.Data["Err_Content"] = true
  513. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &f)
  514. case models.IsErrKeyNameAlreadyUsed(err):
  515. ctx.Data["Err_Title"] = true
  516. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &f)
  517. default:
  518. ctx.Handle(500, "AddDeployKey", err)
  519. }
  520. return
  521. }
  522. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  523. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  524. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  525. }
  526. func DeleteDeployKey(ctx *context.Context) {
  527. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  528. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  529. } else {
  530. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  531. }
  532. ctx.JSON(200, map[string]interface{}{
  533. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  534. })
  535. }