Repository: xanzy/go-gitlab
Pull request #920 (2020-08-18 13:21:29)

Issues.GetIssueParticipants
cf. https://docs.gitlab.com/ee/api/issues.html#participants-on-issues

based on `GetMergeRequestParticipants`
Note: BasicUser.CreatedAt not filled

Edit: add TestGetMergeRequestParticipants too (similar test, 1 penny cost)


Patch:
diff --git a/issues.go b/issues.go
index 37eb4e7..3b202ba 100644
--- a/issues.go
+++ b/issues.go
@@ -673,3 +673,28 @@ func (s *IssuesService) ResetSpentTime(pid interface{}, issue int, options ...Re
 func (s *IssuesService) GetTimeSpent(pid interface{}, issue int, options ...RequestOptionFunc) (*TimeStats, *Response, error) {
 	return s.timeStats.getTimeSpent(pid, "issues", issue, options...)
 }
+
+// GetParticipants gets a list of issue participants.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ce/api/issues.html#participants-on-issues
+func (s *IssuesService) GetParticipants(pid interface{}, issue int, options ...RequestOptionFunc) ([]*BasicUser, *Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("projects/%s/issues/%d/participants", pathEscape(project), issue)
+
+	req, err := s.client.NewRequest("GET", u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	var bu []*BasicUser
+	resp, err := s.client.Do(req, &bu)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return bu, resp, err
+}


Test patch:
diff --git a/issues_test.go b/issues_test.go
index d89f879..0b686c1 100644
--- a/issues_test.go
+++ b/issues_test.go
@@ -548,3 +548,28 @@ func TestGetTimeSpent(t *testing.T) {
 		t.Errorf("Issues.GetTimeSpent returned %+v, want %+v", timeState, want)
 	}
 }
+
+func TestGetIssueParticipants(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/issues/5/participants", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		testURL(t, r, "/api/v4/projects/1/issues/5/participants")
+
+		fmt.Fprint(w, `[{"id":1,"name":"User1","username":"User1","state":"active","avatar_url":"","web_url":"https://localhost/User1"},
+		{"id":2,"name":"User2","username":"User2","state":"active","avatar_url":"https://localhost/uploads/-/system/user/avatar/2/avatar.png","web_url":"https://localhost/User2"}]`)
+	})
+
+	issueParticipants, _, err := client.Issues.GetParticipants("1", 5)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	want := []*BasicUser{{ID: 1, Name: "User1", Username: "User1", State: "active", AvatarURL: "", WebURL: "https://localhost/User1"},
+		{ID: 2, Name: "User2", Username: "User2", State: "active", AvatarURL: "https://localhost/uploads/-/system/user/avatar/2/avatar.png", WebURL: "https://localhost/User2"}}
+
+	if !reflect.DeepEqual(want, issueParticipants) {
+		t.Errorf("Issues.GetIssueParticipants returned %+v, want %+v", issueParticipants, want)
+	}
+}
diff --git a/merge_requests_test.go b/merge_requests_test.go
index 3d6d446..75e0ad0 100644
--- a/merge_requests_test.go
+++ b/merge_requests_test.go
@@ -2,7 +2,9 @@ package gitlab
 
 import (
 	"fmt"
+	"log"
 	"net/http"
+	"reflect"
 	"testing"
 	"time"
 
@@ -179,3 +181,28 @@ func TestCreateMergeRequestPipeline(t *testing.T) {
 	assert.Equal(t, 1, pipeline.ID)
 	assert.Equal(t, "pending", pipeline.Status)
 }
+
+func TestGetMergeRequestParticipants(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/merge_requests/5/participants", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		testURL(t, r, "/api/v4/projects/1/merge_requests/5/participants")
+
+		fmt.Fprint(w, `[{"id":1,"name":"User1","username":"User1","state":"active","avatar_url":"","web_url":"https://localhost/User1"},
+		{"id":2,"name":"User2","username":"User2","state":"active","avatar_url":"https://localhost/uploads/-/system/user/avatar/2/avatar.png","web_url":"https://localhost/User2"}]`)
+	})
+
+	mergeRequestParticipants, _, err := client.MergeRequests.GetMergeRequestParticipants("1", 5)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	want := []*BasicUser{{ID: 1, Name: "User1", Username: "User1", State: "active", AvatarURL: "", WebURL: "https://localhost/User1"},
+		{ID: 2, Name: "User2", Username: "User2", State: "active", AvatarURL: "https://localhost/uploads/-/system/user/avatar/2/avatar.png", WebURL: "https://localhost/User2"}}
+
+	if !reflect.DeepEqual(want, mergeRequestParticipants) {
+		t.Errorf("Issues.GetMergeRequestParticipants returned %+v, want %+v", mergeRequestParticipants, want)
+	}
+}