setting.go 18 KB

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