Repository: xanzy/go-gitlab
Pull request #573 (2019-03-01 21:25:06)

added support for group badges API


Patch:
diff --git a/gitlab.go b/gitlab.go
index a3da78b..29a7b12 100644
--- a/gitlab.go
+++ b/gitlab.go
@@ -302,6 +302,7 @@ type Client struct {
 	Events                *EventsService
 	Features              *FeaturesService
 	GitIgnoreTemplates    *GitIgnoreTemplatesService
+	GroupBadges           *GroupBadgesService
 	GroupIssueBoards      *GroupIssueBoardsService
 	GroupMembers          *GroupMembersService
 	GroupMilestones       *GroupMilestonesService
@@ -444,6 +445,7 @@ func newClient(httpClient *http.Client) *Client {
 	c.Events = &EventsService{client: c}
 	c.Features = &FeaturesService{client: c}
 	c.GitIgnoreTemplates = &GitIgnoreTemplatesService{client: c}
+	c.GroupBadges = &GroupBadgesService{client: c}
 	c.GroupIssueBoards = &GroupIssueBoardsService{client: c}
 	c.GroupMembers = &GroupMembersService{client: c}
 	c.GroupMilestones = &GroupMilestonesService{client: c}
diff --git a/group_badges.go b/group_badges.go
new file mode 100644
index 0000000..366fcc8
--- /dev/null
+++ b/group_badges.go
@@ -0,0 +1,214 @@
+package gitlab
+
+import (
+	"fmt"
+	"net/url"
+)
+
+// GroupBadgesService handles communication with the group badges
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html
+type GroupBadgesService struct {
+	client *Client
+}
+
+// BadgeKind represents a GitLab Badge Kind
+type BadgeKind string
+
+// all possible values Badge Kind
+const (
+	ProjectBadgeKind BadgeKind = "project"
+	GroupBadgeKind BadgeKind = "group"
+)
+
+// GroupBadge represents a group badge.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html
+type GroupBadge struct {
+	ID               int    `json:"id"`
+	LinkURL          string `json:"link_url"`
+	ImageURL         string `json:"image_url"`
+	RenderedLinkURL  string `json:"rendered_link_url"`
+	RenderedImageURL string `json:"rendered_image_url"`
+	Kind BadgeKind `json:"kind"`
+}
+
+// ListGroupBadgesOptions represents the available ListGroupBadges() options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#list-all-badges-of-a-group
+type ListGroupBadgesOptions ListOptions
+
+// ListGroupBadges gets a list of a group badges.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#list-all-badges-of-a-group
+func (s *GroupBadgesService) ListGroupBadges(gid interface{}, opt *ListGroupBadgesOptions, options ...OptionFunc) ([]*GroupBadge, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges", url.QueryEscape(group))
+
+	req, err := s.client.NewRequest("GET", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var gb []*GroupBadge
+	resp, err := s.client.Do(req, &gb)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gb, resp, err
+}
+
+// GetGroupBadge gets a group badge.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#get-a-badge-of-a-group
+func (s *GroupBadgesService) GetGroupBadge(gid interface{}, badge int, options ...OptionFunc) (*GroupBadge, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges/%d", url.QueryEscape(group), badge)
+
+	req, err := s.client.NewRequest("GET", u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	gb := new(GroupBadge)
+	resp, err := s.client.Do(req, gb)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gb, resp, err
+}
+
+// AddGroupBadgeOptions represents the available AddGroupBadge() options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#add-a-badge-to-a-group
+type AddGroupBadgeOptions struct {
+	LinkURL  *string `url:"link_url,omitempty" json:"link_url,omitempty"`
+	ImageURL *string `url:"image_url,omitempty" json:"image_url,omitempty"`
+}
+
+// AddGroupBadge adds a badge to a group.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#add-a-badge-to-a-group
+func (s *GroupBadgesService) AddGroupBadge(gid interface{}, opt *AddGroupBadgeOptions, options ...OptionFunc) (*GroupBadge, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges", url.QueryEscape(group))
+
+	req, err := s.client.NewRequest("POST", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	gb := new(GroupBadge)
+	resp, err := s.client.Do(req, gb)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gb, resp, err
+}
+
+// EditGroupBadgeOptions represents the available EditGroupBadge() options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#edit-a-badge-of-a-group
+type EditGroupBadgeOptions struct {
+	LinkURL  *string `url:"link_url,omitempty" json:"link_url,omitempty"`
+	ImageURL *string `url:"image_url,omitempty" json:"image_url,omitempty"`
+}
+
+// EditGroupBadge updates a badge of a group.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#edit-a-badge-of-a-group
+func (s *GroupBadgesService) EditGroupBadge(gid interface{}, badge int, opt *EditGroupBadgeOptions, options ...OptionFunc) (*GroupBadge, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges/%d", url.QueryEscape(group), badge)
+
+	req, err := s.client.NewRequest("PUT", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	gb := new(GroupBadge)
+	resp, err := s.client.Do(req, gb)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gb, resp, err
+}
+
+// DeleteGroupBadge removes a badge from a group.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#remove-a-badge-from-a-group
+func (s *GroupBadgesService) DeleteGroupBadge(gid interface{}, badge int, options ...OptionFunc) (*Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges/%d", url.QueryEscape(group), badge)
+
+	req, err := s.client.NewRequest("DELETE", u, nil, options)
+	if err != nil {
+		return nil, err
+	}
+
+	return s.client.Do(req, nil)
+}
+
+// GroupBadgePreviewOptions represents the available PreviewGroupBadge() options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#preview-a-badge-from-a-group
+type GroupBadgePreviewOptions struct {
+	LinkURL  *string `url:"link_url,omitempty" json:"link_url,omitempty"`
+	ImageURL *string `url:"image_url,omitempty" json:"image_url,omitempty"`
+}
+
+// PreviewGroupBadge returns how the link_url and image_url final URLs would be after
+// resolving the placeholder interpolation.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/group_badges.html#preview-a-badge-from-a-group
+func (s *GroupBadgesService) PreviewGroupBadge(gid interface{}, opt *GroupBadgePreviewOptions, options ...OptionFunc) (*GroupBadge, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/badges/render", url.QueryEscape(group))
+
+	req, err := s.client.NewRequest("GET", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	gb := new(GroupBadge)
+	resp, err := s.client.Do(req, &gb)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gb, resp, err
+}


Test patch:
diff --git a/groups_badges_test.go b/groups_badges_test.go
new file mode 100644
index 0000000..cfe28c1
--- /dev/null
+++ b/groups_badges_test.go
@@ -0,0 +1,118 @@
+package gitlab
+
+import (
+	"fmt"
+	"net/http"
+	"reflect"
+	"testing"
+)
+
+func TestListGroupBadges(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/badges",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, "GET")
+			fmt.Fprint(w, `[{"id":1, "kind":"group"},{"id":2, "kind":"group"}]`)
+		})
+
+	badges, _, err := client.GroupBadges.ListGroupBadges(1, &ListGroupBadgesOptions{})
+	if err != nil {
+		t.Errorf("GroupBadges.ListGroupBadges returned error: %v", err)
+	}
+
+	want := []*GroupBadge{{ID: 1, Kind: GroupBadgeKind}, {ID: 2, Kind: GroupBadgeKind}}
+	if !reflect.DeepEqual(want, badges) {
+		t.Errorf("GroupBadges.ListGroupBadges returned %+v, want %+v", badges, want)
+	}
+}
+
+func TestGetGroupBadge(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/badges/2",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, "GET")
+			fmt.Fprint(w, `{"id":2, "kind":"group"}`)
+		})
+
+	badge, _, err := client.GroupBadges.GetGroupBadge(1, 2)
+	if err != nil {
+		t.Errorf("GroupBadges.GetGroupBadge returned error: %v", err)
+	}
+
+	want := &GroupBadge{ID: 2, Kind: GroupBadgeKind}
+	if !reflect.DeepEqual(want, badge) {
+		t.Errorf("GroupBadges.GetGroupBadge returned %+v, want %+v", badge, want)
+	}
+}
+
+func TestAddGroupBadge(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/badges",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, "POST")
+			fmt.Fprint(w, `{"id":3, "link_url":"LINK", "image_url":"IMAGE", "kind":"group"}`)
+		})
+
+	opt := &AddGroupBadgeOptions{ImageURL: String("IMAGE"), LinkURL: String("LINK"),}
+	badge, _, err := client.GroupBadges.AddGroupBadge(1, opt)
+	if err != nil {
+		t.Errorf("GroupBadges.AddGroupBadge returned error: %v", err)
+	}
+
+	want := &GroupBadge{ID: 3, ImageURL: "IMAGE", LinkURL: "LINK", Kind: GroupBadgeKind}
+	if !reflect.DeepEqual(want, badge) {
+		t.Errorf("GroupBadges.AddGroupBadge returned %+v, want %+v", badge, want)
+	}
+}
+
+func TestEditGroupBadge(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/badges/2",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, "PUT")
+			fmt.Fprint(w, `{"id":2, "link_url":"NEW_LINK", "image_url":"NEW_IMAGE", "kind":"group"}`)
+		})
+
+	opt := &EditGroupBadgeOptions{ImageURL: String("NEW_IMAGE"), LinkURL: String("NEW_LINK"),}
+	badge, _, err := client.GroupBadges.EditGroupBadge(1, 2, opt)
+	if err != nil {
+		t.Errorf("GroupBadges.EditGroupBadge returned error: %v", err)
+	}
+
+	want := &GroupBadge{ID: 2, ImageURL: "NEW_IMAGE", LinkURL: "NEW_LINK", Kind: GroupBadgeKind}
+	if !reflect.DeepEqual(want, badge) {
+		t.Errorf("GroupBadges.EditGroupBadge returned %+v, want %+v", badge, want)
+	}
+}
+
+func TestRemoveGroupBadge(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/badges/2",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, "DELETE")
+			w.WriteHeader(http.StatusAccepted)
+		},
+	)
+
+	resp, err := client.GroupBadges.DeleteGroupBadge(1, 2)
+	if err != nil {
+		t.Errorf("GroupBadges.DeleteGroupBadge returned error: %v", err)
+	}
+
+	want := http.StatusAccepted
+	got := resp.StatusCode
+	if got != want {
+		t.Errorf("GroupsBadges.DeleteGroupBadge returned %d, want %d", got, want)
+	}
+
+}