setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. ct := models.JSON
  299. if form.ContentType == "2" {
  300. ct = models.FORM
  301. }
  302. w := &models.Webhook{
  303. RepoId: orCtx.RepoId,
  304. Url: form.PayloadUrl,
  305. ContentType: ct,
  306. Secret: form.Secret,
  307. HookEvent: &models.HookEvent{
  308. PushOnly: form.PushOnly,
  309. },
  310. IsActive: form.Active,
  311. HookTaskType: models.GOGS,
  312. Meta: "",
  313. OrgId: orCtx.OrgId,
  314. }
  315. if err := w.UpdateEvent(); err != nil {
  316. ctx.Handle(500, "UpdateEvent", err)
  317. return
  318. } else if err := models.CreateWebhook(w); err != nil {
  319. ctx.Handle(500, "CreateWebhook", err)
  320. return
  321. }
  322. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  323. ctx.Redirect(orCtx.Link + "/settings/hooks")
  324. }
  325. func WebHooksEdit(ctx *middleware.Context) {
  326. ctx.Data["Title"] = ctx.Tr("repo.settings")
  327. ctx.Data["PageIsSettingsHooks"] = true
  328. ctx.Data["PageIsSettingsHooksEdit"] = true
  329. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  330. if hookId == 0 {
  331. ctx.Handle(404, "setting.WebHooksEdit", nil)
  332. return
  333. }
  334. w, err := models.GetWebhookById(hookId)
  335. if err != nil {
  336. if err == models.ErrWebhookNotExist {
  337. ctx.Handle(404, "GetWebhookById", nil)
  338. } else {
  339. ctx.Handle(500, "GetWebhookById", err)
  340. }
  341. return
  342. }
  343. // set data per HookTaskType
  344. switch w.HookTaskType {
  345. case models.SLACK:
  346. {
  347. ctx.Data["SlackHook"] = w.GetSlackHook()
  348. ctx.Data["HookType"] = "Slack"
  349. }
  350. default:
  351. {
  352. ctx.Data["HookType"] = "Gogs"
  353. }
  354. }
  355. w.GetEvent()
  356. ctx.Data["Webhook"] = w
  357. orCtx, err := getOrgRepoCtx(ctx)
  358. if err != nil {
  359. ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
  360. return
  361. }
  362. ctx.HTML(200, orCtx.NewTemplate)
  363. }
  364. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  365. ctx.Data["Title"] = ctx.Tr("repo.settings")
  366. ctx.Data["PageIsSettingsHooks"] = true
  367. ctx.Data["PageIsSettingsHooksEdit"] = true
  368. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  369. if hookId == 0 {
  370. ctx.Handle(404, "setting.WebHooksEditPost", nil)
  371. return
  372. }
  373. w, err := models.GetWebhookById(hookId)
  374. if err != nil {
  375. if err == models.ErrWebhookNotExist {
  376. ctx.Handle(404, "GetWebhookById", nil)
  377. } else {
  378. ctx.Handle(500, "GetWebhookById", err)
  379. }
  380. return
  381. }
  382. w.GetEvent()
  383. ctx.Data["Webhook"] = w
  384. orCtx, err := getOrgRepoCtx(ctx)
  385. if err != nil {
  386. ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
  387. return
  388. }
  389. if ctx.HasError() {
  390. ctx.HTML(200, orCtx.NewTemplate)
  391. return
  392. }
  393. ct := models.JSON
  394. if form.ContentType == "2" {
  395. ct = models.FORM
  396. }
  397. w.Url = form.PayloadUrl
  398. w.ContentType = ct
  399. w.Secret = form.Secret
  400. w.HookEvent = &models.HookEvent{
  401. PushOnly: form.PushOnly,
  402. }
  403. w.IsActive = form.Active
  404. if err := w.UpdateEvent(); err != nil {
  405. ctx.Handle(500, "UpdateEvent", err)
  406. return
  407. } else if err := models.UpdateWebhook(w); err != nil {
  408. ctx.Handle(500, "WebHooksEditPost", err)
  409. return
  410. }
  411. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  412. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  413. }
  414. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  415. ctx.Data["Title"] = ctx.Tr("repo.settings")
  416. ctx.Data["PageIsSettingsHooks"] = true
  417. ctx.Data["PageIsSettingsHooksNew"] = true
  418. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  419. orCtx, err := getOrgRepoCtx(ctx)
  420. if err != nil {
  421. ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
  422. return
  423. }
  424. if ctx.HasError() {
  425. ctx.HTML(200, orCtx.NewTemplate)
  426. return
  427. }
  428. meta, err := json.Marshal(&models.Slack{
  429. Channel: form.Channel,
  430. })
  431. if err != nil {
  432. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  433. return
  434. }
  435. w := &models.Webhook{
  436. RepoId: orCtx.RepoId,
  437. Url: form.PayloadUrl,
  438. ContentType: models.JSON,
  439. Secret: "",
  440. HookEvent: &models.HookEvent{
  441. PushOnly: form.PushOnly,
  442. },
  443. IsActive: form.Active,
  444. HookTaskType: models.SLACK,
  445. Meta: string(meta),
  446. OrgId: orCtx.OrgId,
  447. }
  448. if err := w.UpdateEvent(); err != nil {
  449. ctx.Handle(500, "UpdateEvent", err)
  450. return
  451. } else if err := models.CreateWebhook(w); err != nil {
  452. ctx.Handle(500, "CreateWebhook", err)
  453. return
  454. }
  455. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  456. ctx.Redirect(orCtx.Link + "/settings/hooks")
  457. }
  458. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  459. ctx.Data["Title"] = ctx.Tr("repo.settings")
  460. ctx.Data["PageIsSettingsHooks"] = true
  461. ctx.Data["PageIsSettingsHooksEdit"] = true
  462. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  463. if hookId == 0 {
  464. ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
  465. return
  466. }
  467. orCtx, err := getOrgRepoCtx(ctx)
  468. if err != nil {
  469. ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
  470. return
  471. }
  472. w, err := models.GetWebhookById(hookId)
  473. if err != nil {
  474. if err == models.ErrWebhookNotExist {
  475. ctx.Handle(404, "GetWebhookById", nil)
  476. } else {
  477. ctx.Handle(500, "GetWebhookById", err)
  478. }
  479. return
  480. }
  481. w.GetEvent()
  482. ctx.Data["Webhook"] = w
  483. if ctx.HasError() {
  484. ctx.HTML(200, orCtx.NewTemplate)
  485. return
  486. }
  487. meta, err := json.Marshal(&models.Slack{
  488. Channel: form.Channel,
  489. })
  490. if err != nil {
  491. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  492. return
  493. }
  494. w.Url = form.PayloadUrl
  495. w.Meta = string(meta)
  496. w.HookEvent = &models.HookEvent{
  497. PushOnly: form.PushOnly,
  498. }
  499. w.IsActive = form.Active
  500. if err := w.UpdateEvent(); err != nil {
  501. ctx.Handle(500, "UpdateEvent", err)
  502. return
  503. } else if err := models.UpdateWebhook(w); err != nil {
  504. ctx.Handle(500, "SlackHooksEditPost", err)
  505. return
  506. }
  507. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  508. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  509. }
  510. type OrgRepoCtx struct {
  511. OrgId int64
  512. RepoId int64
  513. Link string
  514. NewTemplate base.TplName
  515. }
  516. // determines whether this is a repo context or organization context
  517. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  518. if _, ok := ctx.Data["RepoLink"]; ok {
  519. return &OrgRepoCtx{
  520. OrgId: int64(0),
  521. RepoId: ctx.Repo.Repository.Id,
  522. Link: ctx.Repo.RepoLink,
  523. NewTemplate: HOOK_NEW,
  524. }, nil
  525. } else if _, ok := ctx.Data["OrgLink"]; ok {
  526. return &OrgRepoCtx{
  527. OrgId: ctx.Org.Organization.Id,
  528. RepoId: int64(0),
  529. Link: ctx.Org.OrgLink,
  530. NewTemplate: ORG_HOOK_NEW,
  531. }, nil
  532. } else {
  533. return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
  534. }
  535. }
  536. func GitHooks(ctx *middleware.Context) {
  537. ctx.Data["Title"] = ctx.Tr("repo.settings")
  538. ctx.Data["PageIsSettingsGitHooks"] = true
  539. hooks, err := ctx.Repo.GitRepo.Hooks()
  540. if err != nil {
  541. ctx.Handle(500, "Hooks", err)
  542. return
  543. }
  544. ctx.Data["Hooks"] = hooks
  545. ctx.HTML(200, GITHOOKS)
  546. }
  547. func GitHooksEdit(ctx *middleware.Context) {
  548. ctx.Data["Title"] = ctx.Tr("repo.settings")
  549. ctx.Data["PageIsSettingsGitHooks"] = true
  550. name := ctx.Params(":name")
  551. hook, err := ctx.Repo.GitRepo.GetHook(name)
  552. if err != nil {
  553. if err == git.ErrNotValidHook {
  554. ctx.Handle(404, "GetHook", err)
  555. } else {
  556. ctx.Handle(500, "GetHook", err)
  557. }
  558. return
  559. }
  560. ctx.Data["Hook"] = hook
  561. ctx.HTML(200, GITHOOK_EDIT)
  562. }
  563. func GitHooksEditPost(ctx *middleware.Context) {
  564. name := ctx.Params(":name")
  565. hook, err := ctx.Repo.GitRepo.GetHook(name)
  566. if err != nil {
  567. if err == git.ErrNotValidHook {
  568. ctx.Handle(404, "GetHook", err)
  569. } else {
  570. ctx.Handle(500, "GetHook", err)
  571. }
  572. return
  573. }
  574. hook.Content = ctx.Query("content")
  575. if err = hook.Update(); err != nil {
  576. ctx.Handle(500, "hook.Update", err)
  577. return
  578. }
  579. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  580. }