This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1352 from M0roSan/group_access_token
adding group access token
- Loading branch information
Showing
6 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// | ||
// Copyright 2022, Masahiro Yoshida | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package gitlab | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// GroupAccessTokensService handles communication with the | ||
// groups access tokens related methods of the GitLab API. | ||
// | ||
// GitLab API docs: https://docs.gitlab.com/ee/api/group_access_tokens.html | ||
type GroupAccessTokensService struct { | ||
client *Client | ||
} | ||
|
||
// GroupAccessToken represents a GitLab Group Access Token. | ||
// | ||
// GitLab API docs: https://docs.gitlab.com/ee/api/group_access_tokens.html | ||
type GroupAccessToken struct { | ||
ID int `json:"id"` | ||
UserID int `json:"user_id"` | ||
Name string `json:"name"` | ||
Scopes []string `json:"scopes"` | ||
CreatedAt *time.Time `json:"created_at"` | ||
ExpiresAt *ISOTime `json:"expires_at"` | ||
Active bool `json:"active"` | ||
Revoked bool `json:"revoked"` | ||
Token string `json:"token"` | ||
AccessLevel AccessLevelValue `json:"access_level"` | ||
} | ||
|
||
func (v GroupAccessToken) String() string { | ||
return Stringify(v) | ||
} | ||
|
||
// ListGroupAccessTokensOptions represents the available options for | ||
// listing variables in a group. | ||
// | ||
// GitLab API docs: | ||
// https://docs.gitlab.com/ee/api/group_access_tokens.html#list-group-access-tokens | ||
type ListGroupAccessTokensOptions ListOptions | ||
|
||
// ListGroupAccessTokens gets a list of all Group Access Tokens in a | ||
// group. | ||
// | ||
// GitLab API docs: | ||
// https://docs.gitlab.com/ee/api/group_access_tokens.html#list-group-access-tokens | ||
func (s *GroupAccessTokensService) ListGroupAccessTokens(gid interface{}, opt *ListGroupAccessTokensOptions, options ...RequestOptionFunc) ([]*GroupAccessToken, *Response, error) { | ||
groups, err := parseID(gid) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
u := fmt.Sprintf("groups/%s/access_tokens", PathEscape(groups)) | ||
|
||
req, err := s.client.NewRequest(http.MethodGet, u, opt, options) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var gats []*GroupAccessToken | ||
resp, err := s.client.Do(req, &gats) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return gats, resp, err | ||
} | ||
|
||
// CreateGroupAccessTokenOptions represents the available CreateVariable() | ||
// options. | ||
// | ||
// GitLab API docs: | ||
// https://docs.gitlab.com/ee/api/group_access_tokens.html#create-a-group-access-token | ||
type CreateGroupAccessTokenOptions struct { | ||
Name *string `url:"name,omitempty" json:"name,omitempty"` | ||
Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"` | ||
AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"` | ||
ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"` | ||
} | ||
|
||
// CreateGroupAccessToken creates a new Group Access Token. | ||
// | ||
// GitLab API docs: | ||
// https://docs.gitlab.com/ee/api/group_access_tokens.html#create-a-group-access-token | ||
func (s *GroupAccessTokensService) CreateGroupAccessToken(gid interface{}, opt *CreateGroupAccessTokenOptions, options ...RequestOptionFunc) (*GroupAccessToken, *Response, error) { | ||
groups, err := parseID(gid) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
u := fmt.Sprintf("groups/%s/access_tokens", PathEscape(groups)) | ||
|
||
req, err := s.client.NewRequest(http.MethodPost, u, opt, options) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
pat := new(GroupAccessToken) | ||
resp, err := s.client.Do(req, pat) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return pat, resp, err | ||
} | ||
|
||
// DeleteGroupAccessToken deletes a Group Access Token. | ||
// | ||
// GitLab API docs: | ||
// https://docs.gitlab.com/ee/api/group_access_tokens.html#revoke-a-group-access-token | ||
func (s *GroupAccessTokensService) DeleteGroupAccessToken(gid interface{}, id int, options ...RequestOptionFunc) (*Response, error) { | ||
groups, err := parseID(gid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
u := fmt.Sprintf("groups/%s/access_tokens/%d", PathEscape(groups), id) | ||
|
||
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return s.client.Do(req, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// | ||
// Copyright 2022, Masahiro Yoshida | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package gitlab | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestListGroupAccessTokens(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v4/groups/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodGet) | ||
mustWriteHTTPResponse(t, w, "testdata/list_group_access_tokens.json") | ||
}) | ||
|
||
groupAccessTokens, _, err := client.GroupAccessTokens.ListGroupAccessTokens(1, &ListGroupAccessTokensOptions{Page: 1, PerPage: 20}) | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err) | ||
} | ||
|
||
time1, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.271Z") | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err) | ||
} | ||
time2, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.340Z") | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned error: %v", err) | ||
} | ||
|
||
want := []*GroupAccessToken{ | ||
{ | ||
ID: 1876, | ||
UserID: 2453, | ||
Name: "token 10", | ||
Scopes: []string{"api", "read_api", "read_repository", "write_repository"}, | ||
CreatedAt: &time1, | ||
Active: true, | ||
Revoked: false, | ||
AccessLevel: AccessLevelValue(40), | ||
}, | ||
{ | ||
ID: 1877, | ||
UserID: 2456, | ||
Name: "token 8", | ||
Scopes: []string{"api", "read_api", "read_repository", "write_repository"}, | ||
CreatedAt: &time2, | ||
Active: true, | ||
Revoked: false, | ||
AccessLevel: AccessLevelValue(30), | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(want, groupAccessTokens) { | ||
t.Errorf("GroupAccessTokens.ListGroupAccessTokens returned %+v, want %+v", groupAccessTokens, want) | ||
} | ||
} | ||
|
||
func TestCreateGroupAccessToken(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v4/groups/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodPost) | ||
mustWriteHTTPResponse(t, w, "testdata/create_group_access_token.json") | ||
}) | ||
|
||
groupAccessToken, _, err := client.GroupAccessTokens.CreateGroupAccessToken(1, nil) | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned error: %v", err) | ||
} | ||
|
||
time1, err := time.Parse(time.RFC3339, "2021-03-09T21:11:47.271Z") | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned error: %v", err) | ||
} | ||
want := &GroupAccessToken{ | ||
ID: 1876, | ||
UserID: 2453, | ||
Name: "token 10", | ||
Scopes: []string{"api", "read_api", "read_repository", "write_repository"}, | ||
ExpiresAt: nil, | ||
CreatedAt: &time1, | ||
Active: true, | ||
Revoked: false, | ||
Token: "2UsevZE1x1ZdFZW4MNzH", | ||
AccessLevel: AccessLevelValue(40), | ||
} | ||
|
||
if !reflect.DeepEqual(want, groupAccessToken) { | ||
t.Errorf("GroupAccessTokens.CreateGroupAccessToken returned %+v, want %+v", groupAccessToken, want) | ||
} | ||
} | ||
|
||
func TestDeleteGroupAccessToken(t *testing.T) { | ||
mux, server, client := setup(t) | ||
defer teardown(server) | ||
|
||
mux.HandleFunc("/api/v4/groups/1/access_tokens/1234", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodDelete) | ||
}) | ||
|
||
_, err := client.GroupAccessTokens.DeleteGroupAccessToken("1", 1234) | ||
if err != nil { | ||
t.Errorf("GroupAccessTokens.DeleteGroupAccessToken returned error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"id": 1876, | ||
"name": "token 10", | ||
"revoked": false, | ||
"created_at": "2021-03-09T21:11:47.271Z", | ||
"scopes": [ | ||
"api", | ||
"read_api", | ||
"read_repository", | ||
"write_repository" | ||
], | ||
"user_id": 2453, | ||
"active": true, | ||
"expires_at": null, | ||
"token": "2UsevZE1x1ZdFZW4MNzH", | ||
"access_level": 40 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"id": 1876, | ||
"name": "token 10", | ||
"revoked": false, | ||
"created_at": "2021-03-09T21:11:47.271Z", | ||
"scopes": [ | ||
"api", | ||
"read_api", | ||
"read_repository", | ||
"write_repository" | ||
], | ||
"user_id": 2453, | ||
"active": true, | ||
"expires_at": null, | ||
"access_level": 40 | ||
}, | ||
{ | ||
"id": 1877, | ||
"name": "token 8", | ||
"revoked": false, | ||
"created_at": "2021-03-09T21:11:47.340Z", | ||
"scopes": [ | ||
"api", | ||
"read_api", | ||
"read_repository", | ||
"write_repository" | ||
], | ||
"user_id": 2456, | ||
"active": true, | ||
"expires_at": null, | ||
"access_level": 30 | ||
} | ||
] |