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	"net/url"
22	"time"
23)
24
25// EventsService handles communication with the event related methods of
26// the GitLab API.
27//
28// GitLab API docs: https://docs.gitlab.com/ce/api/events.html
29type EventsService struct {
30	client *Client
31}
32
33// ContributionEvent represents a user's contribution
34//
35// GitLab API docs:
36// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
37type ContributionEvent struct {
38	Title       string     `json:"title"`
39	ProjectID   int        `json:"project_id"`
40	ActionName  string     `json:"action_name"`
41	TargetID    int        `json:"target_id"`
42	TargetIID   int        `json:"target_iid"`
43	TargetType  string     `json:"target_type"`
44	AuthorID    int        `json:"author_id"`
45	TargetTitle string     `json:"target_title"`
46	CreatedAt   *time.Time `json:"created_at"`
47	PushData    struct {
48		CommitCount int    `json:"commit_count"`
49		Action      string `json:"action"`
50		RefType     string `json:"ref_type"`
51		CommitFrom  string `json:"commit_from"`
52		CommitTo    string `json:"commit_to"`
53		Ref         string `json:"ref"`
54		CommitTitle string `json:"commit_title"`
55	} `json:"push_data"`
56	Note   *Note `json:"note"`
57	Author struct {
58		Name      string `json:"name"`
59		Username  string `json:"username"`
60		ID        int    `json:"id"`
61		State     string `json:"state"`
62		AvatarURL string `json:"avatar_url"`
63		WebURL    string `json:"web_url"`
64	} `json:"author"`
65	AuthorUsername string `json:"author_username"`
66}
67
68// ListContributionEventsOptions represents the options for GetUserContributionEvents
69//
70// GitLap API docs:
71// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
72type ListContributionEventsOptions struct {
73	ListOptions
74	Action     *EventTypeValue       `json:"action,omitempty"`
75	TargetType *EventTargetTypeValue `json:"target_type,omitempty"`
76	Before     *ISOTime              `json:"before,omitempty"`
77	After      *ISOTime              `json:"after,omitempty"`
78	Sort       *string               `json:"sort,omitempty"`
79}
80
81// ListUserContributionEvents retrieves user contribution events
82// for the specified user, sorted from newest to oldest.
83//
84// GitLab API docs:
85// https://docs.gitlab.com/ce/api/events.html#get-user-contribution-events
86func (s *UsersService) ListUserContributionEvents(uid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
87	user, err := parseID(uid)
88	if err != nil {
89		return nil, nil, err
90	}
91	u := fmt.Sprintf("users/%s/events", user)
92
93	req, err := s.client.NewRequest("GET", u, opt, options)
94	if err != nil {
95		return nil, nil, err
96	}
97
98	var cs []*ContributionEvent
99	resp, err := s.client.Do(req, &cs)
100	if err != nil {
101		return nil, resp, err
102	}
103
104	return cs, resp, err
105}
106
107// ListCurrentUserContributionEvents gets a list currently authenticated user's events
108//
109// GitLab API docs: https://docs.gitlab.com/ce/api/events.html#list-currently-authenticated-user-39-s-events
110func (s *EventsService) ListCurrentUserContributionEvents(opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
111	req, err := s.client.NewRequest("GET", "events", opt, options)
112	if err != nil {
113		return nil, nil, err
114	}
115
116	var cs []*ContributionEvent
117	resp, err := s.client.Do(req, &cs)
118	if err != nil {
119		return nil, resp, err
120	}
121
122	return cs, resp, err
123}
124
125// ListProjectVisibleEvents gets a list of visible events for a particular project
126//
127// GitLab API docs: https://docs.gitlab.com/ee/api/events.html#list-a-project-s-visible-events
128func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListContributionEventsOptions, options ...OptionFunc) ([]*ContributionEvent, *Response, error) {
129	project, err := parseID(pid)
130	if err != nil {
131		return nil, nil, err
132	}
133	u := fmt.Sprintf("projects/%s/events", url.QueryEscape(project))
134
135	req, err := s.client.NewRequest("GET", u, opt, options)
136	if err != nil {
137		return nil, nil, err
138	}
139
140	var cs []*ContributionEvent
141	resp, err := s.client.Do(req, &cs)
142	if err != nil {
143		return nil, resp, err
144	}
145
146	return cs, resp, err
147}
148