...

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

Documentation: github.com/xanzy/go-gitlab

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  // AuditEvent represents an audit event for a group, a project or the instance.
    10  //
    11  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html
    12  type AuditEvent struct {
    13  	ID         int               `json:"id"`
    14  	AuthorID   int               `json:"author_id"`
    15  	EntityID   int               `json:"entity_id"`
    16  	EntityType string            `json:"entity_type"`
    17  	Details    AuditEventDetails `json:"details"`
    18  	CreatedAt  *time.Time        `json:"created_at"`
    19  	EventType  string            `json:"event_type"`
    20  }
    21  
    22  // AuditEventDetails represents the details portion of an audit event for
    23  // a group, a project or the instance. The exact fields that are returned
    24  // for an audit event depend on the action being recorded.
    25  //
    26  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html
    27  type AuditEventDetails struct {
    28  	With          string      `json:"with"`
    29  	Add           string      `json:"add"`
    30  	As            string      `json:"as"`
    31  	Change        string      `json:"change"`
    32  	From          string      `json:"from"`
    33  	To            string      `json:"to"`
    34  	Remove        string      `json:"remove"`
    35  	CustomMessage string      `json:"custom_message"`
    36  	AuthorName    string      `json:"author_name"`
    37  	AuthorEmail   string      `json:"author_email"`
    38  	AuthorClass   string      `json:"author_class"`
    39  	TargetID      interface{} `json:"target_id"`
    40  	TargetType    string      `json:"target_type"`
    41  	TargetDetails string      `json:"target_details"`
    42  	IPAddress     string      `json:"ip_address"`
    43  	EntityPath    string      `json:"entity_path"`
    44  	FailedLogin   string      `json:"failed_login"`
    45  }
    46  
    47  // AuditEventsService handles communication with the project/group/instance
    48  // audit event related methods of the GitLab API.
    49  //
    50  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html
    51  type AuditEventsService struct {
    52  	client *Client
    53  }
    54  
    55  // ListAuditEventsOptions represents the available ListProjectAuditEvents(),
    56  // ListGroupAuditEvents() or ListInstanceAuditEvents() options.
    57  //
    58  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html
    59  type ListAuditEventsOptions struct {
    60  	ListOptions
    61  	CreatedAfter  *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
    62  	CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
    63  }
    64  
    65  // ListInstanceAuditEvents gets a list of audit events for instance.
    66  // Authentication as Administrator is required.
    67  //
    68  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html#retrieve-all-instance-audit-events
    69  func (s *AuditEventsService) ListInstanceAuditEvents(opt *ListAuditEventsOptions, options ...RequestOptionFunc) ([]*AuditEvent, *Response, error) {
    70  	req, err := s.client.NewRequest(http.MethodGet, "audit_events", opt, options)
    71  	if err != nil {
    72  		return nil, nil, err
    73  	}
    74  
    75  	var aes []*AuditEvent
    76  	resp, err := s.client.Do(req, &aes)
    77  	if err != nil {
    78  		return nil, resp, err
    79  	}
    80  
    81  	return aes, resp, nil
    82  }
    83  
    84  // GetInstanceAuditEvent gets a specific instance audit event.
    85  // Authentication as Administrator is required.
    86  //
    87  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html#retrieve-single-instance-audit-event
    88  func (s *AuditEventsService) GetInstanceAuditEvent(event int, options ...RequestOptionFunc) (*AuditEvent, *Response, error) {
    89  	u := fmt.Sprintf("audit_events/%d", event)
    90  
    91  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  
    96  	ae := new(AuditEvent)
    97  	resp, err := s.client.Do(req, ae)
    98  	if err != nil {
    99  		return nil, resp, err
   100  	}
   101  
   102  	return ae, resp, nil
   103  }
   104  
   105  // ListGroupAuditEvents gets a list of audit events for the specified group
   106  // viewable by the authenticated user.
   107  //
   108  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html#retrieve-all-group-audit-events
   109  func (s *AuditEventsService) ListGroupAuditEvents(gid interface{}, opt *ListAuditEventsOptions, options ...RequestOptionFunc) ([]*AuditEvent, *Response, error) {
   110  	group, err := parseID(gid)
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  	u := fmt.Sprintf("groups/%s/audit_events", PathEscape(group))
   115  
   116  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   117  	if err != nil {
   118  		return nil, nil, err
   119  	}
   120  
   121  	var aes []*AuditEvent
   122  	resp, err := s.client.Do(req, &aes)
   123  	if err != nil {
   124  		return nil, resp, err
   125  	}
   126  
   127  	return aes, resp, nil
   128  }
   129  
   130  // GetGroupAuditEvent gets a specific group audit event.
   131  //
   132  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html#retrieve-a-specific-group-audit-event
   133  func (s *AuditEventsService) GetGroupAuditEvent(gid interface{}, event int, options ...RequestOptionFunc) (*AuditEvent, *Response, error) {
   134  	group, err := parseID(gid)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  	u := fmt.Sprintf("groups/%s/audit_events/%d", PathEscape(group), event)
   139  
   140  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  
   145  	ae := new(AuditEvent)
   146  	resp, err := s.client.Do(req, ae)
   147  	if err != nil {
   148  		return nil, resp, err
   149  	}
   150  
   151  	return ae, resp, nil
   152  }
   153  
   154  // ListProjectAuditEvents gets a list of audit events for the specified project
   155  // viewable by the authenticated user.
   156  //
   157  // GitLab API docs: https://docs.gitlab.com/ee/api/audit_events.html#retrieve-all-project-audit-events
   158  func (s *AuditEventsService) ListProjectAuditEvents(pid interface{}, opt *ListAuditEventsOptions, options ...RequestOptionFunc) ([]*AuditEvent, *Response, error) {
   159  	project, err := parseID(pid)
   160  	if err != nil {
   161  		return nil, nil, err
   162  	}
   163  	u := fmt.Sprintf("projects/%s/audit_events", PathEscape(project))
   164  
   165  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   166  	if err != nil {
   167  		return nil, nil, err
   168  	}
   169  
   170  	var aes []*AuditEvent
   171  	resp, err := s.client.Do(req, &aes)
   172  	if err != nil {
   173  		return nil, resp, err
   174  	}
   175  
   176  	return aes, resp, nil
   177  }
   178  
   179  // GetProjectAuditEvent gets a specific project audit event.
   180  //
   181  // GitLab API docs:
   182  // https://docs.gitlab.com/ee/api/audit_events.html#retrieve-a-specific-project-audit-event
   183  func (s *AuditEventsService) GetProjectAuditEvent(pid interface{}, event int, options ...RequestOptionFunc) (*AuditEvent, *Response, error) {
   184  	project, err := parseID(pid)
   185  	if err != nil {
   186  		return nil, nil, err
   187  	}
   188  	u := fmt.Sprintf("projects/%s/audit_events/%d", PathEscape(project), event)
   189  
   190  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   191  	if err != nil {
   192  		return nil, nil, err
   193  	}
   194  
   195  	ae := new(AuditEvent)
   196  	resp, err := s.client.Do(req, ae)
   197  	if err != nil {
   198  		return nil, resp, err
   199  	}
   200  
   201  	return ae, resp, nil
   202  }
   203  

View as plain text