Skip to content

Commit 662bddf

Browse files
authored
make the max number of projects configurable (#1844)
* make the max number of projects impacted configurable
1 parent 94a9e00 commit 662bddf

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

backend/config/envgetters.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
)
8+
9+
func GetMaxProjectsCreated() int {
10+
// the maximum number of impacted projects possible for a change
11+
// digger will fail when this number exceeds it
12+
// default value of 0 or negative means unlimited allowed
13+
maxProjects := os.Getenv("DIGGER_MAX_PROJECTS_IMPACTED")
14+
maxProjectsNum, err := strconv.Atoi(maxProjects)
15+
if err != nil {
16+
fmt.Printf("Error converting env var to number: %v\n", err)
17+
return 0
18+
}
19+
return maxProjectsNum
20+
}

backend/controllers/github.go

+57-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"github.com/davecgh/go-spew/spew"
910
"github.com/diggerhq/digger/backend/ci_backends"
11+
config2 "github.com/diggerhq/digger/backend/config"
1012
"github.com/diggerhq/digger/backend/locking"
1113
"github.com/diggerhq/digger/backend/segment"
1214
"github.com/diggerhq/digger/backend/services"
@@ -371,7 +373,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
371373
}
372374
}
373375

374-
diggerYmlStr, ghService, config, projectsGraph, _, _, err := getDiggerConfigForPR(gh, organisationId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, prNumber)
376+
diggerYmlStr, ghService, config, projectsGraph, _, _, changedFiles, err := getDiggerConfigForPR(gh, organisationId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, prNumber)
375377
if err != nil {
376378
log.Printf("getDiggerConfigForPR error: %v", err)
377379
return fmt.Errorf("error getting digger config")
@@ -400,6 +402,25 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
400402
return nil
401403
}
402404

405+
// ratio of impacted projects to changed files should be less than MAX_RATIO
406+
maxProjects := config2.GetMaxProjectsCreated()
407+
if maxProjects > 0 {
408+
if len(impactedProjects) > maxProjects {
409+
log.Printf("Error the number impacted projects %v exceeds maximum allowed: %v", len(impactedProjects), maxProjects)
410+
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Error the number impacted projects %v exceeds maximum allowed: %v", len(impactedProjects), maxProjects))
411+
log.Printf("Information about the event:")
412+
log.Printf("GH payload: %v", payload)
413+
log.Printf("PR changed files: %v", changedFiles)
414+
log.Printf("digger.yml STR: %v", diggerYmlStr)
415+
log.Printf("Parsed config: %v", config)
416+
log.Printf("Dependency graph:")
417+
spew.Dump(projectsGraph)
418+
log.Printf("Impacted Projects: %v", impactedProjects)
419+
log.Printf("Impacted Project jobs: %v", jobsForImpactedProjects)
420+
return fmt.Errorf("error processing event")
421+
}
422+
}
423+
403424
diggerCommand, err := orchestrator_scheduler.GetCommandFromJob(jobsForImpactedProjects[0])
404425
if err != nil {
405426
log.Printf("could not determine digger command from job: %v", jobsForImpactedProjects[0].Commands)
@@ -573,24 +594,24 @@ func GetDiggerConfigForBranch(gh utils.GithubClientProvider, installationId int6
573594
}
574595

575596
// TODO: Refactor this func to receive ghService as input
576-
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) {
597+
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, []string, error) {
577598
ghService, _, err := utils.GetGithubService(gh, installationId, repoFullName, repoOwner, repoName)
578599
if err != nil {
579600
log.Printf("Error getting github service: %v", err)
580-
return "", nil, nil, nil, nil, nil, fmt.Errorf("error getting github service")
601+
return "", nil, nil, nil, nil, nil, nil, fmt.Errorf("error getting github service")
581602
}
582603

583604
var prBranch string
584605
prBranch, prCommitSha, err := ghService.GetBranchName(prNumber)
585606
if err != nil {
586607
log.Printf("Error getting branch name: %v", err)
587-
return "", nil, nil, nil, nil, nil, fmt.Errorf("error getting branch name")
608+
return "", nil, nil, nil, nil, nil, nil, fmt.Errorf("error getting branch name")
588609
}
589610

590611
changedFiles, err := ghService.GetChangedFiles(prNumber)
591612
if err != nil {
592613
log.Printf("Error getting changed files: %v", err)
593-
return "", nil, nil, nil, nil, nil, fmt.Errorf("error getting changed files")
614+
return "", nil, nil, nil, nil, nil, nil, fmt.Errorf("error getting changed files")
594615
}
595616

596617
// check if items should be loaded from cache
@@ -600,17 +621,17 @@ func getDiggerConfigForPR(gh utils.GithubClientProvider, orgId uint, prLabels []
600621
log.Printf("could not load from cache")
601622
} else {
602623
log.Printf("successfully loaded from cache")
603-
return diggerYmlStr, ghService, config, *dependencyGraph, &prBranch, &prCommitSha, nil
624+
return diggerYmlStr, ghService, config, *dependencyGraph, &prBranch, &prCommitSha, changedFiles, nil
604625
}
605626
}
606627

607628
diggerYmlStr, ghService, config, dependencyGraph, err := GetDiggerConfigForBranch(gh, installationId, repoFullName, repoOwner, repoName, cloneUrl, prBranch, changedFiles)
608629
if err != nil {
609630
log.Printf("Error loading digger.yml: %v", err)
610-
return "", nil, nil, nil, nil, nil, fmt.Errorf("error loading digger.yml: %v", err)
631+
return "", nil, nil, nil, nil, nil, nil, fmt.Errorf("error loading digger.yml: %v", err)
611632
}
612633

613-
return diggerYmlStr, ghService, config, dependencyGraph, &prBranch, &prCommitSha, nil
634+
return diggerYmlStr, ghService, config, dependencyGraph, &prBranch, &prCommitSha, changedFiles, nil
614635
}
615636

616637
func retrieveConfigFromCache(orgId uint, repoFullName string) (string, *dg_configuration.DiggerConfig, *graph.Graph[string, dg_configuration.Project], error) {
@@ -717,7 +738,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
717738
}
718739
}
719740

720-
diggerYmlStr, ghService, config, projectsGraph, branch, commitSha, err := getDiggerConfigForPR(gh, orgId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, issueNumber)
741+
diggerYmlStr, ghService, config, projectsGraph, branch, commitSha, changedFiles, err := getDiggerConfigForPR(gh, orgId, prLabelsStr, installationId, repoFullName, repoOwner, repoName, cloneURL, issueNumber)
721742
if err != nil {
722743
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Could not load digger config, error: %v", err))
723744
log.Printf("getDiggerConfigForPR error: %v", err)
@@ -763,6 +784,33 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
763784
}
764785
log.Printf("GitHub IssueComment event processed successfully\n")
765786

787+
jobs, _, err := generic.ConvertIssueCommentEventToJobs(repoFullName, actor, issueNumber, commentBody, impactedProjects, requestedProject, config.Workflows, prBranchName, defaultBranch)
788+
if err != nil {
789+
log.Printf("Error converting event to jobs: %v", err)
790+
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Error converting event to jobs: %v", err))
791+
return fmt.Errorf("error converting event to jobs")
792+
}
793+
log.Printf("GitHub IssueComment event converted to Jobs successfully\n")
794+
795+
// ratio of impacted projects to changed files should be less than MAX_RATIO
796+
maxProjects := config2.GetMaxProjectsCreated()
797+
if maxProjects > 0 {
798+
if len(impactedProjects) > maxProjects {
799+
log.Printf("Error the number impacted projects %v exceeds maximum allowed: %v", len(impactedProjects), maxProjects)
800+
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Error the number impacted projects %v exceeds maximum allowed: %v", len(impactedProjects), maxProjects))
801+
log.Printf("Information about the event:")
802+
log.Printf("GH payload: %v", payload)
803+
log.Printf("PR changed files: %v", changedFiles)
804+
log.Printf("digger.yml STR: %v", diggerYmlStr)
805+
log.Printf("Parsed config: %v", config)
806+
log.Printf("Dependency graph:")
807+
spew.Dump(projectsGraph)
808+
log.Printf("Impacted Projects: %v", impactedProjects)
809+
log.Printf("Impacted Project jobs: %v", jobs)
810+
return fmt.Errorf("error processing event")
811+
}
812+
}
813+
766814
// perform unlocking in backend
767815
if config.PrLocks {
768816
for _, project := range impactedProjects {
@@ -791,14 +839,6 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
791839
return nil
792840
}
793841

794-
jobs, _, err := generic.ConvertIssueCommentEventToJobs(repoFullName, actor, issueNumber, commentBody, impactedProjects, requestedProject, config.Workflows, prBranchName, defaultBranch)
795-
if err != nil {
796-
log.Printf("Error converting event to jobs: %v", err)
797-
commentReporterManager.UpdateComment(fmt.Sprintf(":x: Error converting event to jobs: %v", err))
798-
return fmt.Errorf("error converting event to jobs")
799-
}
800-
log.Printf("GitHub IssueComment event converted to Jobs successfully\n")
801-
802842
err = utils.ReportInitialJobsStatus(commentReporter, jobs)
803843
if err != nil {
804844
log.Printf("Failed to comment initial status for jobs: %v", err)

0 commit comments

Comments
 (0)