setting.go 18 KB

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