Kaynağa Gözat

add branch and github action ID to porter deployment request

sunguroku 4 yıl önce
ebeveyn
işleme
8c93dddcd4

+ 38 - 34
api/server/handlers/environment/create_deployment.go

@@ -1,6 +1,7 @@
 package environment
 
 import (
+	"fmt"
 	"context"
 	"net/http"
 
@@ -50,34 +51,6 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	// create the deployment
-	depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
-		EnvironmentID: env.ID,
-		Namespace:     request.Namespace,
-		Status:        "creating",
-		PullRequestID: request.PullRequestID,
-	})
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-		return
-	}
-
-	// create the backing namespace
-	agent, err := c.GetAgent(r, cluster, "")
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-		return
-	}
-
-	_, err = agent.CreateNamespace(depl.Namespace)
-
-	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-		return
-	}
-
 	// create deployment on GitHub API
 	client, err := getGithubClientFromEnvironment(c.Config(), env)
 
@@ -86,14 +59,16 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
-	envName := "preview"
+	branch := request.Branch
+	envName := "Preview"
+	automerge := false
 
 	deploymentRequest := github.DeploymentRequest{
-		Ref: &envName,
+		Ref: &branch,
 		Environment: &envName,
+		AutoMerge: &automerge,
 	}
 
-	// create deployment in GitHub (This really should belong in create_deployment)
 	deployment, _, err := client.Repositories.CreateDeployment(
 		context.Background(),
 		env.GitRepoOwner,
@@ -105,13 +80,13 @@ 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
 
-	state := "queued"
-	log_url := "https://github.com/actions"
+	state := "in_progress"
+	log_url := fmt.Sprintf("https://github.com/%s/%s/actions/%d", env.GitRepoOwner, env.GitRepoName, request.ActionID)
 
 	deploymentStatusRequest := github.DeploymentStatusRequest{
 		State: &state,
@@ -131,5 +106,34 @@ func (c *CreateDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
+	// create the deployment
+	depl, err := c.Repo().Environment().CreateDeployment(&models.Deployment{
+		EnvironmentID: env.ID,
+		Namespace:     request.Namespace,
+		Status:        "creating",
+		PullRequestID: request.PullRequestID,
+		GitHubDeploymentID: depID,
+	})
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// create the backing namespace
+	agent, err := c.GetAgent(r, cluster, "")
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	_, err = agent.CreateNamespace(depl.Namespace)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
 	c.WriteResult(w, r, depl.ToDeploymentType())
 }

+ 20 - 21
api/server/handlers/environment/finalize_deployment.go

@@ -1,6 +1,7 @@
 package environment
 
 import (
+	"fmt"
 	"context"
 	"net/http"
 
@@ -73,19 +74,22 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	// write comment in PR
-	commentBody := "(TENTATIVE) Porter is deploying this pull request..."
-	prComment := github.IssueComment{
-		Body: &commentBody,
-		User: &github.User{},
+	// Create new deployment status to indicate deployment is ready
+
+	state := "success"
+	env_url := depl.Subdomain
+
+	deploymentStatusRequest := github.DeploymentStatusRequest{
+		State: &state,
+		EnvironmentURL : &env_url,
 	}
 
-	_, _, err = client.Issues.CreateComment(
+	_, _, err = client.Repositories.CreateDeploymentStatus(
 		context.Background(),
 		env.GitRepoOwner,
 		env.GitRepoName,
-		int(depl.PullRequestID),
-		&prComment,
+		depl.GitHubDeploymentID,
+		&deploymentStatusRequest,
 	)
 
 	if err != nil {
@@ -93,24 +97,19 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	// Create new deployment status to indicate deployment is ready
-
-	state := "succeeded"
-	log_url := "https://github.com/actions"
-	env_url := depl.Subdomain
-
-	deploymentStatusRequest := github.DeploymentStatusRequest{
-		State: &state,
-		LogURL: &log_url, // link to specific actions tab 
-		EnvironmentURL : &env_url,
+	// write comment in PR
+	commentBody := fmt.Sprintf("Porter has deployed this pull request to the following URL:\n%s", depl.Subdomain)
+	prComment := github.IssueComment{
+		Body: &commentBody,
+		User: &github.User{},
 	}
 
-	_, _, err = client.Repositories.CreateDeploymentStatus(
+	_, _, err = client.Issues.CreateComment(
 		context.Background(),
 		env.GitRepoOwner,
 		env.GitRepoName,
-		depID,
-		&deploymentStatusRequest,
+		int(depl.PullRequestID),
+		&prComment,
 	)
 
 	if err != nil {

+ 3 - 0
api/types/environment.go

@@ -24,11 +24,14 @@ type Deployment struct {
 	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"`
 }
 
 type FinalizeDeploymentRequest struct {

+ 22 - 2
cli/cmd/apply.go

@@ -571,8 +571,8 @@ func existsInRepo(name, version, url string) (map[string]interface{}, error) {
 type DeploymentHook struct {
 	client                                        *api.Client
 	resourceGroup                                 *switchboardTypes.ResourceGroup
-	gitInstallationID, projectID, clusterID, prID uint
-	namespace                                     string
+	gitInstallationID, projectID, clusterID, prID, actionID uint
+	branch, namespace                                     string
 }
 
 func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
@@ -618,6 +618,24 @@ func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.Resou
 		return nil, fmt.Errorf("cluster id must be set")
 	}
 
+	if branchName := os.Getenv("PORTER_BRANCH_NAME"); branchName != "" {
+		res.branch = branchName
+	} else if branchName == "" {
+		return nil, fmt.Errorf("Branch name must be defined, set by PORTER_BRANCH_NAME")
+	}
+
+	if actionIDStr := os.Getenv("PORTER_ACTION_ID"); actionIDStr != "" {
+		actionID, err := strconv.Atoi(actionIDStr)
+
+		if err != nil {
+			return nil, err
+		}
+
+		res.actionID = uint(actionID)
+	} else if actionIDStr == "" {
+		return nil, fmt.Errorf("Action Run ID must be defined, set by PORTER_ACTION_ID")
+	}
+
 	return res, nil
 }
 
@@ -640,6 +658,8 @@ func (t *DeploymentHook) PreApply() error {
 			&types.CreateDeploymentRequest{
 				Namespace:     t.namespace,
 				PullRequestID: t.prID,
+				Branch: t.branch,
+				ActionID: t.actionID,
 			},
 		)
 

+ 2 - 0
internal/models/environment.go

@@ -37,6 +37,7 @@ type Deployment struct {
 	Status        string
 	Subdomain     string
 	PullRequestID uint
+	GitHubDeploymentID int64
 }
 
 func (d *Deployment) ToDeploymentType() *types.Deployment {
@@ -47,5 +48,6 @@ func (d *Deployment) ToDeploymentType() *types.Deployment {
 		Status:        d.Status,
 		Subdomain:     d.Subdomain,
 		PullRequestID: d.PullRequestID,
+		GitHubDeploymentID: d.GitHubDeploymentID,
 	}
 }