gunk-dev/the-valley

lock.go

source6a6791929c36main's published tip history
924 bytes
1package review
2
3import (
4 "errors"
5 "fmt"
6 "path/filepath"
7 "strings"
8
9 "the-valley/flow/internal/location"
10)
11
12// ErrLocked: another klaus review --post holds this PR's lock.
13var ErrLocked = errors.New("another valley flow review --post is running for this PR")
14
15// lockPR takes a non-blocking exclusive flock on review-<owner>-<repo>-<n>.lock
16// in each of dirs, in order; the returned func releases it.
17func lockPR(dirs []string, repo, prNumber string) (func(), error) {
18 name := strings.NewReplacer("/", "-", string(filepath.Separator), "-").Replace("review-" + repo + "-" + prNumber + ".lock")
19 var paths []string
20 for _, dir := range dirs {
21 paths = append(paths, filepath.Join(dir, name))
22 }
23 l, err := location.TryLock(paths...)
24 var busy *location.BusyError
25 if errors.As(err, &busy) {
26 return nil, fmt.Errorf("%w (lock %s)", ErrLocked, busy.Path)
27 }
28 if err != nil {
29 return nil, err
30 }
31 return l.Release, nil
32}