setting.go 20 KB

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