setting.go 19 KB

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