Przeglądaj źródła

Merge branch 'nafees/pr-env-comments' into dev

Mohammed Nafees 3 lat temu
rodzic
commit
60d6ff0a72

+ 13 - 5
api/server/handlers/environment/finalize_deployment.go

@@ -118,19 +118,25 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 
 	// write comment in PR
 	commentBody := fmt.Sprintf(
-		"## ✅ Porter Preview Environments\n"+
+		"## Porter Preview Environments\n"+
+			"✅ All changes deployed successfully\n"+
 			"||Deployment Information|\n"+
 			"|-|-|\n"+
 			"| Latest SHA | [`%s`](https://github.com/%s/%s/commit/%s) |\n"+
 			"| Live URL | %s |\n"+
-			"| Github Action | %s |\n"+
+			"| Build Logs | %s |\n"+
 			"| Porter Deployments URL | %s/preview-environments/details/%s?environment_id=%d |",
 		depl.CommitSHA, depl.RepoOwner, depl.RepoName, depl.CommitSHA, depl.Subdomain, workflowRun.GetHTMLURL(),
 		c.Config().ServerConf.ServerURL, depl.Namespace, depl.EnvironmentID,
 	)
 
-	prComment := &github.IssueComment{
-		Body: github.String(commentBody),
+	if len(request.SuccessfulResources) > 0 {
+		commentBody += "#### Successfully deployed resources\n"
+
+		for _, res := range request.SuccessfulResources {
+			commentBody += fmt.Sprintf("- [`%s`](%s/applications/%s/%s/%s?project_id=%d)\n",
+				res, c.Config().ServerConf.ServerURL, cluster.Name, depl.Namespace, res, project.ID)
+		}
 	}
 
 	_, _, err = client.Issues.CreateComment(
@@ -138,7 +144,9 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		env.GitRepoOwner,
 		env.GitRepoName,
 		int(depl.PullRequestID),
-		prComment,
+		&github.IssueComment{
+			Body: github.String(commentBody),
+		},
 	)
 
 	if err != nil {

+ 5 - 3
api/server/handlers/environment/finalize_deployment_with_errors.go

@@ -114,11 +114,12 @@ func (c *FinalizeDeploymentWithErrorsHandler) ServeHTTP(w http.ResponseWriter, r
 	}
 
 	commentBody := fmt.Sprintf(
-		"## ❌ Porter Preview Environments\n"+
+		"## Porter Preview Environments\n"+
+			"❌ Errors encountered while deploying the changes\n"+
 			"||Deployment Information|\n"+
 			"|-|-|\n"+
 			"| Latest SHA | [`%s`](https://github.com/%s/%s/commit/%s) |\n"+
-			"| Github Action | %s |\n",
+			"| Build Logs | %s |\n",
 		depl.CommitSHA, depl.RepoOwner, depl.RepoName, depl.CommitSHA, workflowRun.GetHTMLURL(),
 	)
 
@@ -126,7 +127,8 @@ func (c *FinalizeDeploymentWithErrorsHandler) ServeHTTP(w http.ResponseWriter, r
 		commentBody += "#### Successfully deployed resources\n"
 
 		for _, res := range request.SuccessfulResources {
-			commentBody += fmt.Sprintf("- `%s`\n", res)
+			commentBody += fmt.Sprintf("- [`%s`](%s/applications/%s/%s/%s?project_id=%d)\n",
+				res, c.Config().ServerConf.ServerURL, cluster.Name, depl.Namespace, res, project.ID)
 		}
 	}
 

+ 3 - 2
api/types/environment.go

@@ -70,8 +70,9 @@ type CreateDeploymentRequest struct {
 }
 
 type FinalizeDeploymentRequest struct {
-	Namespace string `json:"namespace" form:"required"`
-	Subdomain string `json:"subdomain"`
+	Namespace           string   `json:"namespace" form:"required"`
+	SuccessfulResources []string `json:"successful_resources"`
+	Subdomain           string   `json:"subdomain"`
 }
 
 type FinalizeDeploymentWithErrorsRequest struct {

+ 23 - 6
cli/cmd/apply.go

@@ -859,15 +859,20 @@ func (t *DeploymentHook) PostApply(populatedData map[string]interface{}) error {
 		}
 	}
 
+	req := &types.FinalizeDeploymentRequest{
+		Namespace: t.namespace,
+		Subdomain: strings.Join(subdomains, ","),
+	}
+
+	for _, res := range t.resourceGroup.Resources {
+		req.SuccessfulResources = append(req.SuccessfulResources, getReleaseName(res))
+	}
+
 	// finalize the deployment
 	_, err := t.client.FinalizeDeployment(
 		context.Background(),
 		t.projectID, t.gitInstallationID, t.clusterID,
-		t.repoOwner, t.repoName,
-		&types.FinalizeDeploymentRequest{
-			Namespace: t.namespace,
-			Subdomain: strings.Join(subdomains, ","),
-		},
+		t.repoOwner, t.repoName, req,
 	)
 
 	return err
@@ -918,7 +923,7 @@ func (t *DeploymentHook) OnConsolidatedErrors(allErrors map[string]error) {
 
 		for _, res := range t.resourceGroup.Resources {
 			if _, ok := allErrors[res.Name]; !ok {
-				req.SuccessfulResources = append(req.SuccessfulResources, res.Name)
+				req.SuccessfulResources = append(req.SuccessfulResources, getReleaseName(res))
 			}
 		}
 
@@ -1027,3 +1032,15 @@ func (t *CloneEnvGroupHook) PostApply(map[string]interface{}) error {
 func (t *CloneEnvGroupHook) OnError(error) {}
 
 func (t *CloneEnvGroupHook) OnConsolidatedErrors(map[string]error) {}
+
+func getReleaseName(res *switchboardTypes.Resource) string {
+	// can ignore the error because this method is called once
+	// GetTarget has alrealy been called and validated previously
+	target, _ := preview.GetTarget(res.Target)
+
+	if target.AppName != "" {
+		return target.AppName
+	}
+
+	return res.Name
+}