2
0

github_action.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. )
  9. // CreateGithubActionRequest represents the accepted fields for creating
  10. // a Github action
  11. type CreateGithubActionRequest struct {
  12. ReleaseID uint `json:"release_id" form:"required"`
  13. GitRepo string `json:"git_repo" form:"required"`
  14. GitBranch string `json:"git_branch"`
  15. ImageRepoURI string `json:"image_repo_uri" form:"required"`
  16. DockerfilePath string `json:"dockerfile_path"`
  17. FolderPath string `json:"folder_path"`
  18. GitRepoID uint `json:"git_repo_id" form:"required"`
  19. RegistryID uint `json:"registry_id"`
  20. }
  21. // CreateGithubAction creates a Github action with basic authentication
  22. func (c *Client) CreateGithubAction(
  23. ctx context.Context,
  24. projectID, clusterID uint,
  25. releaseName, releaseNamespace string,
  26. createGH *CreateGithubActionRequest,
  27. ) error {
  28. data, err := json.Marshal(createGH)
  29. if err != nil {
  30. return err
  31. }
  32. req, err := http.NewRequest(
  33. "POST",
  34. fmt.Sprintf(
  35. "%s/projects/%d/ci/actions?cluster_id=%d&name=%s&namespace=%s",
  36. c.BaseURL,
  37. projectID,
  38. clusterID,
  39. releaseName,
  40. releaseNamespace,
  41. ),
  42. strings.NewReader(string(data)),
  43. )
  44. if err != nil {
  45. return err
  46. }
  47. req = req.WithContext(ctx)
  48. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  49. if httpErr != nil {
  50. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  51. }
  52. return err
  53. }
  54. return nil
  55. }