Skip to content

Add JIRA integration #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions pkg/jira/authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jira

import "net/http"

// authenticationService is used to authenticate requests
type authenticationService struct {
client *Client
email string
token string
}

// setBasisAuth sets the username and password on the provided request
func (a *authenticationService) setBasicAuth(req *http.Request) {
req.SetBasicAuth(a.email, a.token)
}
85 changes: 85 additions & 0 deletions pkg/jira/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package jira

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)

// Client allows the program to interact with the JIRA API
type Client struct {
host *url.URL
httpClient HttpClient
authentication *authenticationService
}

// HttpClient is the http client interface used by the JIRA client
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}

func NewClient(host, email, token string, httpClient HttpClient) (*Client, error) {
if host == "" {
return nil, errors.New("could not create jira client: hostname cannot be empty")
}

if email == "" {
return nil, errors.New("could not create jira client: email cannot be empty")
}

if token == "" {
return nil, errors.New("could not create jira client: token cannot be empty")
}

u, err := url.Parse(host)

if err != nil {
return nil, fmt.Errorf("could not create jira client, invalid host provided: %w", err)
}

client := &Client{host: u, httpClient: httpClient}
client.authentication = &authenticationService{
client: client,
email: email,
token: token,
}
return client, nil
}

func (c *Client) GetIssueDescription(key string) (*IssueDescriptionResponse, error) {
endpoint, _ := url.Parse(fmt.Sprintf("/rest/api/3/issue/%v?fields=description", key))
u := c.host.ResolveReference(endpoint).String()

req, err := http.NewRequest("GET", u, nil)

if err != nil {
return nil, err
}

c.authentication.setBasicAuth(req)
req.Header.Add("Content-Type", "application/json")

res, err := c.httpClient.Do(req)

if err != nil {
return nil, err
}

var response IssueDescriptionResponse

data, err := io.ReadAll(res.Body)
defer res.Body.Close()

if err != nil {
return nil, err
}

if err = json.Unmarshal(data, &response); err != nil {
return nil, err
}

return &response, nil
}
85 changes: 85 additions & 0 deletions pkg/jira/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package jira

import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/url"
"testing"
)

type MockHttpClient struct {
t *testing.T
CalledMethod string
CalledWith []string
CalledTimes int
CalledHeaders http.Header
statusCode int
}

func NewMockHttpClient(t *testing.T, statusCode int) *MockHttpClient {
return &MockHttpClient{t: t, statusCode: statusCode, CalledTimes: 0, CalledWith: []string{}}
}

func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error) {
m.CalledTimes += 1
data, err := io.ReadAll(req.Body)
defer req.Body.Close()

if err != nil {
m.t.Fatal("error occurred while doing mock request")
}

m.CalledWith = append(m.CalledWith, string(data))
m.CalledMethod = req.Method
m.CalledHeaders = req.Header

if m.statusCode == http.StatusBadRequest {
body := bytes.NewReader([]byte("{\"errorMessages\":[],\"errors\":{\"name\":\"A version with this name already exists in this project.\"}}"))
return &http.Response{Status: "Bad request", StatusCode: http.StatusBadRequest, Body: io.NopCloser(body)}, nil
}
body := bytes.NewReader([]byte("{\"hello\": \"world\"}"))
return &http.Response{Status: "Created", StatusCode: http.StatusCreated, Body: io.NopCloser(body)}, nil
}

func TestNewClient(t *testing.T) {
m := NewMockHttpClient(t, 200)
parsedHost, _ := url.Parse("https://test.nu")

c, err := NewClient("https://test.nu", "[email protected]", "c0ffee", m)

assert.NoError(t, err)

expected := &Client{
host: parsedHost,
httpClient: m,
authentication: nil,
}

expected.authentication = &authenticationService{
client: expected,
email: "[email protected]",
token: "c0ffee",
}

assert.Equal(t, expected, c)
}

func TestNewClient_missingHost(t *testing.T) {
c, err := NewClient("", "[email protected]", "c0ffee", nil)
assert.Nil(t, c)
assert.EqualError(t, err, "could not create jira client: hostname cannot be empty")
}

func TestNewClient_missingEmail(t *testing.T) {
c, err := NewClient("https://test.nu", "", "c0ffee", nil)
assert.Nil(t, c)
assert.EqualError(t, err, "could not create jira client: email cannot be empty")
}

func TestNewClient_missingToken(t *testing.T) {
c, err := NewClient("https://test.nu", "[email protected]", "", nil)
assert.Nil(t, c)
assert.EqualError(t, err, "could not create jira client: token cannot be empty")
}
49 changes: 49 additions & 0 deletions pkg/jira/data/issue_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "56360",
"self": "https://foo.atlassian.net/rest/api/3/issue/56360",
"key": "MB-1337",
"fields": {
"description": {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "The "
},
{
"type": "text",
"text": "read_file",
"marks": [
{
"type": "code"
}
]
},
{
"type": "text",
"text": " function in "
},
{
"type": "text",
"text": "file_utils.py",
"marks": [
{
"type": "code"
}
]
},
{
"type": "text",
"text": " uses demo text to provide test data"
}
]
}
]
}
}
}
24 changes: 24 additions & 0 deletions pkg/jira/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jira

type IssueDescriptionResponse struct {
Expand string `json:"expand"`
Id string `json:"id"`
Self string `json:"self"`
Key string `json:"key"`
Fields struct {
Description struct {
Version int `json:"version"`
Type string `json:"type"`
Content []struct {
Type string `json:"type"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
Marks []struct {
Type string `json:"type"`
} `json:"marks,omitempty"`
} `json:"content"`
} `json:"content"`
} `json:"description"`
} `json:"fields"`
}