-
Notifications
You must be signed in to change notification settings - Fork 102
/
commit.go
66 lines (60 loc) · 1.73 KB
/
commit.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
package test
import (
"fmt"
"github.com/DATA-DOG/godog/gherkin"
"github.com/Originate/git-town/test/helpers"
"github.com/pkg/errors"
)
// Commit describes a Git commit.
type Commit struct {
Author string
Branch string
FileContent string
FileName string
Locations []string
Message string
SHA string
}
// DefaultCommit provides a new Commit instance populated with the default values used in the absence of value specified by the test.
func DefaultCommit() Commit {
return Commit{
FileName: "default_file_name_" + helpers.UniqueString(),
Message: "default commit message",
Locations: []string{"local", "remote"},
Branch: "main",
FileContent: "default file content",
}
}
// FromGherkinTable provides a Commit collection representing the data in the given Gherkin table.
func FromGherkinTable(table *gherkin.DataTable) (result []Commit, err error) {
columnNames := helpers.TableFields(table)
for _, row := range table.Rows[1:] {
commit := DefaultCommit()
for i, cell := range row.Cells {
err := commit.set(columnNames[i], cell.Value)
if err != nil {
return result, errors.Wrapf(err, "cannot set property %q to %q", columnNames[i], cell.Value)
}
}
result = append(result, commit)
}
return result, nil
}
// Set assigns the given value to the property with the given Gherkin table name.
func (commit *Commit) set(name, value string) (err error) {
switch name {
case "BRANCH":
commit.Branch = value
case "LOCATION":
commit.Locations = []string{value}
case "MESSAGE":
commit.Message = value
case "FILE NAME":
commit.FileName = value
case "FILE CONTENT":
commit.FileContent = value
default:
return fmt.Errorf("unknown Commit property: %s", name)
}
return nil
}