| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package environment
- import (
- "net/http"
- "strconv"
- ghinstallation "github.com/bradleyfalzon/ghinstallation/v2"
- "github.com/google/go-github/v41/github"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
- "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/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/auth/token"
- "github.com/porter-dev/porter/internal/integrations/ci/actions"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- )
- type CreateEnvironmentHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewCreateEnvironmentHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *CreateEnvironmentHandler {
- return &CreateEnvironmentHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
- 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 := gitinstallation.GetOwnerAndNameParams(c, w, r)
- if !ok {
- return
- }
- // create the environment
- request := &types.CreateEnvironmentRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- env, err := c.Repo().Environment().CreateEnvironment(&models.Environment{
- ProjectID: project.ID,
- ClusterID: cluster.ID,
- GitInstallationID: uint(ga.InstallationID),
- Name: request.Name,
- GitRepoOwner: owner,
- GitRepoName: name,
- })
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // write Github actions files to the repo
- client, err := getGithubClientFromEnvironment(c.Config(), env)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // generate porter jwt token
- jwt, err := token.GetTokenForAPI(user.ID, project.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- encoded, err := jwt.EncodeToken(c.Config().TokenConf)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- err = actions.SetupEnv(&actions.EnvOpts{
- Client: client,
- ServerURL: c.Config().ServerConf.ServerURL,
- PorterToken: encoded,
- GitRepoOwner: owner,
- GitRepoName: name,
- ProjectID: project.ID,
- ClusterID: cluster.ID,
- GitInstallationID: uint(ga.InstallationID),
- EnvironmentName: request.Name,
- })
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- c.WriteResult(w, r, env.ToEnvironmentType())
- }
- func getGithubClientFromEnvironment(config *config.Config, env *models.Environment) (*github.Client, error) {
- // get the github app client
- ghAppId, err := strconv.Atoi(config.ServerConf.GithubAppID)
- if err != nil {
- return nil, err
- }
- // authenticate as github app installation
- itr, err := ghinstallation.NewKeyFromFile(
- http.DefaultTransport,
- int64(ghAppId),
- int64(env.GitInstallationID),
- config.ServerConf.GithubAppSecretPath,
- )
- if err != nil {
- return nil, err
- }
- return github.NewClient(&http.Client{Transport: itr}), nil
- }
|