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.

branch.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 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. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // GetProtectedBranchBy get protected branch information
  13. func GetProtectedBranchBy(repoID int64, branchName string) (*models.ProtectedBranch, error) {
  14. // Ask for running deliver hook and test pull request tasks.
  15. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/branch/%d/%s", repoID, branchName)
  16. log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL)
  17. resp, err := newInternalRequest(reqURL, "GET").Response()
  18. if err != nil {
  19. return nil, err
  20. }
  21. var branch models.ProtectedBranch
  22. if err := json.NewDecoder(resp.Body).Decode(&branch); err != nil {
  23. return nil, err
  24. }
  25. defer resp.Body.Close()
  26. // All 2XX status codes are accepted and others will return an error
  27. if resp.StatusCode/100 != 2 {
  28. return nil, fmt.Errorf("Failed to get protected branch: %s", decodeJSONError(resp).Err)
  29. }
  30. return &branch, nil
  31. }
  32. // CanUserPush returns if user can push
  33. func CanUserPush(protectedBranchID, userID int64) (bool, error) {
  34. // Ask for running deliver hook and test pull request tasks.
  35. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/protectedbranch/%d/%d", protectedBranchID, userID)
  36. log.GitLogger.Trace("CanUserPush: %s", reqURL)
  37. resp, err := newInternalRequest(reqURL, "GET").Response()
  38. if err != nil {
  39. return false, err
  40. }
  41. var canPush = make(map[string]interface{})
  42. if err := json.NewDecoder(resp.Body).Decode(&canPush); err != nil {
  43. return false, err
  44. }
  45. defer resp.Body.Close()
  46. // All 2XX status codes are accepted and others will return an error
  47. if resp.StatusCode/100 != 2 {
  48. return false, fmt.Errorf("Failed to retrieve push user: %s", decodeJSONError(resp).Err)
  49. }
  50. return canPush["can_push"].(bool), nil
  51. }