Skip to content

Commit 26c1b90

Browse files
authored
fix: prevent error about lock being acquired by the current PR (#216)
1 parent 6185537 commit 26c1b90

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

pkg/gcp/gcp_lock.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package gcp
22

33
import (
4-
"cloud.google.com/go/storage"
54
"context"
65
"fmt"
76
"log"
87
"strconv"
98
"time"
9+
10+
"cloud.google.com/go/storage"
1011
)
1112

1213
type GoogleStorageLock struct {
@@ -16,14 +17,6 @@ type GoogleStorageLock struct {
1617
}
1718

1819
func (googleLock *GoogleStorageLock) Lock(transactionId int, resource string) (bool, error) {
19-
existingLockTransactionId, err := googleLock.GetLock(resource)
20-
if err != nil {
21-
fmt.Printf("failed to get lock: %v\n", err)
22-
}
23-
if existingLockTransactionId != nil {
24-
return false, nil
25-
}
26-
2720
now := time.Now().Format(time.RFC3339)
2821
fileName := resource
2922

pkg/utils/locking.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"digger/pkg/github"
99
"errors"
1010
"fmt"
11-
"github.com/aws/aws-sdk-go/service/sts"
1211
"log"
1312
"os"
1413
"strconv"
1514
"strings"
1615

16+
"github.com/aws/aws-sdk-go/service/sts"
17+
1718
"cloud.google.com/go/storage"
1819
awssdk "github.com/aws/aws-sdk-go/aws"
1920
"github.com/aws/aws-sdk-go/aws/credentials"
@@ -56,6 +57,21 @@ func (projectLock *ProjectLockImpl) Lock(prNumber int) (bool, error) {
5657
return false, nil
5758
}
5859

60+
existingLockTransactionId, err := projectLock.InternalLock.GetLock(lockId)
61+
if err != nil {
62+
fmt.Printf("failed to get lock: %v\n", err)
63+
return false, err
64+
}
65+
if existingLockTransactionId != nil {
66+
if *existingLockTransactionId == prNumber {
67+
return true, nil
68+
} else {
69+
transactionIdStr := strconv.Itoa(*existingLockTransactionId)
70+
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"
71+
projectLock.PrManager.PublishComment(prNumber, comment)
72+
return false, nil
73+
}
74+
}
5975
lockAcquired, err := projectLock.InternalLock.Lock(prNumber, lockId)
6076
if err != nil {
6177
return false, err
@@ -65,15 +81,9 @@ func (projectLock *ProjectLockImpl) Lock(prNumber int) (bool, error) {
6581
comment := "Project " + projectLock.projectId() + " has been locked by PR #" + strconv.Itoa(prNumber)
6682
projectLock.PrManager.PublishComment(prNumber, comment)
6783
println("project " + projectLock.projectId() + " locked successfully. PR # " + strconv.Itoa(prNumber))
68-
return true, nil
69-
}
70-
var transactionIdStr string
71-
transactionId, _ := projectLock.InternalLock.GetLock(lockId)
72-
transactionIdStr = strconv.Itoa(*transactionId)
7384

74-
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"
75-
projectLock.PrManager.PublishComment(prNumber, comment)
76-
return false, nil
85+
}
86+
return lockAcquired, nil
7787
}
7888

7989
func (projectLock *ProjectLockImpl) verifyNoHangingLocks(prNumber int) (bool, error) {

pkg/utils/plan_storage.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package utils
22

33
import (
4-
"cloud.google.com/go/storage"
54
"context"
65
"fmt"
7-
"github.com/google/go-github/v51/github"
86
"io"
97
"net/http"
108
"net/url"
119
"os"
1210
"strconv"
11+
12+
"cloud.google.com/go/storage"
13+
"github.com/google/go-github/v51/github"
1314
)
1415

1516
type PlanStorage interface {

0 commit comments

Comments
 (0)