Skip to content

Commit

Permalink
Add function that returns if PR is locked
Browse files Browse the repository at this point in the history
This function returns whether a specific pull request for a project is
locked or not.
  • Loading branch information
angeloskaltsikis committed Feb 12, 2021
1 parent af2a806 commit 6fe86f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/events/working_dir_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type WorkingDirLocker interface {
// an error if the workspace is already locked. The error is expected to
// be printed to the pull request.
TryLockPull(repoFullName string, pullNum int) (func(), error)
IsPullLocked(repoFullName string, pullNum int) (bool, error)
}

// DefaultWorkingDirLocker implements WorkingDirLocker.
Expand Down Expand Up @@ -128,3 +129,13 @@ func (d *DefaultWorkingDirLocker) workspaceKey(repo string, pull int, workspace
func (d *DefaultWorkingDirLocker) pullKey(repo string, pull int) string {
return fmt.Sprintf("%s/%d", repo, pull)
}

func (d *DefaultWorkingDirLocker) IsPullLocked(repoFullName string, pullNum int) (bool, error) {
pullKey := d.pullKey(repoFullName, pullNum)
for _, l := range d.locks {
if l == pullKey || strings.HasPrefix(l, pullKey+"/") {
return true, nil
}
}
return false, nil
}
18 changes: 18 additions & 0 deletions server/events/working_dir_locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,21 @@ func TestLockPull_WorkspaceFirst(t *testing.T) {
_, err = locker.TryLockPull("owner/repo", 1)
Ok(t, err)
}

func TestIsPullLocked(t *testing.T) {
locker := events.NewDefaultWorkingDirLocker()

isLocked, err := locker.IsPullLocked("owner/repo", 1)
Assert(t, isLocked == false, "exp unlocked")

unlock, err := locker.TryLockPull("owner/repo", 1)
Ok(t, err)

isLocked, err = locker.IsPullLocked("owner/repo", 1)
Assert(t, isLocked == true, "exp locked")

// After unlocking, should be able to get a pull lock.
unlock()
isLocked, err = locker.IsPullLocked("owner/repo", 1)
Assert(t, isLocked == false, "exp unlocked")
}

0 comments on commit 6fe86f4

Please sign in to comment.