-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdelete_lock_command.go
74 lines (61 loc) · 2.29 KB
/
delete_lock_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package events
import (
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
)
//go:generate pegomock generate --package mocks -o mocks/mock_delete_lock_command.go DeleteLockCommand
// DeleteLockCommand is the first step after a command request has been parsed.
type DeleteLockCommand interface {
DeleteLock(id string) (*models.ProjectLock, error)
DeleteLocksByPull(repoFullName string, pullNum int) (int, error)
}
// DefaultDeleteLockCommand deletes a specific lock after a request from the LocksController.
type DefaultDeleteLockCommand struct {
Locker locking.Locker
Logger logging.SimpleLogging
WorkingDir WorkingDir
WorkingDirLocker WorkingDirLocker
Backend locking.Backend
}
// DeleteLock handles deleting the lock at id
func (l *DefaultDeleteLockCommand) DeleteLock(id string) (*models.ProjectLock, error) {
lock, err := l.Locker.Unlock(id)
if err != nil {
return nil, err
}
if lock == nil {
return nil, nil
}
// The locks controller currently has no implementation of Atlantis project names, so this is hardcoded to an empty string.
projectName := ""
removeErr := l.WorkingDir.DeletePlan(lock.Pull.BaseRepo, lock.Pull, lock.Workspace, lock.Project.Path, projectName)
if removeErr != nil {
l.Logger.Warn("Failed to delete plan: %s", removeErr)
return nil, removeErr
}
return lock, nil
}
// DeleteLocksByPull handles deleting all locks for the pull request
func (l *DefaultDeleteLockCommand) DeleteLocksByPull(repoFullName string, pullNum int) (int, error) {
locks, err := l.Locker.UnlockByPull(repoFullName, pullNum)
numLocks := len(locks)
if err != nil {
return numLocks, err
}
if numLocks == 0 {
l.Logger.Debug("No locks found for repo '%v', pull request: %v", repoFullName, pullNum)
return numLocks, nil
}
// The locks controller currently has no implementation of Atlantis project names, so this is hardcoded to an empty string.
projectName := ""
for i := 0; i < numLocks; i++ {
lock := locks[i]
err := l.WorkingDir.DeletePlan(lock.Pull.BaseRepo, lock.Pull, lock.Workspace, lock.Project.Path, projectName)
if err != nil {
l.Logger.Warn("Failed to delete plan: %s", err)
return numLocks, err
}
}
return numLocks, nil
}