setting.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. GITHOOKS base.TplName = "repo/settings/githooks"
  26. GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  27. HOOK_NEW base.TplName = "repo/settings/hook_new"
  28. ORG_HOOK_NEW base.TplName = "org/settings/hook_new"
  29. )
  30. func Settings(ctx *middleware.Context) {
  31. ctx.Data["Title"] = ctx.Tr("repo.settings")
  32. ctx.Data["PageIsSettingsOptions"] = true
  33. ctx.HTML(200, SETTINGS_OPTIONS)
  34. }
  35. func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
  36. ctx.Data["Title"] = ctx.Tr("repo.settings")
  37. ctx.Data["PageIsSettingsOptions"] = true
  38. switch ctx.Query("action") {
  39. case "update":
  40. if ctx.HasError() {
  41. ctx.HTML(200, SETTINGS_OPTIONS)
  42. return
  43. }
  44. newRepoName := form.RepoName
  45. // Check if repository name has been changed.
  46. if ctx.Repo.Repository.Name != newRepoName {
  47. isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName)
  48. if err != nil {
  49. ctx.Handle(500, "IsRepositoryExist", err)
  50. return
  51. } else if isExist {
  52. ctx.Data["Err_RepoName"] = true
  53. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, nil)
  54. return
  55. } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
  56. if err == models.ErrRepoNameIllegal {
  57. ctx.Data["Err_RepoName"] = true
  58. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), SETTINGS_OPTIONS, nil)
  59. } else {
  60. ctx.Handle(500, "ChangeRepositoryName", err)
  61. }
  62. return
  63. }
  64. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName)
  65. ctx.Repo.Repository.Name = newRepoName
  66. }
  67. br := form.Branch
  68. if ctx.Repo.GitRepo.IsBranchExist(br) {
  69. ctx.Repo.Repository.DefaultBranch = br
  70. }
  71. ctx.Repo.Repository.Description = form.Description
  72. ctx.Repo.Repository.Website = form.Website
  73. ctx.Repo.Repository.IsPrivate = form.Private
  74. ctx.Repo.Repository.IsGoget = form.GoGet
  75. if err := models.UpdateRepository(ctx.Repo.Repository); err != nil {
  76. ctx.Handle(404, "UpdateRepository", err)
  77. return
  78. }
  79. log.Trace("Repository updated: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  80. if ctx.Repo.Repository.IsMirror {
  81. if form.Interval > 0 {
  82. ctx.Repo.Mirror.Interval = form.Interval
  83. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  84. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  85. log.Error(4, "UpdateMirror: %v", err)
  86. }
  87. }
  88. }
  89. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  90. ctx.Redirect(fmt.Sprintf("%s/%s/%s/settings", setting.AppSubUrl, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
  91. case "transfer":
  92. if ctx.Repo.Repository.Name != form.RepoName {
  93. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  94. return
  95. }
  96. newOwner := ctx.Query("new_owner_name")
  97. isExist, err := models.IsUserExist(newOwner)
  98. if err != nil {
  99. ctx.Handle(500, "IsUserExist", err)
  100. return
  101. } else if !isExist {
  102. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  103. return
  104. } else if !ctx.User.ValidtePassword(ctx.Query("password")) {
  105. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
  106. return
  107. } else if err = models.TransferOwnership(ctx.User, newOwner, ctx.Repo.Repository); err != nil {
  108. if err == models.ErrRepoAlreadyExist {
  109. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  110. } else {
  111. ctx.Handle(500, "TransferOwnership", err)
  112. }
  113. return
  114. }
  115. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newOwner)
  116. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  117. ctx.Redirect(setting.AppSubUrl + "/")
  118. case "delete":
  119. if ctx.Repo.Repository.Name != form.RepoName {
  120. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  121. return
  122. }
  123. if ctx.Repo.Owner.IsOrganization() {
  124. if !ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
  125. ctx.Error(404)
  126. return
  127. }
  128. if !ctx.User.ValidtePassword(ctx.Query("password")) {
  129. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
  130. return
  131. }
  132. } else {
  133. if !ctx.Repo.Owner.ValidtePassword(ctx.Query("password")) {
  134. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
  135. return
  136. }
  137. }
  138. if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil {
  139. ctx.Handle(500, "DeleteRepository", err)
  140. return
  141. }
  142. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  143. if ctx.Repo.Owner.IsOrganization() {
  144. ctx.Redirect(setting.AppSubUrl + "/org/" + ctx.Repo.Owner.Name + "/dashboard")
  145. } else {
  146. ctx.Redirect(setting.AppSubUrl + "/")
  147. }
  148. }
  149. }
  150. func SettingsCollaboration(ctx *middleware.Context) {
  151. ctx.Data["Title"] = ctx.Tr("repo.settings")
  152. ctx.Data["PageIsSettingsCollaboration"] = true
  153. repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
  154. if ctx.Req.Method == "POST" {
  155. name := strings.ToLower(ctx.Query("collaborator"))
  156. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  157. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  158. return
  159. }
  160. has, err := models.HasAccess(name, repoLink, models.WRITABLE)
  161. if err != nil {
  162. ctx.Handle(500, "HasAccess", err)
  163. return
  164. } else if has {
  165. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  166. return
  167. }
  168. u, err := models.GetUserByName(name)
  169. if err != nil {
  170. if err == models.ErrUserNotExist {
  171. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  172. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  173. } else {
  174. ctx.Handle(500, "GetUserByName", err)
  175. }
  176. return
  177. }
  178. // Check if user is organization member.
  179. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  180. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  181. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  182. return
  183. }
  184. if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink,
  185. Mode: models.WRITABLE}); err != nil {
  186. ctx.Handle(500, "AddAccess", 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. needDelete := true
  203. if ctx.User.IsOrganization() {
  204. // Check if user belongs to a team that has access to this repository.
  205. auth, err := models.GetHighestAuthorize(ctx.Repo.Owner.Id, ctx.User.Id, ctx.Repo.Repository.Id, 0)
  206. if err != nil {
  207. ctx.Handle(500, "GetHighestAuthorize", err)
  208. return
  209. }
  210. if auth > 0 {
  211. needDelete = false
  212. }
  213. }
  214. if needDelete {
  215. if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil {
  216. ctx.Handle(500, "DeleteAccess", err)
  217. return
  218. }
  219. }
  220. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  221. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  222. return
  223. }
  224. names, err := models.GetCollaboratorNames(repoLink)
  225. if err != nil {
  226. ctx.Handle(500, "GetCollaborators", err)
  227. return
  228. }
  229. collaborators := make([]*models.User, 0, len(names))
  230. for _, name := range names {
  231. u, err := models.GetUserByName(name)
  232. if err != nil {
  233. ctx.Handle(500, "GetUserByName", err)
  234. return
  235. }
  236. // Does not show organization members.
  237. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  238. continue
  239. }
  240. collaborators = append(collaborators, u)
  241. }
  242. ctx.Data["Collaborators"] = collaborators
  243. ctx.HTML(200, COLLABORATION)
  244. }
  245. func Webhooks(ctx *middleware.Context) {
  246. ctx.Data["Title"] = ctx.Tr("repo.settings")
  247. ctx.Data["PageIsSettingsHooks"] = true
  248. // Delete web hook.
  249. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  250. if remove > 0 {
  251. if err := models.DeleteWebhook(remove); err != nil {
  252. ctx.Handle(500, "DeleteWebhook", err)
  253. return
  254. }
  255. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  256. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
  257. return
  258. }
  259. ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id)
  260. if err != nil {
  261. ctx.Handle(500, "GetWebhooksByRepoId", err)
  262. return
  263. }
  264. ctx.Data["Webhooks"] = ws
  265. ctx.HTML(200, HOOKS)
  266. }
  267. func renderHookTypes(ctx *middleware.Context) {
  268. ctx.Data["HookTypes"] = []string{"Gogs", "Slack"}
  269. ctx.Data["HookType"] = "Gogs"
  270. }
  271. func WebHooksNew(ctx *middleware.Context) {
  272. ctx.Data["Title"] = ctx.Tr("repo.settings")
  273. ctx.Data["PageIsSettingsHooks"] = true
  274. ctx.Data["PageIsSettingsHooksNew"] = true
  275. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  276. renderHookTypes(ctx)
  277. orCtx, err := getOrgRepoCtx(ctx)
  278. if err != nil {
  279. ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err)
  280. return
  281. }
  282. ctx.HTML(200, orCtx.NewTemplate)
  283. }
  284. func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  285. ctx.Data["Title"] = ctx.Tr("repo.settings")
  286. ctx.Data["PageIsSettingsHooks"] = true
  287. ctx.Data["PageIsSettingsHooksNew"] = true
  288. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  289. orCtx, err := getOrgRepoCtx(ctx)
  290. if err != nil {
  291. ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err)
  292. return
  293. }
  294. if ctx.HasError() {
  295. ctx.HTML(200, orCtx.NewTemplate)
  296. return
  297. }
  298. // FIXME: code too old here, sync with APIs
  299. ct := models.JSON
  300. if form.ContentType == "2" {
  301. ct = models.FORM
  302. }
  303. w := &models.Webhook{
  304. RepoId: orCtx.RepoId,
  305. Url: form.PayloadUrl,
  306. ContentType: ct,
  307. Secret: form.Secret,
  308. HookEvent: &models.HookEvent{
  309. PushOnly: form.PushOnly,
  310. },
  311. IsActive: form.Active,
  312. HookTaskType: models.GOGS,
  313. Meta: "",
  314. OrgId: orCtx.OrgId,
  315. }
  316. if err := w.UpdateEvent(); err != nil {
  317. ctx.Handle(500, "UpdateEvent", err)
  318. return
  319. } else if err := models.CreateWebhook(w); err != nil {
  320. ctx.Handle(500, "CreateWebhook", err)
  321. return
  322. }
  323. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  324. ctx.Redirect(orCtx.Link + "/settings/hooks")
  325. }
  326. func WebHooksEdit(ctx *middleware.Context) {
  327. ctx.Data["Title"] = ctx.Tr("repo.settings")
  328. ctx.Data["PageIsSettingsHooks"] = true
  329. ctx.Data["PageIsSettingsHooksEdit"] = true
  330. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  331. if hookId == 0 {
  332. ctx.Handle(404, "setting.WebHooksEdit", nil)
  333. return
  334. }
  335. w, err := models.GetWebhookById(hookId)
  336. if err != nil {
  337. if err == models.ErrWebhookNotExist {
  338. ctx.Handle(404, "GetWebhookById", nil)
  339. } else {
  340. ctx.Handle(500, "GetWebhookById", err)
  341. }
  342. return
  343. }
  344. // set data per HookTaskType
  345. switch w.HookTaskType {
  346. case models.SLACK:
  347. {
  348. ctx.Data["SlackHook"] = w.GetSlackHook()
  349. ctx.Data["HookType"] = "Slack"
  350. }
  351. default:
  352. {
  353. ctx.Data["HookType"] = "Gogs"
  354. }
  355. }
  356. w.GetEvent()
  357. ctx.Data["Webhook"] = w
  358. orCtx, err := getOrgRepoCtx(ctx)
  359. if err != nil {
  360. ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
  361. return
  362. }
  363. ctx.HTML(200, orCtx.NewTemplate)
  364. }
  365. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  366. ctx.Data["Title"] = ctx.Tr("repo.settings")
  367. ctx.Data["PageIsSettingsHooks"] = true
  368. ctx.Data["PageIsSettingsHooksEdit"] = true
  369. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  370. if hookId == 0 {
  371. ctx.Handle(404, "setting.WebHooksEditPost", nil)
  372. return
  373. }
  374. w, err := models.GetWebhookById(hookId)
  375. if err != nil {
  376. if err == models.ErrWebhookNotExist {
  377. ctx.Handle(404, "GetWebhookById", nil)
  378. } else {
  379. ctx.Handle(500, "GetWebhookById", err)
  380. }
  381. return
  382. }
  383. w.GetEvent()
  384. ctx.Data["Webhook"] = w
  385. orCtx, err := getOrgRepoCtx(ctx)
  386. if err != nil {
  387. ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
  388. return
  389. }
  390. if ctx.HasError() {
  391. ctx.HTML(200, orCtx.NewTemplate)
  392. return
  393. }
  394. ct := models.JSON
  395. if form.ContentType == "2" {
  396. ct = models.FORM
  397. }
  398. w.Url = form.PayloadUrl
  399. w.ContentType = ct
  400. w.Secret = form.Secret
  401. w.HookEvent = &models.HookEvent{
  402. PushOnly: form.PushOnly,
  403. }
  404. w.IsActive = form.Active
  405. if err := w.UpdateEvent(); err != nil {
  406. ctx.Handle(500, "UpdateEvent", err)
  407. return
  408. } else if err := models.UpdateWebhook(w); err != nil {
  409. ctx.Handle(500, "WebHooksEditPost", err)
  410. return
  411. }
  412. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  413. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  414. }
  415. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  416. ctx.Data["Title"] = ctx.Tr("repo.settings")
  417. ctx.Data["PageIsSettingsHooks"] = true
  418. ctx.Data["PageIsSettingsHooksNew"] = true
  419. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  420. orCtx, err := getOrgRepoCtx(ctx)
  421. if err != nil {
  422. ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
  423. return
  424. }
  425. if ctx.HasError() {
  426. ctx.HTML(200, orCtx.NewTemplate)
  427. return
  428. }
  429. meta, err := json.Marshal(&models.Slack{
  430. Channel: form.Channel,
  431. })
  432. if err != nil {
  433. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  434. return
  435. }
  436. w := &models.Webhook{
  437. RepoId: orCtx.RepoId,
  438. Url: form.PayloadUrl,
  439. ContentType: models.JSON,
  440. Secret: "",
  441. HookEvent: &models.HookEvent{
  442. PushOnly: form.PushOnly,
  443. },
  444. IsActive: form.Active,
  445. HookTaskType: models.SLACK,
  446. Meta: string(meta),
  447. OrgId: orCtx.OrgId,
  448. }
  449. if err := w.UpdateEvent(); err != nil {
  450. ctx.Handle(500, "UpdateEvent", err)
  451. return
  452. } else if err := models.CreateWebhook(w); err != nil {
  453. ctx.Handle(500, "CreateWebhook", err)
  454. return
  455. }
  456. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  457. ctx.Redirect(orCtx.Link + "/settings/hooks")
  458. }
  459. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  460. ctx.Data["Title"] = ctx.Tr("repo.settings")
  461. ctx.Data["PageIsSettingsHooks"] = true
  462. ctx.Data["PageIsSettingsHooksEdit"] = true
  463. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  464. if hookId == 0 {
  465. ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
  466. return
  467. }
  468. orCtx, err := getOrgRepoCtx(ctx)
  469. if err != nil {
  470. ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
  471. return
  472. }
  473. w, err := models.GetWebhookById(hookId)
  474. if err != nil {
  475. if err == models.ErrWebhookNotExist {
  476. ctx.Handle(404, "GetWebhookById", nil)
  477. } else {
  478. ctx.Handle(500, "GetWebhookById", err)
  479. }
  480. return
  481. }
  482. w.GetEvent()
  483. ctx.Data["Webhook"] = w
  484. if ctx.HasError() {
  485. ctx.HTML(200, orCtx.NewTemplate)
  486. return
  487. }
  488. meta, err := json.Marshal(&models.Slack{
  489. Channel: form.Channel,
  490. })
  491. if err != nil {
  492. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  493. return
  494. }
  495. w.Url = form.PayloadUrl
  496. w.Meta = string(meta)
  497. w.HookEvent = &models.HookEvent{
  498. PushOnly: form.PushOnly,
  499. }
  500. w.IsActive = form.Active
  501. if err := w.UpdateEvent(); err != nil {
  502. ctx.Handle(500, "UpdateEvent", err)
  503. return
  504. } else if err := models.UpdateWebhook(w); err != nil {
  505. ctx.Handle(500, "SlackHooksEditPost", err)
  506. return
  507. }
  508. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  509. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  510. }
  511. type OrgRepoCtx struct {
  512. OrgId int64
  513. RepoId int64
  514. Link string
  515. NewTemplate base.TplName
  516. }
  517. // determines whether this is a repo context or organization context
  518. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  519. if _, ok := ctx.Data["RepoLink"]; ok {
  520. return &OrgRepoCtx{
  521. OrgId: int64(0),
  522. RepoId: ctx.Repo.Repository.Id,
  523. Link: ctx.Repo.RepoLink,
  524. NewTemplate: HOOK_NEW,
  525. }, nil
  526. } else if _, ok := ctx.Data["OrgLink"]; ok {
  527. return &OrgRepoCtx{
  528. OrgId: ctx.Org.Organization.Id,
  529. RepoId: int64(0),
  530. Link: ctx.Org.OrgLink,
  531. NewTemplate: ORG_HOOK_NEW,
  532. }, nil
  533. } else {
  534. return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
  535. }
  536. }
  537. func GitHooks(ctx *middleware.Context) {
  538. ctx.Data["Title"] = ctx.Tr("repo.settings")
  539. ctx.Data["PageIsSettingsGitHooks"] = true
  540. hooks, err := ctx.Repo.GitRepo.Hooks()
  541. if err != nil {
  542. ctx.Handle(500, "Hooks", err)
  543. return
  544. }
  545. ctx.Data["Hooks"] = hooks
  546. ctx.HTML(200, GITHOOKS)
  547. }
  548. func GitHooksEdit(ctx *middleware.Context) {
  549. ctx.Data["Title"] = ctx.Tr("repo.settings")
  550. ctx.Data["PageIsSettingsGitHooks"] = true
  551. name := ctx.Params(":name")
  552. hook, err := ctx.Repo.GitRepo.GetHook(name)
  553. if err != nil {
  554. if err == git.ErrNotValidHook {
  555. ctx.Handle(404, "GetHook", err)
  556. } else {
  557. ctx.Handle(500, "GetHook", err)
  558. }
  559. return
  560. }
  561. ctx.Data["Hook"] = hook
  562. ctx.HTML(200, GITHOOK_EDIT)
  563. }
  564. func GitHooksEditPost(ctx *middleware.Context) {
  565. name := ctx.Params(":name")
  566. hook, err := ctx.Repo.GitRepo.GetHook(name)
  567. if err != nil {
  568. if err == git.ErrNotValidHook {
  569. ctx.Handle(404, "GetHook", err)
  570. } else {
  571. ctx.Handle(500, "GetHook", err)
  572. }
  573. return
  574. }
  575. hook.Content = ctx.Query("content")
  576. if err = hook.Update(); err != nil {
  577. ctx.Handle(500, "hook.Update", err)
  578. return
  579. }
  580. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  581. }