forked from thoughtworks/talisman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
114 lines (97 loc) · 3.4 KB
/
runner.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"os"
log "github.com/wendyi/logrus"
"github.com/wendyi/talisman/detector"
"github.com/wendyi/talisman/git_repo"
)
const (
//EmptySha represents the state of a brand new ref
EmptySha string = "0000000000000000000000000000000000000000"
//CompletedSuccessfully is an exit status that says that the current runners run completed without errors
CompletedSuccessfully int = 0
//CompletedWithErrors is an exit status that says that the current runners run completed with failures
CompletedWithErrors int = 1
)
//Runner represents a single run of the validations for a given commit range
type Runner struct {
localRef, localCommit, remoteRef, remoteCommit string
results *detector.DetectionResults
}
//NewRunner returns a new Runner.
func NewRunner(localRef, localCommit, remoteRef, remoteCommit string) *Runner {
return &Runner{localRef, localCommit, remoteRef, remoteCommit, detector.NewDetectionResults()}
}
//RunWithoutErrors will validate the commit range for errors and return either COMPLETED_SUCCESSFULLY or COMPLETED_WITH_ERRORS
//Brand new repositoris are not validated at all
//If the outgoing ref does not exist on the remote, all commits on the local ref will be checked
//If the outgoing ref already exists, all additions in the range beween "localSha" and "remoteSha" will be validated
func (r *Runner) RunWithoutErrors() int {
fmt.Println("RunWithoutErrors")
if r.runningOnDeletedRef() {
log.WithFields(log.Fields{
"localRef": r.localRef,
"localCommit": r.localCommit,
"remoteRef": r.remoteRef,
"remoteCommit": r.remoteCommit,
}).Info("Running on a deleted ref. Nothing to verify as outgoing changes are all deletions.")
return CompletedSuccessfully
}
if r.runningOnNewRef() {
return r.checkAllCommitsInNewRef()
}
return r.checkAllCommitsInRange()
}
func (r *Runner) checkAllCommitsInNewRef() int {
log.WithFields(log.Fields{
"localRef": r.localRef,
"localCommit": r.localCommit,
"remoteRef": r.remoteRef,
"remoteCommit": r.remoteCommit,
}).Info("Running on a new ref. All changes in the ref will be verified.")
return CompletedSuccessfully
}
func (r *Runner) checkAllCommitsInRange() int {
fmt.Println("checkAllCommitsInRange")
log.WithFields(log.Fields{
"localRef": r.localRef,
"localCommit": r.localCommit,
"remoteRef": r.remoteRef,
"remoteCommit": r.remoteCommit,
}).Info("Running on an existing ref. All changes in the commit range will be verified.")
r.doRun()
r.printReport()
return r.exitStatus()
}
func (r *Runner) doRun() {
ignores := detector.ReadIgnoresFromFile(readRepoFile())
detector.DefaultChain().Test(r.getRepoAdditions(), ignores, r.results)
}
func (r *Runner) printReport() {
if r.results.HasIgnores() || r.results.HasFailures() {
fmt.Println(r.results.Report())
}
}
func (r *Runner) exitStatus() int {
if r.results.HasFailures() {
return CompletedWithErrors
}
return CompletedSuccessfully
}
func (r *Runner) getRepoAdditions() []git_repo.Addition {
wd, _ := os.Getwd()
repo := git_repo.RepoLocatedAt(wd)
return repo.Additions(r.remoteCommit, r.localCommit, wd)
}
func (r *Runner) runningOnDeletedRef() bool {
return r.localCommit == EmptySha
}
func (r *Runner) runningOnNewRef() bool {
return r.remoteCommit == EmptySha
}
func readRepoFile() func(string) ([]byte, error) {
wd, _ := os.Getwd()
repo := git_repo.RepoLocatedAt(wd)
return repo.ReadRepoFileOrNothing
}