diff --git a/testdata/post_user_personal_access_tokens.json b/testdata/post_user_personal_access_tokens.json new file mode 100644 index 000000000..7aef8afda --- /dev/null +++ b/testdata/post_user_personal_access_tokens.json @@ -0,0 +1,13 @@ +{ + "id": 3, + "name": "mytoken", + "revoked": false, + "created_at": "2020-10-14T11:58:53.526Z", + "scopes": [ + "k8s_proxy" + ], + "user_id": 42, + "active": true, + "expires_at": "2020-10-15", + "token": "glpat-aaaaaaaa-bbbbbbbbb" +} diff --git a/users.go b/users.go index 04a35ad52..4489034e1 100644 --- a/users.go +++ b/users.go @@ -1301,6 +1301,38 @@ func (s *UsersService) CreatePersonalAccessToken(user int, opt *CreatePersonalAc return t, resp, nil } +// CreatePersonalAccessTokenForCurrentUserOptions represents the available +// CreatePersonalAccessTokenForCurrentUser() options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user +type CreatePersonalAccessTokenForCurrentUserOptions struct { + Name *string `url:"name,omitempty" json:"name,omitempty"` + Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"` + ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"` +} + +// CreatePersonalAccessTokenForCurrentUser creates a personal access token with limited scopes for the currently authenticated user. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user +func (s *UsersService) CreatePersonalAccessTokenForCurrentUser(opt *CreatePersonalAccessTokenForCurrentUserOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) { + u := "user/personal_access_tokens" + + req, err := s.client.NewRequest(http.MethodPost, u, opt, options) + if err != nil { + return nil, nil, err + } + + t := new(PersonalAccessToken) + resp, err := s.client.Do(req, &t) + if err != nil { + return nil, resp, err + } + + return t, resp, nil +} + // UserActivity represents an entry in the user/activities response // // GitLab API docs: diff --git a/users_test.go b/users_test.go index 4ee6887e4..5525ab7af 100644 --- a/users_test.go +++ b/users_test.go @@ -682,3 +682,37 @@ func TestCreateUserRunner(t *testing.T) { require.Equal(t, "glrt-1234567890ABCD", response.Token) require.Equal(t, (*time.Time)(nil), response.TokenExpiresAt) } + +func TestCreatePersonalAccessTokenForCurrentUser(t *testing.T) { + mux, client := setup(t) + + path := "/api/v4/user/personal_access_tokens" + + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + mustWriteHTTPResponse(t, w, "testdata/post_user_personal_access_tokens.json") + }) + + scopes := []string{"k8s_proxy"} + expiresAt := ISOTime(time.Date(2020, time.October, 15, 0, 0, 0, 0, time.UTC)) + user, _, err := client.Users.CreatePersonalAccessTokenForCurrentUser(&CreatePersonalAccessTokenForCurrentUserOptions{ + Name: String("mytoken"), + Scopes: &scopes, + ExpiresAt: &expiresAt, + }) + require.NoError(t, err) + + createdAt := time.Date(2020, time.October, 14, 11, 58, 53, 526000000, time.UTC) + want := &PersonalAccessToken{ + ID: 3, + Name: "mytoken", + Revoked: false, + CreatedAt: &createdAt, + Scopes: scopes, + UserID: 42, + Active: true, + ExpiresAt: &expiresAt, + Token: "glpat-aaaaaaaa-bbbbbbbbb", + } + require.Equal(t, want, user) +}