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

branch.go 2.0 kB

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