setting.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. "github.com/G-Node/gogs/internal/conf"
  14. "github.com/G-Node/gogs/internal/context"
  15. "github.com/G-Node/gogs/internal/db"
  16. "github.com/G-Node/gogs/internal/db/errors"
  17. "github.com/G-Node/gogs/internal/email"
  18. "github.com/G-Node/gogs/internal/form"
  19. "github.com/G-Node/gogs/internal/osutil"
  20. "github.com/G-Node/gogs/internal/tool"
  21. petname "github.com/dustinkirkland/golang-petname"
  22. )
  23. const (
  24. SETTINGS_OPTIONS = "repo/settings/options"
  25. SETTINGS_REPO_AVATAR = "repo/settings/avatar"
  26. SETTINGS_COLLABORATION = "repo/settings/collaboration"
  27. SETTINGS_BRANCHES = "repo/settings/branches"
  28. SETTINGS_PROTECTED_BRANCH = "repo/settings/protected_branch"
  29. SETTINGS_GITHOOKS = "repo/settings/githooks"
  30. SETTINGS_GITHOOK_EDIT = "repo/settings/githook_edit"
  31. SETTINGS_DEPLOY_KEYS = "repo/settings/deploy_keys"
  32. )
  33. func Settings(c *context.Context) {
  34. c.Title("repo.settings")
  35. c.PageIs("SettingsOptions")
  36. c.RequireAutosize()
  37. c.Success(SETTINGS_OPTIONS)
  38. }
  39. func SettingsPost(c *context.Context, f form.RepoSetting) {
  40. c.Title("repo.settings")
  41. c.PageIs("SettingsOptions")
  42. c.RequireAutosize()
  43. repo := c.Repo.Repository
  44. switch c.Query("action") {
  45. case "update":
  46. if c.HasError() {
  47. c.Success(SETTINGS_OPTIONS)
  48. return
  49. }
  50. isNameChanged := false
  51. oldRepoName := repo.Name
  52. newRepoName := f.RepoName
  53. // Check if repository name has been changed.
  54. if repo.LowerName != strings.ToLower(newRepoName) {
  55. isNameChanged = true
  56. if err := db.ChangeRepositoryName(c.Repo.Owner, repo.Name, newRepoName); err != nil {
  57. c.FormErr("RepoName")
  58. switch {
  59. case db.IsErrRepoAlreadyExist(err):
  60. c.RenderWithErr(c.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &f)
  61. case db.IsErrNameNotAllowed(err):
  62. c.RenderWithErr(c.Tr("repo.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()), SETTINGS_OPTIONS, &f)
  63. default:
  64. c.Error(err, "change repository name")
  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. repo.Unlisted = !f.Listed
  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 inviteWithMail(c *context.Context, address string) (*db.User, error) {
  326. name := petname.Generate(2, "_")
  327. pw := petname.Generate(3, "")
  328. user := db.User{
  329. Name: name,
  330. Passwd: pw,
  331. Email: address,
  332. IsActive: true}
  333. err := db.CreateUser(&user)
  334. if err != nil {
  335. return nil, err
  336. }
  337. log.Info("User: %s created", user.Name)
  338. c.Context.Data["Account"] = user.Name
  339. c.Context.Data["Inviter"] = c.User.Name
  340. c.Context.Data["Pw"] = pw
  341. email.SendInviteMail(c.Context, db.NewMailerUser(&user))
  342. return &user, nil
  343. }
  344. func SettingsCollaborationPost(c *context.Context) {
  345. var name string
  346. address := strings.ToLower(c.Query("invite"))
  347. if len(address) > 0 {
  348. u, err := inviteWithMail(c, address)
  349. if err != nil {
  350. log.Info("Problem with inviting user %q: %s", address, err)
  351. if db.IsErrBlockedDomain(err) {
  352. c.Flash.Error(c.Tr("form.invite_email_blocked"))
  353. } else if db.IsErrEmailAlreadyUsed(err) {
  354. c.Flash.Error(c.Tr("form.email_been_used"))
  355. }
  356. c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
  357. return
  358. }
  359. name = u.Name
  360. } else {
  361. name = strings.ToLower(c.Query("collaborator"))
  362. }
  363. if len(name) == 0 || c.Repo.Owner.LowerName == name {
  364. c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
  365. return
  366. }
  367. u, err := db.GetUserByName(name)
  368. if err != nil {
  369. if db.IsErrUserNotExist(err) {
  370. c.Flash.Error(c.Tr("form.user_not_exist"))
  371. c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
  372. } else {
  373. c.Error(err, "get user by name")
  374. }
  375. return
  376. }
  377. // Organization is not allowed to be added as a collaborator
  378. if u.IsOrganization() {
  379. c.Flash.Error(c.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  380. c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
  381. return
  382. }
  383. if err = c.Repo.Repository.AddCollaborator(u); err != nil {
  384. c.Error(err, "add collaborator")
  385. return
  386. }
  387. if conf.User.EnableEmailNotification {
  388. email.SendCollaboratorMail(db.NewMailerUser(u), db.NewMailerUser(c.User), db.NewMailerRepo(c.Repo.Repository))
  389. }
  390. c.Flash.Success(c.Tr("repo.settings.add_collaborator_success"))
  391. c.Redirect(conf.Server.Subpath + c.Req.URL.Path)
  392. }
  393. func ChangeCollaborationAccessMode(c *context.Context) {
  394. if err := c.Repo.Repository.ChangeCollaborationAccessMode(
  395. c.QueryInt64("uid"),
  396. db.AccessMode(c.QueryInt("mode"))); err != nil {
  397. log.Error("ChangeCollaborationAccessMode: %v", err)
  398. return
  399. }
  400. c.Status(204)
  401. }
  402. func DeleteCollaboration(c *context.Context) {
  403. if err := c.Repo.Repository.DeleteCollaboration(c.QueryInt64("id")); err != nil {
  404. c.Flash.Error("DeleteCollaboration: " + err.Error())
  405. } else {
  406. c.Flash.Success(c.Tr("repo.settings.remove_collaborator_success"))
  407. }
  408. c.JSONSuccess(map[string]interface{}{
  409. "redirect": c.Repo.RepoLink + "/settings/collaboration",
  410. })
  411. }
  412. func SettingsBranches(c *context.Context) {
  413. c.Data["Title"] = c.Tr("repo.settings.branches")
  414. c.Data["PageIsSettingsBranches"] = true
  415. if c.Repo.Repository.IsBare {
  416. c.Flash.Info(c.Tr("repo.settings.branches_bare"), true)
  417. c.Success(SETTINGS_BRANCHES)
  418. return
  419. }
  420. protectBranches, err := db.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
  421. if err != nil {
  422. c.Error(err, "get protect branch by repository ID")
  423. return
  424. }
  425. // Filter out deleted branches
  426. branches := make([]string, 0, len(protectBranches))
  427. for i := range protectBranches {
  428. if c.Repo.GitRepo.HasBranch(protectBranches[i].Name) {
  429. branches = append(branches, protectBranches[i].Name)
  430. }
  431. }
  432. c.Data["ProtectBranches"] = branches
  433. c.Success(SETTINGS_BRANCHES)
  434. }
  435. func UpdateDefaultBranch(c *context.Context) {
  436. branch := c.Query("branch")
  437. if c.Repo.GitRepo.HasBranch(branch) &&
  438. c.Repo.Repository.DefaultBranch != branch {
  439. c.Repo.Repository.DefaultBranch = branch
  440. if _, err := c.Repo.GitRepo.SymbolicRef(git.SymbolicRefOptions{
  441. Ref: git.RefsHeads + branch,
  442. }); err != nil {
  443. c.Flash.Warning(c.Tr("repo.settings.update_default_branch_unsupported"))
  444. c.Redirect(c.Repo.RepoLink + "/settings/branches")
  445. return
  446. }
  447. }
  448. if err := db.UpdateRepository(c.Repo.Repository, false); err != nil {
  449. c.Error(err, "update repository")
  450. return
  451. }
  452. c.Flash.Success(c.Tr("repo.settings.update_default_branch_success"))
  453. c.Redirect(c.Repo.RepoLink + "/settings/branches")
  454. }
  455. func SettingsProtectedBranch(c *context.Context) {
  456. branch := c.Params("*")
  457. if !c.Repo.GitRepo.HasBranch(branch) {
  458. c.NotFound()
  459. return
  460. }
  461. c.Data["Title"] = c.Tr("repo.settings.protected_branches") + " - " + branch
  462. c.Data["PageIsSettingsBranches"] = true
  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. Name: branch,
  472. }
  473. }
  474. if c.Repo.Owner.IsOrganization() {
  475. users, err := c.Repo.Repository.GetWriters()
  476. if err != nil {
  477. c.Error(err, "get writers")
  478. return
  479. }
  480. c.Data["Users"] = users
  481. c.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  482. teams, err := c.Repo.Owner.TeamsHaveAccessToRepo(c.Repo.Repository.ID, db.AccessModeWrite)
  483. if err != nil {
  484. c.Error(err, "get teams have access to the repository")
  485. return
  486. }
  487. c.Data["Teams"] = teams
  488. c.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  489. }
  490. c.Data["Branch"] = protectBranch
  491. c.Success(SETTINGS_PROTECTED_BRANCH)
  492. }
  493. func SettingsProtectedBranchPost(c *context.Context, f form.ProtectBranch) {
  494. branch := c.Params("*")
  495. if !c.Repo.GitRepo.HasBranch(branch) {
  496. c.NotFound()
  497. return
  498. }
  499. protectBranch, err := db.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch)
  500. if err != nil {
  501. if !db.IsErrBranchNotExist(err) {
  502. c.Error(err, "get protect branch of repository by name")
  503. return
  504. }
  505. // No options found, create defaults.
  506. protectBranch = &db.ProtectBranch{
  507. RepoID: c.Repo.Repository.ID,
  508. Name: branch,
  509. }
  510. }
  511. protectBranch.Protected = f.Protected
  512. protectBranch.RequirePullRequest = f.RequirePullRequest
  513. protectBranch.EnableWhitelist = f.EnableWhitelist
  514. if c.Repo.Owner.IsOrganization() {
  515. err = db.UpdateOrgProtectBranch(c.Repo.Repository, protectBranch, f.WhitelistUsers, f.WhitelistTeams)
  516. } else {
  517. err = db.UpdateProtectBranch(protectBranch)
  518. }
  519. if err != nil {
  520. c.Error(err, "update protect branch")
  521. return
  522. }
  523. c.Flash.Success(c.Tr("repo.settings.update_protect_branch_success"))
  524. c.Redirect(fmt.Sprintf("%s/settings/branches/%s", c.Repo.RepoLink, branch))
  525. }
  526. func SettingsGitHooks(c *context.Context) {
  527. c.Data["Title"] = c.Tr("repo.settings.githooks")
  528. c.Data["PageIsSettingsGitHooks"] = true
  529. hooks, err := c.Repo.GitRepo.Hooks("custom_hooks")
  530. if err != nil {
  531. c.Error(err, "get hooks")
  532. return
  533. }
  534. c.Data["Hooks"] = hooks
  535. c.Success(SETTINGS_GITHOOKS)
  536. }
  537. func SettingsGitHooksEdit(c *context.Context) {
  538. c.Data["Title"] = c.Tr("repo.settings.githooks")
  539. c.Data["PageIsSettingsGitHooks"] = true
  540. c.Data["RequireSimpleMDE"] = true
  541. name := c.Params(":name")
  542. hook, err := c.Repo.GitRepo.Hook("custom_hooks", git.HookName(name))
  543. if err != nil {
  544. c.NotFoundOrError(osutil.NewError(err), "get hook")
  545. return
  546. }
  547. c.Data["Hook"] = hook
  548. c.Success(SETTINGS_GITHOOK_EDIT)
  549. }
  550. func SettingsGitHooksEditPost(c *context.Context) {
  551. name := c.Params(":name")
  552. hook, err := c.Repo.GitRepo.Hook("custom_hooks", git.HookName(name))
  553. if err != nil {
  554. c.NotFoundOrError(osutil.NewError(err), "get hook")
  555. return
  556. }
  557. if err = hook.Update(c.Query("content")); err != nil {
  558. c.Error(err, "update hook")
  559. return
  560. }
  561. c.Redirect(c.Data["Link"].(string))
  562. }
  563. func SettingsDeployKeys(c *context.Context) {
  564. c.Data["Title"] = c.Tr("repo.settings.deploy_keys")
  565. c.Data["PageIsSettingsKeys"] = true
  566. keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
  567. if err != nil {
  568. c.Error(err, "list deploy keys")
  569. return
  570. }
  571. c.Data["Deploykeys"] = keys
  572. c.Success(SETTINGS_DEPLOY_KEYS)
  573. }
  574. func SettingsDeployKeysPost(c *context.Context, f form.AddSSHKey) {
  575. c.Data["Title"] = c.Tr("repo.settings.deploy_keys")
  576. c.Data["PageIsSettingsKeys"] = true
  577. keys, err := db.ListDeployKeys(c.Repo.Repository.ID)
  578. if err != nil {
  579. c.Error(err, "list deploy keys")
  580. return
  581. }
  582. c.Data["Deploykeys"] = keys
  583. if c.HasError() {
  584. c.Success(SETTINGS_DEPLOY_KEYS)
  585. return
  586. }
  587. content, err := db.CheckPublicKeyString(f.Content)
  588. if err != nil {
  589. if db.IsErrKeyUnableVerify(err) {
  590. c.Flash.Info(c.Tr("form.unable_verify_ssh_key"))
  591. } else {
  592. c.Data["HasError"] = true
  593. c.Data["Err_Content"] = true
  594. c.Flash.Error(c.Tr("form.invalid_ssh_key", err.Error()))
  595. c.Redirect(c.Repo.RepoLink + "/settings/keys")
  596. return
  597. }
  598. }
  599. key, err := db.AddDeployKey(c.Repo.Repository.ID, f.Title, content)
  600. if err != nil {
  601. c.Data["HasError"] = true
  602. switch {
  603. case db.IsErrKeyAlreadyExist(err):
  604. c.Data["Err_Content"] = true
  605. c.RenderWithErr(c.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &f)
  606. case db.IsErrKeyNameAlreadyUsed(err):
  607. c.Data["Err_Title"] = true
  608. c.RenderWithErr(c.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &f)
  609. default:
  610. c.Error(err, "add deploy key")
  611. }
  612. return
  613. }
  614. log.Trace("Deploy key added: %d", c.Repo.Repository.ID)
  615. c.Flash.Success(c.Tr("repo.settings.add_key_success", key.Name))
  616. c.Redirect(c.Repo.RepoLink + "/settings/keys")
  617. }
  618. func DeleteDeployKey(c *context.Context) {
  619. if err := db.DeleteDeployKey(c.User, c.QueryInt64("id")); err != nil {
  620. c.Flash.Error("DeleteDeployKey: " + err.Error())
  621. } else {
  622. c.Flash.Success(c.Tr("repo.settings.deploy_key_deletion_success"))
  623. }
  624. c.JSONSuccess(map[string]interface{}{
  625. "redirect": c.Repo.RepoLink + "/settings/keys",
  626. })
  627. }