Browse Source

enrich github metadata for pr env (cli)

sunguroku 4 năm trước cách đây
mục cha
commit
ac71fc3638
3 tập tin đã thay đổi với 40 bổ sung3 xóa
  1. 2 1
      api/types/environment.go
  2. 34 1
      cli/cmd/apply.go
  3. 4 1
      internal/integrations/ci/actions/steps.go

+ 2 - 1
api/types/environment.go

@@ -26,13 +26,14 @@ type GitHubMetadata struct {
 }
 
 type Deployment struct {
+	*GitHubMetadata
+	
 	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"`
-	GitHubMetadata *GitHubMetadata  `json:"github_metadata"`
 }
 
 type CreateGHDeploymentRequest struct {

+ 34 - 1
cli/cmd/apply.go

@@ -613,7 +613,7 @@ type DeploymentHook struct {
 	client                                                  *api.Client
 	resourceGroup                                           *switchboardTypes.ResourceGroup
 	gitInstallationID, projectID, clusterID, prID, actionID uint
-	branch, namespace                                       string
+	branch, namespace, repoName, repoOwner, prName, commitSHA             string
 }
 
 func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.ResourceGroup, namespace string) (*DeploymentHook, error) {
@@ -677,6 +677,32 @@ func NewDeploymentHook(client *api.Client, resourceGroup *switchboardTypes.Resou
 		return nil, fmt.Errorf("Action Run ID must be defined, set by PORTER_ACTION_ID")
 	}
 
+	if repoName := os.Getenv("PORTER_REPO_NAME"); repoName != "" {
+		res.repoName = repoName
+	} else if repoName == "" {
+		return nil, fmt.Errorf("Repo name must be defined, set by PORTER_REPO_NAME")
+	}
+
+	if repoOwner := os.Getenv("PORTER_REPO_OWNER"); repoOwner != "" {
+		res.repoOwner = repoOwner
+	} else if repoOwner == "" {
+		return nil, fmt.Errorf("Repo owner must be defined, set by PORTER_REPO_OWNER")
+	}
+
+	if prName := os.Getenv("PORTER_PR_NAME"); prName != "" {
+		res.prName = prName
+	} else if prName == "" {
+		return nil, fmt.Errorf("PR Name must be supplied, set by PORTER_PR_NAME")
+	}
+
+	commit, err := git.LastCommit()
+
+	if err != nil {
+		return nil, fmt.Errorf(err.Error())
+	}
+
+	res.commitSHA = commit.Sha[:7]
+
 	return res, nil
 }
 
@@ -703,6 +729,12 @@ func (t *DeploymentHook) PreApply() error {
 					Branch:   t.branch,
 					ActionID: t.actionID,
 				},
+				GitHubMetadata: &types.GitHubMetadata{
+					PRName:		t.prName,
+					RepoName: t.repoName,
+					RepoOwner: t.repoOwner,
+					CommitSHA: t.commitSHA,
+				},
 			},
 		)
 	} else if err == nil {
@@ -715,6 +747,7 @@ func (t *DeploymentHook) PreApply() error {
 					Branch:   t.branch,
 					ActionID: t.actionID,
 				},
+				CommitSHA: t.commitSHA,
 			},
 		)
 	}

+ 4 - 1
internal/integrations/ci/actions/steps.go

@@ -51,9 +51,12 @@ func getCreatePreviewEnvStep(serverURL, porterTokenSecretName string, projectID,
 			"token":           fmt.Sprintf("${{ secrets.%s }}", porterTokenSecretName),
 			"namespace":       fmt.Sprintf("pr-${{ github.event.pull_request.number }}-%s", repoName),
 			"pr_id":           "${{ github.event.pull_request.number }}",
+			"pr_name":		   "${{ github.event.pull_request.title }}",
 			"installation_id": fmt.Sprintf("%d", gitInstallationID),
 			"branch":          "${{ github.head_ref }}",
-			"action_id":       "${{ github.run_id }}",
+			"action_id":       "${{ github.job }}",
+			"repo_owner":      "${{ github.repository_owner }}",
+			"repo_name":       fmt.Sprintf("%s", repoName),
 		},
 		Timeout: 30,
 	}