setting.go 20 KB

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