setting.go 18 KB

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