-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpivotal.go
189 lines (140 loc) · 4.04 KB
/
pivotal.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gong
import (
"fmt"
"gopkg.in/salsita/go-pivotaltracker.v1/v5/pivotal"
"regexp"
"strconv"
)
// PivotalClient : Struct implementing the generic Client interface
type PivotalClient struct {
client *pivotal.Client
config map[string]string
}
// NewPivotalClient : Returns a pointer to PivotalClient
func NewPivotalClient() *PivotalClient {
return &PivotalClient{}
}
func (p *PivotalClient) Create() (string, error) {
fields, err := Load()
if err != nil {
fmt.Println(err)
}
projectId := fields["projectId"]
domain := "https://www.pivotaltracker.com/n/projects"
url := fmt.Sprintf("%s/%s", domain, projectId)
if err != nil {
return "", err
}
return url, nil
}
// GetName : Return the string name of the struct eg: pivotal
func (p *PivotalClient) GetName() string {
return "pivotal"
}
// Browse : Browse to the URL of the issue related to the branch name
func (p *PivotalClient) Browse(branchName string) (string, error) {
issueID := GetPivotalIssueID(branchName)
domain := "https://www.pivotaltracker.com/story/show"
url := fmt.Sprintf("%s/%s", domain, issueID)
return url, nil
}
func GetPivotalIssueID(branchName string) string {
re := regexp.MustCompile(`([\d]+)`)
matches := re.FindAllString(branchName, -1)
if len(matches) == 0 {
return ""
}
return matches[0]
}
// Start : Start an issue
func (p *PivotalClient) Start(issueType string, issueID string) (string, error) {
projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID)
if err != nil {
fmt.Println(err)
}
request := &pivotal.StoryRequest{}
request.State = "started"
_, _, err = p.client.Stories.Update(projectIdInt, issueIdInt, request)
if err != nil {
fmt.Println(err)
}
branchName, err := p.GetBranchName(issueType, issueID)
if err != nil {
return "", err
}
fmt.Println("branch name:", branchName)
return branchName, nil
}
func (p *PivotalClient) Comment(branchName, comment string) error {
fmt.Println(branchName)
issueID := GetPivotalIssueID(branchName)
fmt.Println(issueID)
projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID)
if err != nil {
fmt.Println(err)
}
commentObject := &pivotal.Comment{}
commentObject.Text = comment
_, _, err = p.client.Stories.AddComment(projectIdInt, issueIdInt, commentObject)
if err != nil {
fmt.Println(err)
}
fmt.Println("added comment", comment)
return nil
}
func (p *PivotalClient) PrepareCommitMessage(branchName, commitMessage string) string {
issueID := GetPivotalIssueID(branchName)
url, err := p.Browse(branchName)
if err != nil {
return commitMessage
}
patchedCommitMessage := fmt.Sprintf(`[%s](%s)`, issueID, url)
return patchedCommitMessage
}
func (p *PivotalClient) GetBranchName(issueType string, issueID string) (string, error) {
projectIdInt, issueIdInt, _ := p.GetProjectIdAndIssueId(issueID)
story, _, err := p.client.Stories.Get(projectIdInt, issueIdInt)
if err != nil {
fmt.Println(err)
}
issueTitleSlug := SlugifyTitle(story.Name)
return fmt.Sprintf("%s/%s-%s", issueType, issueID, issueTitleSlug), nil
}
func (p *PivotalClient) GetProjectIdAndIssueId(issueID string) (int, int, error) {
fields, err := Load()
if err != nil {
return 0, 0, err
}
projectIdInt, err := strconv.Atoi(fields["projectId"])
if err != nil {
return 0, 0, err
}
fmt.Println("projectIdInt", projectIdInt)
issueIdInt, err := strconv.Atoi(issueID)
if err != nil {
return 0, 0, err
}
fmt.Println("issueIdInt", issueIdInt)
return projectIdInt, issueIdInt, nil
}
// FormatField : Returns a formatted field based on internal rules
func (p *PivotalClient) FormatField(fieldName string, value string) string {
return value
}
// Authenticate : Authenticates using the fields passed in
func (p *PivotalClient) Authenticate(fields map[string]string) bool {
pivotalClient := pivotal.NewClient(fields["apiToken"])
_, _, err := pivotalClient.Me.Get()
if err != nil {
return false
}
p.client = pivotalClient
p.config = fields
return true
}
func (p *PivotalClient) GetAuthFields() map[string]bool {
return map[string]bool{
"projectId": false,
"apiToken": true,
}
}