setting.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 SettingsCollaboration(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")
  221. ctx.Data["PageIsSettingsHooks"] = true
  222. // Delete web hook.
  223. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  224. if remove > 0 {
  225. if err := models.DeleteWebhook(remove); err != nil {
  226. ctx.Handle(500, "DeleteWebhook", err)
  227. return
  228. }
  229. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  230. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
  231. return
  232. }
  233. ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID)
  234. if err != nil {
  235. ctx.Handle(500, "GetWebhooksByRepoId", err)
  236. return
  237. }
  238. ctx.Data["Webhooks"] = ws
  239. ctx.HTML(200, HOOKS)
  240. }
  241. func renderHookTypes(ctx *middleware.Context) {
  242. ctx.Data["HookTypes"] = []string{"Gogs", "Slack"}
  243. ctx.Data["HookType"] = "Gogs"
  244. }
  245. func WebHooksNew(ctx *middleware.Context) {
  246. ctx.Data["Title"] = ctx.Tr("repo.settings")
  247. ctx.Data["PageIsSettingsHooks"] = true
  248. ctx.Data["PageIsSettingsHooksNew"] = true
  249. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  250. renderHookTypes(ctx)
  251. orCtx, err := getOrgRepoCtx(ctx)
  252. if err != nil {
  253. ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err)
  254. return
  255. }
  256. ctx.HTML(200, orCtx.NewTemplate)
  257. }
  258. func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  259. ctx.Data["Title"] = ctx.Tr("repo.settings")
  260. ctx.Data["PageIsSettingsHooks"] = true
  261. ctx.Data["PageIsSettingsHooksNew"] = true
  262. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  263. renderHookTypes(ctx)
  264. orCtx, err := getOrgRepoCtx(ctx)
  265. if err != nil {
  266. ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err)
  267. return
  268. }
  269. if ctx.HasError() {
  270. ctx.HTML(200, orCtx.NewTemplate)
  271. return
  272. }
  273. // FIXME: code too old here, sync with APIs
  274. ct := models.JSON
  275. if form.ContentType == "2" {
  276. ct = models.FORM
  277. }
  278. w := &models.Webhook{
  279. RepoId: orCtx.RepoId,
  280. Url: form.PayloadUrl,
  281. ContentType: ct,
  282. Secret: form.Secret,
  283. HookEvent: &models.HookEvent{
  284. PushOnly: form.PushOnly,
  285. },
  286. IsActive: form.Active,
  287. HookTaskType: models.GOGS,
  288. Meta: "",
  289. OrgId: orCtx.OrgId,
  290. }
  291. if err := w.UpdateEvent(); err != nil {
  292. ctx.Handle(500, "UpdateEvent", err)
  293. return
  294. } else if err := models.CreateWebhook(w); err != nil {
  295. ctx.Handle(500, "CreateWebhook", err)
  296. return
  297. }
  298. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  299. ctx.Redirect(orCtx.Link + "/settings/hooks")
  300. }
  301. func WebHooksEdit(ctx *middleware.Context) {
  302. ctx.Data["Title"] = ctx.Tr("repo.settings")
  303. ctx.Data["PageIsSettingsHooks"] = true
  304. ctx.Data["PageIsSettingsHooksEdit"] = true
  305. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  306. if hookId == 0 {
  307. ctx.Handle(404, "setting.WebHooksEdit", nil)
  308. return
  309. }
  310. w, err := models.GetWebhookById(hookId)
  311. if err != nil {
  312. if err == models.ErrWebhookNotExist {
  313. ctx.Handle(404, "GetWebhookById", nil)
  314. } else {
  315. ctx.Handle(500, "GetWebhookById", err)
  316. }
  317. return
  318. }
  319. // set data per HookTaskType
  320. switch w.HookTaskType {
  321. case models.SLACK:
  322. ctx.Data["SlackHook"] = w.GetSlackHook()
  323. ctx.Data["HookType"] = "Slack"
  324. default:
  325. ctx.Data["HookType"] = "Gogs"
  326. }
  327. w.GetEvent()
  328. ctx.Data["Webhook"] = w
  329. orCtx, err := getOrgRepoCtx(ctx)
  330. if err != nil {
  331. ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
  332. return
  333. }
  334. ctx.HTML(200, orCtx.NewTemplate)
  335. }
  336. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  337. ctx.Data["Title"] = ctx.Tr("repo.settings")
  338. ctx.Data["PageIsSettingsHooks"] = true
  339. ctx.Data["PageIsSettingsHooksEdit"] = true
  340. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  341. if hookId == 0 {
  342. ctx.Handle(404, "setting.WebHooksEditPost", nil)
  343. return
  344. }
  345. w, err := models.GetWebhookById(hookId)
  346. if err != nil {
  347. if err == models.ErrWebhookNotExist {
  348. ctx.Handle(404, "GetWebhookById", nil)
  349. } else {
  350. ctx.Handle(500, "GetWebhookById", err)
  351. }
  352. return
  353. }
  354. // set data per HookTaskType
  355. switch w.HookTaskType {
  356. case models.SLACK:
  357. ctx.Data["SlackHook"] = w.GetSlackHook()
  358. ctx.Data["HookType"] = "Slack"
  359. default:
  360. ctx.Data["HookType"] = "Gogs"
  361. }
  362. w.GetEvent()
  363. ctx.Data["Webhook"] = w
  364. orCtx, err := getOrgRepoCtx(ctx)
  365. if err != nil {
  366. ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
  367. return
  368. }
  369. if ctx.HasError() {
  370. ctx.HTML(200, orCtx.NewTemplate)
  371. return
  372. }
  373. ct := models.JSON
  374. if form.ContentType == "2" {
  375. ct = models.FORM
  376. }
  377. w.Url = form.PayloadUrl
  378. w.ContentType = ct
  379. w.Secret = form.Secret
  380. w.HookEvent = &models.HookEvent{
  381. PushOnly: form.PushOnly,
  382. }
  383. w.IsActive = form.Active
  384. if err := w.UpdateEvent(); err != nil {
  385. ctx.Handle(500, "UpdateEvent", err)
  386. return
  387. } else if err := models.UpdateWebhook(w); err != nil {
  388. ctx.Handle(500, "WebHooksEditPost", err)
  389. return
  390. }
  391. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  392. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  393. }
  394. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  395. ctx.Data["Title"] = ctx.Tr("repo.settings")
  396. ctx.Data["PageIsSettingsHooks"] = true
  397. ctx.Data["PageIsSettingsHooksNew"] = true
  398. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  399. orCtx, err := getOrgRepoCtx(ctx)
  400. if err != nil {
  401. ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
  402. return
  403. }
  404. if ctx.HasError() {
  405. ctx.HTML(200, orCtx.NewTemplate)
  406. return
  407. }
  408. meta, err := json.Marshal(&models.Slack{
  409. Channel: form.Channel,
  410. })
  411. if err != nil {
  412. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  413. return
  414. }
  415. w := &models.Webhook{
  416. RepoId: orCtx.RepoId,
  417. Url: form.PayloadUrl,
  418. ContentType: models.JSON,
  419. Secret: "",
  420. HookEvent: &models.HookEvent{
  421. PushOnly: form.PushOnly,
  422. },
  423. IsActive: form.Active,
  424. HookTaskType: models.SLACK,
  425. Meta: string(meta),
  426. OrgId: orCtx.OrgId,
  427. }
  428. if err := w.UpdateEvent(); err != nil {
  429. ctx.Handle(500, "UpdateEvent", err)
  430. return
  431. } else if err := models.CreateWebhook(w); err != nil {
  432. ctx.Handle(500, "CreateWebhook", err)
  433. return
  434. }
  435. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  436. ctx.Redirect(orCtx.Link + "/settings/hooks")
  437. }
  438. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  439. ctx.Data["Title"] = ctx.Tr("repo.settings")
  440. ctx.Data["PageIsSettingsHooks"] = true
  441. ctx.Data["PageIsSettingsHooksEdit"] = true
  442. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  443. if hookId == 0 {
  444. ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
  445. return
  446. }
  447. orCtx, err := getOrgRepoCtx(ctx)
  448. if err != nil {
  449. ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
  450. return
  451. }
  452. w, err := models.GetWebhookById(hookId)
  453. if err != nil {
  454. if err == models.ErrWebhookNotExist {
  455. ctx.Handle(404, "GetWebhookById", nil)
  456. } else {
  457. ctx.Handle(500, "GetWebhookById", err)
  458. }
  459. return
  460. }
  461. w.GetEvent()
  462. ctx.Data["Webhook"] = w
  463. if ctx.HasError() {
  464. ctx.HTML(200, orCtx.NewTemplate)
  465. return
  466. }
  467. meta, err := json.Marshal(&models.Slack{
  468. Channel: form.Channel,
  469. })
  470. if err != nil {
  471. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  472. return
  473. }
  474. w.Url = form.PayloadUrl
  475. w.Meta = string(meta)
  476. w.HookEvent = &models.HookEvent{
  477. PushOnly: form.PushOnly,
  478. }
  479. w.IsActive = form.Active
  480. if err := w.UpdateEvent(); err != nil {
  481. ctx.Handle(500, "UpdateEvent", err)
  482. return
  483. } else if err := models.UpdateWebhook(w); err != nil {
  484. ctx.Handle(500, "SlackHooksEditPost", err)
  485. return
  486. }
  487. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  488. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  489. }
  490. type OrgRepoCtx struct {
  491. OrgId int64
  492. RepoId int64
  493. Link string
  494. NewTemplate base.TplName
  495. }
  496. // determines whether this is a repo context or organization context
  497. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  498. if _, ok := ctx.Data["RepoLink"]; ok {
  499. return &OrgRepoCtx{
  500. OrgId: int64(0),
  501. RepoId: ctx.Repo.Repository.ID,
  502. Link: ctx.Repo.RepoLink,
  503. NewTemplate: HOOK_NEW,
  504. }, nil
  505. } else if _, ok := ctx.Data["OrgLink"]; ok {
  506. return &OrgRepoCtx{
  507. OrgId: ctx.Org.Organization.Id,
  508. RepoId: int64(0),
  509. Link: ctx.Org.OrgLink,
  510. NewTemplate: ORG_HOOK_NEW,
  511. }, nil
  512. } else {
  513. return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
  514. }
  515. }
  516. func TriggerHook(ctx *middleware.Context) {
  517. u, err := models.GetUserByName(ctx.Params(":username"))
  518. if err != nil {
  519. if models.IsErrUserNotExist(err) {
  520. ctx.Handle(404, "GetUserByName", err)
  521. } else {
  522. ctx.Handle(500, "GetUserByName", err)
  523. }
  524. return
  525. }
  526. repo, err := models.GetRepositoryByName(u.Id, ctx.Params(":reponame"))
  527. if err != nil {
  528. if models.IsErrRepoNotExist(err) {
  529. ctx.Handle(404, "GetRepositoryByName", err)
  530. } else {
  531. ctx.Handle(500, "GetRepositoryByName", err)
  532. }
  533. return
  534. }
  535. models.HookQueue.AddRepoID(repo.ID)
  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. }
  582. func SettingsDeployKeys(ctx *middleware.Context) {
  583. ctx.Data["Title"] = ctx.Tr("repo.settings")
  584. ctx.Data["PageIsSettingsKeys"] = true
  585. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  586. if err != nil {
  587. ctx.Handle(500, "ListDeployKeys", err)
  588. return
  589. }
  590. ctx.Data["Deploykeys"] = keys
  591. ctx.HTML(200, DEPLOY_KEYS)
  592. }
  593. func SettingsDeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  594. ctx.Data["Title"] = ctx.Tr("repo.settings")
  595. ctx.Data["PageIsSettingsKeys"] = true
  596. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  597. if err != nil {
  598. ctx.Handle(500, "ListDeployKeys", err)
  599. return
  600. }
  601. ctx.Data["Deploykeys"] = keys
  602. if ctx.HasError() {
  603. ctx.HTML(200, DEPLOY_KEYS)
  604. return
  605. }
  606. content, err := models.CheckPublicKeyString(form.Content)
  607. if err != nil {
  608. if err == models.ErrKeyUnableVerify {
  609. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  610. } else {
  611. ctx.Data["HasError"] = true
  612. ctx.Data["Err_Content"] = true
  613. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  614. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  615. return
  616. }
  617. }
  618. if err = models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content); err != nil {
  619. ctx.Data["HasError"] = true
  620. switch {
  621. case models.IsErrKeyAlreadyExist(err):
  622. ctx.Data["Err_Content"] = true
  623. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  624. case models.IsErrKeyNameAlreadyUsed(err):
  625. ctx.Data["Err_Title"] = true
  626. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  627. default:
  628. ctx.Handle(500, "AddDeployKey", err)
  629. }
  630. return
  631. }
  632. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  633. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", form.Title))
  634. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  635. }
  636. func DeleteDeployKey(ctx *middleware.Context) {
  637. if err := models.DeleteDeployKey(ctx.QueryInt64("id")); err != nil {
  638. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  639. } else {
  640. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  641. }
  642. ctx.JSON(200, map[string]interface{}{
  643. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  644. })
  645. }