github_action.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. BuildEnv map[string]string `json:"env"`
  20. RegistryID uint `json:"registry_id"`
  21. }
  22. // CreateGithubAction creates a Github action with basic authentication
  23. func (c *Client) CreateGithubAction(
  24. ctx context.Context,
  25. projectID, clusterID uint,
  26. releaseName, releaseNamespace string,
  27. createGH *CreateGithubActionRequest,
  28. ) error {
  29. data, err := json.Marshal(createGH)
  30. if err != nil {
  31. return err
  32. }
  33. req, err := http.NewRequest(
  34. "POST",
  35. fmt.Sprintf(
  36. "%s/projects/%d/ci/actions?cluster_id=%d&name=%s&namespace=%s",
  37. c.BaseURL,
  38. projectID,
  39. clusterID,
  40. releaseName,
  41. releaseNamespace,
  42. ),
  43. strings.NewReader(string(data)),
  44. )
  45. if err != nil {
  46. return err
  47. }
  48. req = req.WithContext(ctx)
  49. if httpErr, err := c.sendRequest(req, nil, true); httpErr != nil || err != nil {
  50. if httpErr != nil {
  51. return fmt.Errorf("code %d, errors %v", httpErr.Code, httpErr.Errors)
  52. }
  53. return err
  54. }
  55. return nil
  56. }