setting.go 19 KB

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