diff --git a/README.md b/README.md index ee22f4d..cec9f66 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,14 @@ brew install leozhantw/tap/gojira Before using the CLI, you need to configure your Jira information through the `gojira configure` command. ```shell script $ gojira configure -Your website URL: https://gojira.atlassian.net -Your token: your-jira-token +Your website URL: https://mysite.atlassian.net +Your account (e.g. me@example.com): me@example.com +Your API token: your-jira-api-token ``` + +### Create an API token +Create an API token from your Atlassian account: + +1. Log in to https://id.atlassian.com/manage/api-tokens. +2. Click Create API token. +3. From the dialog that appears, enter a memorable and concise Label for your token and click Create. diff --git a/cmd/configure.go b/cmd/configure.go index 3c2d387..fc99b2e 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -14,14 +14,21 @@ var configureCmd = &cobra.Command{ Short: "Set up your Jira Configuration", RunE: func(cmd *cobra.Command, args []string) error { var siteURL string - fmt.Print("Your website URL: ") + fmt.Print("Your website URL (e.g. https://mysite.atlassian.net): ") if _, err := fmt.Scan(&siteURL); err != nil { return err } viper.Set("siteurl", siteURL) + var account string + fmt.Print("Your account (e.g. me@example.com): ") + if _, err := fmt.Scan(&account); err != nil { + return err + } + viper.Set("account", account) + var token string - fmt.Print("Your token: ") + fmt.Print("Your API token: ") if _, err := fmt.Scan(&token); err != nil { return err } diff --git a/cmd/issue_list.go b/cmd/issue_list.go index 5ad0036..9617845 100644 --- a/cmd/issue_list.go +++ b/cmd/issue_list.go @@ -42,7 +42,7 @@ var issueListCmd = &cobra.Command{ jql += fmt.Sprintf(" AND status NOT IN ('%s')", strings.Join(excludeStatus, "','")) } - client, err := jira.NewClient(cfg.SiteURL, cfg.Token) + client, err := jira.NewClient(cfg.SiteURL, cfg.Account, cfg.Token) if err != nil { return err } diff --git a/internal/config/config.go b/internal/config/config.go index dc61377..e845562 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,7 @@ import ( type Config struct { SiteURL string `yaml:"siteurl"` + Account string `yaml:"account"` Token string `yaml:"token"` } diff --git a/pkg/jira/client.go b/pkg/jira/client.go index 6d5eb91..3a9407f 100644 --- a/pkg/jira/client.go +++ b/pkg/jira/client.go @@ -3,6 +3,7 @@ package jira import ( "bytes" "context" + "encoding/base64" "encoding/json" "fmt" "io" @@ -15,12 +16,13 @@ type Client struct { client *http.Client baseURL *url.URL + account string token string Issue *IssueService } -func NewClient(baseURL, token string) (*Client, error) { +func NewClient(baseURL, account, token string) (*Client, error) { parsedBaseURL, err := url.Parse(baseURL) if err != nil { return nil, err @@ -29,6 +31,7 @@ func NewClient(baseURL, token string) (*Client, error) { c := &Client{ client: http.DefaultClient, baseURL: parsedBaseURL, + account: account, token: token, } c.Issue = &IssueService{client: c} @@ -60,7 +63,10 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ } req.Header.Set("Content-Type", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Basic %s", c.token)) + + str := fmt.Sprintf("%s:%s", "leozhan@kkstream.com", c.token) + encStr := base64.StdEncoding.EncodeToString([]byte(str)) + req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encStr)) return req, nil }