Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

internal.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package private
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "code.gitea.io/gitea/modules/httplib"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. func newRequest(url, method string) *httplib.Request {
  12. return httplib.NewRequest(url, method).Header("Authorization",
  13. fmt.Sprintf("Bearer %s", setting.InternalToken))
  14. }
  15. // Response internal request response
  16. type Response struct {
  17. Err string `json:"err"`
  18. }
  19. func decodeJSONError(resp *http.Response) *Response {
  20. var res Response
  21. err := json.NewDecoder(resp.Body).Decode(&res)
  22. if err != nil {
  23. res.Err = err.Error()
  24. }
  25. return &res
  26. }
  27. // UpdatePublicKeyUpdated update publick key updates
  28. func UpdatePublicKeyUpdated(keyID int64) error {
  29. // Ask for running deliver hook and test pull request tasks.
  30. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
  31. log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)
  32. resp, err := newRequest(reqURL, "POST").SetTLSClientConfig(&tls.Config{
  33. InsecureSkipVerify: true,
  34. }).Response()
  35. if err != nil {
  36. return err
  37. }
  38. defer resp.Body.Close()
  39. // All 2XX status codes are accepted and others will return an error
  40. if resp.StatusCode/100 != 2 {
  41. return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  42. }
  43. return nil
  44. }