You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
16 KiB

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