From 35eedbff63f048d97eb8d74867377de996258eef Mon Sep 17 00:00:00 2001 From: myoshida Date: Thu, 27 Jan 2022 10:50:05 -0800 Subject: [PATCH] adding group access token rename test name --- gitlab.go | 2 + group_access_tokens.go | 140 ++++++++++++++++++++++++ group_access_tokens_test.go | 125 +++++++++++++++++++++ project_access_tokens_test.go | 2 +- testdata/create_group_access_token.json | 17 +++ testdata/list_group_access_tokens.json | 34 ++++++ 6 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 group_access_tokens.go create mode 100644 group_access_tokens_test.go create mode 100644 testdata/create_group_access_token.json create mode 100644 testdata/list_group_access_tokens.json diff --git a/gitlab.go b/gitlab.go index 7c6277712..aaf434b35 100644 --- a/gitlab.go +++ b/gitlab.go @@ -127,6 +127,7 @@ type Client struct { GenericPackages *GenericPackagesService GeoNodes *GeoNodesService GitIgnoreTemplates *GitIgnoreTemplatesService + GroupAccessTokens *GroupAccessTokensService GroupBadges *GroupBadgesService GroupCluster *GroupClustersService GroupImportExport *GroupImportExportService @@ -321,6 +322,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) { c.GenericPackages = &GenericPackagesService{client: c} c.GeoNodes = &GeoNodesService{client: c} c.GitIgnoreTemplates = &GitIgnoreTemplatesService{client: c} + c.GroupAccessTokens = &GroupAccessTokensService{client: c} c.GroupBadges = &GroupBadgesService{client: c} c.GroupCluster = &GroupClustersService{client: c} c.GroupImportExport = &GroupImportExportService{client: c} diff --git a/group_access_tokens.go b/group_access_tokens.go new file mode 100644 index 000000000..41532bf08 --- /dev/null +++ b/group_access_tokens.go @@ -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) +} diff --git a/group_access_tokens_test.go b/group_access_tokens_test.go new file mode 100644 index 000000000..8b9be4fb8 --- /dev/null +++ b/group_access_tokens_test.go @@ -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) + } +} diff --git a/project_access_tokens_test.go b/project_access_tokens_test.go index f960b32f1..6630fef68 100644 --- a/project_access_tokens_test.go +++ b/project_access_tokens_test.go @@ -120,6 +120,6 @@ func TestDeleteProjectAccessToken(t *testing.T) { _, err := client.ProjectAccessTokens.DeleteProjectAccessToken("1", 1234) if err != nil { - t.Errorf("Pipelines.DeleteProjectAccessToken returned error: %v", err) + t.Errorf("ProjectAccessTokens.DeleteProjectAccessToken returned error: %v", err) } } diff --git a/testdata/create_group_access_token.json b/testdata/create_group_access_token.json new file mode 100644 index 000000000..d645538ac --- /dev/null +++ b/testdata/create_group_access_token.json @@ -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 +} diff --git a/testdata/list_group_access_tokens.json b/testdata/list_group_access_tokens.json new file mode 100644 index 000000000..99327ba37 --- /dev/null +++ b/testdata/list_group_access_tokens.json @@ -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 + } +]