Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PR locking configuration at the digger.yml level #1578

Merged
merged 5 commits into from
Jun 21, 2024
Merged
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
66 changes: 36 additions & 30 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,24 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
}

// perform locking/unlocking in backend
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
InternalLock: locking.BackendDBLock{
OrgId: organisationId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: prNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed to perform lock action on project: %v, %v", project.Name, err)
if config.PrLocks {
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
Enable: config.PrLocks,
InternalLock: locking.BackendDBLock{
OrgId: organisationId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: prNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed to perform lock action on project: %v, %v", project.Name, err)
}
}
}

Expand Down Expand Up @@ -765,21 +768,24 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
log.Printf("GitHub IssueComment event processed successfully\n")

// perform unlocking in backend
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
InternalLock: locking.BackendDBLock{
OrgId: orgId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: issueNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed perform lock action on project: %v %v", project.Name, err)
if config.PrLocks {
for _, project := range impactedProjects {
prLock := dg_locking.PullRequestLock{
Enable: config.PrLocks,
InternalLock: locking.BackendDBLock{
OrgId: orgId,
},
CIService: ghService,
Reporter: comment_updater.NoopReporter{},
ProjectName: project.Name,
ProjectNamespace: repoFullName,
PrNumber: issueNumber,
}
err = PerformLockingActionFromCommand(prLock, *diggerCommand)
if err != nil {
utils.InitCommentReporter(ghService, issueNumber, fmt.Sprintf(":x: Failed perform lock action on project: %v %v", project.Name, err))
return fmt.Errorf("failed perform lock action on project: %v %v", project.Name, err)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/pkg/core/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Executor interface {
}

type LockingExecutorWrapper struct {
Enable bool
ProjectLock locking.ProjectLock
Executor Executor
}
Expand Down Expand Up @@ -82,6 +83,9 @@ func (l LockingExecutorWrapper) Unlock() error {
}

func (l LockingExecutorWrapper) Lock() error {
if !l.Enable {
return nil
}
_, err := l.ProjectLock.Lock()
if err != nil {
return fmt.Errorf("failed to aquire lock: %s, %v", l.ProjectLock.LockId(), err)
Expand Down
3 changes: 1 addition & 2 deletions cli/pkg/digger/digger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"strings"
"time"

"github.com/diggerhq/digger/libs/comment_utils/summary"

"github.com/diggerhq/digger/cli/pkg/core/backend"
core_drift "github.com/diggerhq/digger/cli/pkg/core/drift"
"github.com/diggerhq/digger/cli/pkg/core/execution"
Expand Down Expand Up @@ -206,6 +204,7 @@ func run(command string, job orchestrator.Job, policyChecker policy.Checker, org
}

projectLock := &locking2.PullRequestLock{
Enable: config.PrLocks,
InternalLock: lock,
Reporter: reporter,
CIService: prService,
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/digger.yml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ auto_merge: true
```yml
traverse_to_nested_projects: true
auto_merge: false
pr_locks: true
projects:
- name: prod
dir: prod
Expand Down Expand Up @@ -101,6 +102,7 @@ workflows:
| --------------------------- | ---------------------------------------------------------- | ------- | -------- | ------------------------------------------------------ | ----- |
| telemetry | boolean | true | no | allows collecting anonymised usage and debugging data | |
| auto_merge | boolean | false | no | automatically merge pull requests when all checks pass | |
| pr_locks | boolean | true | no | Enable PR-level locking | |
| projects | array of [Projects](/reference/digger.yml#project) | \[\] | no | list of projects to manage | |
| generate_projects | [GenerateProjects](/reference/digger.yml#generateprojects) | {} | no | generate projects from a directory structure | |
| workflows | map of [Workflows](/reference/digger.yml#workflows) | {} | no | workflows and configurations to run on events | |
Expand Down
1 change: 1 addition & 0 deletions libs/digger_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type DiggerConfig struct {
AllowDraftPRs bool
CommentRenderMode string
DependencyConfiguration DependencyConfiguration
PrLocks bool
Projects []Project
AutoMerge bool
Telemetry bool
Expand Down
6 changes: 6 additions & 0 deletions libs/digger_config/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml) (*DiggerConfig, gra
diggerConfig.MentionDriftedProjectsInPR = false
}

if diggerYaml.PrLocks != nil {
diggerConfig.PrLocks = *diggerYaml.PrLocks
} else {
diggerConfig.PrLocks = true
}

if diggerYaml.Telemetry != nil {
diggerConfig.Telemetry = *diggerYaml.Telemetry
} else {
Expand Down
1 change: 1 addition & 0 deletions libs/digger_config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type DiggerConfigYaml struct {
ApplyAfterMerge *bool `yaml:"apply_after_merge"`
AllowDraftPRs *bool `yaml:"allow_draft_prs"`
DependencyConfiguration *DependencyConfigurationYaml `yaml:"dependency_configuration"`
PrLocks *bool `yaml:"pr_locks"`
Projects []*ProjectYaml `yaml:"projects"`
AutoMerge *bool `yaml:"auto_merge"`
CommentRenderMode *string `yaml:"comment_render_mode"`
Expand Down
7 changes: 6 additions & 1 deletion libs/locking/locking.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

type PullRequestLock struct {
Enable bool
InternalLock Lock
CIService orchestrator.PullRequestService
Reporter reporting.Reporter
Expand Down Expand Up @@ -62,7 +63,11 @@ func (projectLock *PullRequestLock) Lock() (bool, error) {
return false, nil
}

existingLockTransactionId, err := projectLock.InternalLock.GetLock(lockId)
var existingLockTransactionId *int
if projectLock.Enable {
existingLockTransactionId, err = projectLock.InternalLock.GetLock(lockId)
}

if err != nil {
log.Printf("failed to get lock: %v\n", err)
return false, err
Expand Down
Loading