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.

restore_repo.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2020 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. "context"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "time"
  11. "code.gitea.io/gitea/modules/json"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // RestoreParams structure holds a data for restore repository
  15. type RestoreParams struct {
  16. RepoDir string
  17. OwnerName string
  18. RepoName string
  19. Units []string
  20. }
  21. // RestoreRepo calls the internal RestoreRepo function
  22. func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units []string) (int, string) {
  23. reqURL := setting.LocalURL + "api/internal/restore_repo"
  24. req := newInternalRequest(ctx, reqURL, "POST")
  25. req.SetTimeout(3*time.Second, 0) // since the request will spend much time, don't timeout
  26. req = req.Header("Content-Type", "application/json")
  27. jsonBytes, _ := json.Marshal(RestoreParams{
  28. RepoDir: repoDir,
  29. OwnerName: ownerName,
  30. RepoName: repoName,
  31. Units: units,
  32. })
  33. req.Body(jsonBytes)
  34. resp, err := req.Response()
  35. if err != nil {
  36. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v, could you confirm it's running?", err.Error())
  37. }
  38. defer resp.Body.Close()
  39. if resp.StatusCode != 200 {
  40. var ret = struct {
  41. Err string `json:"err"`
  42. }{}
  43. body, err := io.ReadAll(resp.Body)
  44. if err != nil {
  45. return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error())
  46. }
  47. if err := json.Unmarshal(body, &ret); err != nil {
  48. return http.StatusInternalServerError, fmt.Sprintf("Response body Unmarshal error: %v", err.Error())
  49. }
  50. return http.StatusInternalServerError, ret.Err
  51. }
  52. return http.StatusOK, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName)
  53. }