| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package environment
- import (
- "context"
- "errors"
- "fmt"
- "net/http"
- "strings"
- "github.com/google/go-github/v41/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/commonutils"
- "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"
- "github.com/porter-dev/porter/internal/repository"
- "gorm.io/gorm"
- )
- 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)
- owner, name, ok := commonutils.GetOwnerAndNameParams(c, w, r)
- if !ok {
- return
- }
- request := &types.FinalizeDeploymentRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- if request.Namespace == "" && request.PRNumber == 0 {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("either namespace or pr_number must be present in request body"), http.StatusBadRequest,
- ))
- return
- }
- var err error
- // read the environment to get the environment id
- env, err := c.Repo().Environment().ReadEnvironment(project.ID, cluster.ID, uint(ga.InstallationID), owner, name)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrNotFound(errEnvironmentNotFound))
- return
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- var depl *models.Deployment
- // read the deployment
- if request.PRNumber != 0 {
- depl, err = c.Repo().Environment().ReadDeploymentByGitDetails(env.ID, owner, name, request.PRNumber)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
- return
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- } else if request.Namespace != "" {
- depl, err = c.Repo().Environment().ReadDeployment(env.ID, request.Namespace)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
- return
- }
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- }
- if depl == nil {
- c.HandleAPIError(w, r, apierrors.NewErrNotFound(errDeploymentNotFound))
- return
- }
- depl.Subdomain = request.Subdomain
- depl.Status = types.DeploymentStatusCreated
- depl.LastErrors = ""
- // 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
- }
- // Create new deployment status to indicate deployment is ready
- state := "success"
- env_url := depl.Subdomain
- deploymentStatusRequest := github.DeploymentStatusRequest{
- State: &state,
- EnvironmentURL: &env_url,
- }
- _, _, err = client.Repositories.CreateDeploymentStatus(
- context.Background(),
- env.GitRepoOwner,
- env.GitRepoName,
- depl.GHDeploymentID,
- &deploymentStatusRequest,
- )
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- if !depl.IsBranchDeploy() {
- // add a check for the PR to be open before creating a comment
- prClosed, err := isGithubPRClosed(client, owner, name, int(depl.PullRequestID))
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
- fmt.Errorf("error fetching details of github PR for deployment ID: %d. Error: %w",
- depl.ID, err), http.StatusConflict,
- ))
- return
- }
- if prClosed {
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Github PR has been closed"),
- http.StatusConflict))
- return
- }
- commentBody := "## Porter Preview Environments\n"
- if depl.Subdomain == "" {
- commentBody += fmt.Sprintf(
- "✅ The latest SHA ([`%s`](https://github.com/%s/%s/commit/%s)) has been successfully deployed.",
- depl.CommitSHA, depl.RepoOwner, depl.RepoName, depl.CommitSHA,
- )
- } else {
- commentBody += fmt.Sprintf(
- "✅ The latest SHA ([`%s`](https://github.com/%s/%s/commit/%s)) has been successfully deployed to %s",
- depl.CommitSHA, depl.RepoOwner, depl.RepoName, depl.CommitSHA, depl.Subdomain,
- )
- }
- err = createOrUpdateComment(client, c.Repo(), env.NewCommentsDisabled, depl, github.String(commentBody))
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- }
- c.WriteResult(w, r, depl.ToDeploymentType())
- }
- func createOrUpdateComment(
- client *github.Client,
- repo repository.Repository,
- newCommentsDisabled bool,
- depl *models.Deployment,
- commentBody *string,
- ) error {
- // when updating a PR comment, we have to handle several cases:
- // 1. when a Porter environment has deployment status repeat-comments enabled
- // - nothing special here, simply create a new comment in the PR
- // 2. when a Porter environment has deployment status repeat-comments disabled
- // - when a Porter deployment has Github comment ID saved in the DB
- // - try to update the comment using the Github comment ID
- // - if the above fails, try creating a new comment and save the new comment ID in the DB
- // - when a Porter deployment does not have a Github comment ID saved in the DB
- // - create a new comment and save the Github comment ID in the DB
- if newCommentsDisabled {
- if depl.GHPRCommentID == 0 {
- // create a new comment
- err := createGithubComment(client, repo, depl, commentBody)
- if err != nil {
- return err
- }
- } else {
- err := updateGithubComment(
- client, depl.RepoOwner, depl.RepoName, depl.GHPRCommentID, commentBody,
- )
- if err != nil {
- if strings.Contains(err.Error(), "404") {
- // perhaps a deleted comment?
- // create a new comment
- err := createGithubComment(client, repo, depl, commentBody)
- if err != nil {
- return fmt.Errorf("invalid github comment ID for deployment with ID: %d. Error creating "+
- "new comment: %w", depl.ID, err)
- }
- }
- return err
- }
- }
- } else {
- err := createGithubComment(client, repo, depl, commentBody)
- if err != nil {
- return err
- }
- }
- return nil
- }
- func createGithubComment(
- client *github.Client,
- repo repository.Repository,
- depl *models.Deployment,
- body *string,
- ) error {
- ghResp, _, err := client.Issues.CreateComment(
- context.Background(),
- depl.RepoOwner,
- depl.RepoName,
- int(depl.PullRequestID),
- &github.IssueComment{
- Body: body,
- },
- )
- if err != nil {
- return fmt.Errorf("error creating new github comment for owner: %s repo %s prNumber: %d. Error: %w",
- depl.RepoOwner, depl.RepoName, depl.PullRequestID, err)
- }
- depl.GHPRCommentID = ghResp.GetID()
- _, err = repo.Environment().UpdateDeployment(depl)
- if err != nil {
- return fmt.Errorf("error updating deployment with ID: %d. Error: %w", depl.ID, err)
- }
- return nil
- }
- func updateGithubComment(
- client *github.Client,
- owner, repo string,
- commentID int64,
- body *string,
- ) error {
- _, _, err := client.Issues.EditComment(
- context.Background(),
- owner,
- repo,
- commentID,
- &github.IssueComment{
- Body: body,
- },
- )
- return err
- }
|