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.

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