Skip to content

Commit ebcce82

Browse files
authored
add repo owner to locks (#79)
1 parent 007be7f commit ebcce82

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

pkg/digger/digger.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func RunCommandsPerProject(commandsPerProject []ProjectCommand, repoOwner string
5656
PrManager: prManager,
5757
ProjectName: projectCommands.ProjectName,
5858
RepoName: repoName,
59+
RepoOwner: repoOwner,
5960
}
6061
diggerExecutor := DiggerExecutor{
6162
workingDir,
@@ -216,7 +217,7 @@ type DiggerExecutor struct {
216217
}
217218

218219
func (d DiggerExecutor) LockId() string {
219-
return d.repoName + "#" + d.projectName
220+
return d.repoOwner + "/" + d.repoName + "#" + d.projectName
220221
}
221222

222223
func (d DiggerExecutor) Plan(prNumber int) {

pkg/integration/integration_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func getProjectLockForTests() (error, *utils.ProjectLockImpl) {
4343
PrManager: githubPrService,
4444
ProjectName: "test_dynamodb_lock",
4545
RepoName: repositoryName,
46+
RepoOwner: repoOwner,
4647
}
4748
return err, projectLock
4849
}
@@ -388,6 +389,7 @@ func TestHappyPath(t *testing.T) {
388389
PrManager: githubPrService,
389390
ProjectName: "dev",
390391
RepoName: repositoryName,
392+
RepoOwner: repoOwner,
391393
}
392394
resource := repositoryName + "#" + projectLock.ProjectName
393395
transactionId, err := projectLock.InternalLock.GetLock(resource)
@@ -428,6 +430,7 @@ func TestHappyPath(t *testing.T) {
428430
PrManager: githubPrService,
429431
ProjectName: "dev",
430432
RepoName: repositoryName,
433+
RepoOwner: repoOwner,
431434
}
432435
transactionId, err = projectLock.InternalLock.GetLock(resource)
433436
assert.NoError(t, err)
@@ -451,6 +454,7 @@ func TestHappyPath(t *testing.T) {
451454
PrManager: githubPrService,
452455
ProjectName: "dev",
453456
RepoName: repositoryName,
457+
RepoOwner: repoOwner,
454458
}
455459
transactionId, err = projectLock.InternalLock.GetLock(resource)
456460
assert.NoError(t, err)
@@ -528,6 +532,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
528532
PrManager: githubPrService,
529533
ProjectName: "digger_demo",
530534
RepoName: repositoryName,
535+
RepoOwner: repoOwner,
531536
}
532537
resource := "digger_demo#default"
533538
transactionId, err := projectLock.InternalLock.GetLock(resource)
@@ -567,6 +572,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
567572
PrManager: githubPrService,
568573
ProjectName: "digger_demo",
569574
RepoName: repositoryName,
575+
RepoOwner: repoOwner,
570576
}
571577
transactionId, err = projectLock.InternalLock.GetLock(resource)
572578
assert.NoError(t, err)
@@ -590,6 +596,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
590596
PrManager: githubPrService,
591597
ProjectName: "digger_demo",
592598
RepoName: repositoryName,
599+
RepoOwner: repoOwner,
593600
}
594601
transactionId, err = projectLock.InternalLock.GetLock(resource)
595602
assert.NoError(t, err)

pkg/utils/locking.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type ProjectLockImpl struct {
2121
PrManager github.PullRequestManager
2222
ProjectName string
2323
RepoName string
24+
RepoOwner string
2425
}
2526

2627
type Lock interface {
@@ -47,11 +48,11 @@ func (projectLock *ProjectLockImpl) Lock(lockId string, prNumber int) (bool, err
4748
if transactionId != nil {
4849
transactionIdStr := strconv.Itoa(*transactionId)
4950
if *transactionId != prNumber {
50-
comment := "Project " + projectLock.ProjectName + " locked by another PR #" + transactionIdStr + "(failed to acquire lock " + projectLock.ProjectName + "). The locking plan must be applied or discarded before future plans can execute"
51+
comment := "Project " + projectLock.projectId() + " locked by another PR #" + transactionIdStr + "(failed to acquire lock " + projectLock.ProjectName + "). The locking plan must be applied or discarded before future plans can execute"
5152
projectLock.PrManager.PublishComment(prNumber, comment)
5253
return false, nil
5354
}
54-
comment := "Project " + projectLock.ProjectName + " locked by this PR #" + transactionIdStr + " already."
55+
comment := "Project " + projectLock.projectId() + " locked by this PR #" + transactionIdStr + " already."
5556
projectLock.PrManager.PublishComment(prNumber, comment)
5657
return true, nil
5758
}
@@ -62,16 +63,16 @@ func (projectLock *ProjectLockImpl) Lock(lockId string, prNumber int) (bool, err
6263
}
6364

6465
if lockAcquired {
65-
comment := "Project " + projectLock.ProjectName + " has been locked by PR #" + strconv.Itoa(prNumber)
66+
comment := "Project " + projectLock.projectId() + " has been locked by PR #" + strconv.Itoa(prNumber)
6667
projectLock.PrManager.PublishComment(prNumber, comment)
67-
println("project " + projectLock.ProjectName + " locked successfully. PR # " + strconv.Itoa(prNumber))
68+
println("project " + projectLock.projectId() + " locked successfully. PR # " + strconv.Itoa(prNumber))
6869
return true, nil
6970
}
7071

7172
transactionId, _ = projectLock.InternalLock.GetLock(lockId)
7273
transactionIdStr = strconv.Itoa(*transactionId)
7374

74-
comment := "Project " + projectLock.ProjectName + " locked by another PR #" + transactionIdStr + " (failed to acquire lock " + projectLock.RepoName + "). The locking plan must be applied or discarded before future plans can execute"
75+
comment := "Project " + projectLock.projectId() + " locked by another PR #" + transactionIdStr + " (failed to acquire lock " + projectLock.RepoName + "). The locking plan must be applied or discarded before future plans can execute"
7576
projectLock.PrManager.PublishComment(prNumber, comment)
7677
println(comment)
7778
return false, nil
@@ -92,7 +93,7 @@ func (projectLock *ProjectLockImpl) Unlock(lockId string, prNumber int) (bool, e
9293
return false, err
9394
}
9495
if lockReleased {
95-
comment := "Project unlocked (" + projectLock.ProjectName + ")."
96+
comment := "Project unlocked (" + projectLock.projectId() + ")."
9697
projectLock.PrManager.PublishComment(prNumber, comment)
9798
println("Project unlocked")
9899
return true, nil
@@ -109,13 +110,17 @@ func (projectLock *ProjectLockImpl) ForceUnlock(lockId string, prNumber int) {
109110
lockReleased, _ := projectLock.InternalLock.Unlock(lockId)
110111

111112
if lockReleased {
112-
comment := "Project unlocked (" + projectLock.ProjectName + ")."
113+
comment := "Project unlocked (" + projectLock.projectId() + ")."
113114
projectLock.PrManager.PublishComment(prNumber, comment)
114115
println("Project unlocked")
115116
}
116117
}
117118
}
118119

120+
func (projectLock *ProjectLockImpl) projectId() string {
121+
return projectLock.RepoOwner + "/" + projectLock.RepoName
122+
}
123+
119124
func GetLock() (Lock, error) {
120125
awsRegion := strings.ToLower(os.Getenv("AWS_REGION"))
121126
awsProfile := strings.ToLower(os.Getenv("AWS_PROFILE"))

pkg/utils/locking_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestLockingTwiceThrowsError(t *testing.T) {
1414
PrManager: &mockPrManager,
1515
ProjectName: "",
1616
RepoName: "",
17+
RepoOwner: "",
1718
}
1819
state1, err1 := pl.Lock("a", 1)
1920
assert.True(t, state1)

0 commit comments

Comments
 (0)