Commit 22272f90 authored by Michael Lihs's avatar Michael Lihs Committed by Sander van Harmelen

add support for GET user/activities #248

Since the response of `GET user/activities` contains a date format (`YYYY-MM-DD`) that cannot be unmarshalled into a `time.Time` automatically, additional logic was added to to cover the un-marshalling of the timestamps:

`UserActivityTime.UnmarshalJSON()`
`UserActivityTime.MarshalJSON()`
`UserActivityTime.IsSet()`

see https://stackoverflow.com/questions/25087960/json-unmarshal-time-that-isnt-in-rfc-3339-format
parent 7c28a6b8
......@@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"time"
"strings"
)
// UsersService handles communication with the user related methods of
......@@ -696,3 +697,67 @@ func (s *UsersService) RevokeImpersonationToken(user, token int, options ...Opti
return s.client.Do(req, nil)
}
// UserActivityTime represents a custom time format
// used by Gitlab in the user/activities response (YYYY-MM-DD)
type UserActivityTime struct {
time.Time
}
// User Activity represents an entry in the user/activities response
// LastActivityAt is deprecated and only available for downward compatibility
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
type UserActivity struct {
Username string `json:"username"`
LastActivityOn *UserActivityTime `json:"last_activity_on"`
LastActivityAt *UserActivityTime `json:"last_activity_at"` // deprecated!
}
// layout of UserActivityTime for parsing time string
const uatLayout = "2006-01-02"
// custom unmarshaller for for UserActivityTime
func (uat *UserActivityTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
uat.Time = time.Time{}
return
}
uat.Time, err = time.Parse(uatLayout, s)
return
}
// custom marshaller for UserActivityTime
func (uat *UserActivityTime) MarshalJSON() ([]byte, error) {
if uat.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", uat.Time.Format(uatLayout))), nil
}
// helper method to check whether UserActivityTime is set
var nilTime = (time.Time{}).UnixNano()
func (uat *UserActivityTime) IsSet() bool {
return uat.UnixNano() != nilTime
}
// Get user activities (admin only)
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
func (s *UsersService) GetUserActivities(options ...OptionFunc) ([]*UserActivity, *Response, error) {
req, err := s.client.NewRequest("GET", "user/activities", nil, options)
if err != nil {
return nil, nil, err
}
var t []*UserActivity
resp, err := s.client.Do(req, &t)
if err != nil {
return nil, resp, err
}
return t, resp, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment