setting.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. ctx.Flash.Warning(ctx.Tr("repo.settings.update_default_branch_unsupported"))
  346. ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches")
  347. return
  348. }
  349. }
  350. if err := models.UpdateRepository(ctx.Repo.Repository, false); err != nil {
  351. ctx.Handle(500, "UpdateRepository", err)
  352. return
  353. }
  354. ctx.Flash.Success(ctx.Tr("repo.settings.update_default_branch_success"))
  355. ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches")
  356. }
  357. func SettingsProtectedBranch(ctx *context.Context) {
  358. branch := ctx.Params("*")
  359. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  360. ctx.NotFound()
  361. return
  362. }
  363. ctx.Data["Title"] = ctx.Tr("repo.settings.protected_branches") + " - " + branch
  364. ctx.Data["PageIsSettingsBranches"] = true
  365. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  366. if err != nil {
  367. if !models.IsErrBranchNotExist(err) {
  368. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  369. return
  370. }
  371. // No options found, create defaults.
  372. protectBranch = &models.ProtectBranch{
  373. Name: branch,
  374. }
  375. }
  376. if ctx.Repo.Owner.IsOrganization() {
  377. users, err := ctx.Repo.Repository.GetWriters()
  378. if err != nil {
  379. ctx.Handle(500, "Repo.Repository.GetPushers", err)
  380. return
  381. }
  382. ctx.Data["Users"] = users
  383. ctx.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  384. teams, err := ctx.Repo.Owner.TeamsHaveAccessToRepo(ctx.Repo.Repository.ID, models.ACCESS_MODE_WRITE)
  385. if err != nil {
  386. ctx.Handle(500, "Repo.Owner.TeamsHaveAccessToRepo", err)
  387. return
  388. }
  389. ctx.Data["Teams"] = teams
  390. ctx.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  391. }
  392. ctx.Data["Branch"] = protectBranch
  393. ctx.HTML(200, SETTINGS_PROTECTED_BRANCH)
  394. }
  395. func SettingsProtectedBranchPost(ctx *context.Context, f form.ProtectBranch) {
  396. branch := ctx.Params("*")
  397. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  398. ctx.NotFound()
  399. return
  400. }
  401. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  402. if err != nil {
  403. if !models.IsErrBranchNotExist(err) {
  404. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  405. return
  406. }
  407. // No options found, create defaults.
  408. protectBranch = &models.ProtectBranch{
  409. RepoID: ctx.Repo.Repository.ID,
  410. Name: branch,
  411. }
  412. }
  413. protectBranch.Protected = f.Protected
  414. protectBranch.RequirePullRequest = f.RequirePullRequest
  415. protectBranch.EnableWhitelist = f.EnableWhitelist
  416. if ctx.Repo.Owner.IsOrganization() {
  417. err = models.UpdateOrgProtectBranch(ctx.Repo.Repository, protectBranch, f.WhitelistUsers, f.WhitelistTeams)
  418. } else {
  419. err = models.UpdateProtectBranch(protectBranch)
  420. }
  421. if err != nil {
  422. ctx.Handle(500, "UpdateOrgProtectBranch/UpdateProtectBranch", err)
  423. return
  424. }
  425. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success"))
  426. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  427. }
  428. func SettingsGitHooks(ctx *context.Context) {
  429. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  430. ctx.Data["PageIsSettingsGitHooks"] = true
  431. hooks, err := ctx.Repo.GitRepo.Hooks()
  432. if err != nil {
  433. ctx.Handle(500, "Hooks", err)
  434. return
  435. }
  436. ctx.Data["Hooks"] = hooks
  437. ctx.HTML(200, SETTINGS_GITHOOKS)
  438. }
  439. func SettingsGitHooksEdit(ctx *context.Context) {
  440. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  441. ctx.Data["PageIsSettingsGitHooks"] = true
  442. ctx.Data["RequireSimpleMDE"] = true
  443. name := ctx.Params(":name")
  444. hook, err := ctx.Repo.GitRepo.GetHook(name)
  445. if err != nil {
  446. if err == git.ErrNotValidHook {
  447. ctx.Handle(404, "GetHook", err)
  448. } else {
  449. ctx.Handle(500, "GetHook", err)
  450. }
  451. return
  452. }
  453. ctx.Data["Hook"] = hook
  454. ctx.HTML(200, SETTINGS_GITHOOK_EDIT)
  455. }
  456. func SettingsGitHooksEditPost(ctx *context.Context) {
  457. name := ctx.Params(":name")
  458. hook, err := ctx.Repo.GitRepo.GetHook(name)
  459. if err != nil {
  460. if err == git.ErrNotValidHook {
  461. ctx.Handle(404, "GetHook", err)
  462. } else {
  463. ctx.Handle(500, "GetHook", err)
  464. }
  465. return
  466. }
  467. hook.Content = ctx.Query("content")
  468. if err = hook.Update(); err != nil {
  469. ctx.Handle(500, "hook.Update", err)
  470. return
  471. }
  472. ctx.Redirect(ctx.Data["Link"].(string))
  473. }
  474. func SettingsDeployKeys(ctx *context.Context) {
  475. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  476. ctx.Data["PageIsSettingsKeys"] = true
  477. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  478. if err != nil {
  479. ctx.Handle(500, "ListDeployKeys", err)
  480. return
  481. }
  482. ctx.Data["Deploykeys"] = keys
  483. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  484. }
  485. func SettingsDeployKeysPost(ctx *context.Context, f form.AddSSHKey) {
  486. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  487. ctx.Data["PageIsSettingsKeys"] = true
  488. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  489. if err != nil {
  490. ctx.Handle(500, "ListDeployKeys", err)
  491. return
  492. }
  493. ctx.Data["Deploykeys"] = keys
  494. if ctx.HasError() {
  495. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  496. return
  497. }
  498. content, err := models.CheckPublicKeyString(f.Content)
  499. if err != nil {
  500. if models.IsErrKeyUnableVerify(err) {
  501. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  502. } else {
  503. ctx.Data["HasError"] = true
  504. ctx.Data["Err_Content"] = true
  505. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  506. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  507. return
  508. }
  509. }
  510. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, f.Title, content)
  511. if err != nil {
  512. ctx.Data["HasError"] = true
  513. switch {
  514. case models.IsErrKeyAlreadyExist(err):
  515. ctx.Data["Err_Content"] = true
  516. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &f)
  517. case models.IsErrKeyNameAlreadyUsed(err):
  518. ctx.Data["Err_Title"] = true
  519. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &f)
  520. default:
  521. ctx.Handle(500, "AddDeployKey", err)
  522. }
  523. return
  524. }
  525. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  526. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  527. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  528. }
  529. func DeleteDeployKey(ctx *context.Context) {
  530. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  531. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  532. } else {
  533. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  534. }
  535. ctx.JSON(200, map[string]interface{}{
  536. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  537. })
  538. }