git_repo.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/server/api"
  8. )
  9. // ListGitRepoResponse is the list of Git repo integrations for a project
  10. type ListGitRepoResponse []uint
  11. // ListGitRepos returns a list of Git repos for a project
  12. func (c *Client) ListGitRepos(
  13. ctx context.Context,
  14. projectID uint,
  15. ) (ListGitRepoResponse, error) {
  16. req, err := http.NewRequest(
  17. "GET",
  18. fmt.Sprintf("%s/projects/%d/gitrepos", c.BaseURL, projectID),
  19. nil,
  20. )
  21. if err != nil {
  22. return nil, err
  23. }
  24. req = req.WithContext(ctx)
  25. bodyResp := &ListGitRepoResponse{}
  26. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  27. if httpErr != nil {
  28. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  29. }
  30. return nil, err
  31. }
  32. return *bodyResp, nil
  33. }
  34. type GetRepoTarballDownloadURLResp api.HandleGetRepoZIPDownloadURLResp
  35. // ListGitRepos returns a list of Git repos for a project
  36. func (c *Client) GetRepoZIPDownloadURL(
  37. ctx context.Context,
  38. projectID uint,
  39. gitActionConfig *models.GitActionConfigExternal,
  40. ) (*GetRepoTarballDownloadURLResp, error) {
  41. req, err := http.NewRequest(
  42. "GET",
  43. fmt.Sprintf(
  44. "%s/projects/%d/gitrepos/%d/repos/%s/%s/%s/tarball_url",
  45. c.BaseURL,
  46. projectID,
  47. gitActionConfig.GitRepoID,
  48. "github",
  49. gitActionConfig.GitRepo,
  50. gitActionConfig.GitBranch,
  51. ),
  52. nil,
  53. )
  54. if err != nil {
  55. return nil, err
  56. }
  57. req = req.WithContext(ctx)
  58. bodyResp := &GetRepoTarballDownloadURLResp{}
  59. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  60. if httpErr != nil {
  61. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  62. }
  63. return nil, err
  64. }
  65. return bodyResp, nil
  66. }
  67. // ListGitRepoResponse is the list of Git repo integrations for a project
  68. type ListGithubReposResponse []*api.Repo
  69. // ListGitRepos returns a list of Git repos for a project
  70. func (c *Client) ListGithubRepos(
  71. ctx context.Context,
  72. projectID, gitRepoID uint,
  73. ) (ListGithubReposResponse, error) {
  74. req, err := http.NewRequest(
  75. "GET",
  76. fmt.Sprintf("%s/projects/%d/gitrepos/%d/repos", c.BaseURL, projectID, gitRepoID),
  77. nil,
  78. )
  79. if err != nil {
  80. return nil, err
  81. }
  82. req = req.WithContext(ctx)
  83. bodyResp := &ListGithubReposResponse{}
  84. if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
  85. if httpErr != nil {
  86. return nil, fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  87. }
  88. return nil, err
  89. }
  90. return *bodyResp, nil
  91. }