Przeglądaj źródła

fix create from github

Alexander Belanger 4 lat temu
rodzic
commit
014b845d6a
5 zmienionych plików z 153 dodań i 143 usunięć
  1. 8 0
      cli/cmd/api/api.go
  2. 1 1
      cli/cmd/api/git_repo.go
  3. 18 18
      cli/cmd/connect.go
  4. 122 122
      cli/cmd/connect/actions.go
  5. 4 2
      cli/cmd/deploy/create.go

+ 8 - 0
cli/cmd/api/api.go

@@ -1,6 +1,7 @@
 package api
 
 import (
+	"bytes"
 	"encoding/base64"
 	"encoding/json"
 	"fmt"
@@ -95,6 +96,13 @@ func (c *Client) sendRequest(req *http.Request, v interface{}, useCookie bool) (
 	}
 
 	if v != nil {
+		body, _ := ioutil.ReadAll(res.Body)
+
+		fmt.Println("BODY IS", string(body))
+
+		// need to create a new stream for the body
+		res.Body = ioutil.NopCloser(bytes.NewReader(body))
+
 		if err = json.NewDecoder(res.Body).Decode(v); err != nil {
 			return nil, err
 		}

+ 1 - 1
cli/cmd/api/git_repo.go

@@ -10,7 +10,7 @@ import (
 )
 
 // ListGitRepoResponse is the list of Git repo integrations for a project
-type ListGitRepoResponse []models.GitRepoExternal
+type ListGitRepoResponse []uint
 
 // ListGitRepos returns a list of Git repos for a project
 func (c *Client) ListGitRepos(

+ 18 - 18
cli/cmd/connect.go

@@ -67,17 +67,17 @@ var connectRegistryCmd = &cobra.Command{
 	},
 }
 
-var connectActionsCmd = &cobra.Command{
-	Use:   "actions",
-	Short: "Adds Github Actions to a project",
-	Run: func(cmd *cobra.Command, args []string) {
-		err := checkLoginAndRun(args, runConnectActions)
-
-		if err != nil {
-			os.Exit(1)
-		}
-	},
-}
+// var connectActionsCmd = &cobra.Command{
+// 	Use:   "actions",
+// 	Short: "Adds Github Actions to a project",
+// 	Run: func(cmd *cobra.Command, args []string) {
+// 		err := checkLoginAndRun(args, runConnectActions)
+
+// 		if err != nil {
+// 			os.Exit(1)
+// 		}
+// 	},
+// }
 
 var connectGCRCmd = &cobra.Command{
 	Use:   "gcr",
@@ -135,7 +135,7 @@ func init() {
 		"the context to connect (defaults to the current context)",
 	)
 
-	connectCmd.AddCommand(connectActionsCmd)
+	// connectCmd.AddCommand(connectActionsCmd)
 	connectCmd.AddCommand(connectECRCmd)
 	connectCmd.AddCommand(connectRegistryCmd)
 	connectCmd.AddCommand(connectDockerhubCmd)
@@ -244,9 +244,9 @@ func runConnectHelmRepoBasic(_ *api.AuthCheckResponse, client *api.Client, _ []s
 	return config.SetHelmRepo(hrID)
 }
 
-func runConnectActions(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
-	return connect.Actions(
-		client,
-		config.Project,
-	)
-}
+// func runConnectActions(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
+// 	return connect.Actions(
+// 		client,
+// 		config.Project,
+// 	)
+// }

+ 122 - 122
cli/cmd/connect/actions.go

@@ -1,125 +1,125 @@
 package connect
 
-import (
-	"context"
-	"fmt"
-	"strconv"
-	"time"
-
-	"github.com/porter-dev/porter/cli/cmd/api"
-	"github.com/porter-dev/porter/cli/cmd/utils"
-
-	ints "github.com/porter-dev/porter/internal/models/integrations"
-)
-
-// Actions creates a github actions integration
-func Actions(
-	client *api.Client,
-	projectID uint,
-) error {
-	// if project ID is 0, ask the user to set the project ID or create a project
-	if projectID == 0 {
-		return fmt.Errorf("no project set, please run porter project set [id]")
-	}
-
-	// list oauth integrations and make sure Github exists
-	oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
-
-	if err != nil {
-		return err
-	}
-
-	linkedGH := false
-
-	// iterate through oauth integrations to find do
-	for _, oauthInt := range oauthInts {
-		if oauthInt.Client == ints.OAuthGithub {
-			linkedGH = true
-			break
-		}
-	}
-
-	if !linkedGH {
-		_, err = triggerGithubOAuth(client, projectID)
-
-		if err != nil {
-			return err
-		}
-	}
-
-	gitRepos, err := client.ListGitRepos(context.TODO(), projectID)
-
-	gitRepoID := gitRepos[0].ID
-
-	// prompts (unfortunately a lot)
-	clusterIDStr, _ := utils.PromptPlaintext(fmt.Sprintf(`Please provide the cluster id (can be found with "porter clusters list").
-Cluster ID: `))
-	clusterID, err := strconv.ParseUint(clusterIDStr, 10, 64)
-
-	if err != nil {
-		return err
-	}
-
-	releaseName, _ := utils.PromptPlaintext(fmt.Sprintf(`Release name:`))
-	releaseNamespace, _ := utils.PromptPlaintext(fmt.Sprintf(`Release namespace:`))
-	gitRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the Github repo, in the form ${owner}/${repo_name}. For example, porter-dev/porter.
-Github repo:`))
-
-	imageRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the image repo url.
-Image repo:`))
-
-	dockerfilePath, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the path in the repo to your dockerfile.
-Dockerfile path:`))
-
-	err = client.CreateGithubAction(
-		context.Background(),
-		projectID,
-		uint(clusterID),
-		releaseName,
-		releaseNamespace,
-		&api.CreateGithubActionRequest{
-			GitRepo:        gitRepo,
-			ImageRepoURI:   imageRepo,
-			DockerfilePath: dockerfilePath,
-			GitRepoID:      gitRepoID,
-		},
-	)
-
-	return err
-}
-
-func triggerGithubOAuth(client *api.Client, projectID uint) (ints.OAuthIntegrationExternal, error) {
-	var ghAuth ints.OAuthIntegrationExternal
-
-	oauthURL := fmt.Sprintf("%s/oauth/projects/%d/github", client.BaseURL, projectID)
-
-	fmt.Printf("Please visit %s in your browser to connect to Github (it should open automatically).", oauthURL)
-	utils.OpenBrowser(oauthURL)
-
-	for {
-		oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
-
-		if err != nil {
-			return ghAuth, err
-		}
-
-		linkedGH := false
-
-		// iterate through oauth integrations to find do
-		for _, oauthInt := range oauthInts {
-			if oauthInt.Client == ints.OAuthGithub {
-				linkedGH = true
-				ghAuth = oauthInt
-				break
-			}
-		}
-
-		if linkedGH {
-			break
-		}
-
-		time.Sleep(2 * time.Second)
-	}
+// import (
+// 	"context"
+// 	"fmt"
+// 	"strconv"
+// 	"time"
+
+// 	"github.com/porter-dev/porter/cli/cmd/api"
+// 	"github.com/porter-dev/porter/cli/cmd/utils"
+
+// 	ints "github.com/porter-dev/porter/internal/models/integrations"
+// )
+
+// // Actions creates a github actions integration
+// func Actions(
+// 	client *api.Client,
+// 	projectID uint,
+// ) error {
+// 	// if project ID is 0, ask the user to set the project ID or create a project
+// 	if projectID == 0 {
+// 		return fmt.Errorf("no project set, please run porter project set [id]")
+// 	}
+
+// 	// list oauth integrations and make sure Github exists
+// 	oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
+
+// 	if err != nil {
+// 		return err
+// 	}
+
+// 	linkedGH := false
+
+// 	// iterate through oauth integrations to find do
+// 	for _, oauthInt := range oauthInts {
+// 		if oauthInt.Client == ints.OAuthGithub {
+// 			linkedGH = true
+// 			break
+// 		}
+// 	}
+
+// 	if !linkedGH {
+// 		_, err = triggerGithubOAuth(client, projectID)
+
+// 		if err != nil {
+// 			return err
+// 		}
+// 	}
+
+// 	gitRepos, err := client.ListGitRepos(context.TODO(), projectID)
+
+// 	gitRepoID := gitRepos[0].ID
+
+// 	// prompts (unfortunately a lot)
+// 	clusterIDStr, _ := utils.PromptPlaintext(fmt.Sprintf(`Please provide the cluster id (can be found with "porter clusters list").
+// Cluster ID: `))
+// 	clusterID, err := strconv.ParseUint(clusterIDStr, 10, 64)
+
+// 	if err != nil {
+// 		return err
+// 	}
+
+// 	releaseName, _ := utils.PromptPlaintext(fmt.Sprintf(`Release name:`))
+// 	releaseNamespace, _ := utils.PromptPlaintext(fmt.Sprintf(`Release namespace:`))
+// 	gitRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the Github repo, in the form ${owner}/${repo_name}. For example, porter-dev/porter.
+// Github repo:`))
+
+// 	imageRepo, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the image repo url.
+// Image repo:`))
+
+// 	dockerfilePath, _ := utils.PromptPlaintext(fmt.Sprintf(`Please enter the path in the repo to your dockerfile.
+// Dockerfile path:`))
+
+// 	err = client.CreateGithubAction(
+// 		context.Background(),
+// 		projectID,
+// 		uint(clusterID),
+// 		releaseName,
+// 		releaseNamespace,
+// 		&api.CreateGithubActionRequest{
+// 			GitRepo:        gitRepo,
+// 			ImageRepoURI:   imageRepo,
+// 			DockerfilePath: dockerfilePath,
+// 			GitRepoID:      gitRepoID,
+// 		},
+// 	)
+
+// 	return err
+// }
+
+// func triggerGithubOAuth(client *api.Client, projectID uint) (ints.OAuthIntegrationExternal, error) {
+// 	var ghAuth ints.OAuthIntegrationExternal
+
+// 	oauthURL := fmt.Sprintf("%s/oauth/projects/%d/github", client.BaseURL, projectID)
+
+// 	fmt.Printf("Please visit %s in your browser to connect to Github (it should open automatically).", oauthURL)
+// 	utils.OpenBrowser(oauthURL)
+
+// 	for {
+// 		oauthInts, err := client.ListOAuthIntegrations(context.TODO(), projectID)
+
+// 		if err != nil {
+// 			return ghAuth, err
+// 		}
+
+// 		linkedGH := false
+
+// 		// iterate through oauth integrations to find do
+// 		for _, oauthInt := range oauthInts {
+// 			if oauthInt.Client == ints.OAuthGithub {
+// 				linkedGH = true
+// 				ghAuth = oauthInt
+// 				break
+// 			}
+// 		}
+
+// 		if linkedGH {
+// 			break
+// 		}
+
+// 		time.Sleep(2 * time.Second)
+// 	}
 
-	return ghAuth, nil
-}
+// 	return ghAuth, nil
+// }

+ 4 - 2
cli/cmd/deploy/create.go

@@ -49,6 +49,7 @@ func (c *CreateAgent) CreateFromGithub(
 	)
 
 	if err != nil {
+		fmt.Println("could not list git repos")
 		return "", err
 	}
 
@@ -59,16 +60,17 @@ func (c *CreateAgent) CreateFromGithub(
 		githubRepos, err := c.Client.ListGithubRepos(
 			context.Background(),
 			c.CreateOpts.ProjectID,
-			gitRepo.ID,
+			gitRepo,
 		)
 
 		if err != nil {
+			fmt.Println("could not list github repos")
 			return "", err
 		}
 
 		for _, githubRepo := range githubRepos {
 			if githubRepo.FullName == ghOpts.Repo {
-				gitRepoMatch = gitRepo.ID
+				gitRepoMatch = gitRepo
 				break
 			}
 		}