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

114 lines
4.0 KiB
Go
Raw Normal View History

2018-01-22 11:34:41 +01:00
package jira
import (
"context"
2018-01-22 11:34:41 +01:00
"encoding/json"
"fmt"
"io/ioutil"
)
// VersionService handles Versions for the Jira instance / API.
2018-01-22 11:34:41 +01:00
//
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/version
2018-01-22 11:34:41 +01:00
type VersionService struct {
client *Client
}
// Version represents a single release version of a project
type Version struct {
Self string `json:"self,omitempty" structs:"self,omitempty"`
ID string `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
Description string `json:"description,omitempty" structs:"description,omitempty"`
2018-01-22 11:34:41 +01:00
Archived bool `json:"archived,omitempty" structs:"archived,omitempty"`
Released bool `json:"released,omitempty" structs:"released,omitempty"`
ReleaseDate string `json:"releaseDate,omitempty" structs:"releaseDate,omitempty"`
UserReleaseDate string `json:"userReleaseDate,omitempty" structs:"userReleaseDate,omitempty"`
ProjectID int `json:"projectId,omitempty" structs:"projectId,omitempty"` // Unlike other IDs, this is returned as a number
2018-10-29 23:36:28 +01:00
StartDate string `json:"startDate,omitempty" structs:"startDate,omitempty"`
2018-01-22 11:34:41 +01:00
}
// GetWithContext gets version info from Jira
2018-01-22 11:34:41 +01:00
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-id-get
func (s *VersionService) GetWithContext(ctx context.Context, versionID int) (*Version, *Response, error) {
2018-01-22 11:34:41 +01:00
apiEndpoint := fmt.Sprintf("/rest/api/2/version/%v", versionID)
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
2018-01-22 11:34:41 +01:00
if err != nil {
return nil, nil, err
}
version := new(Version)
resp, err := s.client.Do(req, version)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}
return version, resp, nil
}
// Get wraps GetWithContext using the background context.
func (s *VersionService) Get(versionID int) (*Version, *Response, error) {
return s.GetWithContext(context.Background(), versionID)
}
// CreateWithContext creates a version in Jira.
2018-01-22 11:34:41 +01:00
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-post
func (s *VersionService) CreateWithContext(ctx context.Context, version *Version) (*Version, *Response, error) {
2018-01-22 11:34:41 +01:00
apiEndpoint := "/rest/api/2/version"
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, version)
2018-01-22 11:34:41 +01:00
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return nil, resp, err
}
responseVersion := new(Version)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
style: Fix staticcheck (static analysis) errors for this library (#283) * style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)" staticcheck is a static analysis tool for go. It reports several "error strings should not be capitalized (ST1005)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)" staticcheck is a static analysis tool for go. It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "type X is unused (U1000)" staticcheck is a static analysis tool for go. It reports several "type X is unused (U1000)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)" staticcheck is a static analysis tool for go. It reports several - should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003) - should use strings.EqualFold instead (SA6005) messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)" staticcheck is a static analysis tool for go. It report several "unnecessary use of fmt.Sprintf (S1039)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "this value of X is never used (SA4006)" staticcheck is a static analysis tool for go. It report several "this value of X is never used (SA4006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "redundant return statement (S1023)" staticcheck is a static analysis tool for go. It report several "redundant return statement (S1023)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)" staticcheck is a static analysis tool for go. It report several file.go:Line:character: possible nil pointer dereference (SA5011) file.go:Line:character: this check suggests that the pointer can be nil messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "this value of X is never used (SA4006)" staticcheck is a static analysis tool for go. It report several "this value of X is never used (SA4006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280
2020-05-02 23:08:01 +02:00
e := fmt.Errorf("could not read the returned data")
2018-01-22 11:34:41 +01:00
return nil, resp, NewJiraError(resp, e)
}
err = json.Unmarshal(data, responseVersion)
if err != nil {
style: Fix staticcheck (static analysis) errors for this library (#283) * style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)" staticcheck is a static analysis tool for go. It reports several "error strings should not be capitalized (ST1005)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)" staticcheck is a static analysis tool for go. It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "type X is unused (U1000)" staticcheck is a static analysis tool for go. It reports several "type X is unused (U1000)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)" staticcheck is a static analysis tool for go. It reports several - should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003) - should use strings.EqualFold instead (SA6005) messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)" staticcheck is a static analysis tool for go. It report several "unnecessary use of fmt.Sprintf (S1039)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "this value of X is never used (SA4006)" staticcheck is a static analysis tool for go. It report several "this value of X is never used (SA4006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "redundant return statement (S1023)" staticcheck is a static analysis tool for go. It report several "redundant return statement (S1023)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)" staticcheck is a static analysis tool for go. It report several file.go:Line:character: possible nil pointer dereference (SA5011) file.go:Line:character: this check suggests that the pointer can be nil messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280 * style: Fix staticcheck errors for "this value of X is never used (SA4006)" staticcheck is a static analysis tool for go. It report several "this value of X is never used (SA4006)" messages. Here, we fix it to be more compliant with the go coding styleguide. Related: #280
2020-05-02 23:08:01 +02:00
e := fmt.Errorf("could not unmarshall the data into struct")
2018-01-22 11:34:41 +01:00
return nil, resp, NewJiraError(resp, e)
}
return responseVersion, resp, nil
}
// Create wraps CreateWithContext using the background context.
func (s *VersionService) Create(version *Version) (*Version, *Response, error) {
return s.CreateWithContext(context.Background(), version)
}
// UpdateWithContext updates a version from a JSON representation.
2018-01-22 11:34:41 +01:00
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-version-id-put
func (s *VersionService) UpdateWithContext(ctx context.Context, version *Version) (*Version, *Response, error) {
2018-01-22 11:34:41 +01:00
apiEndpoint := fmt.Sprintf("rest/api/2/version/%v", version.ID)
req, err := s.client.NewRequestWithContext(ctx, "PUT", apiEndpoint, version)
2018-01-22 11:34:41 +01:00
if err != nil {
return nil, nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
// This is just to follow the rest of the API's convention of returning a version.
// Returning the same pointer here is pointless, so we return a copy instead.
ret := *version
return &ret, resp, nil
}
// Update wraps UpdateWithContext using the background context.
func (s *VersionService) Update(version *Version) (*Version, *Response, error) {
return s.UpdateWithContext(context.Background(), version)
}