Browse Source

run gofmt and add environment creation

Alexander Belanger 4 years ago
parent
commit
26cfd0e5bd

+ 11 - 11
api/server/handlers/environment/create_deployment.go

@@ -1,11 +1,11 @@
 package environment
 
 import (
-	"fmt"
 	"context"
+	"fmt"
 	"net/http"
 
-	"github.com/google/go-github/github"
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
@@ -64,9 +64,9 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 	automerge := false
 
 	deploymentRequest := github.DeploymentRequest{
-		Ref: &branch,
+		Ref:         &branch,
 		Environment: &envName,
-		AutoMerge: &automerge,
+		AutoMerge:   &automerge,
 	}
 
 	deployment, _, err := client.Repositories.CreateDeployment(
@@ -80,7 +80,7 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 	}
-	
+
 	depID := deployment.GetID()
 
 	// Create Deployment Status to indicate it's in progress
@@ -89,8 +89,8 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 	log_url := fmt.Sprintf("https://github.com/%s/%s/actions/%d", env.GitRepoOwner, env.GitRepoName, request.ActionID)
 
 	deploymentStatusRequest := github.DeploymentStatusRequest{
-		State: &state,
-		LogURL: &log_url, // link to actions tab 
+		State:  &state,
+		LogURL: &log_url, // link to actions tab
 	}
 
 	_, _, err = client.Repositories.CreateDeploymentStatus(
@@ -108,10 +108,10 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 
 	// create the deployment
 	depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
-		EnvironmentID: env.ID,
-		Namespace:     request.Namespace,
-		Status:        "creating",
-		PullRequestID: request.PullRequestID,
+		EnvironmentID:      env.ID,
+		Namespace:          request.Namespace,
+		Status:             "creating",
+		PullRequestID:      request.PullRequestID,
 		GitHubDeploymentID: depID,
 	})
 

+ 3 - 3
api/server/handlers/environment/finalize_deployment.go

@@ -1,8 +1,8 @@
 package environment
 
 import (
-	"fmt"
 	"context"
+	"fmt"
 	"net/http"
 
 	"github.com/google/go-github/v41/github"
@@ -80,8 +80,8 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 	env_url := depl.Subdomain
 
 	deploymentStatusRequest := github.DeploymentStatusRequest{
-		State: &state,
-		EnvironmentURL : &env_url,
+		State:          &state,
+		EnvironmentURL: &env_url,
 	}
 
 	_, _, err = client.Repositories.CreateDeploymentStatus(

+ 9 - 9
api/types/environment.go

@@ -18,20 +18,20 @@ type CreateEnvironmentRequest struct {
 }
 
 type Deployment struct {
-	ID            uint   `json:"id"`
-	EnvironmentID uint   `json:"environment_id"`
-	Namespace     string `json:"namespace"`
-	Status        string `json:"status"`
-	Subdomain     string `json:"subdomain"`
-	PullRequestID uint   `json:"pull_request_id"`
-	GitHubDeploymentID int64 `json:"github_deployment_id"`
+	ID                 uint   `json:"id"`
+	EnvironmentID      uint   `json:"environment_id"`
+	Namespace          string `json:"namespace"`
+	Status             string `json:"status"`
+	Subdomain          string `json:"subdomain"`
+	PullRequestID      uint   `json:"pull_request_id"`
+	GitHubDeploymentID int64  `json:"github_deployment_id"`
 }
 
 type CreateDeploymentRequest struct {
 	Namespace     string `json:"namespace" form:"required"`
 	PullRequestID uint   `json:"pull_request_id" form:"required"`
-	Branch string `json:"branch" form:"required"`
-	ActionID uint `json:"action_id" form:"required"`
+	Branch        string `json:"branch" form:"required"`
+	ActionID      uint   `json:"action_id" form:"required"`
 }
 
 type FinalizeDeploymentRequest struct {

+ 5 - 5
cli/cmd/apply.go

@@ -569,10 +569,10 @@ func existsInRepo(name, version, url string) (map[string]interface{}, error) {
 }
 
 type DeploymentHook struct {
-	client                                        *api.Client
-	resourceGroup                                 *switchboardTypes.ResourceGroup
+	client                                                  *api.Client
+	resourceGroup                                           *switchboardTypes.ResourceGroup
 	gitInstallationID, projectID, clusterID, prID, actionID uint
-	branch, namespace                                     string
+	branch, namespace                                       string
 }
 
 func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
@@ -658,8 +658,8 @@ func (t *DeploymentHook) PreApply() error {
 			&types.CreateDeploymentRequest{
 				Namespace:     t.namespace,
 				PullRequestID: t.prID,
-				Branch: t.branch,
-				ActionID: t.actionID,
+				Branch:        t.branch,
+				ActionID:      t.actionID,
 			},
 		)
 

+ 24 - 23
internal/integrations/ci/actions/preview.go

@@ -3,6 +3,7 @@ package actions
 import (
 	"context"
 	"fmt"
+	"net/http"
 	"strings"
 
 	"github.com/google/go-github/v41/github"
@@ -21,31 +22,31 @@ type EnvOpts struct {
 
 func SetupEnv(opts *EnvOpts) ([]byte, error) {
 	// create Github environment if it does not exist
-	// _, resp, err := opts.Client.Repositories.GetEnvironment(
-	// 	context.Background(),
-	// 	opts.GitRepoOwner,
-	// 	opts.GitRepoName,
-	// 	opts.EnvironmentName,
-	// )
-
-	// if resp.StatusCode == http.StatusNotFound {
-	// 	_, _, err := opts.Client.Repositories.CreateUpdateEnvironment(
-	// 		context.Background(),
-	// 		opts.GitRepoOwner,
-	// 		opts.GitRepoName,
-	// 		opts.EnvironmentName,
-	// 		nil,
-	// 	)
-
-	// 	if err != nil {
-	// 		return nil, err
-	// 	}
-	// } else if err != nil {
-	// 	return nil, err
-	// }
+	_, resp, err := opts.Client.Repositories.GetEnvironment(
+		context.Background(),
+		opts.GitRepoOwner,
+		opts.GitRepoName,
+		opts.EnvironmentName,
+	)
+
+	if resp.StatusCode == http.StatusNotFound {
+		_, _, err := opts.Client.Repositories.CreateUpdateEnvironment(
+			context.Background(),
+			opts.GitRepoOwner,
+			opts.GitRepoName,
+			opts.EnvironmentName,
+			nil,
+		)
+
+		if err != nil {
+			return nil, err
+		}
+	} else if err != nil {
+		return nil, err
+	}
 
 	// create porter token secret
-	err := createGithubSecret(
+	err = createGithubSecret(
 		opts.Client,
 		getPorterTokenSecretName(opts.ProjectID),
 		opts.PorterToken,

+ 2 - 0
internal/integrations/ci/actions/steps.go

@@ -51,6 +51,8 @@ func getCreatePreviewEnvStep(serverURL, porterTokenSecretName string, projectID,
 			"namespace":       fmt.Sprintf("pr-${{ github.event.pull_request.number }}-%s", repoName),
 			"pr_id":           "${{ github.event.pull_request.number }}",
 			"installation_id": fmt.Sprintf("%d", gitInstallationID),
+			"branch":          "${{ github.head_ref }}",
+			"action_id":       "${{ github.run_id }}",
 		},
 		Timeout: 30,
 	}

+ 11 - 11
internal/models/environment.go

@@ -32,22 +32,22 @@ func (e *Environment) ToEnvironmentType() *types.Environment {
 type Deployment struct {
 	gorm.Model
 
-	EnvironmentID uint
-	Namespace     string
-	Status        string
-	Subdomain     string
-	PullRequestID uint
+	EnvironmentID      uint
+	Namespace          string
+	Status             string
+	Subdomain          string
+	PullRequestID      uint
 	GitHubDeploymentID int64
 }
 
 func (d *Deployment) ToDeploymentType() *types.Deployment {
 	return &types.Deployment{
-		ID:            d.Model.ID,
-		EnvironmentID: d.EnvironmentID,
-		Namespace:     d.Namespace,
-		Status:        d.Status,
-		Subdomain:     d.Subdomain,
-		PullRequestID: d.PullRequestID,
+		ID:                 d.Model.ID,
+		EnvironmentID:      d.EnvironmentID,
+		Namespace:          d.Namespace,
+		Status:             d.Status,
+		Subdomain:          d.Subdomain,
+		PullRequestID:      d.PullRequestID,
 		GitHubDeploymentID: d.GitHubDeploymentID,
 	}
 }