setting.go 18 KB

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