setting.go 19 KB

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