Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Tweak the PR a little following the docs...
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed May 23, 2022
1 parent 85b0822 commit 9494bb0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 61 deletions.
105 changes: 60 additions & 45 deletions error_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,77 @@ type ErrorTrackingService struct {
client *Client
}

// ErrorTracking represents error tracking settings for a GitLab project.
// ErrorTrackingClientKey represents an error tracking client key.
//
// GitLab docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#error-tracking-client-keys
type ErrorTrackingClientKey struct {
ID int `json:"id"`
Active bool `json:"active"`
PublicKey string `json:"public_key"`
SentryDsn string `json:"sentry_dsn"`
}

func (p ErrorTrackingClientKey) String() string {
return Stringify(p)
}

// ErrorTrackingSettings represents error tracking settings for a GitLab project.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html
type ErrorTracking struct {
type ErrorTrackingSettings struct {
Active bool `json:"active"`
ProjectName string `json:"project_name"`
SentryExternalURL string `json:"sentry_external_url"`
APIURL string `json:"api_url"`
Integrated bool `json:"integrated"`
}

func (p ErrorTracking) String() string {
func (p ErrorTrackingSettings) String() string {
return Stringify(p)
}

// GetErrorTracking gets Sentry error tracking settings for a specific project
// GetErrorTrackingSettings gets error tracking settings.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#get-error-tracking-settings
func (s *ErrorTrackingService) GetErrorTracking(pid interface{}, options ...RequestOptionFunc) (*ErrorTracking, *Response, error) {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#get-error-tracking-settings
func (s *ErrorTrackingService) GetErrorTrackingSettings(pid interface{}, options ...RequestOptionFunc) (*ErrorTrackingSettings, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}

u := fmt.Sprintf("projects/%s/error_tracking/settings", PathEscape(project))

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

ErrorTracking := new(ErrorTracking)
resp, err := s.client.Do(req, ErrorTracking)
ets := new(ErrorTrackingSettings)
resp, err := s.client.Do(req, ets)
if err != nil {
return nil, resp, err
}

return ErrorTracking, resp, err
return ets, resp, err
}

type ConfigureErrorTrackingOptions struct {
// EnableDisableErrorTrackingOptions represents the available
// EnableDisableErrorTracking() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#enable-or-disable-the-error-tracking-project-settings
type EnableDisableErrorTrackingOptions struct {
Active *bool `url:"active,omitempty" json:"active,omitempty"`
Integrated *bool `url:"integrated,omitempty" json:"integrated,omitempty"`
}

// EnableDisableErrorTracking allows the enabling or disabling of sentry integration on a specified project
// EnableDisableErrorTracking allows you to enable or disable the error tracking
// settings for a project.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#enable-or-disable-the-error-tracking-project-settings
func (s *ErrorTrackingService) EnableDisableErrorTracking(pid interface{}, opt *ConfigureErrorTrackingOptions, options ...RequestOptionFunc) (*ErrorTracking, *Response, error) {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#enable-or-disable-the-error-tracking-project-settings
func (s *ErrorTrackingService) EnableDisableErrorTracking(pid interface{}, opt *EnableDisableErrorTrackingOptions, options ...RequestOptionFunc) (*ErrorTrackingSettings, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
Expand All @@ -89,87 +111,80 @@ func (s *ErrorTrackingService) EnableDisableErrorTracking(pid interface{}, opt *
return nil, nil, err
}

ET := new(ErrorTracking)
resp, err := s.client.Do(req, &ET)
ets := new(ErrorTrackingSettings)
resp, err := s.client.Do(req, &ets)
if err != nil {
return nil, resp, err
}

return ET, resp, err
return ets, resp, err
}

// ListErrorTrackingClientKeysOptions represents the available
// ListErrorTrackingClientKeys() options.
// ListClientKeysOptions represents the available ListClientKeys() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#list-project-client-keys
type ListErrorTrackingClientKeysOptions ListOptions

type ProjectClientKey struct {
ID int `json:"id"`
Active bool `json:"active"`
PublicKey string `json:"public_key"`
SentryDsn string `json:"sentry_dsn"`
}
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#list-project-client-keys
type ListClientKeysOptions ListOptions

// ListErrorTrackingClientKeys gets a list of project client keys for sentry.
// ListClientKeys lists error tracking project client keys.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#list-project-client-keys
func (s *ErrorTrackingService) ListErrorTrackingClientKeys(pid interface{}, opt *ListErrorTrackingClientKeysOptions, options ...RequestOptionFunc) ([]*ProjectClientKey, *Response, error) {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#list-project-client-keys
func (s *ErrorTrackingService) ListClientKeys(pid interface{}, opt *ListClientKeysOptions, options ...RequestOptionFunc) ([]*ErrorTrackingClientKey, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}

u := fmt.Sprintf("projects/%s/error_tracking/client_keys", PathEscape(project))

req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}

var projectClientKeys []*ProjectClientKey
resp, err := s.client.Do(req, &projectClientKeys)
var cks []*ErrorTrackingClientKey
resp, err := s.client.Do(req, &cks)
if err != nil {
return nil, resp, err
}

return projectClientKeys, resp, err
return cks, resp, err
}

// CreateClientKey creates a project client key for sentry integration in a GitLab project.
// CreateClientKey creates a new client key for a project.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#create-a-client-key
func (s *ErrorTrackingService) CreateClientKey(pid interface{}, options ...RequestOptionFunc) (*ProjectClientKey, *Response, error) {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#create-a-client-key
func (s *ErrorTrackingService) CreateClientKey(pid interface{}, options ...RequestOptionFunc) (*ErrorTrackingClientKey, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}

u := fmt.Sprintf("projects/%s/error_tracking/client_keys", PathEscape(project))

req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
if err != nil {
return nil, nil, err
}

projectClientKey := new(ProjectClientKey)
resp, err := s.client.Do(req, projectClientKey)
ck := new(ErrorTrackingClientKey)
resp, err := s.client.Do(req, ck)
if err != nil {
return nil, resp, err
}

return projectClientKey, resp, err
return ck, resp, err
}

// DeleteClientKey removes a sentry client key from a project.
// DeleteClientKey removes a client key from the project.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/error_tracking.html#delete-a-client-key
// GitLab API docs:
// https://docs.gitlab.com/ee/api/error_tracking.html#delete-a-client-key
func (s *ErrorTrackingService) DeleteClientKey(pid interface{}, keyID int, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}

u := fmt.Sprintf("projects/%s/error_tracking/client_keys/%d", PathEscape(project), keyID)

req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
Expand Down
38 changes: 22 additions & 16 deletions error_tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ func TestGetErrorTracking(t *testing.T) {
}`)
})

ET, _, err := client.ErrorTracking.GetErrorTracking(1)
et, _, err := client.ErrorTracking.GetErrorTrackingSettings(1)
if err != nil {
t.Errorf("ErrorTracking.GetErrorTracking returned error: %v", err)
}

want := &ErrorTracking{
want := &ErrorTrackingSettings{
Active: true,
ProjectName: "sample sentry project",
SentryExternalURL: "https://sentry.io/myawesomeproject/project",
APIURL: "https://sentry.io/api/1/projects/myawesomeproject/project",
Integrated: false,
}

if !reflect.DeepEqual(want, ET) {
t.Errorf("ErrorTracking.GetErrorTracking returned %+v, want %+v", ET, want)
if !reflect.DeepEqual(want, et) {
t.Errorf("ErrorTracking.GetErrorTracking returned %+v, want %+v", et, want)
}
}

Expand All @@ -71,21 +71,27 @@ func TestDisableErrorTracking(t *testing.T) {
}`)
})

ET, _, err := client.ErrorTracking.EnableDisableErrorTracking(1, &ConfigureErrorTrackingOptions{Active: Bool(false), Integrated: Bool(false)})
et, _, err := client.ErrorTracking.EnableDisableErrorTracking(
1,
&EnableDisableErrorTrackingOptions{
Active: Bool(false),
Integrated: Bool(false),
},
)
if err != nil {
t.Errorf("ErrorTracking.EnableDisableErrorTracking returned error: %v", err)
}

want := &ErrorTracking{
want := &ErrorTrackingSettings{
Active: false,
ProjectName: "sample sentry project",
SentryExternalURL: "https://sentry.io/myawesomeproject/project",
APIURL: "https://sentry.io/api/1/projects/myawesomeproject/project",
Integrated: false,
}

if !reflect.DeepEqual(want, ET) {
t.Errorf("ErrorTracking.EnableDisableErrorTracking returned %+v, want %+v", ET, want)
if !reflect.DeepEqual(want, et) {
t.Errorf("ErrorTracking.EnableDisableErrorTracking returned %+v, want %+v", et, want)
}
}

Expand All @@ -105,20 +111,20 @@ func TestListErrorTrackingClientKeys(t *testing.T) {
]`)
})

CKS, _, err := client.ErrorTracking.ListErrorTrackingClientKeys(1, &ListErrorTrackingClientKeysOptions{Page: 1, PerPage: 10})
cks, _, err := client.ErrorTracking.ListClientKeys(1, &ListClientKeysOptions{Page: 1, PerPage: 10})
if err != nil {
t.Errorf("ErrorTracking.ListErrorTrackingClientKeys returned error: %v", err)
}

want := []*ProjectClientKey{{
want := []*ErrorTrackingClientKey{{
ID: 1,
Active: true,
PublicKey: "glet_aa77551d849c083f76d0bc545ed053a3",
SentryDsn: "https://glet_aa77551d849c083f76d0bc545ed053a3@gitlab.example.com/api/v4/error_tracking/collector/5",
}}

if !reflect.DeepEqual(want, CKS) {
t.Errorf("ErrorTracking.ListErrorTrackingClientKeys returned %+v, want %+v", CKS, want)
if !reflect.DeepEqual(want, cks) {
t.Errorf("ErrorTracking.ListErrorTrackingClientKeys returned %+v, want %+v", cks, want)
}
}

Expand All @@ -136,20 +142,20 @@ func TestCreateClientKey(t *testing.T) {
}`)
})

CK, _, err := client.ErrorTracking.CreateClientKey(1)
ck, _, err := client.ErrorTracking.CreateClientKey(1)
if err != nil {
t.Errorf("ErrorTracking.CreateClientKey returned error: %v", err)
}

want := &ProjectClientKey{
want := &ErrorTrackingClientKey{
ID: 1,
Active: true,
PublicKey: "glet_aa77551d849c083f76d0bc545ed053a3",
SentryDsn: "https://glet_aa77551d849c083f76d0bc545ed053a3@gitlab.example.com/api/v4/error_tracking/collector/5",
}

if !reflect.DeepEqual(want, CK) {
t.Errorf("ErrorTracking.CreateClientKey returned %+v, want %+v", CK, want)
if !reflect.DeepEqual(want, ck) {
t.Errorf("ErrorTracking.CreateClientKey returned %+v, want %+v", ck, want)
}
}

Expand Down

0 comments on commit 9494bb0

Please sign in to comment.