Skip to content

Commit 07934f5

Browse files
committed
Merge branch 'release/1.4.0'
2 parents c1d507e + ea81b2e commit 07934f5

File tree

6 files changed

+246
-7
lines changed

6 files changed

+246
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ If you have any issues, please open one here on Github or hit me up on twitter [
112112

113113
## CHANGELOG
114114

115+
116+
## 1.4.0
117+
118+
* Added the pivotal tracker client. Thanks to [@stephensxu](http://github.com/stephensxu).
119+
In order to create the client and connect to pivotal tracker, you run `gong login pivotal`
120+
115121
### 1.3.4
116122

117123
* Added the `create` command. Opens up the browser on the create ticket URL for

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Create(client Client) (string, error) {
3030
func PrepareCommitMessage(client Client, branchName, commitMessage string) string {
3131
return client.PrepareCommitMessage(branchName, commitMessage)
3232
}
33-
33+
3434
// Comment : Comment on an issue
3535
func Comment(client Client, branchName, comment string) error {
3636
return client.Comment(branchName, comment)
@@ -52,9 +52,13 @@ func NewClient(clientName string) (Client, error) {
5252
return NewJiraClient(), nil
5353
}
5454

55+
if clientName == "pivotal" {
56+
return NewPivotalClient(), nil
57+
}
58+
5559
return nil, fmt.Errorf("Could not find client: %v", clientName)
5660
}
57-
61+
5862
// NewAuthenticatedClient : Return a new client authenticated
5963
func NewAuthenticatedClient() (Client, error) {
6064
fields, err := Load()

cmd/gong/.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.3.4
2+
current_version = 1.4.0
33
commit = False
44
tag = False
55

cmd/gong/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import (
66
"os"
77
"os/exec"
88

9+
"github.com/KensoDev/gong"
910
"github.com/fatih/color"
10-
"github.com/kensodev/gong"
1111
"github.com/urfave/cli"
1212
)
1313

1414
func main() {
1515
app := cli.NewApp()
16-
app.Version = "1.3.4"
16+
app.Version = "1.4.0"
1717

1818
var branchType string
1919

2020
app.Commands = []cli.Command{
2121
{
2222
Name: "login",
23-
Usage: "Login to your Jira Instance",
23+
Usage: "Login to your project managment tool instance",
2424
Action: func(c *cli.Context) error {
2525
clientName := c.Args()[0]
2626
client, err := gong.NewClient(clientName)
@@ -68,7 +68,6 @@ func main() {
6868
}
6969

7070
branchName, err := gong.Start(client, branchType, issueId)
71-
7271
if err != nil {
7372
color.Red("Problem with starting the issue")
7473
}

pivotal.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package gong
2+
3+
import (
4+
"fmt"
5+
"gopkg.in/salsita/go-pivotaltracker.v1/v5/pivotal"
6+
"strconv"
7+
"regexp"
8+
)
9+
10+
// PivotalClient : Struct implementing the generic Client interface
11+
type PivotalClient struct {
12+
client *pivotal.Client
13+
config map[string]string
14+
}
15+
16+
// NewPivotalClient : Returns a pointer to PivotalClient
17+
func NewPivotalClient() *PivotalClient {
18+
return &PivotalClient{}
19+
}
20+
21+
func (p *PivotalClient) Create() (string, error) {
22+
fields, err := Load()
23+
24+
if err != nil {
25+
fmt.Println(err)
26+
}
27+
28+
projectId := fields["projectId"]
29+
30+
domain := "https://www.pivotaltracker.com/n/projects"
31+
url := fmt.Sprintf("%s/%s", domain, projectId)
32+
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
return url, nil
38+
}
39+
40+
// GetName : Return the string name of the struct eg: pivotal
41+
func (p *PivotalClient) GetName() string {
42+
return "pivotal"
43+
}
44+
45+
// Browse : Browse to the URL of the issue related to the branch name
46+
func (p *PivotalClient) Browse(branchName string) (string, error) {
47+
issueID := GetPivotalIssueID(branchName)
48+
domain := "https://www.pivotaltracker.com/story/show"
49+
url := fmt.Sprintf("%s/%s", domain, issueID)
50+
return url, nil
51+
}
52+
53+
func GetPivotalIssueID(branchName string) string {
54+
re := regexp.MustCompile(`([\d]+)`)
55+
matches := re.FindAllString(branchName, -1)
56+
57+
if len(matches) == 0 {
58+
return ""
59+
}
60+
61+
return matches[0]
62+
}
63+
64+
// Start : Start an issue
65+
func (p *PivotalClient) Start(issueType string, issueID string) (string, error) {
66+
projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID)
67+
68+
if err != nil {
69+
fmt.Println(err)
70+
}
71+
72+
request := &pivotal.StoryRequest{}
73+
request.State = "started"
74+
75+
_, _, err = p.client.Stories.Update(projectIdInt, issueIdInt, request)
76+
77+
if err != nil {
78+
fmt.Println(err)
79+
}
80+
81+
branchName, err := p.GetBranchName(issueType, issueID)
82+
83+
if err != nil {
84+
return "", err
85+
}
86+
87+
fmt.Println("branch name:", branchName)
88+
return branchName, nil
89+
}
90+
91+
func (p *PivotalClient) Comment(branchName, comment string) error {
92+
fmt.Println(branchName)
93+
issueID := GetPivotalIssueID(branchName)
94+
fmt.Println(issueID)
95+
96+
projectIdInt, issueIdInt, err := p.GetProjectIdAndIssueId(issueID)
97+
98+
if err != nil {
99+
fmt.Println(err)
100+
}
101+
102+
commentObject := &pivotal.Comment{}
103+
commentObject.Text = comment
104+
105+
_, _, err = p.client.Stories.AddComment(projectIdInt, issueIdInt, commentObject)
106+
107+
if err != nil {
108+
fmt.Println(err)
109+
}
110+
111+
fmt.Println("added comment", comment)
112+
return nil
113+
}
114+
115+
func (p *PivotalClient) PrepareCommitMessage(branchName, commitMessage string) string {
116+
issueID := GetPivotalIssueID(branchName)
117+
url, err := p.Browse(branchName)
118+
119+
if err != nil {
120+
return commitMessage
121+
}
122+
123+
patchedCommitMessage := fmt.Sprintf(`[%s](%s)`, issueID, url)
124+
125+
return patchedCommitMessage
126+
}
127+
128+
func (p *PivotalClient) GetBranchName(issueType string, issueID string) (string, error) {
129+
projectIdInt, issueIdInt, _ := p.GetProjectIdAndIssueId(issueID)
130+
131+
story, _, err := p.client.Stories.Get(projectIdInt, issueIdInt)
132+
133+
if err != nil {
134+
fmt.Println(err)
135+
}
136+
137+
issueTitleSlug := SlugifyTitle(story.Name)
138+
139+
return fmt.Sprintf("%s/%s-%s", issueType, issueID, issueTitleSlug), nil
140+
}
141+
142+
func (p *PivotalClient) GetProjectIdAndIssueId(issueID string) (int, int, error) {
143+
fields, err := Load()
144+
if err != nil {
145+
return 0, 0, err
146+
}
147+
148+
projectIdInt, err := strconv.Atoi(fields["projectId"])
149+
if err != nil {
150+
return 0, 0, err
151+
}
152+
153+
fmt.Println("projectIdInt", projectIdInt)
154+
155+
issueIdInt, err := strconv.Atoi(issueID)
156+
if err != nil {
157+
return 0, 0, err
158+
}
159+
160+
fmt.Println("issueIdInt", issueIdInt)
161+
return projectIdInt, issueIdInt, nil
162+
}
163+
164+
// FormatField : Returns a formatted field based on internal rules
165+
func (p *PivotalClient) FormatField(fieldName string, value string) string {
166+
return value
167+
}
168+
169+
// Authenticate : Authenticates using the fields passed in
170+
func (p *PivotalClient) Authenticate(fields map[string]string) bool {
171+
pivotalClient := pivotal.NewClient(fields["apiToken"])
172+
_, _, err := pivotalClient.Me.Get()
173+
174+
if err != nil {
175+
return false
176+
}
177+
178+
p.client = pivotalClient
179+
p.config = fields
180+
181+
return true
182+
}
183+
184+
func (p *PivotalClient) GetAuthFields() map[string]bool {
185+
return map[string]bool{
186+
"projectId": false,
187+
"apiToken": true,
188+
}
189+
}

pivotal_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package gong
2+
3+
import (
4+
. "gopkg.in/check.v1"
5+
"testing"
6+
)
7+
8+
func TestPivotalClient(t *testing.T) { TestingT(t) }
9+
10+
type PivotalClientSuit struct{}
11+
12+
var _ = Suite(&PivotalClientSuit{})
13+
14+
func (p *PivotalClientSuit) TestBrowseWithCorrectBranchName(c * C) {
15+
pivotalClient := &PivotalClient{}
16+
17+
url, err := pivotalClient.Browse("feature/124352-test-only")
18+
c.Assert(err, Equals, nil)
19+
c.Assert(url, Equals, "https://www.pivotaltracker.com/story/show/124352")
20+
}
21+
22+
func (p *PivotalClientSuit) TestBrowseWithIncorrectBranchName(c * C) {
23+
pivotalClient := &PivotalClient{}
24+
25+
url, err := pivotalClient.Browse("feature/test-only")
26+
c.Assert(err, Equals, nil)
27+
c.Assert(url, Equals, "https://www.pivotaltracker.com/story/show/")
28+
}
29+
30+
func (p *PivotalClientSuit) TestGetPivotalIssueIDWithCorrectBranchName(c * C) {
31+
branchName := "feature/1234-test-only"
32+
issueId := GetPivotalIssueID(branchName)
33+
c.Assert(issueId, Equals, "1234")
34+
}
35+
36+
37+
func (p *PivotalClientSuit) TestGetPivotalIssueIDWithInCorrectBranchName(c * C) {
38+
branchName := "feature/test-only"
39+
issueId := GetPivotalIssueID(branchName)
40+
c.Assert(issueId, Equals, "")
41+
}

0 commit comments

Comments
 (0)