...

Source file src/github.com/xanzy/go-gitlab/events.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, 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  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    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/ee/api/events.html
    29  type EventsService struct {
    30  	client *Client
    31  }
    32  
    33  // ContributionEvent represents a user's contribution
    34  //
    35  // GitLab API docs:
    36  // https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events
    37  type ContributionEvent struct {
    38  	ID          int        `json:"id"`
    39  	Title       string     `json:"title"`
    40  	ProjectID   int        `json:"project_id"`
    41  	ActionName  string     `json:"action_name"`
    42  	TargetID    int        `json:"target_id"`
    43  	TargetIID   int        `json:"target_iid"`
    44  	TargetType  string     `json:"target_type"`
    45  	AuthorID    int        `json:"author_id"`
    46  	TargetTitle string     `json:"target_title"`
    47  	CreatedAt   *time.Time `json:"created_at"`
    48  	PushData    struct {
    49  		CommitCount int    `json:"commit_count"`
    50  		Action      string `json:"action"`
    51  		RefType     string `json:"ref_type"`
    52  		CommitFrom  string `json:"commit_from"`
    53  		CommitTo    string `json:"commit_to"`
    54  		Ref         string `json:"ref"`
    55  		CommitTitle string `json:"commit_title"`
    56  	} `json:"push_data"`
    57  	Note   *Note `json:"note"`
    58  	Author struct {
    59  		Name      string `json:"name"`
    60  		Username  string `json:"username"`
    61  		ID        int    `json:"id"`
    62  		State     string `json:"state"`
    63  		AvatarURL string `json:"avatar_url"`
    64  		WebURL    string `json:"web_url"`
    65  	} `json:"author"`
    66  	AuthorUsername string `json:"author_username"`
    67  }
    68  
    69  // ListContributionEventsOptions represents the options for GetUserContributionEvents
    70  //
    71  // GitLap API docs:
    72  // https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events
    73  type ListContributionEventsOptions struct {
    74  	ListOptions
    75  	Action     *EventTypeValue       `url:"action,omitempty" json:"action,omitempty"`
    76  	TargetType *EventTargetTypeValue `url:"target_type,omitempty" json:"target_type,omitempty"`
    77  	Before     *ISOTime              `url:"before,omitempty" json:"before,omitempty"`
    78  	After      *ISOTime              `url:"after,omitempty" json:"after,omitempty"`
    79  	Sort       *string               `url:"sort,omitempty" json:"sort,omitempty"`
    80  }
    81  
    82  // ListUserContributionEvents retrieves user contribution events
    83  // for the specified user, sorted from newest to oldest.
    84  //
    85  // GitLab API docs:
    86  // https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events
    87  func (s *UsersService) ListUserContributionEvents(uid interface{}, opt *ListContributionEventsOptions, options ...RequestOptionFunc) ([]*ContributionEvent, *Response, error) {
    88  	user, err := parseID(uid)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  	u := fmt.Sprintf("users/%s/events", user)
    93  
    94  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  
    99  	var cs []*ContributionEvent
   100  	resp, err := s.client.Do(req, &cs)
   101  	if err != nil {
   102  		return nil, resp, err
   103  	}
   104  
   105  	return cs, resp, nil
   106  }
   107  
   108  // ListCurrentUserContributionEvents gets a list currently authenticated user's events
   109  //
   110  // GitLab API docs: https://docs.gitlab.com/ee/api/events.html#list-currently-authenticated-users-events
   111  func (s *EventsService) ListCurrentUserContributionEvents(opt *ListContributionEventsOptions, options ...RequestOptionFunc) ([]*ContributionEvent, *Response, error) {
   112  	req, err := s.client.NewRequest(http.MethodGet, "events", opt, options)
   113  	if err != nil {
   114  		return nil, nil, err
   115  	}
   116  
   117  	var cs []*ContributionEvent
   118  	resp, err := s.client.Do(req, &cs)
   119  	if err != nil {
   120  		return nil, resp, err
   121  	}
   122  
   123  	return cs, resp, nil
   124  }
   125  
   126  // ProjectEvent represents a GitLab project event.
   127  //
   128  // GitLab API docs:
   129  // https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
   130  type ProjectEvent struct {
   131  	ID          int    `json:"id"`
   132  	Title       string `json:"title"`
   133  	ProjectID   int    `json:"project_id"`
   134  	ActionName  string `json:"action_name"`
   135  	TargetID    int    `json:"target_id"`
   136  	TargetIID   int    `json:"target_iid"`
   137  	TargetType  string `json:"target_type"`
   138  	AuthorID    int    `json:"author_id"`
   139  	TargetTitle string `json:"target_title"`
   140  	CreatedAt   string `json:"created_at"`
   141  	Author      struct {
   142  		Name      string `json:"name"`
   143  		Username  string `json:"username"`
   144  		ID        int    `json:"id"`
   145  		State     string `json:"state"`
   146  		AvatarURL string `json:"avatar_url"`
   147  		WebURL    string `json:"web_url"`
   148  	} `json:"author"`
   149  	AuthorUsername string `json:"author_username"`
   150  	Data           struct {
   151  		Before            string      `json:"before"`
   152  		After             string      `json:"after"`
   153  		Ref               string      `json:"ref"`
   154  		UserID            int         `json:"user_id"`
   155  		UserName          string      `json:"user_name"`
   156  		Repository        *Repository `json:"repository"`
   157  		Commits           []*Commit   `json:"commits"`
   158  		TotalCommitsCount int         `json:"total_commits_count"`
   159  	} `json:"data"`
   160  	Note struct {
   161  		ID         int    `json:"id"`
   162  		Body       string `json:"body"`
   163  		Attachment string `json:"attachment"`
   164  		Author     struct {
   165  			ID        int    `json:"id"`
   166  			Username  string `json:"username"`
   167  			Email     string `json:"email"`
   168  			Name      string `json:"name"`
   169  			State     string `json:"state"`
   170  			AvatarURL string `json:"avatar_url"`
   171  			WebURL    string `json:"web_url"`
   172  		} `json:"author"`
   173  		CreatedAt    *time.Time `json:"created_at"`
   174  		System       bool       `json:"system"`
   175  		NoteableID   int        `json:"noteable_id"`
   176  		NoteableType string     `json:"noteable_type"`
   177  		NoteableIID  int        `json:"noteable_iid"`
   178  	} `json:"note"`
   179  	PushData struct {
   180  		CommitCount int    `json:"commit_count"`
   181  		Action      string `json:"action"`
   182  		RefType     string `json:"ref_type"`
   183  		CommitFrom  string `json:"commit_from"`
   184  		CommitTo    string `json:"commit_to"`
   185  		Ref         string `json:"ref"`
   186  		CommitTitle string `json:"commit_title"`
   187  	} `json:"push_data"`
   188  }
   189  
   190  func (s ProjectEvent) String() string {
   191  	return Stringify(s)
   192  }
   193  
   194  // ListProjectVisibleEventsOptions represents the available
   195  // ListProjectVisibleEvents() options.
   196  //
   197  // GitLab API docs:
   198  // https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
   199  type ListProjectVisibleEventsOptions struct {
   200  	ListOptions
   201  	Action     *EventTypeValue       `url:"action,omitempty" json:"action,omitempty"`
   202  	TargetType *EventTargetTypeValue `url:"target_type,omitempty" json:"target_type,omitempty"`
   203  	Before     *ISOTime              `url:"before,omitempty" json:"before,omitempty"`
   204  	After      *ISOTime              `url:"after,omitempty" json:"after,omitempty"`
   205  	Sort       *string               `url:"sort,omitempty" json:"sort,omitempty"`
   206  }
   207  
   208  // ListProjectVisibleEvents gets the events for the specified project.
   209  //
   210  // GitLab API docs:
   211  // https://docs.gitlab.com/ee/api/events.html#list-a-projects-visible-events
   212  func (s *EventsService) ListProjectVisibleEvents(pid interface{}, opt *ListProjectVisibleEventsOptions, options ...RequestOptionFunc) ([]*ProjectEvent, *Response, error) {
   213  	project, err := parseID(pid)
   214  	if err != nil {
   215  		return nil, nil, err
   216  	}
   217  	u := fmt.Sprintf("projects/%s/events", PathEscape(project))
   218  
   219  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   220  	if err != nil {
   221  		return nil, nil, err
   222  	}
   223  
   224  	var p []*ProjectEvent
   225  	resp, err := s.client.Do(req, &p)
   226  	if err != nil {
   227  		return nil, resp, err
   228  	}
   229  
   230  	return p, resp, nil
   231  }
   232  

View as plain text