git_action_handler.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "github.com/go-chi/chi"
  9. "github.com/porter-dev/porter/internal/forms"
  10. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  11. )
  12. // HandleCreateGitAction creates a new Github action in a repository for a given
  13. // release
  14. func (app *App) HandleCreateGitAction(w http.ResponseWriter, r *http.Request) {
  15. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  16. if err != nil || projID == 0 {
  17. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  18. return
  19. }
  20. form := &forms.CreateGitAction{}
  21. // decode from JSON to form value
  22. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  23. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  24. return
  25. }
  26. // validate the form
  27. if err := app.validator.Struct(form); err != nil {
  28. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  29. return
  30. }
  31. // convert the form to a git action config
  32. gitAction, err := form.ToGitActionConfig()
  33. if err != nil {
  34. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  35. return
  36. }
  37. // read the git repo
  38. gr, err := app.Repo.GitRepo.ReadGitRepo(gitAction.GitRepoID)
  39. if err != nil {
  40. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  41. return
  42. }
  43. repoSplit := strings.Split(gitAction.GitRepo, "/")
  44. if len(repoSplit) != 2 {
  45. app.handleErrorFormDecoding(fmt.Errorf("invalid formatting of repo name"), ErrProjectDecode, w)
  46. return
  47. }
  48. // get webhook token from release
  49. // generate porter jwt token
  50. // create the commit in the git repo
  51. _ = &actions.GithubActions{
  52. GitIntegration: gr,
  53. GitRepoName: repoSplit[1],
  54. GitRepoOwner: repoSplit[0],
  55. Repo: *app.Repo,
  56. GithubConf: app.GithubConf,
  57. // WebhookToken string
  58. // PorterToken string
  59. // ProjectID uint
  60. // ReleaseName string
  61. // DockerFilePath string
  62. // ImageRepoURL string
  63. // defaultBranch string
  64. }
  65. // handle write to the database
  66. // hr, err = app.Repo.HelmRepo.CreateHelmRepo(hr)
  67. // if err != nil {
  68. // app.handleErrorDataWrite(err, w)
  69. // return
  70. // }
  71. // app.Logger.Info().Msgf("New helm repo created: %d", hr.ID)
  72. // w.WriteHeader(http.StatusCreated)
  73. // hrExt := hr.Externalize()
  74. // if err := json.NewEncoder(w).Encode(hrExt); err != nil {
  75. // app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  76. // return
  77. // }
  78. }