Repository: xanzy/go-gitlab
Pull request #770 (2020-02-11 11:53:06)

implement custom issue tracker service
gitlab docs:
https://docs.gitlab.com/ce/api/services.html#custom-issue-tracker


Patch:
diff --git a/services.go b/services.go
index ba64576..bc75d42 100644
--- a/services.go
+++ b/services.go
@@ -960,3 +960,99 @@ func (s *ServicesService) DeleteSlackService(pid interface{}, options ...OptionF
 
 	return s.client.Do(req, nil)
 }
+
+// CustomIssueTrackerService represents Custom Issue Tracker service settings.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#custom-issue-tracker
+type CustomIssueTrackerService struct {
+	Service
+	Properties *CustomIssueTrackerServiceProperties `json:"properties"`
+}
+
+// CustomIssueTrackerServiceProperties represents Custom Issue Tracker specific properties.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#custom-issue-tracker
+type CustomIssueTrackerServiceProperties struct {
+	ProjectURL  string `json:"project_url,omitempty"`
+	IssuesURL   string `json:"issues_url,omitempty"`
+	NewIssueURL string `json:"new_issue_url,omitempty"`
+}
+
+// GetCustomIssueTrackerService gets Custom Issue Tracker service settings for a project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#get-custom-issue-tracker-service-settings
+func (s *ServicesService) GetCustomIssueTrackerService(pid interface{}, options ...OptionFunc) (*CustomIssueTrackerService, *Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/custom-issue-tracker", pathEscape(project))
+
+	req, err := s.client.NewRequest("GET", u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	svc := new(CustomIssueTrackerService)
+	resp, err := s.client.Do(req, svc)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return svc, resp, err
+}
+
+// SetCustomIssueTrackerServiceOptions represents the available SetCustomIssueTrackerService()
+// options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#createedit-custom-issue-tracker-service
+type SetCustomIssueTrackerServiceOptions struct {
+	NewIssueURL *string `url:"new_issue_url,omitempty" json:"new_issue_url,omitempty"`
+	IssuesURL   *string `url:"issues_url,omitempty" json:"issues_url,omitempty"`
+	ProjectURL  *string `url:"project_url,omitempty" json:"project_url,omitempty"`
+	Description *string `url:"description,omitempty" json:"description,omitempty"`
+	Title       *string `url:"title,omitempty" json:"title,omitempty"`
+	PushEvents  *bool   `url:"push_events,omitempty" json:"push_events,omitempty"`
+}
+
+// SetCustomIssueTrackerService sets Custom Issue Tracker service for a project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#createedit-custom-issue-tracker-service
+func (s *ServicesService) SetCustomIssueTrackerService(pid interface{}, opt *SetCustomIssueTrackerServiceOptions, options ...OptionFunc) (*Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/custom-issue-tracker", pathEscape(project))
+
+	req, err := s.client.NewRequest("PUT", u, opt, options)
+	if err != nil {
+		return nil, err
+	}
+
+	return s.client.Do(req, nil)
+}
+
+// DeleteCustomIssueTrackerService deletes Custom Issue Tracker service settings for a project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/services.html#delete-custom-issue-tracker-service
+func (s *ServicesService) DeleteCustomIssueTrackerService(pid interface{}, options ...OptionFunc) (*Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("projects/%s/services/custom-issue-tracker", pathEscape(project))
+
+	req, err := s.client.NewRequest("DELETE", u, nil, options)
+	if err != nil {
+		return nil, err
+	}
+
+	return s.client.Do(req, nil)
+}


Test patch:
diff --git a/services_test.go b/services_test.go
index eef51f4..7380c18 100644
--- a/services_test.go
+++ b/services_test.go
@@ -262,3 +262,70 @@ func TestDeletePipelinesEmailService(t *testing.T) {
 		t.Fatalf("Services.DeletePipelinesEmailService returns an error: %v", err)
 	}
 }
+
+func TestCustomIssueTrackerService(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/custom-issue-tracker", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		fmt.Fprint(w, `{"id": 1, "title": "5", "push_events": true, "properties": {"new_issue_url":"1", "issues_url": "2", "project_url": "3"}}`)
+	})
+	want := &CustomIssueTrackerService{
+		Service: Service{
+			ID:         1,
+			Title:      "5",
+			PushEvents: true,
+		},
+		Properties: &CustomIssueTrackerServiceProperties{
+			NewIssueURL: "1",
+			IssuesURL:   "2",
+			ProjectURL:  "3",
+		},
+	}
+
+	service, _, err := client.Services.GetCustomIssueTrackerService(1)
+	if err != nil {
+		t.Fatalf("Services.GetCustomIssueTrackerService returns an error: %v", err)
+	}
+	if !reflect.DeepEqual(want, service) {
+		t.Errorf("Services.GetCustomIssueTrackerService returned %+v, want %+v", service, want)
+	}
+}
+
+func TestSetCustomIssueTrackerService(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/custom-issue-tracker", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "PUT")
+	})
+
+	opt := &SetCustomIssueTrackerServiceOptions{
+		NewIssueURL: String("1"),
+		IssuesURL:   String("2"),
+		ProjectURL:  String("3"),
+		Description: String("4"),
+		Title:       String("5"),
+		PushEvents:  Bool(true),
+	}
+
+	_, err := client.Services.SetCustomIssueTrackerService(1, opt)
+	if err != nil {
+		t.Fatalf("Services.SetCustomIssueTrackerService returns an error: %v", err)
+	}
+}
+
+func TestDeleteCustomIssueTrackerService(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/services/custom-issue-tracker", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "DELETE")
+	})
+
+	_, err := client.Services.DeleteCustomIssueTrackerService(1)
+	if err != nil {
+		t.Fatalf("Services.DeleteCustomIssueTrackerService returns an error: %v", err)
+	}
+}