Procházet zdrojové kódy

return list of open PRs

Mohammed Nafees před 4 roky
rodič
revize
b9e256556c

+ 1 - 1
api/server/handlers/environment/list_deployments.go

@@ -61,7 +61,7 @@ func (c *ListDeploymentsHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		return
 	}
 
-	depls, err := c.Repo().Environment().ListDeployments(env.ID, req.Status...)
+	depls, err := c.Repo().Environment().ListDeployments(env.ID)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 94 - 7
api/server/handlers/environment/list_deployments_by_cluster.go

@@ -1,8 +1,10 @@
 package environment
 
 import (
+	"context"
 	"net/http"
 
+	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
@@ -35,18 +37,103 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 		return
 	}
 
-	depls, err := c.Repo().Environment().ListDeploymentsByCluster(project.ID, cluster.ID, req.Status...)
+	var deployments []*types.Deployment
+	var pullRequests []*types.PullRequest
+
+	if req.EnvironmentID == 0 {
+		depls, err := c.Repo().Environment().ListDeploymentsByCluster(project.ID, cluster.ID)
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+
+		for _, depl := range depls {
+			deployments = append(deployments, depl.ToDeploymentType())
+		}
+
+		envList, err := c.Repo().Environment().ListEnvironments(project.ID, cluster.ID)
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+
+		for _, env := range envList {
+			err = populateOpenPullRequests(r.Context(), c.Config(), env, pullRequests)
+
+			if err != nil {
+				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+				return
+			}
+		}
+	} else {
+		depls, err := c.Repo().Environment().ListDeployments(req.EnvironmentID)
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+
+		for _, depl := range depls {
+			deployments = append(deployments, depl.ToDeploymentType())
+		}
+
+		env, err := c.Repo().Environment().ReadEnvironmentByID(project.ID, cluster.ID, req.EnvironmentID)
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+
+		err = populateOpenPullRequests(r.Context(), c.Config(), env, pullRequests)
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+	}
+
+	c.WriteResult(w, r, map[string]interface{}{
+		"pull_requests": pullRequests,
+		"deployments":   deployments,
+	})
+}
+
+func populateOpenPullRequests(
+	ctx context.Context,
+	config *config.Config,
+	env *models.Environment,
+	pullRequests []*types.PullRequest,
+) error {
+	client, err := getGithubClientFromEnvironment(config, env)
 
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-		return
+		return err
 	}
 
-	res := make([]*types.Deployment, 0)
+	openPRs, _, err := client.PullRequests.List(ctx, env.GitRepoOwner, env.GitRepoName,
+		&github.PullRequestListOptions{
+			ListOptions: github.ListOptions{
+				PerPage: 50,
+			},
+		},
+	)
+
+	if err != nil {
+		return err
+	}
 
-	for _, depl := range depls {
-		res = append(res, depl.ToDeploymentType())
+	for _, pr := range openPRs {
+		pullRequests = append(pullRequests, &types.PullRequest{
+			Title:      pr.GetTitle(),
+			Number:     uint(pr.GetNumber()),
+			RepoOwner:  pr.GetHead().GetRepo().GetOwner().GetName(),
+			RepoName:   pr.GetHead().GetRepo().GetName(),
+			BranchFrom: pr.GetHead().GetRef(),
+			BranchInto: pr.GetBase().GetRef(),
+		})
 	}
 
-	c.WriteResult(w, r, res)
+	return nil
 }

+ 22 - 22
api/server/handlers/webhook/github_incoming.go

@@ -89,33 +89,33 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 		if err != nil {
 			return err
 		}
-	}
-
-	depl, err := c.Repo().Environment().ReadDeploymentByGitDetails(
-		env.ID, owner, repo, uint(event.GetPullRequest().GetNumber()),
-	)
-
-	if err != nil {
-		return err
-	}
-
-	if depl.Status != "disabled" {
-		_, err := client.Actions.CreateWorkflowDispatchEventByFileName(
-			r.Context(), owner, repo, fmt.Sprintf("porter_%s_env.yml", env.Name),
-			github.CreateWorkflowDispatchEventRequest{
-				Ref: event.PullRequest.GetHead().GetRef(),
-				Inputs: map[string]interface{}{
-					"pr_number":      event.PullRequest.GetNumber(),
-					"pr_title":       event.PullRequest.GetTitle(),
-					"pr_branch_from": event.PullRequest.GetHead().GetRef(),
-					"pr_branch_into": event.PullRequest.GetBase().GetRef(),
-				},
-			},
+	} else if event.GetAction() == "synchronize" {
+		depl, err := c.Repo().Environment().ReadDeploymentByGitDetails(
+			env.ID, owner, repo, uint(event.GetPullRequest().GetNumber()),
 		)
 
 		if err != nil {
 			return err
 		}
+
+		if depl.Status != "disabled" {
+			_, err := client.Actions.CreateWorkflowDispatchEventByFileName(
+				r.Context(), owner, repo, fmt.Sprintf("porter_%s_env.yml", env.Name),
+				github.CreateWorkflowDispatchEventRequest{
+					Ref: event.PullRequest.GetHead().GetRef(),
+					Inputs: map[string]interface{}{
+						"pr_number":      event.PullRequest.GetNumber(),
+						"pr_title":       event.PullRequest.GetTitle(),
+						"pr_branch_from": event.PullRequest.GetHead().GetRef(),
+						"pr_branch_into": event.PullRequest.GetBase().GetRef(),
+					},
+				},
+			)
+
+			if err != nil {
+				return err
+			}
+		}
 	}
 
 	return nil

+ 10 - 1
api/types/environment.go

@@ -81,7 +81,7 @@ type UpdateDeploymentRequest struct {
 }
 
 type ListDeploymentRequest struct {
-	Status []string `schema:"status"`
+	EnvironmentID uint `schema:"environment_id"`
 }
 
 type UpdateDeploymentStatusRequest struct {
@@ -99,3 +99,12 @@ type DeleteDeploymentRequest struct {
 type GetDeploymentRequest struct {
 	Namespace string `schema:"namespace" form:"required"`
 }
+
+type PullRequest struct {
+	Title      string `json:"pr_title"`
+	Number     uint   `json:"pr_number"`
+	RepoOwner  string `json:"repo_owner"`
+	RepoName   string `json:"repo_name"`
+	BranchFrom string `json:"branch_from"`
+	BranchInto string `json:"branch_into"`
+}