@@ -24,6 +24,7 @@ import (
24
24
"os"
25
25
"path"
26
26
"reflect"
27
+ "slices"
27
28
"strconv"
28
29
"strings"
29
30
@@ -53,7 +54,7 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
53
54
log .Printf ("GithubAppWebHook" )
54
55
55
56
appID := c .GetHeader ("X-GitHub-Hook-Installation-Target-ID" )
56
- log . Printf ( "app id from header is: %v" , appID )
57
+
57
58
_ , _ , webhookSecret , _ , err := d .GithubClientProvider .FetchCredentials (appID )
58
59
59
60
payload , err := github .ValidatePayload (c .Request , []byte (webhookSecret ))
@@ -316,6 +317,10 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
316
317
commitSha := payload .PullRequest .Head .GetSHA ()
317
318
branch := payload .PullRequest .Head .GetRef ()
318
319
action := * payload .Action
320
+ labels := payload .PullRequest .Labels
321
+ prLabelsStr := lo .Map (labels , func (label * github.Label , i int ) string {
322
+ return * label .Name
323
+ })
319
324
320
325
link , err := models .DB .GetGithubAppInstallationLink (installationId )
321
326
if err != nil {
@@ -361,7 +366,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
361
366
}
362
367
}
363
368
364
- diggerYmlStr , ghService , config , projectsGraph , _ , _ , err := getDiggerConfigForPR (gh , installationId , repoFullName , repoOwner , repoName , cloneURL , prNumber )
369
+ diggerYmlStr , ghService , config , projectsGraph , _ , _ , err := getDiggerConfigForPR (gh , organisationId , prLabelsStr , installationId , repoFullName , repoOwner , repoName , cloneURL , prNumber )
365
370
if err != nil {
366
371
log .Printf ("getDiggerConfigForPR error: %v" , err )
367
372
return fmt .Errorf ("error getting digger config" )
@@ -563,7 +568,7 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6
563
568
}
564
569
565
570
// TODO: Refactor this func to receive ghService as input
566
- func getDiggerConfigForPR (gh utils.GithubClientProvider , installationId int64 , repoFullName string , repoOwner string , repoName string , cloneUrl string , prNumber int ) (string , * dg_github.GithubService , * dg_configuration.DiggerConfig , graph.Graph [string , dg_configuration.Project ], * string , * string , error ) {
571
+ func getDiggerConfigForPR (gh utils.GithubClientProvider , orgId uint , prLabels [] string , installationId int64 , repoFullName string , repoOwner string , repoName string , cloneUrl string , prNumber int ) (string , * dg_github.GithubService , * dg_configuration.DiggerConfig , graph.Graph [string , dg_configuration.Project ], * string , * string , error ) {
567
572
ghService , _ , err := utils .GetGithubService (gh , installationId , repoFullName , repoOwner , repoName )
568
573
if err != nil {
569
574
log .Printf ("Error getting github service: %v" , err )
@@ -583,6 +588,17 @@ func getDiggerConfigForPR(gh utils.GithubClientProvider, installationId int64, r
583
588
return "" , nil , nil , nil , nil , nil , fmt .Errorf ("error getting changed files" )
584
589
}
585
590
591
+ // check if items should be loaded from cache
592
+ if val , _ := os .LookupEnv ("DIGGER_CONFIG_REPO_CACHE_ENABLED" ); val == "1" && ! slices .Contains (prLabels , "digger:no-cache" ) {
593
+ diggerYmlStr , config , dependencyGraph , err := retrieveConfigFromCache (orgId , repoFullName )
594
+ if err != nil {
595
+ log .Printf ("could not load from cache" )
596
+ } else {
597
+ log .Printf ("successfully loaded from cache" )
598
+ return diggerYmlStr , ghService , config , * dependencyGraph , & prBranch , & prCommitSha , nil
599
+ }
600
+ }
601
+
586
602
diggerYmlStr , ghService , config , dependencyGraph , err := GetDiggerConfigForBranch (gh , installationId , repoFullName , repoOwner , repoName , cloneUrl , prBranch , changedFiles )
587
603
if err != nil {
588
604
log .Printf ("Error loading digger.yml: %v" , err )
@@ -593,6 +609,28 @@ func getDiggerConfigForPR(gh utils.GithubClientProvider, installationId int64, r
593
609
return diggerYmlStr , ghService , config , dependencyGraph , & prBranch , & prCommitSha , nil
594
610
}
595
611
612
+ func retrieveConfigFromCache (orgId uint , repoFullName string ) (string , * dg_configuration.DiggerConfig , * graph.Graph [string , dg_configuration.Project ], error ) {
613
+ repoCache , err := models .DB .GetRepoCache (orgId , repoFullName )
614
+ if err != nil {
615
+ log .Printf ("Error: failed to load repoCache, going to try live load %v" , err )
616
+ return "" , nil , nil , fmt .Errorf ("" )
617
+ }
618
+ var config dg_configuration.DiggerConfig
619
+ err = json .Unmarshal (repoCache .DiggerConfig , & config )
620
+ if err != nil {
621
+ log .Printf ("Error: failed to load repoCache unmarshall config %v" , err )
622
+ return "" , nil , nil , fmt .Errorf ("failed to load repoCache unmarshall config %v" , err )
623
+ }
624
+
625
+ projectsGraph , err := dg_configuration .CreateProjectDependencyGraph (config .Projects )
626
+ if err != nil {
627
+ log .Printf ("error retrieving graph of dependencies: %v" , err )
628
+ return "" , nil , nil , fmt .Errorf ("error retrieving graph of dependencies: %v" , err )
629
+ }
630
+
631
+ return repoCache .DiggerYmlStr , & config , & projectsGraph , nil
632
+ }
633
+
596
634
func GetRepoByInstllationId (installationId int64 , repoOwner string , repoName string ) (* models.Repo , error ) {
597
635
link , err := models .DB .GetGithubAppInstallationLink (installationId )
598
636
if err != nil {
@@ -634,6 +672,10 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
634
672
commentBody := * payload .Comment .Body
635
673
defaultBranch := * payload .Repo .DefaultBranch
636
674
isPullRequest := payload .Issue .IsPullRequest ()
675
+ labels := payload .Issue .Labels
676
+ prLabelsStr := lo .Map (labels , func (label * github.Label , i int ) string {
677
+ return * label .Name
678
+ })
637
679
638
680
if ! isPullRequest {
639
681
log .Printf ("comment not on pullrequest, ignroning" )
@@ -671,7 +713,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
671
713
}
672
714
}
673
715
674
- diggerYmlStr , ghService , config , projectsGraph , branch , commitSha , err := getDiggerConfigForPR (gh , installationId , repoFullName , repoOwner , repoName , cloneURL , issueNumber )
716
+ diggerYmlStr , ghService , config , projectsGraph , branch , commitSha , err := getDiggerConfigForPR (gh , orgId , prLabelsStr , installationId , repoFullName , repoOwner , repoName , cloneURL , issueNumber )
675
717
if err != nil {
676
718
commentReporterManager .UpdateComment (fmt .Sprintf (":x: Could not load digger config, error: %v" , err ))
677
719
log .Printf ("getDiggerConfigForPR error: %v" , err )
0 commit comments