setting.go 20 KB

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