actions.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package connect
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. ints "github.com/porter-dev/porter/internal/models/integrations"
  10. )
  11. // Actions creates a github actions integration
  12. func Actions(
  13. client *api.Client,
  14. projectID uint,
  15. ) error {
  16. // if project ID is 0, ask the user to set the project ID or create a project
  17. if projectID == 0 {
  18. return fmt.Errorf("no project set, please run porter project set [id]")
  19. }
  20. // list oauth integrations and make sure Github exists
  21. oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  22. if err != nil {
  23. return err
  24. }
  25. linkedGH := false
  26. // iterate through oauth integrations to find do
  27. for _, oauthInt := range oauthInts {
  28. if oauthInt.Client == ints.OAuthGithub {
  29. linkedGH = true
  30. break
  31. }
  32. }
  33. if !linkedGH {
  34. _, err = triggerGithubOAuth(client, projectID)
  35. if err != nil {
  36. return err
  37. }
  38. }
  39. gitRepos, err := client.ListGitRepos(context.TODO(), projectID)
  40. gitRepoID := gitRepos[0].ID
  41. // prompts (unfortunately a lot)
  42. clusterIDStr, _ := utils.PromptPlaintext(fmt.Sprintf(`Please provide the cluster id (can be found with "porter clusters list").
  43. Cluster ID: `))
  44. clusterID, err := strconv.ParseUint(clusterIDStr, 10, 64)
  45. if err != nil {
  46. return err
  47. }
  48. releaseName, _ := utils.PromptPlaintext(fmt.Sprintf(`Release name:`))
  49. releaseNamespace, _ := utils.PromptPlaintext(fmt.Sprintf(`Release namespace:`))
  50. gitRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the Github repo, in the form ${owner}/${repo_name}. For example, porter-dev/porter.
  51. Github repo:`))
  52. imageRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the image repo url.
  53. Image repo:`))
  54. dockerfilePath, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the path in the repo to your dockerfile.
  55. Dockerfile path:`))
  56. err = client.CreateGithubAction(
  57. context.Background(),
  58. projectID,
  59. uint(clusterID),
  60. releaseName,
  61. releaseNamespace,
  62. &api.CreateGithubActionRequest{
  63. GitRepo: gitRepo,
  64. ImageRepoURI: imageRepo,
  65. DockerfilePath: dockerfilePath,
  66. GitRepoID: gitRepoID,
  67. },
  68. )
  69. return err
  70. }
  71. func triggerGithubOAuth(client *api.Client, projectID uint) (ints.OAuthIntegrationExternal, error) {
  72. var ghAuth ints.OAuthIntegrationExternal
  73. oauthURL := fmt.Sprintf("%s/oauth/projects/%d/github", client.BaseURL, projectID)
  74. fmt.Printf("Please visit %s in your browser to connect to Github (it should open automatically).", oauthURL)
  75. utils.OpenBrowser(oauthURL)
  76. for {
  77. oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
  78. if err != nil {
  79. return ghAuth, err
  80. }
  81. linkedGH := false
  82. // iterate through oauth integrations to find do
  83. for _, oauthInt := range oauthInts {
  84. if oauthInt.Client == ints.OAuthGithub {
  85. linkedGH = true
  86. ghAuth = oauthInt
  87. break
  88. }
  89. }
  90. if linkedGH {
  91. break
  92. }
  93. time.Sleep(2 * time.Second)
  94. }
  95. return ghAuth, nil
  96. }