83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package jiraclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func NewJiraClient(username, token, domain string) *JiraClient {
|
|
client := &http.Client{}
|
|
|
|
return &JiraClient{
|
|
Username: username,
|
|
Token: token,
|
|
Domain: domain,
|
|
Client: client,
|
|
}
|
|
}
|
|
|
|
// JiraClient manages communication with Jira Cloud's V2 REST API.
|
|
type JiraClient struct {
|
|
Username string
|
|
Token string
|
|
Domain string
|
|
Client *http.Client
|
|
}
|
|
|
|
func (c JiraClient) get(url string, queryParams map[string]string) ([]byte, error) {
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
req.SetBasicAuth(c.Username, c.Token)
|
|
|
|
if queryParams != nil {
|
|
q := req.URL.Query()
|
|
for k, v := range queryParams {
|
|
q.Add(k, v)
|
|
}
|
|
req.URL.RawQuery = q.Encode()
|
|
}
|
|
|
|
resp, err := c.Client.Do(req)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
return body, err
|
|
}
|
|
|
|
func (c JiraClient) GetIssue(keyOrId string) (Issue, error) {
|
|
var issue Issue
|
|
|
|
url := fmt.Sprintf("https://%s.atlassian.net/rest/api/2/issue/%s", c.Domain, keyOrId)
|
|
|
|
body, err := c.get(url, nil)
|
|
if err != nil {
|
|
return issue, err
|
|
}
|
|
|
|
err = json.Unmarshal(body, &issue)
|
|
|
|
return issue, err
|
|
}
|
|
|
|
func (c JiraClient) GetProject(keyOrId string) (Project, error) {
|
|
var project Project
|
|
|
|
url := fmt.Sprintf("https://%s.atlassian.net/rest/api/2/project/%s", c.Domain, keyOrId)
|
|
|
|
body, err := c.get(url, nil)
|
|
if err != nil {
|
|
return project, err
|
|
}
|
|
|
|
err = json.Unmarshal(body, &project)
|
|
|
|
return project, err
|
|
}
|