Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1814 from timofurrer/feature/user-pat
Browse files Browse the repository at this point in the history
Support CreatePersonalAccessTokenForCurrentUser endpoint
  • Loading branch information
svanharmelen authored Oct 5, 2023
2 parents 285184d + bce1c13 commit e104217
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions testdata/post_user_personal_access_tokens.json
Original file line number Diff line number Diff line change
@@ -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"
}
32 changes: 32 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit e104217

Please sign in to comment.