Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions lib/config.go → cfg/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package lib
package cfg

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
Expand Down Expand Up @@ -96,18 +97,20 @@ func NewConfig(cmd *cobra.Command) (Config, error) {
// LoadJIRAConfig loads the JIRA configuration (project key,
// custom field IDs) from a remote JIRA server.
func (c *Config) LoadJIRAConfig(client jira.Client) error {
proj, res, err := MakeJIRARequest(*c, func() (interface{}, *jira.Response, error) {
return client.Project.Get(c.cmdConfig.GetString("jira-project"))
})
proj, res, err := client.Project.Get(c.cmdConfig.GetString("jira-project"))
if err != nil {
c.log.Errorf("Error retrieving JIRA project; check key and credentials. Error: %v", err)
return GetErrorBody(*c, res)
}
if _, ok := proj.(*jira.Project); !ok {
c.log.Errorf("Get JIRA project did not return project! Value: %v", proj)
return errors.New(fmt.Sprintf("Get project failed; expected *jira.Project; got %T", proj))
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
c.log.Errorf("Error occured trying to read error body: %v", err)
return err
} else {
c.log.Debugf("Error body: %s", body)
return errors.New(string(body))
}
}
c.project = *(proj.(*jira.Project))
c.project = *proj

c.fieldIDs, err = c.getFieldIDs(client)
if err != nil {
Expand Down Expand Up @@ -295,7 +298,7 @@ func newLogger(app, level string) *logrus.Entry {
// validateConfig checks the values provided to all of the configuration
// options, ensuring that e.g. `since` is a valid date, `jira-uri` is a
// real URI, etc. This is the first level of checking. It does not confirm
// if a JIRA client is running at `jira-uri` for example; that is checked
// if a JIRA cli is running at `jira-uri` for example; that is checked
// in getJIRAClient when we actually make a call to the API.
func (c Config) validateConfig() error {
// Log level and config file location are validated already
Expand All @@ -318,6 +321,7 @@ func (c Config) validateConfig() error {
if err != nil {
return errors.New("Jira password required")
}
fmt.Println()
c.cmdConfig.Set("jira-pass", string(bytePass))
}

Expand Down Expand Up @@ -388,10 +392,7 @@ func (c Config) getFieldIDs(client jira.Client) (fields, error) {
}
jFields := new([]jiraField)

_, _, err = MakeJIRARequest(c, func() (interface{}, *jira.Response, error) {
res, err := client.Do(req, jFields)
return nil, res, err
})
_, err = client.Do(req, jFields)
if err != nil {
return fields{}, err
}
Expand Down
25 changes: 13 additions & 12 deletions lib/clients.go → cli/clients.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lib
package cli

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (

"github.com/andygrunwald/go-jira"
"github.com/cenkalti/backoff"
"github.com/coreos/issue-sync/cfg"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
Expand All @@ -16,7 +17,7 @@ import (
// of the body. If an error occurs during reading, that error is
// instead printed and returned. This function closes the body for
// further reading.
func GetErrorBody(config Config, res *jira.Response) error {
func GetErrorBody(config cfg.Config, res *jira.Response) error {
log := config.GetLogger()
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
Expand All @@ -37,7 +38,7 @@ func GetErrorBody(config Config, res *jira.Response) error {
// nil HTTP response and a timeout error are returned.
//
// It is nearly identical to MakeJIRARequest, but returns a GitHub API response.
func MakeGHRequest(config Config, f func() (interface{}, *github.Response, error)) (interface{}, *github.Response, error) {
func MakeGHRequest(config cfg.Config, f func() (interface{}, *github.Response, error)) (interface{}, *github.Response, error) {
var ret interface{}
var res *github.Response
var err error
Expand Down Expand Up @@ -66,7 +67,7 @@ func MakeGHRequest(config Config, f func() (interface{}, *github.Response, error
// nil HTTP response and a timeout error are returned.
//
// It is nearly identical to MakeGHRequest, but returns a JIRA API response.
func MakeJIRARequest(config Config, f func() (interface{}, *jira.Response, error)) (interface{}, *jira.Response, error) {
func MakeJIRARequest(config cfg.Config, f func() (interface{}, *jira.Response, error)) (interface{}, *jira.Response, error) {
var ret interface{}
var res *jira.Response
var err error
Expand All @@ -87,10 +88,10 @@ func MakeJIRARequest(config Config, f func() (interface{}, *jira.Response, error
return ret, res, err
}

// GetGitHubClient initializes a GitHub API client with an OAuth client for authentication,
// GetGitHubClient initializes a GitHub API cli with an OAuth cli for authentication,
// then makes an API request to confirm that the service is running and the auth token
// is valid.
func GetGitHubClient(config Config) (*github.Client, error) {
func GetGitHubClient(config cfg.Config) (*github.Client, error) {
log := config.GetLogger()

ctx := context.Background()
Expand All @@ -117,22 +118,22 @@ func GetGitHubClient(config Config) (*github.Client, error) {
return client, nil
}

// GetJIRAClient initializes a JIRA API client, then sets the Basic Auth credentials
// GetJIRAClient initializes a JIRA API cli, then sets the Basic Auth credentials
// passed to it. (OAuth token support is planned.)
//
// The validity of the client and its authentication are not checked here. One way
// to check them would be to call config.LoadJIRAConfig() after this function.
func GetJIRAClient(config Config) (*jira.Client, error) {
// The validity of the cli and its authentication are not checked here. One way
// to check them would be to call cfg.LoadJIRAConfig() after this function.
func GetJIRAClient(config cfg.Config) (*jira.Client, error) {
log := config.GetLogger()

client, err := jira.NewClient(nil, config.GetConfigString("jira-uri"))
if err != nil {
log.Errorf("Error initializing JIRA client; check your base URI. Error: %v", err)
log.Errorf("Error initializing JIRA cli; check your base URI. Error: %v", err)
return nil, err
}

client.Authentication.SetBasicAuth(config.GetConfigString("jira-user"), config.GetConfigString("jira-pass"))

log.Debug("JIRA client initialized")
log.Debug("JIRA cli initialized")
return client, nil
}
Loading