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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "time"
  10. "code.gitea.io/gitea/modules/json"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // RestoreParams structure holds a data for restore repository
  14. type RestoreParams struct {
  15. RepoDir string
  16. OwnerName string
  17. RepoName string
  18. Units []string
  19. Validation bool
  20. }
  21. // RestoreRepo calls the internal RestoreRepo function
  22. func RestoreRepo(ctx context.Context, repoDir, ownerName, repoName string, units []string, validation bool) (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. Validation: validation,
  33. })
  34. req.Body(jsonBytes)
  35. resp, err := req.Response()
  36. if err != nil {
  37. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v, could you confirm it's running?", err.Error())
  38. }
  39. defer resp.Body.Close()
  40. if resp.StatusCode != http.StatusOK {
  41. ret := struct {
  42. Err string `json:"err"`
  43. }{}
  44. body, err := io.ReadAll(resp.Body)
  45. if err != nil {
  46. return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error())
  47. }
  48. if err := json.Unmarshal(body, &ret); err != nil {
  49. return http.StatusInternalServerError, fmt.Sprintf("Response body Unmarshal error: %v", err.Error())
  50. }
  51. return http.StatusInternalServerError, ret.Err
  52. }
  53. return http.StatusOK, fmt.Sprintf("Restore repo %s/%s successfully", ownerName, repoName)
  54. }