@@ -3,6 +3,13 @@ package controllers
33import (
44 "encoding/json"
55 "fmt"
6+ "log/slog"
7+ "os"
8+ "runtime/debug"
9+ "slices"
10+ "strconv"
11+ "time"
12+
613 "github.com/diggerhq/digger/backend/ci_backends"
714 config2 "github.com/diggerhq/digger/backend/config"
815 locking2 "github.com/diggerhq/digger/backend/locking"
@@ -16,12 +23,6 @@ import (
1623 "github.com/diggerhq/digger/libs/scheduler"
1724 "github.com/google/go-github/v61/github"
1825 "github.com/samber/lo"
19- "log/slog"
20- "os"
21- "runtime/debug"
22- "slices"
23- "strconv"
24- "time"
2526)
2627
2728func handlePullRequestEvent (gh utils.GithubClientProvider , payload * github.PullRequestEvent , ciBackendProvider ci_backends.CiBackendProvider , appId int64 ) error {
@@ -49,6 +50,12 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
4950 branch := payload .PullRequest .Head .GetRef ()
5051 action := * payload .Action
5152 labels := payload .PullRequest .Labels
53+ var vcsActorId string = ""
54+ if payload .Sender != nil && payload .Sender .Email != nil {
55+ vcsActorId = * payload .Sender .Email
56+ } else if payload .Sender != nil && payload .Sender .Login != nil {
57+ vcsActorId = * payload .Sender .Login
58+ }
5259 prLabelsStr := lo .Map (labels , func (label * github.Label , i int ) string {
5360 return * label .Name
5461 })
@@ -141,6 +148,15 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
141148 }
142149 }
143150
151+ org , err := models .DB .GetOrganisationById (organisationId )
152+ if err != nil || org == nil {
153+ slog .Error ("Error getting organisation" ,
154+ "orgId" , organisationId ,
155+ "error" , err )
156+ commentReporterManager .UpdateComment (fmt .Sprintf (":x: Failed to get organisation by ID" ))
157+ return fmt .Errorf ("error getting organisation" )
158+ }
159+
144160 diggerYmlStr , ghService , config , projectsGraph , _ , _ , changedFiles , err := getDiggerConfigForPR (gh , organisationId , prLabelsStr , installationId , repoFullName , repoOwner , repoName , cloneURL , prNumber )
145161 if err != nil {
146162 slog .Error ("Error getting Digger config for PR" ,
@@ -244,6 +260,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
244260 "commands" , jobsForImpactedProjects [0 ].Commands ,
245261 "error" , err ,
246262 )
263+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
247264 commentReporterManager .UpdateComment (fmt .Sprintf (":x: could not determine digger command from job: %v" , err ))
248265 return fmt .Errorf ("unknown digger command in comment %v" , err )
249266 }
@@ -340,12 +357,16 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
340357 return nil
341358 }
342359
360+ // a pull request has led to jobs which would be triggered (ignoring closed event by here)
361+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request" , map [string ]string {})
362+
343363 commentReporter , err := commentReporterManager .UpdateComment (":construction_worker: Digger starting... Config loaded successfully" )
344364 if err != nil {
345365 slog .Error ("Error initializing comment reporter" ,
346366 "prNumber" , prNumber ,
347367 "error" , err ,
348368 )
369+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
349370 return fmt .Errorf ("error initializing comment reporter" )
350371 }
351372
@@ -355,6 +376,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
355376 "prNumber" , prNumber ,
356377 "error" , err ,
357378 )
379+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
358380 commentReporterManager .UpdateComment (fmt .Sprintf (":x: error setting status for PR: %v" , err ))
359381 return fmt .Errorf ("error setting status for PR: %v" , err )
360382 }
@@ -415,6 +437,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
415437 "commentId" , commentReporter .CommentId ,
416438 "error" , err ,
417439 )
440+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
418441 commentReporterManager .UpdateComment (fmt .Sprintf (":x: could not handle commentId: %v" , err ))
419442 }
420443
@@ -427,6 +450,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
427450 "prNumber" , prNumber ,
428451 "error" , err ,
429452 )
453+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
430454 commentReporterManager .UpdateComment (fmt .Sprintf (":x: could not post ai comment summary comment id: %v" , err ))
431455 return fmt .Errorf ("could not post ai summary comment: %v" , err )
432456 }
@@ -470,6 +494,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
470494 "prNumber" , prNumber ,
471495 "error" , err ,
472496 )
497+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
473498 commentReporterManager .UpdateComment (fmt .Sprintf (":x: ConvertJobsToDiggerJobs error: %v" , err ))
474499 return fmt .Errorf ("error converting jobs" )
475500 }
@@ -525,7 +550,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
525550 slog .Debug ("Successfully updated batch with source details" , "batchId" , batchId )
526551 }
527552
528- segment .Track (strconv . Itoa ( int ( organisationId )), " backend_trigger_job" )
553+ segment .Track (* org , repoOwner , vcsActorId , "github" , " backend_trigger_job", map [ string ] string {} )
529554
530555 slog .Info ("Getting CI backend" ,
531556 "prNumber" , prNumber ,
@@ -548,6 +573,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
548573 "repoFullName" , repoFullName ,
549574 "error" , err ,
550575 )
576+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
551577 commentReporterManager .UpdateComment (fmt .Sprintf (":x: GetCiBackend error: %v" , err ))
552578 return fmt .Errorf ("error fetching ci backed %v" , err )
553579 }
@@ -565,6 +591,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
565591 "batchId" , batchId ,
566592 "error" , err ,
567593 )
594+ segment .Track (* org , repoOwner , vcsActorId , "github" , "pull_request_ERROR" , map [string ]string {"error" : err .Error ()})
568595 commentReporterManager .UpdateComment (fmt .Sprintf (":x: TriggerDiggerJobs error: %v" , err ))
569596 return fmt .Errorf ("error triggering Digger Jobs" )
570597 }
0 commit comments