Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
Merge branch 'release/1.3.0'
  • Loading branch information
KensoDev committed Jun 9, 2017
2 parents a4f8872 + 4828283 commit 4e046f3
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 37 deletions.
12 changes: 11 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
We are in the process of refactoring to a generic client interface which you can easily extend to differnet clients.

☐ gong comment
☐ gong comment










36 changes: 25 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,55 @@ package gong

import (
"encoding/json"
"errors"
"fmt"
"github.com/segmentio/go-prompt"
"io/ioutil"
"os/user"
"path/filepath"
)

// Client : Public interface for the generic client
type Client interface {
GetAuthFields() map[string]bool
GetName() string
FormatField(fieldName string, value string) string
Authenticate(fields map[string]string) bool
Start(issueType string, issueId string) (branchName string, err error)
Start(issueType string, issueID string) (branchName string, err error)
Browse(branchName string) (string, error)
Comment(branchName, comment string) error
PrepareCommitMessage(branchName, commitMessage string) string
}

// PrepareCommitMessage : Prepares the commit message and returns a new commit message
func PrepareCommitMessage(client Client, branchName, commitMessage string) string {
return client.PrepareCommitMessage(branchName, commitMessage)
}

// Comment : Comment on an issue
func Comment(client Client, branchName, comment string) error {
return client.Comment(branchName, comment)
}

// Browse : Open a browser on the issue related to the branch
func Browse(client Client, branchName string) (string, error) {
return client.Browse(branchName)
}

func Start(client Client, issueType, issueId string) (string, error) {
return client.Start(issueType, issueId)
// Start : Start working on an issue
func Start(client Client, issueType, issueID string) (string, error) {
return client.Start(issueType, issueID)
}

// NewClient : Return a new client that matches the name passed in
func NewClient(clientName string) (Client, error) {
if clientName == "jira" {
return NewJiraClient(), nil
}

return nil, errors.New(fmt.Sprintf("Could not find client: %v", clientName))
return nil, fmt.Errorf("Could not find client: %v", clientName)
}

// NewAuthenticatedClient : Return a new client authenticated
func NewAuthenticatedClient() (Client, error) {
fields, err := Load()

Expand All @@ -59,9 +70,10 @@ func NewAuthenticatedClient() (Client, error) {
return client, nil
}

return nil, errors.New("Could not load authenticated client")
return nil, fmt.Errorf("Could not load authenticated client")
}

// Login : Logs the user into the specified client
func Login(client Client) (bool, error) {
clientName := client.GetName()

Expand Down Expand Up @@ -96,7 +108,7 @@ func Login(client Client) (bool, error) {
return client.Authenticate(fields), nil
}

func GetUserHomeOrDefault() string {
func getUserHomeOrDefault() string {
usr, err := user.Current()

if err != nil {
Expand All @@ -106,13 +118,14 @@ func GetUserHomeOrDefault() string {
return usr.HomeDir
}

func GetFileLocation() string {
dir := GetUserHomeOrDefault()
func getFileLocation() string {
dir := getUserHomeOrDefault()
return filepath.Join(dir, ".gong.json")
}

// Load : Load the configuration from a file
func Load() (map[string]string, error) {
fileLocation := GetFileLocation()
fileLocation := getFileLocation()
var c = map[string]string{}

file, err := ioutil.ReadFile(fileLocation)
Expand All @@ -130,8 +143,9 @@ func Load() (map[string]string, error) {
return c, nil
}

// Save : saves the configuration to a file
func Save(values map[string]string) error {
fileLocation := GetFileLocation()
fileLocation := getFileLocation()
loginDetails, err := json.Marshal(values)

if err != nil {
Expand Down
26 changes: 19 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,34 @@ func (f *FakeClient) FormatField(fieldName string, value string) string {
return ""
}

func (receiver *FakeClient) GetAuthFields() map[string]bool {
func (f *FakeClient) GetAuthFields() map[string]bool {
return map[string]bool{}
}

func (c *FakeClient) GetName() string {
func (f *FakeClient) GetName() string {
return "fakeclient"
}

func (c *FakeClient) Authenticate(field map[string]string) bool {
func (f *FakeClient) Authenticate(field map[string]string) bool {
return false
}

func (c *FakeClient) Start(issueType, issueId string) (string, error) {
return fmt.Sprintf("%s/%s", issueType, issueId), nil
func (f *FakeClient) Start(issueType, issueID string) (string, error) {
return fmt.Sprintf("%s/%s", issueType, issueID), nil
}

func (c *FakeClient) Browse(branchName string) (string, error) {
func (f *FakeClient) Browse(branchName string) (string, error) {
return "https://www.fake.com/FAKE-1111", nil
}

func (c *FakeClient) Comment(branchName, comment string) error {
func (f *FakeClient) Comment(branchName, comment string) error {
return nil
}

func (f *FakeClient) PrepareCommitMessage(branchName, commitMessage string) string {
return "Fake commit message"
}

func (s *ClientSuite) TestClientStartIssue(c *C) {
client := &FakeClient{}
branchName, _ := Start(client, "feature", "111")
Expand All @@ -62,3 +66,11 @@ func (s *ClientSuite) TestComment(c *C) {
err := Comment(client, branchName, comment)
c.Assert(err, Equals, nil)
}

func (s *ClientSuite) TestPrepareCommitMessage(c *C) {
client := &FakeClient{}
branchName := "feature/FAKE-1111-some-issue-title"
commitMessage := "This is a sample comment"
newMessage := PrepareCommitMessage(client, branchName, commitMessage)
c.Assert(newMessage, Equals, "Fake commit message")
}
2 changes: 1 addition & 1 deletion cmd/gong/.bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.2.0
current_version = 1.3.0
commit = False
tag = False

Expand Down
39 changes: 38 additions & 1 deletion cmd/gong/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -12,7 +13,7 @@ import (

func main() {
app := cli.NewApp()
app.Version = "v1.2.0"
app.Version = "v1.3.0"

var branchType string

Expand Down Expand Up @@ -166,6 +167,42 @@ func main() {

},
},
{
Name: "prepare-commit-message",
Usage: "This is a prepare-commit-message hook for git",
Action: func(c *cli.Context) error {
client, err := gong.NewAuthenticatedClient()

if err != nil {
color.Red("Problem with starting the issue")
return err
}

cmd := "git"
args := []string{"rev-parse", "--abbrev-ref", "HEAD"}

out, err := exec.Command(cmd, args...).Output()

if err != nil {
return err
}

branchName := string(out)
bytes, err := ioutil.ReadAll(os.Stdin)

if err != nil {
color.Red("Could not read stdin")
return err
}

commitMessage := string(bytes)
newCommitMessge := gong.PrepareCommitMessage(client, branchName, commitMessage)

fmt.Println(newCommitMessge)

return nil
},
},
}
app.Run(os.Args)
}
Loading

0 comments on commit 4e046f3

Please sign in to comment.