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.

43 lines
1.2 KiB

  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. "crypto/tls"
  7. "encoding/json"
  8. "fmt"
  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, branchName)
  17. log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL)
  18. resp, err := newRequest(reqURL, "GET").SetTLSClientConfig(&tls.Config{
  19. InsecureSkipVerify: true,
  20. }).Response()
  21. if err != nil {
  22. return nil, err
  23. }
  24. var branch models.ProtectedBranch
  25. if err := json.NewDecoder(resp.Body).Decode(&branch); err != nil {
  26. return nil, 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, fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  32. }
  33. return &branch, nil
  34. }