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

729 lines
20 KiB

10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "time"
  11. "github.com/Unknwon/com"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/auth"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/git"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/mailer"
  18. "github.com/gogits/gogs/modules/middleware"
  19. "github.com/gogits/gogs/modules/setting"
  20. )
  21. const (
  22. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  23. COLLABORATION base.TplName = "repo/settings/collaboration"
  24. HOOKS base.TplName = "repo/settings/hooks"
  25. HOOK_NEW base.TplName = "repo/settings/hook_new"
  26. ORG_HOOK_NEW base.TplName = "org/settings/hook_new"
  27. GITHOOKS base.TplName = "repo/settings/githooks"
  28. GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  29. DEPLOY_KEYS base.TplName = "repo/settings/deploy_keys"
  30. )
  31. func Settings(ctx *middleware.Context) {
  32. ctx.Data["Title"] = ctx.Tr("repo.settings")
  33. ctx.Data["PageIsSettingsOptions"] = true
  34. ctx.HTML(200, SETTINGS_OPTIONS)
  35. }
  36. func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
  37. ctx.Data["Title"] = ctx.Tr("repo.settings")
  38. ctx.Data["PageIsSettingsOptions"] = true
  39. repo := ctx.Repo.Repository
  40. switch ctx.Query("action") {
  41. case "update":
  42. if ctx.HasError() {
  43. ctx.HTML(200, SETTINGS_OPTIONS)
  44. return
  45. }
  46. isNameChanged := false
  47. oldRepoName := repo.Name
  48. newRepoName := form.RepoName
  49. // Check if repository name has been changed.
  50. if repo.LowerName != strings.ToLower(newRepoName) {
  51. isNameChanged = true
  52. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  53. ctx.Data["Err_RepoName"] = true
  54. switch {
  55. case models.IsErrRepoAlreadyExist(err):
  56. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form)
  57. case models.IsErrNameReserved(err):
  58. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form)
  59. case models.IsErrNamePatternNotAllowed(err):
  60. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form)
  61. default:
  62. ctx.Handle(500, "ChangeRepositoryName", err)
  63. }
  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. if ctx.Repo.GitRepo.IsBranchExist(form.Branch) &&
  72. repo.DefaultBranch != form.Branch {
  73. repo.DefaultBranch = form.Branch
  74. if err := ctx.Repo.GitRepo.SetDefaultBranch(form.Branch); err != nil {
  75. if !git.IsErrUnsupportedVersion(err) {
  76. ctx.Handle(500, "SetDefaultBranch", err)
  77. return
  78. }
  79. }
  80. }
  81. repo.Description = form.Description
  82. repo.Website = form.Website
  83. // Visibility of forked repository is forced sync with base repository.
  84. if repo.IsFork {
  85. form.Private = repo.BaseRepo.IsPrivate
  86. }
  87. visibilityChanged := repo.IsPrivate != form.Private
  88. repo.IsPrivate = form.Private
  89. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  90. ctx.Handle(500, "UpdateRepository", err)
  91. return
  92. }
  93. log.Trace("Repository updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  94. if isNameChanged {
  95. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  96. log.Error(4, "RenameRepoAction: %v", err)
  97. }
  98. }
  99. if repo.IsMirror {
  100. if form.Interval > 0 {
  101. ctx.Repo.Mirror.Interval = form.Interval
  102. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  103. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  104. log.Error(4, "UpdateMirror: %v", err)
  105. }
  106. }
  107. }
  108. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  109. ctx.Redirect(fmt.Sprintf("%s/%s/%s/settings", setting.AppSubUrl, ctx.Repo.Owner.Name, repo.Name))
  110. case "transfer":
  111. if repo.Name != form.RepoName {
  112. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  113. return
  114. }
  115. if ctx.Repo.Owner.IsOrganization() {
  116. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  117. ctx.Error(404)
  118. return
  119. }
  120. }
  121. newOwner := ctx.Query("new_owner_name")
  122. isExist, err := models.IsUserExist(0, newOwner)
  123. if err != nil {
  124. ctx.Handle(500, "IsUserExist", err)
  125. return
  126. } else if !isExist {
  127. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  128. return
  129. }
  130. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  131. if models.IsErrRepoAlreadyExist(err) {
  132. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  133. } else {
  134. ctx.Handle(500, "TransferOwnership", err)
  135. }
  136. return
  137. }
  138. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  139. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  140. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  141. case "delete":
  142. if repo.Name != form.RepoName {
  143. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  144. return
  145. }
  146. if ctx.Repo.Owner.IsOrganization() {
  147. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  148. ctx.Error(404)
  149. return
  150. }
  151. }
  152. if err := models.DeleteRepository(ctx.Repo.Owner.Id, repo.ID); err != nil {
  153. ctx.Handle(500, "DeleteRepository", err)
  154. return
  155. }
  156. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  157. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  158. }
  159. }
  160. func Collaboration(ctx *middleware.Context) {
  161. ctx.Data["Title"] = ctx.Tr("repo.settings")
  162. ctx.Data["PageIsSettingsCollaboration"] = true
  163. if ctx.Req.Method == "POST" {
  164. name := strings.ToLower(ctx.Query("collaborator"))
  165. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  166. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  167. return
  168. }
  169. u, err := models.GetUserByName(name)
  170. if err != nil {
  171. if models.IsErrUserNotExist(err) {
  172. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  173. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  174. } else {
  175. ctx.Handle(500, "GetUserByName", err)
  176. }
  177. return
  178. }
  179. // Check if user is organization member.
  180. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  181. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  182. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  183. return
  184. }
  185. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  186. ctx.Handle(500, "AddCollaborator", err)
  187. return
  188. }
  189. if setting.Service.EnableNotifyMail {
  190. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  191. ctx.Handle(500, "SendCollaboratorMail", err)
  192. return
  193. }
  194. }
  195. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  196. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  197. return
  198. }
  199. // Delete collaborator.
  200. remove := strings.ToLower(ctx.Query("remove"))
  201. if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
  202. u, err := models.GetUserByName(remove)
  203. if err != nil {
  204. ctx.Handle(500, "GetUserByName", err)
  205. return
  206. }
  207. if err := ctx.Repo.Repository.DeleteCollaborator(u); err != nil {
  208. ctx.Handle(500, "DeleteCollaborator", err)
  209. return
  210. }
  211. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  212. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  213. return
  214. }
  215. users, err := ctx.Repo.Repository.GetCollaborators()
  216. if err != nil {
  217. ctx.Handle(500, "GetCollaborators", err)
  218. return
  219. }
  220. ctx.Data["Collaborators"] = users
  221. ctx.HTML(200, COLLABORATION)
  222. }
  223. func Webhooks(ctx *middleware.Context) {
  224. ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
  225. ctx.Data["PageIsSettingsHooks"] = true
  226. ctx.Data["BaseLink"] = ctx.Repo.RepoLink
  227. ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories---Webhooks")
  228. ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  229. if err != nil {
  230. ctx.Handle(500, "GetWebhooksByRepoID", err)
  231. return
  232. }
  233. ctx.Data["Webhooks"] = ws
  234. ctx.HTML(200, HOOKS)
  235. }
  236. type OrgRepoCtx struct {
  237. OrgID int64
  238. RepoID int64
  239. Link string
  240. NewTemplate base.TplName
  241. }
  242. // getOrgRepoCtx determines whether this is a repo context or organization context.
  243. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  244. if len(ctx.Repo.RepoLink) > 0 {
  245. return &OrgRepoCtx{
  246. RepoID: ctx.Repo.Repository.ID,
  247. Link: ctx.Repo.RepoLink,
  248. NewTemplate: HOOK_NEW,
  249. }, nil
  250. }
  251. if len(ctx.Org.OrgLink) > 0 {
  252. return &OrgRepoCtx{
  253. OrgID: ctx.Org.Organization.Id,
  254. Link: ctx.Org.OrgLink,
  255. NewTemplate: ORG_HOOK_NEW,
  256. }, nil
  257. }
  258. return nil, errors.New("Unable to set OrgRepo context")
  259. }
  260. func checkHookType(ctx *middleware.Context) string {
  261. hookType := strings.ToLower(ctx.Params(":type"))
  262. if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
  263. ctx.Handle(404, "checkHookType", nil)
  264. return ""
  265. }
  266. return hookType
  267. }
  268. func WebhooksNew(ctx *middleware.Context) {
  269. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  270. ctx.Data["PageIsSettingsHooks"] = true
  271. ctx.Data["PageIsSettingsHooksNew"] = true
  272. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  273. orCtx, err := getOrgRepoCtx(ctx)
  274. if err != nil {
  275. ctx.Handle(500, "getOrgRepoCtx", err)
  276. return
  277. }
  278. ctx.Data["HookType"] = checkHookType(ctx)
  279. if ctx.Written() {
  280. return
  281. }
  282. ctx.Data["BaseLink"] = orCtx.Link
  283. ctx.HTML(200, orCtx.NewTemplate)
  284. }
  285. func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
  286. return &models.HookEvent{
  287. PushOnly: form.PushOnly(),
  288. SendEverything: form.SendEverything(),
  289. ChooseEvents: form.ChooseEvents(),
  290. HookEvents: models.HookEvents{
  291. Create: form.Create,
  292. Push: form.Push,
  293. },
  294. }
  295. }
  296. func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  297. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  298. ctx.Data["PageIsSettingsHooks"] = true
  299. ctx.Data["PageIsSettingsHooksNew"] = true
  300. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  301. ctx.Data["HookType"] = "gogs"
  302. orCtx, err := getOrgRepoCtx(ctx)
  303. if err != nil {
  304. ctx.Handle(500, "getOrgRepoCtx", err)
  305. return
  306. }
  307. ctx.Data["BaseLink"] = orCtx.Link
  308. if ctx.HasError() {
  309. ctx.HTML(200, orCtx.NewTemplate)
  310. return
  311. }
  312. contentType := models.JSON
  313. if models.HookContentType(form.ContentType) == models.FORM {
  314. contentType = models.FORM
  315. }
  316. w := &models.Webhook{
  317. RepoID: orCtx.RepoID,
  318. URL: form.PayloadURL,
  319. ContentType: contentType,
  320. Secret: form.Secret,
  321. HookEvent: ParseHookEvent(form.WebhookForm),
  322. IsActive: form.Active,
  323. HookTaskType: models.GOGS,
  324. OrgID: orCtx.OrgID,
  325. }
  326. if err := w.UpdateEvent(); err != nil {
  327. ctx.Handle(500, "UpdateEvent", err)
  328. return
  329. } else if err := models.CreateWebhook(w); err != nil {
  330. ctx.Handle(500, "CreateWebhook", err)
  331. return
  332. }
  333. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  334. ctx.Redirect(orCtx.Link + "/settings/hooks")
  335. }
  336. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  337. ctx.Data["Title"] = ctx.Tr("repo.settings")
  338. ctx.Data["PageIsSettingsHooks"] = true
  339. ctx.Data["PageIsSettingsHooksNew"] = true
  340. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  341. orCtx, err := getOrgRepoCtx(ctx)
  342. if err != nil {
  343. ctx.Handle(500, "getOrgRepoCtx", err)
  344. return
  345. }
  346. if ctx.HasError() {
  347. ctx.HTML(200, orCtx.NewTemplate)
  348. return
  349. }
  350. meta, err := json.Marshal(&models.SlackMeta{
  351. Channel: form.Channel,
  352. Username: form.Username,
  353. IconURL: form.IconURL,
  354. Color: form.Color,
  355. })
  356. if err != nil {
  357. ctx.Handle(500, "Marshal", err)
  358. return
  359. }
  360. w := &models.Webhook{
  361. RepoID: orCtx.RepoID,
  362. URL: form.PayloadURL,
  363. ContentType: models.JSON,
  364. HookEvent: ParseHookEvent(form.WebhookForm),
  365. IsActive: form.Active,
  366. HookTaskType: models.SLACK,
  367. Meta: string(meta),
  368. OrgID: orCtx.OrgID,
  369. }
  370. if err := w.UpdateEvent(); err != nil {
  371. ctx.Handle(500, "UpdateEvent", err)
  372. return
  373. } else if err := models.CreateWebhook(w); err != nil {
  374. ctx.Handle(500, "CreateWebhook", err)
  375. return
  376. }
  377. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  378. ctx.Redirect(orCtx.Link + "/settings/hooks")
  379. }
  380. func checkWebhook(ctx *middleware.Context) (*OrgRepoCtx, *models.Webhook) {
  381. ctx.Data["RequireHighlightJS"] = true
  382. orCtx, err := getOrgRepoCtx(ctx)
  383. if err != nil {
  384. ctx.Handle(500, "getOrgRepoCtx", err)
  385. return nil, nil
  386. }
  387. ctx.Data["BaseLink"] = orCtx.Link
  388. w, err := models.GetWebhookByID(ctx.ParamsInt64(":id"))
  389. if err != nil {
  390. if models.IsErrWebhookNotExist(err) {
  391. ctx.Handle(404, "GetWebhookByID", nil)
  392. } else {
  393. ctx.Handle(500, "GetWebhookByID", err)
  394. }
  395. return nil, nil
  396. }
  397. switch w.HookTaskType {
  398. case models.SLACK:
  399. ctx.Data["SlackHook"] = w.GetSlackHook()
  400. ctx.Data["HookType"] = "slack"
  401. default:
  402. ctx.Data["HookType"] = "gogs"
  403. }
  404. ctx.Data["History"], err = w.History(1)
  405. if err != nil {
  406. ctx.Handle(500, "History", err)
  407. }
  408. return orCtx, w
  409. }
  410. func WebHooksEdit(ctx *middleware.Context) {
  411. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  412. ctx.Data["PageIsSettingsHooks"] = true
  413. ctx.Data["PageIsSettingsHooksEdit"] = true
  414. orCtx, w := checkWebhook(ctx)
  415. if ctx.Written() {
  416. return
  417. }
  418. ctx.Data["Webhook"] = w
  419. ctx.HTML(200, orCtx.NewTemplate)
  420. }
  421. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  422. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  423. ctx.Data["PageIsSettingsHooks"] = true
  424. ctx.Data["PageIsSettingsHooksEdit"] = true
  425. orCtx, w := checkWebhook(ctx)
  426. if ctx.Written() {
  427. return
  428. }
  429. ctx.Data["Webhook"] = w
  430. if ctx.HasError() {
  431. ctx.HTML(200, orCtx.NewTemplate)
  432. return
  433. }
  434. contentType := models.JSON
  435. if models.HookContentType(form.ContentType) == models.FORM {
  436. contentType = models.FORM
  437. }
  438. w.URL = form.PayloadURL
  439. w.ContentType = contentType
  440. w.Secret = form.Secret
  441. w.HookEvent = ParseHookEvent(form.WebhookForm)
  442. w.IsActive = form.Active
  443. if err := w.UpdateEvent(); err != nil {
  444. ctx.Handle(500, "UpdateEvent", err)
  445. return
  446. } else if err := models.UpdateWebhook(w); err != nil {
  447. ctx.Handle(500, "WebHooksEditPost", err)
  448. return
  449. }
  450. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  451. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  452. }
  453. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  454. ctx.Data["Title"] = ctx.Tr("repo.settings")
  455. ctx.Data["PageIsSettingsHooks"] = true
  456. ctx.Data["PageIsSettingsHooksEdit"] = true
  457. orCtx, w := checkWebhook(ctx)
  458. if ctx.Written() {
  459. return
  460. }
  461. ctx.Data["Webhook"] = w
  462. if ctx.HasError() {
  463. ctx.HTML(200, orCtx.NewTemplate)
  464. return
  465. }
  466. meta, err := json.Marshal(&models.SlackMeta{
  467. Channel: form.Channel,
  468. Username: form.Username,
  469. IconURL: form.IconURL,
  470. Color: form.Color,
  471. })
  472. if err != nil {
  473. ctx.Handle(500, "Marshal", err)
  474. return
  475. }
  476. w.URL = form.PayloadURL
  477. w.Meta = string(meta)
  478. w.HookEvent = ParseHookEvent(form.WebhookForm)
  479. w.IsActive = form.Active
  480. if err := w.UpdateEvent(); err != nil {
  481. ctx.Handle(500, "UpdateEvent", err)
  482. return
  483. } else if err := models.UpdateWebhook(w); err != nil {
  484. ctx.Handle(500, "UpdateWebhook", err)
  485. return
  486. }
  487. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  488. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  489. }
  490. func DeleteWebhook(ctx *middleware.Context) {
  491. if err := models.DeleteWebhook(ctx.QueryInt64("id")); err != nil {
  492. ctx.Flash.Error("DeleteWebhook: " + err.Error())
  493. } else {
  494. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  495. }
  496. ctx.JSON(200, map[string]interface{}{
  497. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  498. })
  499. }
  500. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  501. owner, err := models.GetUserByName(ctx.Params(":username"))
  502. if err != nil {
  503. if models.IsErrUserNotExist(err) {
  504. ctx.Handle(404, "GetUserByName", err)
  505. } else {
  506. ctx.Handle(500, "GetUserByName", err)
  507. }
  508. return nil, nil
  509. }
  510. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  511. if err != nil {
  512. if models.IsErrRepoNotExist(err) {
  513. ctx.Handle(404, "GetRepositoryByName", err)
  514. } else {
  515. ctx.Handle(500, "GetRepositoryByName", err)
  516. }
  517. return nil, nil
  518. }
  519. return owner, repo
  520. }
  521. func GitHooks(ctx *middleware.Context) {
  522. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  523. ctx.Data["PageIsSettingsGitHooks"] = true
  524. hooks, err := ctx.Repo.GitRepo.Hooks()
  525. if err != nil {
  526. ctx.Handle(500, "Hooks", err)
  527. return
  528. }
  529. ctx.Data["Hooks"] = hooks
  530. ctx.HTML(200, GITHOOKS)
  531. }
  532. func GitHooksEdit(ctx *middleware.Context) {
  533. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  534. ctx.Data["PageIsSettingsGitHooks"] = true
  535. name := ctx.Params(":name")
  536. hook, err := ctx.Repo.GitRepo.GetHook(name)
  537. if err != nil {
  538. if err == git.ErrNotValidHook {
  539. ctx.Handle(404, "GetHook", err)
  540. } else {
  541. ctx.Handle(500, "GetHook", err)
  542. }
  543. return
  544. }
  545. ctx.Data["Hook"] = hook
  546. ctx.HTML(200, GITHOOK_EDIT)
  547. }
  548. func GitHooksEditPost(ctx *middleware.Context) {
  549. name := ctx.Params(":name")
  550. hook, err := ctx.Repo.GitRepo.GetHook(name)
  551. if err != nil {
  552. if err == git.ErrNotValidHook {
  553. ctx.Handle(404, "GetHook", err)
  554. } else {
  555. ctx.Handle(500, "GetHook", err)
  556. }
  557. return
  558. }
  559. hook.Content = ctx.Query("content")
  560. if err = hook.Update(); err != nil {
  561. ctx.Handle(500, "hook.Update", err)
  562. return
  563. }
  564. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  565. }
  566. func DeployKeys(ctx *middleware.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, DEPLOY_KEYS)
  576. }
  577. func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  578. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  579. ctx.Data["PageIsSettingsKeys"] = true
  580. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  581. if err != nil {
  582. ctx.Handle(500, "ListDeployKeys", err)
  583. return
  584. }
  585. ctx.Data["Deploykeys"] = keys
  586. if ctx.HasError() {
  587. ctx.HTML(200, DEPLOY_KEYS)
  588. return
  589. }
  590. content, err := models.CheckPublicKeyString(form.Content)
  591. if err != nil {
  592. if models.IsErrKeyUnableVerify(err) {
  593. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  594. } else {
  595. ctx.Data["HasError"] = true
  596. ctx.Data["Err_Content"] = true
  597. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  598. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  599. return
  600. }
  601. }
  602. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  603. if err != nil {
  604. ctx.Data["HasError"] = true
  605. switch {
  606. case models.IsErrKeyAlreadyExist(err):
  607. ctx.Data["Err_Content"] = true
  608. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  609. case models.IsErrKeyNameAlreadyUsed(err):
  610. ctx.Data["Err_Title"] = true
  611. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  612. default:
  613. ctx.Handle(500, "AddDeployKey", err)
  614. }
  615. return
  616. }
  617. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  618. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  619. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  620. }
  621. func DeleteDeployKey(ctx *middleware.Context) {
  622. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  623. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  624. } else {
  625. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  626. }
  627. ctx.JSON(200, map[string]interface{}{
  628. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  629. })
  630. }