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.

83 lines
2.1 KiB

  1. // Copyright 2018 The Gitea 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 private
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. macaron "gopkg.in/macaron.v1"
  9. )
  10. // GetRepository return the default branch of a repository
  11. func GetRepository(ctx *macaron.Context) {
  12. repoID := ctx.ParamsInt64(":rid")
  13. repository, err := models.GetRepositoryByID(repoID)
  14. repository.MustOwnerName()
  15. allowPulls := repository.AllowsPulls()
  16. // put it back to nil because json unmarshal can't unmarshal it
  17. repository.Units = nil
  18. if err != nil {
  19. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  20. "err": err.Error(),
  21. })
  22. return
  23. }
  24. if repository.IsFork {
  25. repository.GetBaseRepo()
  26. if err != nil {
  27. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  28. "err": err.Error(),
  29. })
  30. return
  31. }
  32. repository.BaseRepo.MustOwnerName()
  33. allowPulls = repository.BaseRepo.AllowsPulls()
  34. // put it back to nil because json unmarshal can't unmarshal it
  35. repository.BaseRepo.Units = nil
  36. }
  37. ctx.JSON(http.StatusOK, struct {
  38. Repository *models.Repository
  39. AllowPullRequest bool
  40. }{
  41. Repository: repository,
  42. AllowPullRequest: allowPulls,
  43. })
  44. }
  45. // GetActivePullRequest return an active pull request when it exists or an empty object
  46. func GetActivePullRequest(ctx *macaron.Context) {
  47. baseRepoID := ctx.QueryInt64("baseRepoID")
  48. headRepoID := ctx.QueryInt64("headRepoID")
  49. baseBranch := ctx.QueryTrim("baseBranch")
  50. if len(baseBranch) == 0 {
  51. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  52. "err": "QueryTrim failed",
  53. })
  54. return
  55. }
  56. headBranch := ctx.QueryTrim("headBranch")
  57. if len(headBranch) == 0 {
  58. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  59. "err": "QueryTrim failed",
  60. })
  61. return
  62. }
  63. pr, err := models.GetUnmergedPullRequest(headRepoID, baseRepoID, headBranch, baseBranch)
  64. if err != nil && !models.IsErrPullRequestNotExist(err) {
  65. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  66. "err": err.Error(),
  67. })
  68. return
  69. }
  70. ctx.JSON(http.StatusOK, pr)
  71. }