Repository: xanzy/go-gitlab
Pull request #1673 (2023-03-15 11:32:02)

update gitlab project event struct
It seems gitlab project event api has upgrade, and the old struct is not correct to now api, so i update the project event struct to match the new api.


Patch:
diff --git a/events.go b/events.go
index cdbee0d..fb77219 100644
--- a/events.go
+++ b/events.go
@@ -123,10 +123,86 @@ func (s *EventsService) ListCurrentUserContributionEvents(opt *ListContributionE
 	return cs, resp, err
 }
 
-// ListProjectVisibleEvents gets a list of visible events for a particular project
+// ProjectEvent represents a GitLab project event.
 //
-// GitLab API docs: https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
-func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListContributionEventsOptions, options ...RequestOptionFunc) ([]*ContributionEvent, *Response, error) {
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
+type ProjectEvent struct {
+	ID          int    `json:"id"`
+	Title       string `json:"title"`
+	ProjectID   int    `json:"project_id"`
+	ActionName  string `json:"action_name"`
+	TargetID    int    `json:"target_id"`
+	TargetIID   int    `json:"target_iid"`
+	TargetType  string `json:"target_type"`
+	AuthorID    int    `json:"author_id"`
+	TargetTitle string `json:"target_title"`
+	CreatedAt   string `json:"created_at"`
+	Author      struct {
+		Name      string `json:"name"`
+		Username  string `json:"username"`
+		ID        int    `json:"id"`
+		State     string `json:"state"`
+		AvatarURL string `json:"avatar_url"`
+		WebURL    string `json:"web_url"`
+	} `json:"author"`
+	AuthorUsername string `json:"author_username"`
+	Data           struct {
+		Before            string      `json:"before"`
+		After             string      `json:"after"`
+		Ref               string      `json:"ref"`
+		UserID            int         `json:"user_id"`
+		UserName          string      `json:"user_name"`
+		Repository        *Repository `json:"repository"`
+		Commits           []*Commit   `json:"commits"`
+		TotalCommitsCount int         `json:"total_commits_count"`
+	} `json:"data"`
+	Note struct {
+		ID         int    `json:"id"`
+		Body       string `json:"body"`
+		Attachment string `json:"attachment"`
+		Author     struct {
+			ID        int    `json:"id"`
+			Username  string `json:"username"`
+			Email     string `json:"email"`
+			Name      string `json:"name"`
+			State     string `json:"state"`
+			AvatarURL string `json:"avatar_url"`
+			WebURL    string `json:"web_url"`
+		} `json:"author"`
+		CreatedAt    *time.Time `json:"created_at"`
+		System       bool       `json:"system"`
+		NoteableID   int        `json:"noteable_id"`
+		NoteableType string     `json:"noteable_type"`
+		NoteableIID  int        `json:"noteable_iid"`
+	} `json:"note"`
+	PushData struct {
+		CommitCount int    `json:"commit_count"`
+		Action      string `json:"action"`
+		RefType     string `json:"ref_type"`
+		CommitFrom  string `json:"commit_from"`
+		CommitTo    string `json:"commit_to"`
+		Ref         string `json:"ref"`
+		CommitTitle string `json:"commit_title"`
+	} `json:"push_data"`
+}
+
+func (s ProjectEvent) String() string {
+	return Stringify(s)
+}
+
+// ListProjectVisibleEventsOptions represents the available
+// ListProjectVisibleEvents() options.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
+type ListProjectVisibleEventsOptions ListOptions
+
+// ListProjectVisibleEvents gets the events for the specified project.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
+func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListProjectVisibleEventsOptions, options ...RequestOptionFunc) ([]*ProjectEvent, *Response, error) {
 	project, err := parseID(pid)
 	if err != nil {
 		return nil, nil, err
@@ -138,11 +214,11 @@ func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListContr
 		return nil, nil, err
 	}
 
-	var cs []*ContributionEvent
-	resp, err := s.client.Do(req, &cs)
+	var p []*ProjectEvent
+	resp, err := s.client.Do(req, &p)
 	if err != nil {
 		return nil, resp, err
 	}
 
-	return cs, resp, err
+	return p, resp, err
 }
diff --git a/projects.go b/projects.go
index e85b192..662a67c 100644
--- a/projects.go
+++ b/projects.go
@@ -567,67 +567,6 @@ func (s *ProjectsService) GetProject(pid interface{}, opt *GetProjectOptions, op
 	return p, resp, err
 }
 
-// ProjectEvent represents a GitLab project event.
-//
-// GitLab API docs:
-// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
-type ProjectEvent struct {
-	Title          interface{} `json:"title"`
-	ProjectID      int         `json:"project_id"`
-	ActionName     string      `json:"action_name"`
-	TargetID       interface{} `json:"target_id"`
-	TargetType     interface{} `json:"target_type"`
-	AuthorID       int         `json:"author_id"`
-	AuthorUsername string      `json:"author_username"`
-	Data           struct {
-		Before            string      `json:"before"`
-		After             string      `json:"after"`
-		Ref               string      `json:"ref"`
-		UserID            int         `json:"user_id"`
-		UserName          string      `json:"user_name"`
-		Repository        *Repository `json:"repository"`
-		Commits           []*Commit   `json:"commits"`
-		TotalCommitsCount int         `json:"total_commits_count"`
-	} `json:"data"`
-	TargetTitle interface{} `json:"target_title"`
-}
-
-func (s ProjectEvent) String() string {
-	return Stringify(s)
-}
-
-// GetProjectEventsOptions represents the available GetProjectEvents() options.
-//
-// GitLab API docs:
-// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
-type GetProjectEventsOptions ListOptions
-
-// GetProjectEvents gets the events for the specified project. Sorted from
-// newest to latest.
-//
-// GitLab API docs:
-// https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
-func (s *ProjectsService) GetProjectEvents(pid interface{}, opt *GetProjectEventsOptions, options ...RequestOptionFunc) ([]*ProjectEvent, *Response, error) {
-	project, err := parseID(pid)
-	if err != nil {
-		return nil, nil, err
-	}
-	u := fmt.Sprintf("projects/%s/events", PathEscape(project))
-
-	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
-	if err != nil {
-		return nil, nil, err
-	}
-
-	var p []*ProjectEvent
-	resp, err := s.client.Do(req, &p)
-	if err != nil {
-		return nil, resp, err
-	}
-
-	return p, resp, err
-}
-
 // CreateProjectOptions represents the available CreateProject() options.
 //
 // GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#create-project


Test patch:
diff --git a/events_test.go b/events_test.go
index 448893d..5f7477b 100644
--- a/events_test.go
+++ b/events_test.go
@@ -201,12 +201,40 @@ func TestEventsService_ListProjectVisibleEvents(t *testing.T) {
 				  "web_url": "http://localhost:3000/venky333"
 				},
 				"author_username": "venky333"
+			},
+			{
+				"id": 4,
+				"title": null,
+				"project_id": 15,
+				"action_name": "pushed",
+				"target_id": null,
+				"target_type": null,
+				"author_id": 1,
+				"author": {
+				"name": "Dmitriy Zaporozhets",
+				"username": "root",
+				"id": 1,
+				"state": "active",
+				"avatar_url": "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
+				"web_url": "http://localhost:3000/root"
+				},
+				"author_username": "john",
+				"push_data": {
+				"commit_count": 1,
+				"action": "pushed",
+				"ref_type": "branch",
+				"commit_from": "50d4420237a9de7be1304607147aec22e4a14af7",
+				"commit_to": "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
+				"ref": "master",
+				"commit_title": "Add simple search to projects in public area"
+				},
+				"target_title": null
 			  }
 			]
 		`)
 	})
 
-	want := []*ContributionEvent{
+	want := []*ProjectEvent{
 		{
 			ID:          3,
 			Title:       "",
@@ -217,7 +245,6 @@ func TestEventsService_ListProjectVisibleEvents(t *testing.T) {
 			TargetType:  "Issue",
 			AuthorID:    1,
 			TargetTitle: "Public project search field",
-			Note:        nil,
 			Author: struct {
 				Name      string `json:"name"`
 				Username  string `json:"username"`
@@ -235,6 +262,52 @@ func TestEventsService_ListProjectVisibleEvents(t *testing.T) {
 			},
 			AuthorUsername: "venky333",
 		},
+		// example from https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events
+		{
+			ID:          4,
+			Title:       "",
+			ProjectID:   15,
+			ActionName:  "pushed",
+			TargetID:    0,
+			TargetIID:   0,
+			TargetType:  "",
+			AuthorID:    1,
+			TargetTitle: "",
+			CreatedAt:   "",
+			Author: struct {
+				Name      string `json:"name"`
+				Username  string `json:"username"`
+				ID        int    `json:"id"`
+				State     string `json:"state"`
+				AvatarURL string `json:"avatar_url"`
+				WebURL    string `json:"web_url"`
+			}{
+				Name:      "Dmitriy Zaporozhets",
+				Username:  "root",
+				ID:        1,
+				State:     "active",
+				AvatarURL: "http://localhost:3000/uploads/user/avatar/1/fox_avatar.png",
+				WebURL:    "http://localhost:3000/root",
+			},
+			AuthorUsername: "john",
+			PushData: struct {
+				CommitCount int    `json:"commit_count"`
+				Action      string `json:"action"`
+				RefType     string `json:"ref_type"`
+				CommitFrom  string `json:"commit_from"`
+				CommitTo    string `json:"commit_to"`
+				Ref         string `json:"ref"`
+				CommitTitle string `json:"commit_title"`
+			}{
+				CommitCount: 1,
+				Action:      "pushed",
+				RefType:     "branch",
+				CommitFrom:  "50d4420237a9de7be1304607147aec22e4a14af7",
+				CommitTo:    "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
+				Ref:         "master",
+				CommitTitle: "Add simple search to projects in public area",
+			},
+		},
 	}
 
 	ces, resp, err := client.Events.ListProjectVisibleEvents(15, nil, nil)