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.

repository.go 2.1KB

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