Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from prithvijit-dasgupta/master
Browse files Browse the repository at this point in the history
[Issue #1] List PR Feature
  • Loading branch information
thamaraiselvam committed Nov 2, 2019
2 parents f86b817 + 530e63d commit ab1e335
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 5 deletions.
47 changes: 47 additions & 0 deletions cmd/list/pull_request.go
@@ -0,0 +1,47 @@
package list

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/thamaraiselvam/git-api-cli/cmd/service"
"github.com/thamaraiselvam/git-api-cli/cmd/types"
"github.com/thamaraiselvam/git-api-cli/cmd/util"
)

func init() {
listCmd.AddCommand(prInfoCmd)
}

var prInfoCmd = &cobra.Command{
Use: "pr",
Short: "list pull-requests of current user",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("enter your name")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
client := service.CreateClient(fmt.Sprintf("/search/issues?q=type:pr+author:%%22%s%%22&sort=created&order=desc", args[0]))
prList, err := client.GetPRList()
if err != nil {
_ = fmt.Errorf("%v", err)
os.Exit(1)
}

displayPRList(prList)
},
}

//Generates Table to be displayeds
func displayPRList(prList types.PRItemList) {
table := util.CreateTable()
table.SetHeader([]string{"Title", "URL", "State"})
for i := 0; i < len(prList.Items); i++ {
table.Append([]string{prList.Items[i].Title, prList.Items[i].PullRequest.URL, prList.Items[i].State})
}
table.Render()

}
18 changes: 18 additions & 0 deletions cmd/service/service.go
Expand Up @@ -17,6 +17,7 @@ type Client interface {
GetGists() (types.Gists, error)
GetFollowing() (types.FollowingUsers, error)
GetFollowers() (types.Followers, error)
GetPRList() (types.PRItemList, error)
}

type config struct {
Expand All @@ -31,6 +32,23 @@ func CreateClient(path string) Client {
}
}

//GetPRList get pull-request information for certain user
func (config config) GetPRList() (types.PRItemList, error) {
resp, err := makeRequest(http.MethodGet, config.URL, nil)

if err != nil {
return types.PRItemList{}, err
}

var prItemList types.PRItemList

if err := json.NewDecoder(resp.Body).Decode(&prItemList); err != nil {
return types.PRItemList{}, err
}

return prItemList, nil
}

//GetFollowers fetches followers list of user
func (config config) GetFollowers() (types.Followers, error) {
resp, err := makeRequest(http.MethodGet, config.URL, nil)
Expand Down
51 changes: 46 additions & 5 deletions cmd/service/service_test.go
@@ -1,12 +1,13 @@
package service

import (
"github.com/stretchr/testify/assert"
"github.com/thamaraiselvam/git-api-cli/cmd/types"
"gopkg.in/h2non/gock.v1"
"io"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/thamaraiselvam/git-api-cli/cmd/types"
"gopkg.in/h2non/gock.v1"
)

func Test_makeRequest(t *testing.T) {
Expand Down Expand Up @@ -233,13 +234,13 @@ func TestHTTPConfig_GetGists(t *testing.T) {

expectedGists := types.Gists{
{
Files: map[string]interface{}{"test.go": map[string]interface{}{"type": "go"}},
Files: map[string]interface{}{"test.go": map[string]interface{}{"type": "go"}},
URL: "http://github.com/username/test",
CreatedAt: "2019-10-22T14:29:31Z",
Description: "this is test gist",
},
{
Files: map[string]interface{}{"test1.go": map[string]interface{}{"type": "go"}, "test2.go": map[string]interface{}{"type": "go"}},
Files: map[string]interface{}{"test1.go": map[string]interface{}{"type": "go"}, "test2.go": map[string]interface{}{"type": "go"}},
URL: "http://github.com/username/test2",
CreatedAt: "2019-10-21T14:29:31Z",
Description: "this is test gist2",
Expand Down Expand Up @@ -279,3 +280,43 @@ func TestHTTPConfig_GetGists(t *testing.T) {
assert.Equal(t, "404 Not Found", err.Error())
})
}
func TestConfig_GetPRList(t *testing.T) {
t.Run("Should return valid pull-request list on valid request", func(t *testing.T) {
gock.New(githubHost).
Get("/search/issues").
Reply(200).
BodyString(`{"items":[{ "title":"Test Issue","state":"open","pull_request":{"html_url":"www.github.com"}}]}`)

client := CreateClient("/search/issues")
actualPRList, err := client.GetPRList()
assert.NoError(t, err)
assert.Equal(t, "Test Issue", actualPRList.Items[0].Title)
assert.Equal(t, "open", actualPRList.Items[0].State)
assert.Equal(t, "www.github.com", actualPRList.Items[0].PullRequest.URL)
})

t.Run("should return error if response is not a valid json", func(t *testing.T) {
gock.New(githubHost).
Get("/search/issues").
Reply(200).
BodyString(`yo`)

client := CreateClient("/search/issues")
_, err := client.GetPRList()

assert.Error(t, err)
assert.Equal(t, "invalid character 'y' looking for beginning of value", err.Error())
})
t.Run("should return not found on invalid username", func(t *testing.T) {
gock.New(githubHost).
Get("/search/issues").
Reply(404).
BodyString(`{}`)

client := CreateClient("/search/issues")
_, err := client.GetPRList()

assert.Error(t, err)
assert.Equal(t, "404 Not Found", err.Error())
})
}
17 changes: 17 additions & 0 deletions cmd/types/types.go
Expand Up @@ -16,6 +16,23 @@ type follower struct {
//Followers represents list of followers
type Followers []follower

//pRInfo contains URL information for PR
type pRInfo struct {
URL string `json:"html_url"`
}

//pRItem contains details of each PR
type pRItem struct {
Title string `json:"title"`
PullRequest pRInfo `json:"pull_request"`
State string `json:"state"`
}

//PRItemList List of all PR items
type PRItemList struct {
Items []pRItem `json:"items"`
}

//followingUser contains following user information
type followingUser struct {
Name string `json:"login"`
Expand Down
15 changes: 15 additions & 0 deletions cmd/util/table_writer.go
@@ -0,0 +1,15 @@
package util

import (
"os"

"github.com/olekukonko/tablewriter"
)

//CreateTable creates a pre-specified table style to be written in std-out
func CreateTable() *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetRowSeparator(".")
table.SetRowLine(true)
return table
}
14 changes: 14 additions & 0 deletions cmd/util/table_writer_test.go
@@ -0,0 +1,14 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateTable(t *testing.T) {
t.Run("should return user information on valid request", func(t *testing.T) {
var table = CreateTable()
assert.NotNil(t, table)
})
}
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -4,6 +4,8 @@ go 1.13

require (
github.com/golang/mock v1.3.1
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.3.0
Expand Down

0 comments on commit ab1e335

Please sign in to comment.