Skip to content

Commit

Permalink
[FEAT] Add support for personal access tokens (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourplusone authored Jan 5, 2023
1 parent 84146b6 commit bd14e4d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
19 changes: 17 additions & 2 deletions jira/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jira

import (
"log"
"net/http"
"sync"

jira "github.com/andygrunwald/go-jira"
Expand All @@ -16,11 +17,25 @@ type Config struct {

func (c *Config) createAndAuthenticateClient(d *schema.ResourceData) error {
log.Printf("[INFO] creating jira client using environment variables")
jiraClient, err := jira.NewClient(nil, d.Get("url").(string))

var httpClient *http.Client

token, ok := d.GetOk("token")
if ok {
transport := jira.BearerAuthTransport{Token: token.(string)}
httpClient = transport.Client()
} else {
transport := &jira.BasicAuthTransport{
Username: d.Get("user").(string),
Password: d.Get("password").(string),
}
httpClient = transport.Client()
}

jiraClient, err := jira.NewClient(httpClient, d.Get("url").(string))
if err != nil {
return errors.Wrap(err, "creating jira client failed")
}
jiraClient.Authentication.SetBasicAuth(d.Get("user").(string), d.Get("password").(string))

c.jiraClient = jiraClient

Expand Down
23 changes: 16 additions & 7 deletions jira/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ func Provider() *schema.Provider {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("JIRA_URL", nil),
Description: "Base url of the JIRA instance.",
Description: "URL for your Jira instance. Can be specified with the JIRA_URL environment variable.",
},
"user": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("JIRA_USER", nil),
Description: "User to be used",
Type: schema.TypeString,
ExactlyOneOf: []string{"token"},
RequiredWith: []string{"password"},
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("JIRA_USER", nil),
Description: "Username for your user. Can be specified with the JIRA_USER environment variable.",
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("JIRA_PASSWORD", nil),
Description: "Password/API Key of the user",
Description: "Password for the user, can also be an API Token. Can be specified with the JIRA_PASSWORD environment variable.",
},
"token": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("JIRA_TOKEN", nil),
Description: "Personal access token of a user. Can be specified with the JIRA_TOKEN environment variable.",
},
},
ResourcesMap: map[string]*schema.Resource{
Expand Down
21 changes: 16 additions & 5 deletions jira/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,22 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("JIRA_URL must be set for acceptance tests")
}

if v := os.Getenv("JIRA_USER"); v == "" {
t.Fatal("JIRA_USER must be set for acceptance tests")
if os.Getenv("JIRA_TOKEN") == "" {
if os.Getenv("JIRA_USER") == "" {
t.Fatal("JIRA_USER or JIRA_TOKEN must be set for acceptance tests")
}

if v := os.Getenv("JIRA_PASSWORD"); v == "" {
t.Fatal("JIRA_PASSWORD or JIRA_TOKEN must be set for acceptance tests")
}
} else {
if os.Getenv("JIRA_USER") != "" {
t.Fatal("Either JIRA_USER or JIRA_TOKEN must be set for acceptance tests")
}

if os.Getenv("JIRA_PASSWORD") != "" {
t.Fatal("Either JIRA_PASSWORD or JIRA_TOKEN must be set for acceptance tests")
}
}

if v := os.Getenv("JIRA_PASSWORD"); v == "" {
t.Fatal("JIRA_PASSWORD must be set for acceptance tests")
}
}
7 changes: 5 additions & 2 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ provider "jira" {
url = "https://myjira.atlassian.net" # Can also be set using the JIRA_URL environment variable
user = "xxxx" # Can also be set using the JIRA_USER environment variable
password = "xxxx" # Can also be set using the JIRA_PASSWORD environment variable
token = "xxxx" # Can also be set using the JIRA_TOKEN environment variable
}
```

## Schema

- **url** (String, Required) URL for your Jira instance. Can be specified with the JIRA_URL environment variable.

- **user** (String, Required) Username for your user. Can be specified with the JIRA_USER environment variable.
- **user** (String, Optional) Username for your user. Can be specified with the JIRA_USER environment variable.

- **password** (String, Required) Password for the user, can also be an API Token. Can be specified with the JIRA_PASSWORD environment variable.
- **password** (String, Optional) Password for the user, can also be an API Token. Can be specified with the JIRA_PASSWORD environment variable.

- **token** (String, Optional) Password for the user, can also be an API Token. Can be specified with the JIRA_PASSWORD environment variable.

0 comments on commit bd14e4d

Please sign in to comment.