Skip to content
Merged

segment #2304

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions backend/controllers/github_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package controllers

import (
"fmt"
"log/slog"
"net/http"
"strconv"
"strings"

"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/segment"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/ci/github"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"log/slog"
"net/http"
"strconv"
"strings"
)

func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
Expand Down Expand Up @@ -111,7 +113,7 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
"externalId", externalId,
)

org, err := models.DB.CreateOrganisation(name, "digger", externalId)
org, err := models.DB.CreateOrganisation(name, "digger", externalId, nil)
if err != nil {
slog.Error("Error creating organization",
"name", name,
Expand Down Expand Up @@ -151,6 +153,13 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
org := link.Organisation
orgId := link.OrganisationId

var vcsOwner string = ""
if installation.Account.Login != nil {
vcsOwner = *installation.Account.Login
}
// we have multiple repos here, we don't really want to send an track event for each repo, so we just send the vcs owner
segment.Track(*org, vcsOwner, "", "github", "vcs_repo_installed", map[string]string{})

// create a github installation link (org ID matched to installation ID)
_, err = models.DB.CreateGithubInstallationLink(org, installationId64)
if err != nil {
Expand Down
36 changes: 28 additions & 8 deletions backend/controllers/github_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package controllers
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"runtime/debug"
"strconv"
"strings"

"github.com/diggerhq/digger/backend/ci_backends"
config2 "github.com/diggerhq/digger/backend/config"
locking2 "github.com/diggerhq/digger/backend/locking"
Expand All @@ -18,11 +24,6 @@ import (
"github.com/diggerhq/digger/libs/scheduler"
"github.com/google/go-github/v61/github"
"github.com/samber/lo"
"log/slog"
"os"
"runtime/debug"
"strconv"
"strings"
)

func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.IssueCommentEvent, ciBackendProvider ci_backends.CiBackendProvider, appId int64, postCommentHooks []IssueCommentHook) error {
Expand Down Expand Up @@ -55,6 +56,12 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
isDraft := payload.Issue.GetDraft()
userCommentId := *payload.GetComment().ID
actor := *payload.Sender.Login
var vcsActorID string = ""
if payload.Sender != nil && payload.Sender.Email != nil {
vcsActorID = *payload.Sender.Email
} else if payload.Sender != nil && payload.Sender.Login != nil {
vcsActorID = *payload.Sender.Login
}
commentBody := *payload.Comment.Body
defaultBranch := *payload.Repo.DefaultBranch
isPullRequest := payload.Issue.IsPullRequest()
Expand Down Expand Up @@ -145,6 +152,15 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
"orgId", orgId,
)

org, err := models.DB.GetOrganisationById(orgId)
if err != nil || org == nil {
slog.Error("Error getting organisation",
"orgId", orgId,
"error", err)
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to get organisation by ID"))
return fmt.Errorf("error getting organisation")
}

diggerYmlStr, ghService, config, projectsGraph, prSourceBranch, commitSha, changedFiles, err := getDiggerConfigForPR(gh, orgId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, issueNumber)
if err != nil {
slog.Error("Error getting Digger config for PR",
Expand Down Expand Up @@ -445,6 +461,9 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
return nil
}

// If we reach here then we have created a comment that would have led to more events
segment.Track(*org, repoOwner, vcsActorID, "github", "issue_digger_comment", map[string]string{"comment": commentBody})

err = utils.SetPRStatusForJobs(ghService, issueNumber, jobs)
if err != nil {
slog.Error("Error setting status for PR",
Expand Down Expand Up @@ -583,12 +602,9 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
commentReporterManager.UpdateComment(fmt.Sprintf(":x: UpdateDiggerBatch error: %v", err))
return fmt.Errorf("error updating digger batch")
}

slog.Debug("Successfully updated batch with source details", "batchId", batchId)
}

segment.Track(strconv.Itoa(int(orgId)), "backend_trigger_job")

slog.Info("Getting CI backend",
"issueNumber", issueNumber,
"repoFullName", repoFullName,
Expand All @@ -614,6 +630,10 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
return fmt.Errorf("error fetching ci backed %v", err)
}

segment.Track(*org, repoOwner, vcsActorID, "github", "backend_trigger_job", map[string]string{
"comment": commentBody,
})

slog.Info("Triggering Digger jobs",
"issueNumber", issueNumber,
"batchId", batchId,
Expand Down
41 changes: 34 additions & 7 deletions backend/controllers/github_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package controllers
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"runtime/debug"
"slices"
"strconv"
"time"

"github.com/diggerhq/digger/backend/ci_backends"
config2 "github.com/diggerhq/digger/backend/config"
locking2 "github.com/diggerhq/digger/backend/locking"
Expand All @@ -16,12 +23,6 @@ import (
"github.com/diggerhq/digger/libs/scheduler"
"github.com/google/go-github/v61/github"
"github.com/samber/lo"
"log/slog"
"os"
"runtime/debug"
"slices"
"strconv"
"time"
)

func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullRequestEvent, ciBackendProvider ci_backends.CiBackendProvider, appId int64) error {
Expand Down Expand Up @@ -49,6 +50,12 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
branch := payload.PullRequest.Head.GetRef()
action := *payload.Action
labels := payload.PullRequest.Labels
var vcsActorId string = ""
if payload.Sender != nil && payload.Sender.Email != nil {
vcsActorId = *payload.Sender.Email
} else if payload.Sender != nil && payload.Sender.Login != nil {
vcsActorId = *payload.Sender.Login
}
prLabelsStr := lo.Map(labels, func(label *github.Label, i int) string {
return *label.Name
})
Expand Down Expand Up @@ -141,6 +148,15 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
}
}

org, err := models.DB.GetOrganisationById(organisationId)
if err != nil || org == nil {
slog.Error("Error getting organisation",
"orgId", organisationId,
"error", err)
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to get organisation by ID"))
return fmt.Errorf("error getting organisation")
}

diggerYmlStr, ghService, config, projectsGraph, _, _, changedFiles, err := getDiggerConfigForPR(gh, organisationId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, prNumber)
if err != nil {
slog.Error("Error getting Digger config for PR",
Expand Down Expand Up @@ -244,6 +260,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"commands", jobsForImpactedProjects[0].Commands,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not determine digger command from job: %v", err))
return fmt.Errorf("unknown digger command in comment %v", err)
}
Expand Down Expand Up @@ -340,12 +357,16 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
return nil
}

// a pull request has led to jobs which would be triggered (ignoring closed event by here)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request", map[string]string{})

commentReporter, err := commentReporterManager.UpdateComment(":construction_worker: Digger starting... Config loaded successfully")
if err != nil {
slog.Error("Error initializing comment reporter",
"prNumber", prNumber,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
return fmt.Errorf("error initializing comment reporter")
}

Expand All @@ -355,6 +376,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"prNumber", prNumber,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: error setting status for PR: %v", err))
return fmt.Errorf("error setting status for PR: %v", err)
}
Expand Down Expand Up @@ -415,6 +437,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"commentId", commentReporter.CommentId,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not handle commentId: %v", err))
}

Expand All @@ -427,6 +450,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"prNumber", prNumber,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not post ai comment summary comment id: %v", err))
return fmt.Errorf("could not post ai summary comment: %v", err)
}
Expand Down Expand Up @@ -470,6 +494,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"prNumber", prNumber,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: ConvertJobsToDiggerJobs error: %v", err))
return fmt.Errorf("error converting jobs")
}
Expand Down Expand Up @@ -525,7 +550,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
slog.Debug("Successfully updated batch with source details", "batchId", batchId)
}

segment.Track(strconv.Itoa(int(organisationId)), "backend_trigger_job")
segment.Track(*org, repoOwner, vcsActorId, "github", "backend_trigger_job", map[string]string{})

slog.Info("Getting CI backend",
"prNumber", prNumber,
Expand All @@ -548,6 +573,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"repoFullName", repoFullName,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: GetCiBackend error: %v", err))
return fmt.Errorf("error fetching ci backed %v", err)
}
Expand All @@ -565,6 +591,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"batchId", batchId,
"error", err,
)
segment.Track(*org, repoOwner, vcsActorId, "github", "pull_request_ERROR", map[string]string{"error": err.Error()})
commentReporterManager.UpdateComment(fmt.Sprintf(":x: TriggerDiggerJobs error: %v", err))
return fmt.Errorf("error triggering Digger Jobs")
}
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
orgTenantId := "11111111-1111-1111-1111-111111111111"
externalSource := "test"
orgName := "testOrg"
org, err := database.CreateOrganisation(orgName, externalSource, orgTenantId)
org, err := database.CreateOrganisation(orgName, externalSource, orgTenantId, nil)
if err != nil {
panic(err)
}
Expand Down
24 changes: 14 additions & 10 deletions backend/controllers/internal_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"net/http"

"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/segment"
"github.com/gin-gonic/gin"
)

func (d DiggerController) UpsertOrgInternal(c *gin.Context) {
type OrgCreateRequest struct {
Name string `json:"org_name"`
ExternalSource string `json:"external_source"`
ExternalId string `json:"external_id"`
Name string `json:"org_name"`
ExternalSource string `json:"external_source"`
ExternalId string `json:"external_id"`
AdminEmail *string `json:"admin_email,omitempty"`
}

var request OrgCreateRequest
Expand All @@ -26,6 +28,7 @@ func (d DiggerController) UpsertOrgInternal(c *gin.Context) {
name := request.Name
externalSource := request.ExternalSource
externalId := request.ExternalId
adminEmail := request.AdminEmail

slog.Info("Creating or updating organization",
"name", name,
Expand All @@ -42,13 +45,12 @@ func (d DiggerController) UpsertOrgInternal(c *gin.Context) {

if org == nil {
slog.Info("Organization not found, creating new one", "externalId", externalId)
org, err = models.DB.CreateOrganisation(name, externalSource, externalId)
}

if err != nil {
slog.Error("Error creating org", "name", name, "externalId", externalId, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating org"})
return
org, err = models.DB.CreateOrganisation(name, externalSource, externalId, adminEmail)
if err != nil {
slog.Error("Error creating org", "name", name, "externalId", externalId, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating org"})
return
}
}

slog.Info("Successfully upserted organization", "orgId", org.ID, "externalId", externalId)
Expand Down Expand Up @@ -102,6 +104,8 @@ func (d DiggerController) CreateUserInternal(c *gin.Context) {
return
}

segment.IdentifyClient(userEmail, userEmail, userEmail, userEmail, org.Name, org.ExternalId, string(org.BillingPlan))

slog.Info("Successfully created user", "userId", user.ID, "email", userEmail, "orgId", org.ID)
c.JSON(http.StatusOK, gin.H{"status": "success", "user_id": user.ID})
}
2 changes: 1 addition & 1 deletion backend/controllers/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func AssociateTenantIdToDiggerOrg(c *gin.Context) {
}

if org == nil {
newOrg, err := models.DB.CreateOrganisation(nameStr, "", tenantIdStr)
newOrg, err := models.DB.CreateOrganisation(nameStr, "", tenantIdStr, nil)
if err != nil {
slog.Error("Failed to create organisation", "tenantId", tenantIdStr, "name", nameStr, "error", err)
c.AbortWithStatus(http.StatusInternalServerError)
Expand Down
11 changes: 11 additions & 0 deletions backend/controllers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/diggerhq/digger/backend/logging"
"github.com/diggerhq/digger/backend/middleware"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/segment"
"github.com/diggerhq/digger/backend/services"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/comment_utils/reporting"
Expand Down Expand Up @@ -655,6 +656,13 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {

batchId := *job.BatchID

org, err := models.DB.GetOrganisationById(orgId)
if err != nil || org == nil {
slog.Error("Error getting organisation", "jobId", jobId, "error", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching organisation"})
return
}

slog.Info("Processing job status update",
"jobId", jobId,
"currentStatus", job.Status,
Expand Down Expand Up @@ -732,6 +740,8 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
}(c.Request.Context())

case "succeeded":
batch := job.Batch
segment.Track(*org, "", "", "github", "ci_job_successful", map[string]string{"ci_job_type": string(batch.BatchType)})
job.Status = orchestrator_scheduler.DiggerJobSucceeded
job.TerraformOutput = request.TerraformOutput
if request.Footprint != nil {
Expand Down Expand Up @@ -896,6 +906,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
}(c.Request.Context())

case "failed":
segment.Track(*org, "", "", "github", "ci_job_failed", nil)
job.Status = orchestrator_scheduler.DiggerJobFailed
job.TerraformOutput = request.TerraformOutput
err := models.DB.UpdateDiggerJob(job)
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
Loading
Loading