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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // GetRepository return the repository by its ID and a bool about if it's allowed to have PR
  14. func GetRepository(repoID int64) (*models.Repository, bool, error) {
  15. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repository/%d", repoID)
  16. log.GitLogger.Trace("GetRepository: %s", reqURL)
  17. resp, err := newInternalRequest(reqURL, "GET").Response()
  18. if err != nil {
  19. return nil, false, err
  20. }
  21. var repoInfo struct {
  22. Repository *models.Repository
  23. AllowPullRequest bool
  24. }
  25. if err := json.NewDecoder(resp.Body).Decode(&repoInfo); err != nil {
  26. return nil, false, err
  27. }
  28. defer resp.Body.Close()
  29. // All 2XX status codes are accepted and others will return an error
  30. if resp.StatusCode/100 != 2 {
  31. return nil, false, fmt.Errorf("failed to retrieve repository: %s", decodeJSONError(resp).Err)
  32. }
  33. return repoInfo.Repository, repoInfo.AllowPullRequest, nil
  34. }
  35. // ActivePullRequest returns an active pull request if it exists
  36. func ActivePullRequest(baseRepoID int64, headRepoID int64, baseBranch, headBranch string) (*models.PullRequest, error) {
  37. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/active-pull-request?baseRepoID=%d&headRepoID=%d&baseBranch=%s&headBranch=%s", baseRepoID, headRepoID, url.QueryEscape(baseBranch), url.QueryEscape(headBranch))
  38. log.GitLogger.Trace("ActivePullRequest: %s", reqURL)
  39. resp, err := newInternalRequest(reqURL, "GET").Response()
  40. if err != nil {
  41. return nil, err
  42. }
  43. var pr *models.PullRequest
  44. if err := json.NewDecoder(resp.Body).Decode(&pr); err != nil {
  45. return nil, err
  46. }
  47. defer resp.Body.Close()
  48. // All 2XX status codes are accepted and others will return an error
  49. if resp.StatusCode/100 != 2 {
  50. return nil, fmt.Errorf("failed to retrieve pull request: %s", decodeJSONError(resp).Err)
  51. }
  52. return pr, nil
  53. }