|
@@ -4,6 +4,7 @@ import (
|
|
|
"context"
|
|
"context"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
|
|
+ "strings"
|
|
|
|
|
|
|
|
"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"
|
|
@@ -14,6 +15,7 @@ import (
|
|
|
"github.com/porter-dev/porter/api/types"
|
|
"github.com/porter-dev/porter/api/types"
|
|
|
"github.com/porter-dev/porter/internal/models"
|
|
"github.com/porter-dev/porter/internal/models"
|
|
|
"github.com/porter-dev/porter/internal/models/integrations"
|
|
"github.com/porter-dev/porter/internal/models/integrations"
|
|
|
|
|
+ "github.com/porter-dev/porter/internal/repository"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type FinalizeDeploymentHandler struct {
|
|
type FinalizeDeploymentHandler struct {
|
|
@@ -146,20 +148,119 @@ func (c *FinalizeDeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- _, _, err = client.Issues.CreateComment(
|
|
|
|
|
|
|
+ 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(),
|
|
context.Background(),
|
|
|
- env.GitRepoOwner,
|
|
|
|
|
- env.GitRepoName,
|
|
|
|
|
|
|
+ depl.RepoOwner,
|
|
|
|
|
+ depl.RepoName,
|
|
|
int(depl.PullRequestID),
|
|
int(depl.PullRequestID),
|
|
|
&github.IssueComment{
|
|
&github.IssueComment{
|
|
|
- Body: github.String(commentBody),
|
|
|
|
|
|
|
+ Body: body,
|
|
|
},
|
|
},
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ return fmt.Errorf("error creating new github comment for owner: %s repo %s prNumber: %d. Error: %w",
|
|
|
|
|
+ depl.RepoOwner, depl.RepoName, depl.PullRequestID, err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- c.WriteResult(w, r, depl.ToDeploymentType())
|
|
|
|
|
|
|
+ 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
|
|
|
}
|
|
}
|