From 6fe86f4b150d8ab7d8979e88eae8cc338d285a21 Mon Sep 17 00:00:00 2001 From: angeloskaltsikis Date: Fri, 12 Feb 2021 16:16:04 +0200 Subject: [PATCH] Add function that returns if PR is locked This function returns whether a specific pull request for a project is locked or not. --- server/events/working_dir_locker.go | 11 +++++++++++ server/events/working_dir_locker_test.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/server/events/working_dir_locker.go b/server/events/working_dir_locker.go index 7642e0ca28..898cb52192 100644 --- a/server/events/working_dir_locker.go +++ b/server/events/working_dir_locker.go @@ -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. @@ -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 +} \ No newline at end of file diff --git a/server/events/working_dir_locker_test.go b/server/events/working_dir_locker_test.go index 49450e69dc..5c68e3c0ea 100644 --- a/server/events/working_dir_locker_test.go +++ b/server/events/working_dir_locker_test.go @@ -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") +} \ No newline at end of file