Skip to content

Commit

Permalink
chore: Refactor jira client (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel authored Jan 2, 2023
1 parent 16a8a61 commit ed84fe7
Show file tree
Hide file tree
Showing 27 changed files with 36 additions and 54 deletions.
5 changes: 5 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func Client(config jira.Config) *jira.Client {
return jiraClient
}

// DefaultClient returns default jira client.
func DefaultClient(debug bool) *jira.Client {
return Client(jira.Config{Debug: debug})
}

// ProxyCreate uses either a v2 or v3 version of the Jira POST /issue
// endpoint to create an issue based on configured installation type.
// Defaults to v3 if installation type is not defined in the config.
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/board/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func List(cmd *cobra.Command, _ []string) {
s := cmdutil.Info(fmt.Sprintf("Fetching boards in project %s...", project))
defer s.Stop()

resp, err := api.Client(jira.Config{Debug: debug}).Boards(project, jira.BoardTypeAll)
resp, err := api.DefaultClient(debug).Boards(project, jira.BoardTypeAll)
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/epic/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func add(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
projectType := viper.GetString("project.type")
params := parseFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)

qs := getQuestions(params)
if len(qs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/epic/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func create(cmd *cobra.Command, _ []string) {
installation := viper.GetString("installation")

params := parseFlags(cmd.Flags())
client := api.Client(jira.Config{Debug: params.Debug})
client := api.DefaultClient(params.Debug)
cc := createCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/epic/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func epicList(cmd *cobra.Command, args []string) {
debug, err := cmd.Flags().GetBool("debug")
cmdutil.ExitIfError(err)

client := api.Client(jira.Config{Debug: debug})
client := api.DefaultClient(debug)

if len(args) == 0 {
epicExplorerView(cmd, cmd.Flags(), project, projectType, server, client)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/epic/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func remove(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
projectType := viper.GetString("project.type")
params := parseFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)

qs := getQuestions(params)
if len(qs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/assign/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ASSIGNEE Email or display name of the user to assign the issue to`,
func assign(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
ac := assignCmd{
client: client,
users: nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func clone(cmd *cobra.Command, args []string) {
projectType := viper.GetString("project.type")

params := parseFlags(cmd.Flags())
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
cc := cloneCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/comment/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewCmdCommentAdd() *cobra.Command {

func add(cmd *cobra.Command, args []string) {
params := parseArgsAndFlags(args, cmd.Flags())
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
ac := addCmd{
client: client,
linkTypes: nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func create(cmd *cobra.Command, _ []string) {
installation := viper.GetString("installation")

params := parseFlags(cmd.Flags())
client := api.Client(jira.Config{Debug: params.Debug})
client := api.DefaultClient(params.Debug)
cc := createCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewCmdDelete() *cobra.Command {
func del(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
mc := deleteCmd{
client: client,
transitions: nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func edit(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")

params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
ec := editCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCmdLink() *cobra.Command {
func link(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
lc := linkCmd{
client: client,
linkTypes: nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/link/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewCmdRemoteLink() *cobra.Command {
func remotelink(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
lc := linkCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func loadList(cmd *cobra.Command) {
return nil, 0, err
}

resp, err := api.ProxySearch(api.Client(jira.Config{Debug: debug}), q.Get(), q.Params().From, q.Params().Limit)
resp, err := api.ProxySearch(api.DefaultClient(debug), q.Get(), q.Params().From, q.Params().Limit)
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func move(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
installation := viper.GetString("installation")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
mc := moveCmd{
client: client,
transitions: nil,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/unlink/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewCmdUnlink() *cobra.Command {
func unlink(cmd *cobra.Command, args []string) {
project := viper.GetString("project.key")
params := parseArgsAndFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
uc := unlinkCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func view(cmd *cobra.Command, args []string) {
s := cmdutil.Info("Fetching issue details...")
defer s.Stop()

client := api.Client(jira.Config{Debug: debug})
client := api.DefaultClient(debug)
return api.ProxyGetIssue(client, key, issue.NewNumCommentsFilter(comments))
}()
cmdutil.ExitIfError(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/issue/worklog/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewCmdWorklogAdd() *cobra.Command {

func add(cmd *cobra.Command, args []string) {
params := parseArgsAndFlags(args, cmd.Flags())
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)
ac := addCmd{
client: client,
params: params,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/project/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func List(cmd *cobra.Command, _ []string) {
s := cmdutil.Info("Fetching projects...")
defer s.Stop()

projects, err := api.Client(jira.Config{Debug: debug}).Project()
projects, err := api.DefaultClient(debug).Project()
if err != nil {
return nil, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/serverinfo/serverinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func serverInfo(cmd *cobra.Command, _ []string) {
s := cmdutil.Info("Fetching server info...")
defer s.Stop()

info, err := api.Client(jira.Config{Debug: debug}).ServerInfo()
info, err := api.DefaultClient(debug).ServerInfo()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/sprint/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ankitpokhrel/jira-cli/api"
"github.com/ankitpokhrel/jira-cli/internal/cmdutil"
"github.com/ankitpokhrel/jira-cli/internal/query"
"github.com/ankitpokhrel/jira-cli/pkg/jira"
)

const (
Expand Down Expand Up @@ -39,7 +38,7 @@ func add(cmd *cobra.Command, args []string) {
server := viper.GetString("server")
project := viper.GetString("project.key")
params := parseFlags(cmd.Flags(), args, project)
client := api.Client(jira.Config{Debug: params.debug})
client := api.DefaultClient(params.debug)

qs := getQuestions(params)
if len(qs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/sprint/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func sprintList(cmd *cobra.Command, args []string) {
debug, err := cmd.Flags().GetBool("debug")
cmdutil.ExitIfError(err)

client := api.Client(jira.Config{Debug: debug})
client := api.DefaultClient(debug)

sprintQuery, err := query.NewSprint(cmd.Flags())
cmdutil.ExitIfError(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/view/epic.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (el *EpicList) Render() error {
dataFn := func() interface{} {
data := d.(tui.TableData)
ci := data.GetIndex(fieldKey)
iss, _ := api.ProxyGetIssue(api.Client(jira.Config{}), data.Get(r, ci), issue.NewNumCommentsFilter(1))
iss, _ := api.ProxyGetIssue(api.DefaultClient(false), data.Get(r, ci), issue.NewNumCommentsFilter(1))
return iss
}
renderFn := func(i interface{}) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/view/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (l *IssueList) Render() error {
tui.WithViewModeFunc(func(r, c int, _ interface{}) (func() interface{}, func(interface{}) (string, error)) {
dataFn := func() interface{} {
ci := data.GetIndex(fieldKey)
iss, _ := api.ProxyGetIssue(api.Client(jira.Config{}), data.Get(r, ci), issue.NewNumCommentsFilter(1))
iss, _ := api.ProxyGetIssue(api.DefaultClient(false), data.Get(r, ci), issue.NewNumCommentsFilter(1))
return iss
}
renderFn := func(i interface{}) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/view/sprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (sl *SprintList) Render() error {
dataFn := func() interface{} {
data := d.(tui.TableData)
ci := data.GetIndex(fieldKey)
iss, _ := api.ProxyGetIssue(api.Client(jira.Config{}), data.Get(r, ci), issue.NewNumCommentsFilter(1))
iss, _ := api.ProxyGetIssue(api.DefaultClient(false), data.Get(r, ci), issue.NewNumCommentsFilter(1))
return iss
}
renderFn := func(i interface{}) (string, error) {
Expand Down
34 changes: 6 additions & 28 deletions pkg/jira/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,47 +174,27 @@ func (c *Client) GetV1(ctx context.Context, path string, headers Header) (*http.

// Post sends POST request to v3 version of the jira api.
func (c *Client) Post(ctx context.Context, path string, body []byte, headers Header) (*http.Response, error) {
res, err := c.request(ctx, http.MethodPost, c.server+baseURLv3+path, body, headers)
if err != nil {
return res, err
}
return res, err
return c.request(ctx, http.MethodPost, c.server+baseURLv3+path, body, headers)
}

// PostV2 sends POST request to v2 version of the jira api.
func (c *Client) PostV2(ctx context.Context, path string, body []byte, headers Header) (*http.Response, error) {
res, err := c.request(ctx, http.MethodPost, c.server+baseURLv2+path, body, headers)
if err != nil {
return res, err
}
return res, err
return c.request(ctx, http.MethodPost, c.server+baseURLv2+path, body, headers)
}

// PostV1 sends POST request to v1 version of the jira api.
func (c *Client) PostV1(ctx context.Context, path string, body []byte, headers Header) (*http.Response, error) {
res, err := c.request(ctx, http.MethodPost, c.server+baseURLv1+path, body, headers)
if err != nil {
return res, err
}
return res, err
return c.request(ctx, http.MethodPost, c.server+baseURLv1+path, body, headers)
}

// Put sends PUT request to v3 version of the jira api.
func (c *Client) Put(ctx context.Context, path string, body []byte, headers Header) (*http.Response, error) {
res, err := c.request(ctx, http.MethodPut, c.server+baseURLv3+path, body, headers)
if err != nil {
return res, err
}
return res, err
return c.request(ctx, http.MethodPut, c.server+baseURLv3+path, body, headers)
}

// PutV2 sends PUT request to v2 version of the jira api.
func (c *Client) PutV2(ctx context.Context, path string, body []byte, headers Header) (*http.Response, error) {
res, err := c.request(ctx, http.MethodPut, c.server+baseURLv2+path, body, headers)
if err != nil {
return res, err
}
return res, err
return c.request(ctx, http.MethodPut, c.server+baseURLv2+path, body, headers)
}

// DeleteV2 sends DELETE request to v2 version of the jira api.
Expand Down Expand Up @@ -250,9 +230,7 @@ func (c *Client) request(ctx context.Context, method, endpoint string, body []by
req.SetBasicAuth(c.login, c.token)
}

res, err = c.transport.RoundTrip(req.WithContext(ctx))

return res, err
return c.transport.RoundTrip(req.WithContext(ctx))
}

func dump(req *http.Request, res *http.Response) {
Expand Down

0 comments on commit ed84fe7

Please sign in to comment.