1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-01-05 22:53:53 +02:00

Uses a jira client config struct

This commit is contained in:
spmassot 2018-10-17 14:49:25 -04:00
parent 02a41efba4
commit 00a32a0fd7
9 changed files with 101 additions and 27 deletions

View File

@ -29,7 +29,10 @@ func main() {
Password: strings.TrimSpace(password),
}
client, err := jira.NewClient(tp.Client(), strings.TrimSpace(jiraURL), true)
config = jira.ServiceConfig{
Notify: true,
}
client, err := jira.NewClient(tp.Client(), strings.TrimSpace(jiraURL), config)
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return

View File

@ -29,7 +29,10 @@ func main() {
Password: strings.TrimSpace(password),
}
client, err := jira.NewClient(tp.Client(), strings.TrimSpace(jiraURL), true)
config = jira.ServiceConfig{
Notify: true,
}
client, err := jira.NewClient(tp.Client(), strings.TrimSpace(jiraURL), config)
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return

View File

@ -7,7 +7,10 @@ import (
)
func main() {
jiraClient, _ := jira.NewClient(nil, "https://jira.atlassian.com/", true)
config = jira.ServiceConfig{
Notify: true,
}
jiraClient, _ := jira.NewClient(nil, "https://jira.atlassian.com/", config)
req, _ := jiraClient.NewRequest("GET", "/rest/api/2/project", nil)
projects := new([]jira.Project)

View File

@ -14,7 +14,10 @@ func main() {
}
client := &http.Client{Transport: tr}
jiraClient, _ := jira.NewClient(client, "https://issues.apache.org/jira/", true)
config = jira.ServiceConfig{
Notify: true,
}
jiraClient, _ := jira.NewClient(client, "https://issues.apache.org/jira/", config)
issue, _, _ := jiraClient.Issue.Get("MESOS-3325", nil)
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)

View File

@ -7,7 +7,10 @@ import (
)
func main() {
jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/", true)
config = jira.ServiceConfig{
Notify: true,
}
jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/", config)
issue, _, _ := jiraClient.Issue.Get("MESOS-3325", nil)
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)

View File

@ -42,7 +42,10 @@ func main() {
tp = ba.Client()
}
client, err := jira.NewClient(tp, strings.TrimSpace(jiraURL), true)
config = jira.ServiceConfig{
Notify: true,
}
client, err := jira.NewClient(tp, strings.TrimSpace(jiraURL), config)
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return

View File

@ -27,6 +27,11 @@ const (
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue
type IssueService struct {
client *Client
Config *ServiceConfig
}
// ServiceConfig describes some config options that apply to many/all calls made by the service
type ServiceConfig struct {
Notify bool
}
@ -654,7 +659,7 @@ func (s *IssueService) Create(issue *Issue) (*Issue, *Response, error) {
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue
func (s *IssueService) Update(issue *Issue) (*Issue, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%v?notifyUsers=%t", issue.Key, s.Notify)
apiEndpoint := fmt.Sprintf("rest/api/3/issue/%v?notifyUsers=%t", issue.Key, s.Config.Notify)
req, err := s.client.NewRequest("PUT", apiEndpoint, issue)
if err != nil {
return nil, nil, err
@ -675,7 +680,7 @@ func (s *IssueService) Update(issue *Issue) (*Issue, *Response, error) {
//
// https://docs.atlassian.com/jira/REST/7.4.0/#api/2/issue-editIssue
func (s *IssueService) UpdateIssue(jiraID string, data map[string]interface{}) (*Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%v?notifyUsers=%t", jiraID, s.Notify)
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%v?notifyUsers=%t", jiraID, s.Config.Notify)
req, err := s.client.NewRequest("PUT", apiEndpoint, data)
if err != nil {
return nil, err

View File

@ -50,7 +50,7 @@ type Client struct {
// As an alternative you can use Session Cookie based authentication provided by this package as well.
// See https://docs.atlassian.com/jira/REST/latest/#authentication
// baseURL is the HTTP endpoint of your JIRA instance and should always be specified with a trailing slash.
func NewClient(httpClient *http.Client, baseURL string, notify bool) (*Client, error) {
func NewClient(httpClient *http.Client, baseURL string, config *ServiceConfig) (*Client, error) {
if httpClient == nil {
httpClient = http.DefaultClient
}
@ -70,7 +70,7 @@ func NewClient(httpClient *http.Client, baseURL string, notify bool) (*Client, e
baseURL: parsedBaseURL,
}
c.Authentication = &AuthenticationService{client: c}
c.Issue = &IssueService{client: c, Notify: notify}
c.Issue = &IssueService{client: c, Config: config}
c.Project = &ProjectService{client: c}
c.Board = &BoardService{client: c}
c.Sprint = &SprintService{client: c}

View File

@ -38,7 +38,10 @@ func setup() {
testServer = httptest.NewServer(testMux)
// jira client configured to use test server
testClient, _ = NewClient(nil, testServer.URL, true)
config = ServiceConfig{
Notify: true,
}
testClient, _ = NewClient(nil, testServer.URL, config)
}
// teardown closes the test HTTP server.
@ -59,7 +62,10 @@ func testRequestURL(t *testing.T, r *http.Request, want string) {
}
func TestNewClient_WrongUrl(t *testing.T) {
c, err := NewClient(nil, "://issues.apache.org/jira/", true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, "://issues.apache.org/jira/", config)
if err == nil {
t.Error("Expected an error. Got none")
@ -72,7 +78,10 @@ func TestNewClient_WrongUrl(t *testing.T) {
func TestNewClient_WithHttpClient(t *testing.T) {
httpClient := http.DefaultClient
httpClient.Timeout = 10 * time.Minute
c, err := NewClient(httpClient, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(httpClient, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("Got an error: %s", err)
@ -86,7 +95,10 @@ func TestNewClient_WithHttpClient(t *testing.T) {
}
func TestNewClient_WithServices(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("Got an error: %s", err)
@ -142,7 +154,10 @@ func TestCheckResponse(t *testing.T) {
}
func TestClient_NewRequest(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -164,7 +179,10 @@ func TestClient_NewRequest(t *testing.T) {
}
func TestClient_NewRawRequest(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -197,7 +215,10 @@ func testURLParseError(t *testing.T, err error) {
}
func TestClient_NewRequest_BadURL(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -206,7 +227,10 @@ func TestClient_NewRequest_BadURL(t *testing.T) {
}
func TestClient_NewRequest_SessionCookies(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -235,7 +259,10 @@ func TestClient_NewRequest_SessionCookies(t *testing.T) {
}
func TestClient_NewRequest_BasicAuth(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -261,7 +288,10 @@ func TestClient_NewRequest_BasicAuth(t *testing.T) {
// since there is no difference between an HTTP request body that is an empty string versus one that is not set at all.
// However in certain cases, intermediate systems may treat these differently resulting in subtle errors.
func TestClient_NewRequest_EmptyBody(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -275,7 +305,10 @@ func TestClient_NewRequest_EmptyBody(t *testing.T) {
}
func TestClient_NewMultiPartRequest(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -308,7 +341,10 @@ func TestClient_NewMultiPartRequest(t *testing.T) {
}
func TestClient_NewMultiPartRequest_BasicAuth(t *testing.T) {
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("An error occurred. Expected nil. Got %+v.", err)
}
@ -427,7 +463,10 @@ func TestClient_GetBaseURL_WithURL(t *testing.T) {
t.Errorf("URL parsing -> Got an error: %s", err)
}
c, err := NewClient(nil, testJIRAInstanceURL, true)
config = ServiceConfig{
Notify: true,
}
c, err := NewClient(nil, testJIRAInstanceURL, config)
if err != nil {
t.Errorf("Client creation -> Got an error: %s", err)
}
@ -464,7 +503,10 @@ func TestBasicAuthTransport(t *testing.T) {
Password: password,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, true)
config = ServiceConfig{
Notify: true,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, config)
req, _ := basicAuthClient.NewRequest("GET", ".", nil)
basicAuthClient.Do(req, nil)
}
@ -515,7 +557,10 @@ func TestCookieAuthTransport_SessionObject_Exists(t *testing.T) {
SessionObject: []*http.Cookie{testCookie},
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, true)
config = ServiceConfig{
Notify: true,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, config)
req, _ := basicAuthClient.NewRequest("GET", ".", nil)
basicAuthClient.Do(req, nil)
}
@ -551,7 +596,10 @@ func TestCookieAuthTransport_SessionObject_ExistsWithEmptyCookie(t *testing.T) {
SessionObject: []*http.Cookie{emptyCookie, testCookie},
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, true)
config = ServiceConfig{
Notify: true,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, config)
req, _ := basicAuthClient.NewRequest("GET", ".", nil)
basicAuthClient.Do(req, nil)
}
@ -592,7 +640,10 @@ func TestCookieAuthTransport_SessionObject_DoesNotExist(t *testing.T) {
AuthURL: ts.URL,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, true)
config = ServiceConfig{
Notify: true,
}
basicAuthClient, _ := NewClient(tp.Client(), testServer.URL, config)
req, _ := basicAuthClient.NewRequest("GET", ".", nil)
basicAuthClient.Do(req, nil)
}