Explorar el Código

Merge pull request #2079 from porter-dev/master

Parallel GH env listing -> staging
abelanger5 hace 4 años
padre
commit
7e3afabeaf

+ 41 - 5
api/server/handlers/environment/list_deployments_by_cluster.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"net/http"
+	"sync"
 
 	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -65,13 +66,31 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 				return
 			}
 
-			updateDeploymentWithGithubWorkflowRunStatus(r.Context(), c.Config(), env, deployment)
-
 			deployment.InstallationID = env.GitInstallationID
 
 			deployments = append(deployments, deployment)
 		}
 
+		var wg sync.WaitGroup
+		wg.Add(len(deployments))
+
+		for _, deployment := range deployments {
+			env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, deployment.EnvironmentID)
+
+			if err != nil {
+				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+				return
+			}
+
+			go func(depl *types.Deployment) {
+				defer wg.Done()
+
+				updateDeploymentWithGithubWorkflowRunStatus(c.Config(), env, depl)
+			}(deployment)
+		}
+
+		wg.Wait()
+
 		envList, err := c.Repo().Environment().ListEnvironments(project.ID, cluster.ID)
 
 		if err != nil {
@@ -112,13 +131,31 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 				"%s-%s-%d", deployment.RepoOwner, deployment.RepoName, deployment.PullRequestID,
 			)] = true
 
-			updateDeploymentWithGithubWorkflowRunStatus(r.Context(), c.Config(), env, deployment)
-
 			deployment.InstallationID = env.GitInstallationID
 
 			deployments = append(deployments, deployment)
 		}
 
+		var wg sync.WaitGroup
+		wg.Add(len(deployments))
+
+		for _, deployment := range deployments {
+			env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, deployment.EnvironmentID)
+
+			if err != nil {
+				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+				return
+			}
+
+			go func(depl *types.Deployment) {
+				defer wg.Done()
+
+				updateDeploymentWithGithubWorkflowRunStatus(c.Config(), env, depl)
+			}(deployment)
+		}
+
+		wg.Wait()
+
 		prs, err := fetchOpenPullRequests(r.Context(), c.Config(), env, deplInfoMap)
 
 		if err != nil {
@@ -136,7 +173,6 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 }
 
 func updateDeploymentWithGithubWorkflowRunStatus(
-	ctx context.Context,
 	config *config.Config,
 	env *models.Environment,
 	deployment *types.Deployment,

+ 5 - 1
api/server/shared/commonutils/git_utils.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"errors"
 	"net/http"
+	"time"
 
 	"github.com/google/go-github/v41/github"
 )
@@ -12,8 +13,11 @@ var ErrNoWorkflowRuns = errors.New("no previous workflow runs found")
 var ErrWorkflowNotFound = errors.New("no workflow found, file missing")
 
 func GetLatestWorkflowRun(client *github.Client, owner, repo, filename, branch string) (*github.WorkflowRun, error) {
+	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+	defer cancel()
+
 	workflowRuns, ghResponse, err := client.Actions.ListWorkflowRunsByFileName(
-		context.Background(), owner, repo, filename, &github.ListWorkflowRunsOptions{
+		ctx, owner, repo, filename, &github.ListWorkflowRunsOptions{
 			Branch: branch,
 			ListOptions: github.ListOptions{
 				Page:    1,