Ver Fonte

IT WORKS HOLY SHIT

Feroze Mohideen há 3 anos atrás
pai
commit
9ebad6ce1a

+ 35 - 11
api/server/handlers/stacks/create_secret_and_open_pr.go

@@ -37,7 +37,7 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
 
-	request := &types.CreateSecretAndOpenGitHubPullRequest{}
+	request := &types.CreateSecretAndOpenGHPRRequest{}
 	if ok := c.DecodeAndValidate(w, r, request); !ok {
 		return
 	}
@@ -60,16 +60,32 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	// create porter secret
+	secretName := fmt.Sprintf("PORTER_STACK_%d_%d", project.ID, cluster.ID)
+	err = actions.CreateGithubSecret(
+		client,
+		secretName,
+		encoded,
+		request.GithubRepoOwner,
+		request.GithubRepoName,
+	)
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error generating secret: %w", err)))
+		return
+	}
+
+	var pr *github.PullRequest
 	if request.OpenPr {
-		err = actions.OpenGithubPR(&actions.GithubPROpts{
-			Client:       client,
-			GitRepoOwner: request.GithubRepoOwner,
-			GitRepoName:  request.GithubRepoName,
-			StackName:    request.StackName,
-			ProjectID:    project.ID,
-			ClusterID:    cluster.ID,
-			PorterToken:  encoded,
-			ServerURL:    c.Config().ServerConf.ServerURL,
+		pr, err = actions.OpenGithubPR(&actions.GithubPROpts{
+			Client:        client,
+			GitRepoOwner:  request.GithubRepoOwner,
+			GitRepoName:   request.GithubRepoName,
+			StackName:     request.StackName,
+			ProjectID:     project.ID,
+			ClusterID:     cluster.ID,
+			ServerURL:     c.Config().ServerConf.ServerURL,
+			DefaultBranch: request.Branch,
+			SecretName:    secretName,
 		})
 	}
 
@@ -83,13 +99,21 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 				c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusPreconditionFailed))
 			}
 		} else {
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error setting up preview environment in the github "+
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error setting up application in the github "+
 				"repo: %w", err)))
 			return
 		}
 	}
 
+	var resp types.CreateSecretAndOpenGHPRResponse
+	if pr != nil {
+		resp = types.CreateSecretAndOpenGHPRResponse{
+			URL: pr.GetHTMLURL(),
+		}
+	}
+
 	w.WriteHeader(http.StatusCreated)
+	c.WriteResult(w, r, resp)
 }
 
 func getGithubClient(config *config.Config, gitInstallationId int64) (*github.Client, error) {

+ 8 - 3
api/types/stack.go

@@ -4,7 +4,7 @@ type CreateStackReleaseRequest struct {
 	// The Helm values for this release
 	Values map[string]interface{} `json:"values"`
 	// Used to construct the Chart.yaml
-	Dependencies []Dependency `json:"dependencies" form:"required"`
+	Dependencies []Dependency `json:"dependencies"`
 	StackName    string       `json:"stack_name" form:"required,dns1123"`
 }
 
@@ -15,10 +15,15 @@ type Dependency struct {
 	Repository string `json:"repository" form:"required"`
 }
 
-type CreateSecretAndOpenGitHubPullRequest struct {
+type CreateSecretAndOpenGHPRRequest struct {
 	StackName               string `json:"stack_name" form:"required,dns1123"`
 	GithubAppInstallationID int64  `json:"github_app_installation_id" form:"required"`
 	GithubRepoOwner         string `json:"github_repo_owner" form:"required"`
 	GithubRepoName          string `json:"github_repo_name" form:"required"`
-	OpenPr                  bool   `json:"open_pr" form:"required"`
+	OpenPr                  bool   `json:"open_pr"`
+	Branch                  string `json:"branch"`
+}
+
+type CreateSecretAndOpenGHPRResponse struct {
+	URL string `json:"url"`
 }

+ 3 - 3
internal/integrations/ci/actions/actions.go

@@ -80,7 +80,7 @@ func (g *GithubActions) Setup() ([]byte, error) {
 
 	if !g.DryRun {
 		// create porter token secret
-		if err := createGithubSecret(client, g.getPorterTokenSecretName(), g.PorterToken, g.GitRepoOwner, g.GitRepoName); err != nil {
+		if err := CreateGithubSecret(client, g.getPorterTokenSecretName(), g.PorterToken, g.GitRepoOwner, g.GitRepoName); err != nil {
 			return nil, err
 		}
 	}
@@ -310,7 +310,7 @@ func (g *GithubActions) getClient() (*github.Client, error) {
 	return github.NewClient(&http.Client{Transport: itr}), nil
 }
 
-func createGithubSecret(
+func CreateGithubSecret(
 	client *github.Client,
 	secretName,
 	secretValue,
@@ -386,7 +386,7 @@ func (g *GithubActions) createEnvSecret(client *github.Client) error {
 
 	secretName := g.getBuildEnvSecretName()
 
-	return createGithubSecret(client, secretName, strings.Join(lines, "\n"), g.GitRepoOwner, g.GitRepoName)
+	return CreateGithubSecret(client, secretName, strings.Join(lines, "\n"), g.GitRepoOwner, g.GitRepoName)
 }
 
 func (g *GithubActions) getWebhookSecretName() string {

+ 1 - 1
internal/integrations/ci/actions/preview.go

@@ -43,7 +43,7 @@ func SetupEnv(opts *EnvOpts) error {
 	}
 
 	// create porter token secret
-	err = createGithubSecret(
+	err = CreateGithubSecret(
 		opts.Client,
 		getPreviewEnvSecretName(opts.ProjectID, opts.ClusterID, opts.InstanceName),
 		opts.PorterToken,

+ 23 - 36
internal/integrations/ci/actions/stack.go

@@ -15,9 +15,9 @@ type GithubPROpts struct {
 	ApplyWorkflowYAML         string
 	StackName                 string
 	ProjectID, ClusterID      uint
-	PorterToken               string
 	ServerURL                 string
 	DefaultBranch             string
+	SecretName                string
 }
 
 type GetStackApplyActionYAMLOpts struct {
@@ -28,44 +28,31 @@ type GetStackApplyActionYAMLOpts struct {
 	SecretName           string
 }
 
-func OpenGithubPR(opts *GithubPROpts) error {
-	// create porter secret
-	secretName := fmt.Sprintf("PORTER_STACK_%d_%d", opts.ProjectID, opts.ClusterID)
-	err := createGithubSecret(
-		opts.Client,
-		secretName,
-		opts.PorterToken,
-		opts.GitRepoOwner,
-		opts.GitRepoName,
-	)
-	if err != nil {
-		return err
-	}
-
+func OpenGithubPR(opts *GithubPROpts) (*github.PullRequest, error) {
+	var pr *github.PullRequest
 	applyWorkflowYAML, err := getStackApplyActionYAML(&GetStackApplyActionYAMLOpts{
 		ServerURL:     opts.ServerURL,
 		ClusterID:     opts.ClusterID,
 		ProjectID:     opts.ProjectID,
 		StackName:     opts.StackName,
 		DefaultBranch: opts.DefaultBranch,
-		SecretName:    secretName,
+		SecretName:    opts.SecretName,
 	})
 	if err != nil {
-		return err
+		return pr, err
 	}
 
+	prBranchName := "porter-stack"
+
 	err = createNewBranch(opts.Client,
 		opts.GitRepoOwner,
 		opts.GitRepoName,
 		opts.DefaultBranch,
-		"porter-stack")
+		prBranchName)
 	if err != nil {
-		return fmt.Errorf(
-			"Unable to create PR to merge workflow files into protected branch: %s.\n"+
-				"To enable Porter Preview Environment deployments, please create Github workflow "+
-				"files in this branch with the following contents:\n"+
-				"--------\n%s--------\nERROR: %w",
-			opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
+		return pr, fmt.Errorf(
+			"error creating branch: %w",
+			err,
 		)
 	}
 
@@ -73,30 +60,30 @@ func OpenGithubPR(opts *GithubPROpts) error {
 		opts.Client,
 		fmt.Sprintf("porter_stack_%s.yml", strings.ToLower(opts.StackName)),
 		applyWorkflowYAML, opts.GitRepoOwner,
-		opts.GitRepoName, "porter-preview", false,
+		opts.GitRepoName, prBranchName, false,
 	)
 
 	if err != nil {
-		return fmt.Errorf(
-			"Unable to create PR to merge workflow files into protected branch: %s.\n"+
-				"To enable Porter Preview Environment deployments, please create Github workflow "+
-				"files in this branch with the following contents:\n"+
-				"--------\n%s--------\nERROR: %w",
-			opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
+		return pr, fmt.Errorf(
+			"error committing file: %w",
+			err,
 		)
 	}
 
-	_, _, err = opts.Client.PullRequests.Create(
+	pr, _, err = opts.Client.PullRequests.Create(
 		context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
-			Title: github.String("Enable Porter Preview Environment deployments"),
+			Title: github.String("Enable Porter Application"),
 			Base:  github.String(opts.DefaultBranch),
-			Head:  github.String("porter-preview"),
+			Head:  github.String(prBranchName),
 		},
 	)
 	if err != nil {
-		return err
+		return pr, fmt.Errorf(
+			"error creating PR: %w",
+			err,
+		)
 	}
-	return nil
+	return pr, nil
 }
 
 func getStackApplyActionYAML(opts *GetStackApplyActionYAMLOpts) ([]byte, error) {