github_action.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. GitRepo string `json:"git_repo"`
  13. ImageRepoURI string `json:"image_repo_uri"`
  14. DockerfilePath string `json:"dockerfile_path"`
  15. GitRepoID uint `json:"git_repo_id"`
  16. }
  17. // CreateGithubAction creates a Github action with basic authentication
  18. func (c *Client) CreateGithubAction(
  19. ctx context.Context,
  20. projectID, clusterID uint,
  21. releaseName, releaseNamespace string,
  22. createGH *CreateGithubActionRequest,
  23. ) error {
  24. data, err := json.Marshal(createGH)
  25. if err != nil {
  26. return err
  27. }
  28. req, err := http.NewRequest(
  29. "POST",
  30. fmt.Sprintf(
  31. "%s/projects/%d/ci/actions?cluster_id=%d&name=%s&namespace=%s",
  32. c.BaseURL,
  33. projectID,
  34. clusterID,
  35. releaseName,
  36. releaseNamespace,
  37. ),
  38. strings.NewReader(string(data)),
  39. )
  40. if err != nil {
  41. return err
  42. }
  43. req = req.WithContext(ctx)
  44. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  45. if httpErr != nil {
  46. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  47. }
  48. return err
  49. }
  50. return nil
  51. }