| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package environment
- import (
- "context"
- "net/http"
- "github.com/google/go-github/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"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- )
- type FinalizeDeploymentHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewFinalizeDeploymentHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *FinalizeDeploymentHandler {
- return &FinalizeDeploymentHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- ga, _ := r.Context().Value(types.GitInstallationScope).(*integrations.GithubAppInstallation)
- project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
- request := &types.FinalizeDeploymentRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- // read the environment to get the environment id
- env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID))
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // read the deployment
- depl, err := c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- depl.Subdomain = request.Subdomain
- depl.Status = "created"
- // update the deployment
- depl, err = c.Repo().Environment().UpdateDeployment(depl)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- client, err := getGithubClientFromEnvironment(c.Config(), env)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- // write comment in PR
- commentBody := "(TENTATIVE) Porter is deploying this pull request..."
- prComment := github.IssueComment{
- Body: &commentBody,
- User: &github.User{},
- }
- _, _, err = client.Issues.CreateComment(
- context.Background(),
- env.GitRepoOwner,
- env.GitRepoName,
- int(depl.PullRequestID),
- &prComment,
- )
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- c.WriteResult(w, r, depl.ToDeploymentType())
- }
|