Skip to content

Commit

Permalink
fix: upgrade the authenticate way to fix deprecate issue
Browse files Browse the repository at this point in the history
  • Loading branch information
leozhantw committed Feb 6, 2021
1 parent 2d0f7cb commit ac85158
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 9 additions & 2 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Config struct {
SiteURL string `yaml:"siteurl"`
Account string `yaml:"account"`
Token string `yaml:"token"`
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/jira/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jira
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand All @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ac85158

Please # to comment.