Просмотр исходного кода

Merge branch 'nafees/preview-env-new-endpoints' of github.com:porter-dev/porter into nico/preview-envs-frontend-improvements

jnfrati 4 лет назад
Родитель
Сommit
8316b02e97

+ 6 - 1
api/server/handlers/environment/enable_pull_request.go

@@ -47,7 +47,7 @@ func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		return
 		return
 	}
 	}
 
 
-	_, err = client.Actions.CreateWorkflowDispatchEventByFileName(
+	ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		github.CreateWorkflowDispatchEventRequest{
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: request.BranchFrom,
 			Ref: request.BranchFrom,
@@ -60,6 +60,11 @@ func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		},
 		},
 	)
 	)
 
 
+	if ghResp.StatusCode == 404 {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("workflow file not found"), 404))
+		return
+	}
+
 	if err != nil {
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 		return

+ 20 - 11
api/server/handlers/environment/list_deployments_by_cluster.go

@@ -60,12 +60,14 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 		}
 		}
 
 
 		for _, env := range envList {
 		for _, env := range envList {
-			err = populateOpenPullRequests(r.Context(), c.Config(), env, pullRequests)
+			prs, err := populateOpenPullRequests(r.Context(), c.Config(), env)
 
 
 			if err != nil {
 			if err != nil {
 				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 				return
 				return
 			}
 			}
+
+			pullRequests = append(pullRequests, prs...)
 		}
 		}
 	} else {
 	} else {
 		depls, err := c.Repo().Environment().ListDeployments(req.EnvironmentID)
 		depls, err := c.Repo().Environment().ListDeployments(req.EnvironmentID)
@@ -86,12 +88,14 @@ func (c *ListDeploymentsByClusterHandler) ServeHTTP(w http.ResponseWriter, r *ht
 			return
 			return
 		}
 		}
 
 
-		err = populateOpenPullRequests(r.Context(), c.Config(), env, pullRequests)
+		prs, err := populateOpenPullRequests(r.Context(), c.Config(), env)
 
 
 		if err != nil {
 		if err != nil {
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 			return
 			return
 		}
 		}
+
+		pullRequests = append(pullRequests, prs...)
 	}
 	}
 
 
 	c.WriteResult(w, r, map[string]interface{}{
 	c.WriteResult(w, r, map[string]interface{}{
@@ -104,15 +108,14 @@ func populateOpenPullRequests(
 	ctx context.Context,
 	ctx context.Context,
 	config *config.Config,
 	config *config.Config,
 	env *models.Environment,
 	env *models.Environment,
-	pullRequests []*types.PullRequest,
-) error {
+) ([]*types.PullRequest, error) {
 	client, err := getGithubClientFromEnvironment(config, env)
 	client, err := getGithubClientFromEnvironment(config, env)
 
 
 	if err != nil {
 	if err != nil {
-		return err
+		return nil, err
 	}
 	}
 
 
-	openPRs, _, err := client.PullRequests.List(ctx, env.GitRepoOwner, env.GitRepoName,
+	openPRs, resp, err := client.PullRequests.List(ctx, env.GitRepoOwner, env.GitRepoName,
 		&github.PullRequestListOptions{
 		&github.PullRequestListOptions{
 			ListOptions: github.ListOptions{
 			ListOptions: github.ListOptions{
 				PerPage: 50,
 				PerPage: 50,
@@ -120,20 +123,26 @@ func populateOpenPullRequests(
 		},
 		},
 	)
 	)
 
 
+	var prs []*types.PullRequest
+
+	if resp.StatusCode == 404 {
+		return prs, nil
+	}
+
 	if err != nil {
 	if err != nil {
-		return err
+		return nil, err
 	}
 	}
 
 
 	for _, pr := range openPRs {
 	for _, pr := range openPRs {
-		pullRequests = append(pullRequests, &types.PullRequest{
+		prs = append(prs, &types.PullRequest{
 			Title:      pr.GetTitle(),
 			Title:      pr.GetTitle(),
 			Number:     uint(pr.GetNumber()),
 			Number:     uint(pr.GetNumber()),
-			RepoOwner:  pr.GetHead().GetRepo().GetOwner().GetName(),
-			RepoName:   pr.GetHead().GetRepo().GetName(),
+			RepoOwner:  env.GitRepoOwner,
+			RepoName:   env.GitRepoName,
 			BranchFrom: pr.GetHead().GetRef(),
 			BranchFrom: pr.GetHead().GetRef(),
 			BranchInto: pr.GetBase().GetRef(),
 			BranchInto: pr.GetBase().GetRef(),
 		})
 		})
 	}
 	}
 
 
-	return nil
+	return prs, nil
 }
 }

+ 6 - 1
api/server/handlers/environment/reenable_deployment.go

@@ -64,7 +64,7 @@ func (c *ReenableDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 		return
 	}
 	}
 
 
-	_, err = client.Actions.CreateWorkflowDispatchEventByFileName(
+	ghResp, err := client.Actions.CreateWorkflowDispatchEventByFileName(
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		r.Context(), env.GitRepoOwner, env.GitRepoName, fmt.Sprintf("porter_%s_env.yml", env.Name),
 		github.CreateWorkflowDispatchEventRequest{
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: depl.PRBranchFrom,
 			Ref: depl.PRBranchFrom,
@@ -77,6 +77,11 @@ func (c *ReenableDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		},
 		},
 	)
 	)
 
 
+	if ghResp.StatusCode == 404 {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("workflow file not found"), 404))
+		return
+	}
+
 	if err != nil {
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
 		return

+ 3 - 0
api/server/router/cluster.go

@@ -386,6 +386,7 @@ func getClusterRoutes(
 					RelativePath: relPath + "/deployments/{deployment_id}/reenable",
 					RelativePath: relPath + "/deployments/{deployment_id}/reenable",
 				},
 				},
 				Scopes: []types.PermissionScope{
 				Scopes: []types.PermissionScope{
+					types.UserScope,
 					types.ProjectScope,
 					types.ProjectScope,
 					types.ClusterScope,
 					types.ClusterScope,
 				},
 				},
@@ -414,6 +415,7 @@ func getClusterRoutes(
 					RelativePath: relPath + "/deployments/pull_request",
 					RelativePath: relPath + "/deployments/pull_request",
 				},
 				},
 				Scopes: []types.PermissionScope{
 				Scopes: []types.PermissionScope{
+					types.UserScope,
 					types.ProjectScope,
 					types.ProjectScope,
 					types.ClusterScope,
 					types.ClusterScope,
 				},
 				},
@@ -443,6 +445,7 @@ func getClusterRoutes(
 					RelativePath: relPath + "/deployments/{deployment_id}",
 					RelativePath: relPath + "/deployments/{deployment_id}",
 				},
 				},
 				Scopes: []types.PermissionScope{
 				Scopes: []types.PermissionScope{
+					types.UserScope,
 					types.ProjectScope,
 					types.ProjectScope,
 					types.ClusterScope,
 					types.ClusterScope,
 				},
 				},

+ 1 - 1
api/types/request.go

@@ -26,7 +26,7 @@ const (
 	HTTPVerbGet    HTTPVerb = "GET"
 	HTTPVerbGet    HTTPVerb = "GET"
 	HTTPVerbPost   HTTPVerb = "POST"
 	HTTPVerbPost   HTTPVerb = "POST"
 	HTTPVerbPut    HTTPVerb = "PUT"
 	HTTPVerbPut    HTTPVerb = "PUT"
-	HTTPVerbPatch  HTTPVerb = "PUT"
+	HTTPVerbPatch  HTTPVerb = "PATCH"
 	HTTPVerbDelete HTTPVerb = "DELETE"
 	HTTPVerbDelete HTTPVerb = "DELETE"
 )
 )
 
 

+ 1 - 1
internal/integrations/ci/actions/preview.go

@@ -231,7 +231,7 @@ func getPreviewApplyActionYAML(opts *EnvOpts) ([]byte, error) {
 			opts.GitInstallationID,
 			opts.GitInstallationID,
 			opts.GitRepoOwner,
 			opts.GitRepoOwner,
 			opts.GitRepoName,
 			opts.GitRepoName,
-			"v0.1.0",
+			"v0.2.0",
 		),
 		),
 	}
 	}