setting.go 19 KB

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