Explorar o código

progressing the api

Feroze Mohideen %!s(int64=3) %!d(string=hai) anos
pai
achega
3f4dbe48d6

+ 3 - 13
api/server/handlers/stacks/create_secret_and_open_pr.go

@@ -11,7 +11,6 @@ import (
 	"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"
-	"github.com/porter-dev/porter/api/server/shared/commonutils"
 	"github.com/porter-dev/porter/api/server/shared/config"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/auth/token"
@@ -34,25 +33,16 @@ func NewOpenStackPRHandler(
 }
 
 func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	gaid := c.Config().GithubAppConf.AppID
 	user, _ := r.Context().Value(types.UserScope).(*models.User)
 	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
 
-	owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
-	if !ok {
-		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("unable to get github owner and name params")))
-		return
-	}
-
-	// create the environment
 	request := &types.CreateSecretAndOpenGitHubPullRequest{}
-
 	if ok := c.DecodeAndValidate(w, r, request); !ok {
 		return
 	}
 
-	client, err := getGithubClient(c.Config(), gaid)
+	client, err := getGithubClient(c.Config(), request.GithubAppInstallationID)
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
@@ -73,8 +63,8 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	if request.OpenPr {
 		err = actions.OpenGithubPR(&actions.GithubPROpts{
 			Client:       client,
-			GitRepoOwner: owner,
-			GitRepoName:  name,
+			GitRepoOwner: request.GithubRepoOwner,
+			GitRepoName:  request.GithubRepoName,
 			StackName:    request.StackName,
 			ProjectID:    project.ID,
 			ClusterID:    cluster.ID,

+ 5 - 2
api/types/stack.go

@@ -16,6 +16,9 @@ type Dependency struct {
 }
 
 type CreateSecretAndOpenGitHubPullRequest struct {
-	OpenPr    bool `json:"open_pr"`
-	StackName string
+	StackName               string `json:"stack_name" form:"required,dns1123"`
+	GithubAppInstallationID int64  `json:"github_app_installation_id" form:"required"`
+	GithubRepoOwner         string `json:"github_repo_owner" form:"required"`
+	GithubRepoName          string `json:"github_repo_name" form:"required"`
+	OpenPr                  bool   `json:"open_pr" form:"required"`
 }

+ 6 - 17
internal/integrations/ci/actions/stack.go

@@ -17,6 +17,7 @@ type GithubPROpts struct {
 	ProjectID, ClusterID      uint
 	PorterToken               string
 	ServerURL                 string
+	DefaultBranch             string
 }
 
 type GetStackApplyActionYAMLOpts struct {
@@ -41,24 +42,12 @@ func OpenGithubPR(opts *GithubPROpts) error {
 		return err
 	}
 
-	// get the repository to find the default branch
-	repo, _, err := opts.Client.Repositories.Get(
-		context.TODO(),
-		opts.GitRepoOwner,
-		opts.GitRepoName,
-	)
-	if err != nil {
-		return err
-	}
-
-	defaultBranch := repo.GetDefaultBranch()
-
 	applyWorkflowYAML, err := getStackApplyActionYAML(&GetStackApplyActionYAMLOpts{
 		ServerURL:     opts.ServerURL,
 		ClusterID:     opts.ClusterID,
 		ProjectID:     opts.ProjectID,
 		StackName:     opts.StackName,
-		DefaultBranch: defaultBranch,
+		DefaultBranch: opts.DefaultBranch,
 		SecretName:    secretName,
 	})
 	if err != nil {
@@ -68,7 +57,7 @@ func OpenGithubPR(opts *GithubPROpts) error {
 	err = createNewBranch(opts.Client,
 		opts.GitRepoOwner,
 		opts.GitRepoName,
-		defaultBranch,
+		opts.DefaultBranch,
 		"porter-stack")
 	if err != nil {
 		return fmt.Errorf(
@@ -76,7 +65,7 @@ func OpenGithubPR(opts *GithubPROpts) error {
 				"To enable Porter Preview Environment deployments, please create Github workflow "+
 				"files in this branch with the following contents:\n"+
 				"--------\n%s--------\nERROR: %w",
-			defaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
+			opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
 		)
 	}
 
@@ -93,14 +82,14 @@ func OpenGithubPR(opts *GithubPROpts) error {
 				"To enable Porter Preview Environment deployments, please create Github workflow "+
 				"files in this branch with the following contents:\n"+
 				"--------\n%s--------\nERROR: %w",
-			defaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
+			opts.DefaultBranch, string(applyWorkflowYAML), ErrCreatePRForProtectedBranch,
 		)
 	}
 
 	_, _, err = opts.Client.PullRequests.Create(
 		context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
 			Title: github.String("Enable Porter Preview Environment deployments"),
-			Base:  github.String(defaultBranch),
+			Base:  github.String(opts.DefaultBranch),
 			Head:  github.String("porter-preview"),
 		},
 	)