Răsfoiți Sursa

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

jnfrati 4 ani în urmă
părinte
comite
dee2d61019

+ 15 - 8
api/server/handlers/environment/create.go

@@ -63,7 +63,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	})
 	})
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
@@ -71,7 +71,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	client, err := getGithubClientFromEnvironment(c.Config(), env)
 	client, err := getGithubClientFromEnvironment(c.Config(), env)
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
@@ -80,7 +80,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	)
 	)
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
@@ -95,7 +95,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 			)
 			)
 
 
 			if err != nil {
 			if err != nil {
-				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+				c.deleteEnvAndReportError(w, r, env, err)
 				return
 				return
 			}
 			}
 
 
@@ -117,7 +117,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	)
 	)
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
@@ -125,14 +125,14 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	jwt, err := token.GetTokenForAPI(user.ID, project.ID)
 	jwt, err := token.GetTokenForAPI(user.ID, project.ID)
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
 	encoded, err := jwt.EncodeToken(c.Config().TokenConf)
 	encoded, err := jwt.EncodeToken(c.Config().TokenConf)
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
@@ -149,13 +149,20 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	})
 	})
 
 
 	if err != nil {
 	if err != nil {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		c.deleteEnvAndReportError(w, r, env, err)
 		return
 		return
 	}
 	}
 
 
 	c.WriteResult(w, r, env.ToEnvironmentType())
 	c.WriteResult(w, r, env.ToEnvironmentType())
 }
 }
 
 
+func (c *CreateEnvironmentHandler) deleteEnvAndReportError(
+	w http.ResponseWriter, r *http.Request, env *models.Environment, err error,
+) {
+	c.Repo().Environment().DeleteEnvironment(env)
+	c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+}
+
 func getGithubClientFromEnvironment(config *config.Config, env *models.Environment) (*github.Client, error) {
 func getGithubClientFromEnvironment(config *config.Config, env *models.Environment) (*github.Client, error) {
 	// get the github app client
 	// get the github app client
 	ghAppId, err := strconv.Atoi(config.ServerConf.GithubAppID)
 	ghAppId, err := strconv.Atoi(config.ServerConf.GithubAppID)

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

@@ -121,7 +121,7 @@ func createDeployment(
 
 
 	deploymentRequest := github.DeploymentRequest{
 	deploymentRequest := github.DeploymentRequest{
 		Ref:              github.String(branchFrom),
 		Ref:              github.String(branchFrom),
-		Environment:      github.String("Preview"),
+		Environment:      github.String(env.Name),
 		AutoMerge:        github.Bool(false),
 		AutoMerge:        github.Bool(false),
 		RequiredContexts: &requiredContexts,
 		RequiredContexts: &requiredContexts,
 	}
 	}

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

@@ -3,6 +3,7 @@ package environment
 import (
 import (
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
+	"strconv"
 
 
 	"github.com/google/go-github/v41/github"
 	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -52,7 +53,7 @@ func (c *EnablePullRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		github.CreateWorkflowDispatchEventRequest{
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: request.BranchFrom,
 			Ref: request.BranchFrom,
 			Inputs: map[string]interface{}{
 			Inputs: map[string]interface{}{
-				"pr_number":      request.Number,
+				"pr_number":      strconv.FormatUint(uint64(request.Number), 10),
 				"pr_title":       request.Title,
 				"pr_title":       request.Title,
 				"pr_branch_from": request.BranchFrom,
 				"pr_branch_from": request.BranchFrom,
 				"pr_branch_into": request.BranchInto,
 				"pr_branch_into": request.BranchInto,

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

@@ -3,6 +3,7 @@ package environment
 import (
 import (
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
+	"strconv"
 
 
 	"github.com/google/go-github/v41/github"
 	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -69,7 +70,7 @@ func (c *ReenableDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		github.CreateWorkflowDispatchEventRequest{
 		github.CreateWorkflowDispatchEventRequest{
 			Ref: depl.PRBranchFrom,
 			Ref: depl.PRBranchFrom,
 			Inputs: map[string]interface{}{
 			Inputs: map[string]interface{}{
-				"pr_number":      depl.PullRequestID,
+				"pr_number":      strconv.FormatUint(uint64(depl.PullRequestID), 10),
 				"pr_title":       depl.PRName,
 				"pr_title":       depl.PRName,
 				"pr_branch_from": depl.PRBranchFrom,
 				"pr_branch_from": depl.PRBranchFrom,
 				"pr_branch_into": depl.PRBranchInto,
 				"pr_branch_into": depl.PRBranchInto,

+ 2 - 1
api/server/handlers/gitinstallation/get_permissions.go

@@ -38,6 +38,7 @@ func (c *GithubGetPermissionsHandler) ServeHTTP(w http.ResponseWriter, r *http.R
 		PreviewEnvironments: p.Administration == "write" &&
 		PreviewEnvironments: p.Administration == "write" &&
 			p.Deployments == "write" &&
 			p.Deployments == "write" &&
 			p.Environments == "write" &&
 			p.Environments == "write" &&
-			p.PullRequests == "write",
+			p.PullRequests == "write" &&
+			p.RepositoryWebhook == "write",
 	})
 	})
 }
 }

+ 20 - 18
api/server/handlers/gitinstallation/helpers.go

@@ -77,15 +77,16 @@ func GetGithubAppClientFromRequest(config *config.Config, r *http.Request) (*git
 }
 }
 
 
 type GithubAppPermissions struct {
 type GithubAppPermissions struct {
-	Actions        string
-	Administration string
-	Contents       string
-	Deployments    string
-	Environments   string
-	Metadata       string
-	PullRequests   string
-	Secrets        string
-	Workflows      string
+	Actions           string
+	Administration    string
+	Contents          string
+	Deployments       string
+	Environments      string
+	Metadata          string
+	PullRequests      string
+	Secrets           string
+	Workflows         string
+	RepositoryWebhook string
 }
 }
 
 
 // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
 // GetGithubAppClientFromRequest gets the github app installation id from the request and authenticates
@@ -115,15 +116,16 @@ func GetGithubAppPermissions(config *config.Config, r *http.Request) (*GithubApp
 	permissions, err := itr.Permissions()
 	permissions, err := itr.Permissions()
 
 
 	return &GithubAppPermissions{
 	return &GithubAppPermissions{
-		Actions:        permissionToString(permissions.Actions),
-		Administration: permissionToString(permissions.Administration),
-		Contents:       permissionToString(permissions.Contents),
-		Deployments:    permissionToString(permissions.Deployments),
-		Environments:   permissionToString(permissions.Environments),
-		Metadata:       permissionToString(permissions.Metadata),
-		PullRequests:   permissionToString(permissions.PullRequests),
-		Secrets:        permissionToString(permissions.Secrets),
-		Workflows:      permissionToString(permissions.Workflows),
+		Actions:           permissionToString(permissions.Actions),
+		Administration:    permissionToString(permissions.Administration),
+		Contents:          permissionToString(permissions.Contents),
+		Deployments:       permissionToString(permissions.Deployments),
+		Environments:      permissionToString(permissions.Environments),
+		Metadata:          permissionToString(permissions.Metadata),
+		PullRequests:      permissionToString(permissions.PullRequests),
+		Secrets:           permissionToString(permissions.Secrets),
+		Workflows:         permissionToString(permissions.Workflows),
+		RepositoryWebhook: permissionToString(permissions.RepositoryHooks),
 	}, err
 	}, err
 }
 }
 
 

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

@@ -79,7 +79,7 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 			github.CreateWorkflowDispatchEventRequest{
 			github.CreateWorkflowDispatchEventRequest{
 				Ref: event.PullRequest.GetHead().GetRef(),
 				Ref: event.PullRequest.GetHead().GetRef(),
 				Inputs: map[string]interface{}{
 				Inputs: map[string]interface{}{
-					"pr_number":      event.PullRequest.GetNumber(),
+					"pr_number":      strconv.FormatUint(uint64(event.PullRequest.GetNumber()), 10),
 					"pr_title":       event.PullRequest.GetTitle(),
 					"pr_title":       event.PullRequest.GetTitle(),
 					"pr_branch_from": event.PullRequest.GetHead().GetRef(),
 					"pr_branch_from": event.PullRequest.GetHead().GetRef(),
 					"pr_branch_into": event.PullRequest.GetBase().GetRef(),
 					"pr_branch_into": event.PullRequest.GetBase().GetRef(),
@@ -105,7 +105,7 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 				github.CreateWorkflowDispatchEventRequest{
 				github.CreateWorkflowDispatchEventRequest{
 					Ref: event.PullRequest.GetHead().GetRef(),
 					Ref: event.PullRequest.GetHead().GetRef(),
 					Inputs: map[string]interface{}{
 					Inputs: map[string]interface{}{
-						"pr_number":      event.PullRequest.GetNumber(),
+						"pr_number":      strconv.FormatUint(uint64(event.PullRequest.GetNumber()), 10),
 						"pr_title":       event.PullRequest.GetTitle(),
 						"pr_title":       event.PullRequest.GetTitle(),
 						"pr_branch_from": event.PullRequest.GetHead().GetRef(),
 						"pr_branch_from": event.PullRequest.GetHead().GetRef(),
 						"pr_branch_into": event.PullRequest.GetBase().GetRef(),
 						"pr_branch_into": event.PullRequest.GetBase().GetRef(),