You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setting.go 21 kB

10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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. "strings"
  7. "time"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const (
  17. tplSettingsOptions base.TplName = "repo/settings/options"
  18. tplCollaboration base.TplName = "repo/settings/collaboration"
  19. tplBranches base.TplName = "repo/settings/branches"
  20. tplGithooks base.TplName = "repo/settings/githooks"
  21. tplGithookEdit base.TplName = "repo/settings/githook_edit"
  22. tplDeployKeys base.TplName = "repo/settings/deploy_keys"
  23. )
  24. // Settings show a repository's settings page
  25. func Settings(ctx *context.Context) {
  26. ctx.Data["Title"] = ctx.Tr("repo.settings")
  27. ctx.Data["PageIsSettingsOptions"] = true
  28. ctx.HTML(200, tplSettingsOptions)
  29. }
  30. // SettingsPost response for changes of a repository
  31. func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
  32. ctx.Data["Title"] = ctx.Tr("repo.settings")
  33. ctx.Data["PageIsSettingsOptions"] = true
  34. repo := ctx.Repo.Repository
  35. switch ctx.Query("action") {
  36. case "update":
  37. if ctx.HasError() {
  38. ctx.HTML(200, tplSettingsOptions)
  39. return
  40. }
  41. isNameChanged := false
  42. oldRepoName := repo.Name
  43. newRepoName := form.RepoName
  44. // Check if repository name has been changed.
  45. if repo.LowerName != strings.ToLower(newRepoName) {
  46. isNameChanged = true
  47. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  48. ctx.Data["Err_RepoName"] = true
  49. switch {
  50. case models.IsErrRepoAlreadyExist(err):
  51. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tplSettingsOptions, &form)
  52. case models.IsErrNameReserved(err):
  53. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplSettingsOptions, &form)
  54. case models.IsErrNamePatternNotAllowed(err):
  55. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
  56. default:
  57. ctx.Handle(500, "ChangeRepositoryName", err)
  58. }
  59. return
  60. }
  61. err := models.NewRepoRedirect(ctx.Repo.Owner.ID, repo.ID, repo.Name, newRepoName)
  62. if err != nil {
  63. ctx.Handle(500, "NewRepoRedirect", err)
  64. return
  65. }
  66. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  67. }
  68. // In case it's just a case change.
  69. repo.Name = newRepoName
  70. repo.LowerName = strings.ToLower(newRepoName)
  71. repo.Description = form.Description
  72. repo.Website = form.Website
  73. // Visibility of forked repository is forced sync with base repository.
  74. if repo.IsFork {
  75. form.Private = repo.BaseRepo.IsPrivate
  76. }
  77. visibilityChanged := repo.IsPrivate != form.Private
  78. repo.IsPrivate = form.Private
  79. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  80. ctx.Handle(500, "UpdateRepository", err)
  81. return
  82. }
  83. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  84. if isNameChanged {
  85. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  86. log.Error(4, "RenameRepoAction: %v", err)
  87. }
  88. }
  89. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  90. ctx.Redirect(repo.Link() + "/settings")
  91. case "mirror":
  92. if !repo.IsMirror {
  93. ctx.Handle(404, "", nil)
  94. return
  95. }
  96. interval, err := time.ParseDuration(form.Interval)
  97. if err != nil || interval < setting.Mirror.MinInterval {
  98. ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
  99. } else {
  100. ctx.Repo.Mirror.EnablePrune = form.EnablePrune
  101. ctx.Repo.Mirror.Interval = interval
  102. ctx.Repo.Mirror.NextUpdate = time.Now().Add(interval)
  103. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  104. ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
  105. return
  106. }
  107. }
  108. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  109. ctx.Handle(500, "SaveAddress", err)
  110. return
  111. }
  112. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  113. ctx.Redirect(repo.Link() + "/settings")
  114. case "mirror-sync":
  115. if !repo.IsMirror {
  116. ctx.Handle(404, "", nil)
  117. return
  118. }
  119. go models.MirrorQueue.Add(repo.ID)
  120. ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
  121. ctx.Redirect(repo.Link() + "/settings")
  122. case "advanced":
  123. var units []models.RepoUnit
  124. for _, tp := range models.MustRepoUnits {
  125. units = append(units, models.RepoUnit{
  126. RepoID: repo.ID,
  127. Type: tp,
  128. Index: int(tp),
  129. Config: new(models.UnitConfig),
  130. })
  131. }
  132. if form.EnableWiki {
  133. if form.EnableExternalWiki {
  134. if !strings.HasPrefix(form.ExternalWikiURL, "http://") && !strings.HasPrefix(form.ExternalWikiURL, "https://") {
  135. ctx.Flash.Error(ctx.Tr("repo.settings.external_wiki_url_error"))
  136. ctx.Redirect(repo.Link() + "/settings")
  137. return
  138. }
  139. units = append(units, models.RepoUnit{
  140. RepoID: repo.ID,
  141. Type: models.UnitTypeExternalWiki,
  142. Index: int(models.UnitTypeExternalWiki),
  143. Config: &models.ExternalWikiConfig{
  144. ExternalWikiURL: form.ExternalWikiURL,
  145. },
  146. })
  147. } else {
  148. units = append(units, models.RepoUnit{
  149. RepoID: repo.ID,
  150. Type: models.UnitTypeWiki,
  151. Index: int(models.UnitTypeWiki),
  152. Config: new(models.UnitConfig),
  153. })
  154. }
  155. }
  156. if form.EnableIssues {
  157. if form.EnableExternalTracker {
  158. if !strings.HasPrefix(form.ExternalTrackerURL, "http://") && !strings.HasPrefix(form.ExternalTrackerURL, "https://") {
  159. ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error"))
  160. ctx.Redirect(repo.Link() + "/settings")
  161. return
  162. }
  163. units = append(units, models.RepoUnit{
  164. RepoID: repo.ID,
  165. Type: models.UnitTypeExternalTracker,
  166. Index: int(models.UnitTypeExternalTracker),
  167. Config: &models.ExternalTrackerConfig{
  168. ExternalTrackerURL: form.ExternalTrackerURL,
  169. ExternalTrackerFormat: form.TrackerURLFormat,
  170. ExternalTrackerStyle: form.TrackerIssueStyle,
  171. },
  172. })
  173. } else {
  174. units = append(units, models.RepoUnit{
  175. RepoID: repo.ID,
  176. Type: models.UnitTypeIssues,
  177. Index: int(models.UnitTypeIssues),
  178. Config: new(models.UnitConfig),
  179. })
  180. }
  181. }
  182. if form.EnablePulls {
  183. units = append(units, models.RepoUnit{
  184. RepoID: repo.ID,
  185. Type: models.UnitTypePullRequests,
  186. Index: int(models.UnitTypePullRequests),
  187. Config: new(models.UnitConfig),
  188. })
  189. }
  190. if err := models.UpdateRepositoryUnits(repo, units); err != nil {
  191. ctx.Handle(500, "UpdateRepositoryUnits", err)
  192. return
  193. }
  194. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  195. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  196. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  197. case "convert":
  198. if !ctx.Repo.IsOwner() {
  199. ctx.Error(404)
  200. return
  201. }
  202. if repo.Name != form.RepoName {
  203. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  204. return
  205. }
  206. if ctx.Repo.Owner.IsOrganization() {
  207. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  208. ctx.Error(404)
  209. return
  210. }
  211. }
  212. if !repo.IsMirror {
  213. ctx.Error(404)
  214. return
  215. }
  216. repo.IsMirror = false
  217. if _, err := models.CleanUpMigrateInfo(repo); err != nil {
  218. ctx.Handle(500, "CleanUpMigrateInfo", err)
  219. return
  220. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  221. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  222. return
  223. }
  224. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  225. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  226. ctx.Redirect(setting.AppSubURL + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  227. case "transfer":
  228. if !ctx.Repo.IsOwner() {
  229. ctx.Error(404)
  230. return
  231. }
  232. if repo.Name != form.RepoName {
  233. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  234. return
  235. }
  236. if ctx.Repo.Owner.IsOrganization() {
  237. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  238. ctx.Error(404)
  239. return
  240. }
  241. }
  242. newOwner := ctx.Query("new_owner_name")
  243. isExist, err := models.IsUserExist(0, newOwner)
  244. if err != nil {
  245. ctx.Handle(500, "IsUserExist", err)
  246. return
  247. } else if !isExist {
  248. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil)
  249. return
  250. }
  251. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  252. if models.IsErrRepoAlreadyExist(err) {
  253. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplSettingsOptions, nil)
  254. } else {
  255. ctx.Handle(500, "TransferOwnership", err)
  256. }
  257. return
  258. }
  259. log.Trace("Repository transferred: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  260. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  261. ctx.Redirect(setting.AppSubURL + "/" + newOwner + "/" + repo.Name)
  262. case "delete":
  263. if !ctx.Repo.IsOwner() {
  264. ctx.Error(404)
  265. return
  266. }
  267. if repo.Name != form.RepoName {
  268. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  269. return
  270. }
  271. if ctx.Repo.Owner.IsOrganization() {
  272. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  273. ctx.Error(404)
  274. return
  275. }
  276. }
  277. if err := models.DeleteRepository(ctx.Repo.Owner.ID, repo.ID); err != nil {
  278. ctx.Handle(500, "DeleteRepository", err)
  279. return
  280. }
  281. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  282. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  283. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  284. case "delete-wiki":
  285. if !ctx.Repo.IsOwner() {
  286. ctx.Error(404)
  287. return
  288. }
  289. if repo.Name != form.RepoName {
  290. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  291. return
  292. }
  293. if ctx.Repo.Owner.IsOrganization() {
  294. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  295. ctx.Error(404)
  296. return
  297. }
  298. }
  299. repo.DeleteWiki()
  300. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  301. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  302. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  303. default:
  304. ctx.Handle(404, "", nil)
  305. }
  306. }
  307. // Collaboration render a repository's collaboration page
  308. func Collaboration(ctx *context.Context) {
  309. ctx.Data["Title"] = ctx.Tr("repo.settings")
  310. ctx.Data["PageIsSettingsCollaboration"] = true
  311. users, err := ctx.Repo.Repository.GetCollaborators()
  312. if err != nil {
  313. ctx.Handle(500, "GetCollaborators", err)
  314. return
  315. }
  316. ctx.Data["Collaborators"] = users
  317. ctx.HTML(200, tplCollaboration)
  318. }
  319. // CollaborationPost response for actions for a collaboration of a repository
  320. func CollaborationPost(ctx *context.Context) {
  321. name := strings.ToLower(ctx.Query("collaborator"))
  322. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  323. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  324. return
  325. }
  326. u, err := models.GetUserByName(name)
  327. if err != nil {
  328. if models.IsErrUserNotExist(err) {
  329. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  330. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  331. } else {
  332. ctx.Handle(500, "GetUserByName", err)
  333. }
  334. return
  335. }
  336. // Organization is not allowed to be added as a collaborator.
  337. if u.IsOrganization() {
  338. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  339. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  340. return
  341. }
  342. // Check if user is organization member.
  343. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.ID) {
  344. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  345. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  346. return
  347. }
  348. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  349. ctx.Handle(500, "AddCollaborator", err)
  350. return
  351. }
  352. if setting.Service.EnableNotifyMail {
  353. models.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository)
  354. }
  355. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  356. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  357. }
  358. // ChangeCollaborationAccessMode response for changing access of a collaboration
  359. func ChangeCollaborationAccessMode(ctx *context.Context) {
  360. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  361. ctx.QueryInt64("uid"),
  362. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  363. log.Error(4, "ChangeCollaborationAccessMode: %v", err)
  364. }
  365. }
  366. // DeleteCollaboration delete a collaboration for a repository
  367. func DeleteCollaboration(ctx *context.Context) {
  368. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  369. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  370. } else {
  371. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  372. }
  373. ctx.JSON(200, map[string]interface{}{
  374. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  375. })
  376. }
  377. // ProtectedBranch render the page to protect the repository
  378. func ProtectedBranch(ctx *context.Context) {
  379. ctx.Data["Title"] = ctx.Tr("repo.settings")
  380. ctx.Data["PageIsSettingsBranches"] = true
  381. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  382. if err != nil {
  383. ctx.Handle(500, "GetProtectedBranches", err)
  384. return
  385. }
  386. ctx.Data["ProtectedBranches"] = protectedBranches
  387. branches := ctx.Data["Branches"].([]string)
  388. leftBranches := make([]string, 0, len(branches)-len(protectedBranches))
  389. for _, b := range branches {
  390. var protected bool
  391. for _, pb := range protectedBranches {
  392. if b == pb.BranchName {
  393. protected = true
  394. break
  395. }
  396. }
  397. if !protected {
  398. leftBranches = append(leftBranches, b)
  399. }
  400. }
  401. ctx.Data["LeftBranches"] = leftBranches
  402. ctx.HTML(200, tplBranches)
  403. }
  404. // ProtectedBranchPost response for protect for a branch of a repository
  405. func ProtectedBranchPost(ctx *context.Context) {
  406. ctx.Data["Title"] = ctx.Tr("repo.settings")
  407. ctx.Data["PageIsSettingsBranches"] = true
  408. repo := ctx.Repo.Repository
  409. switch ctx.Query("action") {
  410. case "default_branch":
  411. if ctx.HasError() {
  412. ctx.HTML(200, tplBranches)
  413. return
  414. }
  415. branch := ctx.Query("branch")
  416. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  417. ctx.Status(404)
  418. return
  419. } else if repo.DefaultBranch != branch {
  420. repo.DefaultBranch = branch
  421. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  422. if !git.IsErrUnsupportedVersion(err) {
  423. ctx.Handle(500, "SetDefaultBranch", err)
  424. return
  425. }
  426. }
  427. if err := repo.UpdateDefaultBranch(); err != nil {
  428. ctx.Handle(500, "SetDefaultBranch", err)
  429. return
  430. }
  431. }
  432. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  433. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  434. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  435. case "protected_branch":
  436. if ctx.HasError() {
  437. ctx.JSON(200, map[string]string{
  438. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  439. })
  440. return
  441. }
  442. branchName := strings.ToLower(ctx.Query("branchName"))
  443. if len(branchName) == 0 || !ctx.Repo.GitRepo.IsBranchExist(branchName) {
  444. ctx.JSON(200, map[string]string{
  445. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  446. })
  447. return
  448. }
  449. canPush := ctx.QueryBool("canPush")
  450. if canPush {
  451. if err := ctx.Repo.Repository.AddProtectedBranch(branchName, canPush); err != nil {
  452. ctx.Flash.Error(ctx.Tr("repo.settings.add_protected_branch_failed", branchName))
  453. ctx.JSON(200, map[string]string{
  454. "status": "ok",
  455. })
  456. return
  457. }
  458. ctx.Flash.Success(ctx.Tr("repo.settings.add_protected_branch_success", branchName))
  459. ctx.JSON(200, map[string]string{
  460. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  461. })
  462. } else {
  463. if err := ctx.Repo.Repository.DeleteProtectedBranch(ctx.QueryInt64("id")); err != nil {
  464. ctx.Flash.Error("DeleteProtectedBranch: " + err.Error())
  465. } else {
  466. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", branchName))
  467. }
  468. ctx.JSON(200, map[string]interface{}{
  469. "status": "ok",
  470. })
  471. }
  472. default:
  473. ctx.Handle(404, "", nil)
  474. }
  475. }
  476. // ChangeProtectedBranch response for changing access of a protect branch
  477. func ChangeProtectedBranch(ctx *context.Context) {
  478. if err := ctx.Repo.Repository.ChangeProtectedBranch(
  479. ctx.QueryInt64("id"),
  480. ctx.QueryBool("canPush")); err != nil {
  481. log.Error(4, "ChangeProtectedBranch: %v", err)
  482. }
  483. }
  484. // DeleteProtectedBranch delete a protection for a branch of a repository
  485. func DeleteProtectedBranch(ctx *context.Context) {
  486. if err := ctx.Repo.Repository.DeleteProtectedBranch(ctx.QueryInt64("id")); err != nil {
  487. ctx.Flash.Error("DeleteProtectedBranch: " + err.Error())
  488. } else {
  489. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success"))
  490. }
  491. ctx.JSON(200, map[string]interface{}{
  492. "redirect": ctx.Repo.RepoLink + "/settings/branches",
  493. })
  494. }
  495. // parseOwnerAndRepo get repos by owner
  496. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  497. owner, err := models.GetUserByName(ctx.Params(":username"))
  498. if err != nil {
  499. if models.IsErrUserNotExist(err) {
  500. ctx.Handle(404, "GetUserByName", err)
  501. } else {
  502. ctx.Handle(500, "GetUserByName", err)
  503. }
  504. return nil, nil
  505. }
  506. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  507. if err != nil {
  508. if models.IsErrRepoNotExist(err) {
  509. ctx.Handle(404, "GetRepositoryByName", err)
  510. } else {
  511. ctx.Handle(500, "GetRepositoryByName", err)
  512. }
  513. return nil, nil
  514. }
  515. return owner, repo
  516. }
  517. // GitHooks hooks of a repository
  518. func GitHooks(ctx *context.Context) {
  519. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  520. ctx.Data["PageIsSettingsGitHooks"] = true
  521. hooks, err := ctx.Repo.GitRepo.Hooks()
  522. if err != nil {
  523. ctx.Handle(500, "Hooks", err)
  524. return
  525. }
  526. ctx.Data["Hooks"] = hooks
  527. ctx.HTML(200, tplGithooks)
  528. }
  529. // GitHooksEdit render for editing a hook of repository page
  530. func GitHooksEdit(ctx *context.Context) {
  531. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  532. ctx.Data["PageIsSettingsGitHooks"] = true
  533. name := ctx.Params(":name")
  534. hook, err := ctx.Repo.GitRepo.GetHook(name)
  535. if err != nil {
  536. if err == git.ErrNotValidHook {
  537. ctx.Handle(404, "GetHook", err)
  538. } else {
  539. ctx.Handle(500, "GetHook", err)
  540. }
  541. return
  542. }
  543. ctx.Data["Hook"] = hook
  544. ctx.HTML(200, tplGithookEdit)
  545. }
  546. // GitHooksEditPost response for editing a git hook of a repository
  547. func GitHooksEditPost(ctx *context.Context) {
  548. name := ctx.Params(":name")
  549. hook, err := ctx.Repo.GitRepo.GetHook(name)
  550. if err != nil {
  551. if err == git.ErrNotValidHook {
  552. ctx.Handle(404, "GetHook", err)
  553. } else {
  554. ctx.Handle(500, "GetHook", err)
  555. }
  556. return
  557. }
  558. hook.Content = ctx.Query("content")
  559. if err = hook.Update(); err != nil {
  560. ctx.Handle(500, "hook.Update", err)
  561. return
  562. }
  563. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  564. }
  565. // DeployKeys render the deploy keys list of a repository page
  566. func DeployKeys(ctx *context.Context) {
  567. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  568. ctx.Data["PageIsSettingsKeys"] = true
  569. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  570. if err != nil {
  571. ctx.Handle(500, "ListDeployKeys", err)
  572. return
  573. }
  574. ctx.Data["Deploykeys"] = keys
  575. ctx.HTML(200, tplDeployKeys)
  576. }
  577. // DeployKeysPost response for adding a deploy key of a repository
  578. func DeployKeysPost(ctx *context.Context, form auth.AddKeyForm) {
  579. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  580. ctx.Data["PageIsSettingsKeys"] = true
  581. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  582. if err != nil {
  583. ctx.Handle(500, "ListDeployKeys", err)
  584. return
  585. }
  586. ctx.Data["Deploykeys"] = keys
  587. if ctx.HasError() {
  588. ctx.HTML(200, tplDeployKeys)
  589. return
  590. }
  591. content, err := models.CheckPublicKeyString(form.Content)
  592. if err != nil {
  593. if models.IsErrKeyUnableVerify(err) {
  594. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  595. } else {
  596. ctx.Data["HasError"] = true
  597. ctx.Data["Err_Content"] = true
  598. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  599. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  600. return
  601. }
  602. }
  603. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  604. if err != nil {
  605. ctx.Data["HasError"] = true
  606. switch {
  607. case models.IsErrKeyAlreadyExist(err):
  608. ctx.Data["Err_Content"] = true
  609. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), tplDeployKeys, &form)
  610. case models.IsErrKeyNameAlreadyUsed(err):
  611. ctx.Data["Err_Title"] = true
  612. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form)
  613. default:
  614. ctx.Handle(500, "AddDeployKey", err)
  615. }
  616. return
  617. }
  618. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  619. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  620. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  621. }
  622. // DeleteDeployKey response for deleting a deploy key
  623. func DeleteDeployKey(ctx *context.Context) {
  624. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  625. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  626. } else {
  627. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  628. }
  629. ctx.JSON(200, map[string]interface{}{
  630. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  631. })
  632. }