Repository: diggerhq/digger
Pull request #79 (2023-03-30 11:49:35)

add repo owner to locks


Patch:
diff --git a/pkg/digger/digger.go b/pkg/digger/digger.go
index 9904f3d0..d194f72e 100644
--- a/pkg/digger/digger.go
+++ b/pkg/digger/digger.go
@@ -56,6 +56,7 @@ func RunCommandsPerProject(commandsPerProject []ProjectCommand, repoOwner string
 				PrManager:    prManager,
 				ProjectName:  projectCommands.ProjectName,
 				RepoName:     repoName,
+				RepoOwner:    repoOwner,
 			}
 			diggerExecutor := DiggerExecutor{
 				workingDir,
@@ -216,7 +217,7 @@ type DiggerExecutor struct {
 }
 
 func (d DiggerExecutor) LockId() string {
-	return d.repoName + "#" + d.projectName
+	return d.repoOwner + "/" + d.repoName + "#" + d.projectName
 }
 
 func (d DiggerExecutor) Plan(prNumber int) {
diff --git a/pkg/utils/locking.go b/pkg/utils/locking.go
index 0100f079..9d04e859 100644
--- a/pkg/utils/locking.go
+++ b/pkg/utils/locking.go
@@ -21,6 +21,7 @@ type ProjectLockImpl struct {
 	PrManager    github.PullRequestManager
 	ProjectName  string
 	RepoName     string
+	RepoOwner    string
 }
 
 type Lock interface {
@@ -47,11 +48,11 @@ func (projectLock *ProjectLockImpl) Lock(lockId string, prNumber int) (bool, err
 	if transactionId != nil {
 		transactionIdStr := strconv.Itoa(*transactionId)
 		if *transactionId != prNumber {
-			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"
+			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"
 			projectLock.PrManager.PublishComment(prNumber, comment)
 			return false, nil
 		}
-		comment := "Project " + projectLock.ProjectName + " locked by this PR #" + transactionIdStr + " already."
+		comment := "Project " + projectLock.projectId() + " locked by this PR #" + transactionIdStr + " already."
 		projectLock.PrManager.PublishComment(prNumber, comment)
 		return true, nil
 	}
@@ -62,16 +63,16 @@ func (projectLock *ProjectLockImpl) Lock(lockId string, prNumber int) (bool, err
 	}
 
 	if lockAcquired {
-		comment := "Project " + projectLock.ProjectName + " has been locked by PR #" + strconv.Itoa(prNumber)
+		comment := "Project " + projectLock.projectId() + " has been locked by PR #" + strconv.Itoa(prNumber)
 		projectLock.PrManager.PublishComment(prNumber, comment)
-		println("project " + projectLock.ProjectName + " locked successfully. PR # " + strconv.Itoa(prNumber))
+		println("project " + projectLock.projectId() + " locked successfully. PR # " + strconv.Itoa(prNumber))
 		return true, nil
 	}
 
 	transactionId, _ = projectLock.InternalLock.GetLock(lockId)
 	transactionIdStr = strconv.Itoa(*transactionId)
 
-	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"
+	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"
 	projectLock.PrManager.PublishComment(prNumber, comment)
 	println(comment)
 	return false, nil
@@ -92,7 +93,7 @@ func (projectLock *ProjectLockImpl) Unlock(lockId string, prNumber int) (bool, e
 				return false, err
 			}
 			if lockReleased {
-				comment := "Project unlocked (" + projectLock.ProjectName + ")."
+				comment := "Project unlocked (" + projectLock.projectId() + ")."
 				projectLock.PrManager.PublishComment(prNumber, comment)
 				println("Project unlocked")
 				return true, nil
@@ -109,13 +110,17 @@ func (projectLock *ProjectLockImpl) ForceUnlock(lockId string, prNumber int) {
 		lockReleased, _ := projectLock.InternalLock.Unlock(lockId)
 
 		if lockReleased {
-			comment := "Project unlocked (" + projectLock.ProjectName + ")."
+			comment := "Project unlocked (" + projectLock.projectId() + ")."
 			projectLock.PrManager.PublishComment(prNumber, comment)
 			println("Project unlocked")
 		}
 	}
 }
 
+func (projectLock *ProjectLockImpl) projectId() string {
+	return projectLock.RepoOwner + "/" + projectLock.RepoName
+}
+
 func GetLock() (Lock, error) {
 	awsRegion := strings.ToLower(os.Getenv("AWS_REGION"))
 	awsProfile := strings.ToLower(os.Getenv("AWS_PROFILE"))


Test patch:
diff --git a/pkg/integration/integration_test.go b/pkg/integration/integration_test.go
index 253bf957..ad1ba4c0 100644
--- a/pkg/integration/integration_test.go
+++ b/pkg/integration/integration_test.go
@@ -43,6 +43,7 @@ func getProjectLockForTests() (error, *utils.ProjectLockImpl) {
 		PrManager:    githubPrService,
 		ProjectName:  "test_dynamodb_lock",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	return err, projectLock
 }
@@ -388,6 +389,7 @@ func TestHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "dev",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	resource := repositoryName + "#" + projectLock.ProjectName
 	transactionId, err := projectLock.InternalLock.GetLock(resource)
@@ -428,6 +430,7 @@ func TestHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "dev",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	transactionId, err = projectLock.InternalLock.GetLock(resource)
 	assert.NoError(t, err)
@@ -451,6 +454,7 @@ func TestHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "dev",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	transactionId, err = projectLock.InternalLock.GetLock(resource)
 	assert.NoError(t, err)
@@ -528,6 +532,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "digger_demo",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	resource := "digger_demo#default"
 	transactionId, err := projectLock.InternalLock.GetLock(resource)
@@ -567,6 +572,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "digger_demo",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	transactionId, err = projectLock.InternalLock.GetLock(resource)
 	assert.NoError(t, err)
@@ -590,6 +596,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
 		PrManager:    githubPrService,
 		ProjectName:  "digger_demo",
 		RepoName:     repositoryName,
+		RepoOwner:    repoOwner,
 	}
 	transactionId, err = projectLock.InternalLock.GetLock(resource)
 	assert.NoError(t, err)
diff --git a/pkg/utils/locking_test.go b/pkg/utils/locking_test.go
index d04bec85..1a58baae 100644
--- a/pkg/utils/locking_test.go
+++ b/pkg/utils/locking_test.go
@@ -14,6 +14,7 @@ func TestLockingTwiceThrowsError(t *testing.T) {
 		PrManager:    &mockPrManager,
 		ProjectName:  "",
 		RepoName:     "",
+		RepoOwner:    "",
 	}
 	state1, err1 := pl.Lock("a", 1)
 	assert.True(t, state1)