events.go

  1//
  2// Copyright 2017, Sander van Harmelen
  3//
  4// Licensed under the Apache License, Version 2.0 (the "License");
  5// you may not use this file except in compliance with the License.
  6// You may obtain a copy of the License at
  7//
  8//     http://www.apache.org/licenses/LICENSE-2.0
  9//
 10// Unless required by applicable law or agreed to in writing, software
 11// distributed under the License is distributed on an "AS IS" BASIS,
 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13// See the License for the specific language governing permissions and
 14// limitations under the License.
 15//
 16
 17package gitlab
 18
 19import (
 20	"fmt"
 21	"time"
 22)
 23
 24// EventsService handles communication with the event related methods of
 25// the GitLab API.
 26//
 27// GitLab API docs: https://docs.gitlab.com/ce/api/events.html
 28type EventsService struct {
 29	client *Client
 30}
 31
 32// ContributionEvent represents a user's contribution
 33//
 34// GitLab API docs:
 35// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
 36type ContributionEvent struct {
 37	Title       string     `json:"title"`
 38	ProjectID   int        `json:"project_id"`
 39	ActionName  string     `json:"action_name"`
 40	TargetID    int        `json:"target_id"`
 41	TargetIID   int        `json:"target_iid"`
 42	TargetType  string     `json:"target_type"`
 43	AuthorID    int        `json:"author_id"`
 44	TargetTitle string     `json:"target_title"`
 45	CreatedAt   *time.Time `json:"created_at"`
 46	PushData    struct {
 47		CommitCount int    `json:"commit_count"`
 48		Action      string `json:"action"`
 49		RefType     string `json:"ref_type"`
 50		CommitFrom  string `json:"commit_from"`
 51		CommitTo    string `json:"commit_to"`
 52		Ref         string `json:"ref"`
 53		CommitTitle string `json:"commit_title"`
 54	} `json:"push_data"`
 55	Note   *Note `json:"note"`
 56	Author struct {
 57		Name      string `json:"name"`
 58		Username  string `json:"username"`
 59		ID        int    `json:"id"`
 60		State     string `json:"state"`
 61		AvatarURL string `json:"avatar_url"`
 62		WebURL    string `json:"web_url"`
 63	} `json:"author"`
 64	AuthorUsername string `json:"author_username"`
 65}
 66
 67// ListContributionEventsOptions represents the options for GetUserContributionEvents
 68//
 69// GitLap API docs:
 70// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
 71type ListContributionEventsOptions struct {
 72	ListOptions
 73	Action     *EventTypeValue       `url:"action,omitempty" json:"action,omitempty"`
 74	TargetType *EventTargetTypeValue `url:"target_type,omitempty" json:"target_type,omitempty"`
 75	Before     *ISOTime              `url:"before,omitempty" json:"before,omitempty"`
 76	After      *ISOTime              `url:"after,omitempty" json:"after,omitempty"`
 77	Sort       *string               `url:"sort,omitempty" json:"sort,omitempty"`
 78}
 79
 80// ListUserContributionEvents retrieves user contribution events
 81// for the specified user, sorted from newest to oldest.
 82//
 83// GitLab API docs:
 84// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
 85func (s *UsersService) ListUserContributionEvents(uid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
 86	user, err := parseID(uid)
 87	if err != nil {
 88		return nil, nil, err
 89	}
 90	u := fmt.Sprintf("users/%s/events", user)
 91
 92	req, err := s.client.NewRequest("GET", u, opt, options)
 93	if err != nil {
 94		return nil, nil, err
 95	}
 96
 97	var cs []*ContributionEvent
 98	resp, err := s.client.Do(req, &cs)
 99	if err != nil {
100		return nil, resp, err
101	}
102
103	return cs, resp, err
104}
105
106// ListCurrentUserContributionEvents gets a list currently authenticated user's events
107//
108// GitLab API docs: https://docs.gitlab.com/ce/api/events.html#list-currently-authenticated-user-39-s-events
109func (s *EventsService) ListCurrentUserContributionEvents(opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
110	req, err := s.client.NewRequest("GET", "events", opt, options)
111	if err != nil {
112		return nil, nil, err
113	}
114
115	var cs []*ContributionEvent
116	resp, err := s.client.Do(req, &cs)
117	if err != nil {
118		return nil, resp, err
119	}
120
121	return cs, resp, err
122}
123
124// ListProjectVisibleEvents gets a list of visible events for a particular project
125//
126// GitLab API docs: https://docs.gitlab.com/ee/api/events.html#list-a-project-s-visible-events
127func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
128	project, err := parseID(pid)
129	if err != nil {
130		return nil, nil, err
131	}
132	u := fmt.Sprintf("projects/%s/events", pathEscape(project))
133
134	req, err := s.client.NewRequest("GET", u, opt, options)
135	if err != nil {
136		return nil, nil, err
137	}
138
139	var cs []*ContributionEvent
140	resp, err := s.client.Do(req, &cs)
141	if err != nil {
142		return nil, resp, err
143	}
144
145	return cs, resp, err
146}