...

Source file src/github.com/xanzy/go-gitlab/project_access_tokens.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Patrick Webster
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // ProjectAccessTokensService handles communication with the
    26  // project access tokens related methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/project_access_tokens.html
    29  type ProjectAccessTokensService struct {
    30  	client *Client
    31  }
    32  
    33  // ProjectAccessToken represents a GitLab project access token.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/project_access_tokens.html
    36  type ProjectAccessToken struct {
    37  	ID          int              `json:"id"`
    38  	UserID      int              `json:"user_id"`
    39  	Name        string           `json:"name"`
    40  	Scopes      []string         `json:"scopes"`
    41  	CreatedAt   *time.Time       `json:"created_at"`
    42  	LastUsedAt  *time.Time       `json:"last_used_at"`
    43  	ExpiresAt   *ISOTime         `json:"expires_at"`
    44  	Active      bool             `json:"active"`
    45  	Revoked     bool             `json:"revoked"`
    46  	Token       string           `json:"token"`
    47  	AccessLevel AccessLevelValue `json:"access_level"`
    48  }
    49  
    50  func (v ProjectAccessToken) String() string {
    51  	return Stringify(v)
    52  }
    53  
    54  // ListProjectAccessTokensOptions represents the available
    55  // ListProjectAccessTokens() options.
    56  //
    57  // GitLab API docs:
    58  // https://docs.gitlab.com/ee/api/project_access_tokens.html#list-project-access-tokens
    59  type ListProjectAccessTokensOptions ListOptions
    60  
    61  // ListProjectAccessTokens gets a list of all project access tokens in a
    62  // project.
    63  //
    64  // GitLab API docs:
    65  // https://docs.gitlab.com/ee/api/project_access_tokens.html#list-project-access-tokens
    66  func (s *ProjectAccessTokensService) ListProjectAccessTokens(pid interface{}, opt *ListProjectAccessTokensOptions, options ...RequestOptionFunc) ([]*ProjectAccessToken, *Response, error) {
    67  	project, err := parseID(pid)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  	u := fmt.Sprintf("projects/%s/access_tokens", PathEscape(project))
    72  
    73  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  
    78  	var pats []*ProjectAccessToken
    79  	resp, err := s.client.Do(req, &pats)
    80  	if err != nil {
    81  		return nil, resp, err
    82  	}
    83  
    84  	return pats, resp, nil
    85  }
    86  
    87  // GetProjectAccessToken gets a single project access tokens in a project.
    88  //
    89  // GitLab API docs:
    90  // https://docs.gitlab.com/ee/api/project_access_tokens.html#get-a-project-access-token
    91  func (s *ProjectAccessTokensService) GetProjectAccessToken(pid interface{}, id int, options ...RequestOptionFunc) (*ProjectAccessToken, *Response, error) {
    92  	project, err := parseID(pid)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  	u := fmt.Sprintf("projects/%s/access_tokens/%d", PathEscape(project), id)
    97  
    98  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	pat := new(ProjectAccessToken)
   104  	resp, err := s.client.Do(req, &pat)
   105  	if err != nil {
   106  		return nil, resp, err
   107  	}
   108  
   109  	return pat, resp, nil
   110  }
   111  
   112  // CreateProjectAccessTokenOptions represents the available CreateVariable()
   113  // options.
   114  //
   115  // GitLab API docs:
   116  // https://docs.gitlab.com/ee/api/project_access_tokens.html#create-a-project-access-token
   117  type CreateProjectAccessTokenOptions struct {
   118  	Name        *string           `url:"name,omitempty" json:"name,omitempty"`
   119  	Scopes      *[]string         `url:"scopes,omitempty" json:"scopes,omitempty"`
   120  	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   121  	ExpiresAt   *ISOTime          `url:"expires_at,omitempty" json:"expires_at,omitempty"`
   122  }
   123  
   124  // CreateProjectAccessToken creates a new project access token.
   125  //
   126  // GitLab API docs:
   127  // https://docs.gitlab.com/ee/api/project_access_tokens.html#create-a-project-access-token
   128  func (s *ProjectAccessTokensService) CreateProjectAccessToken(pid interface{}, opt *CreateProjectAccessTokenOptions, options ...RequestOptionFunc) (*ProjectAccessToken, *Response, error) {
   129  	project, err := parseID(pid)
   130  	if err != nil {
   131  		return nil, nil, err
   132  	}
   133  	u := fmt.Sprintf("projects/%s/access_tokens", PathEscape(project))
   134  
   135  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   136  	if err != nil {
   137  		return nil, nil, err
   138  	}
   139  
   140  	pat := new(ProjectAccessToken)
   141  	resp, err := s.client.Do(req, pat)
   142  	if err != nil {
   143  		return nil, resp, err
   144  	}
   145  
   146  	return pat, resp, nil
   147  }
   148  
   149  // RotateProjectAccessTokenOptions represents the available RotateProjectAccessToken()
   150  // options.
   151  //
   152  // GitLab API docs:
   153  // https://docs.gitlab.com/ee/api/project_access_tokens.html#rotate-a-project-access-token
   154  type RotateProjectAccessTokenOptions struct {
   155  	ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"`
   156  }
   157  
   158  // RotateProjectAccessToken revokes a project access token and returns a new
   159  // project access token that expires in one week per default.
   160  //
   161  // GitLab API docs:
   162  // https://docs.gitlab.com/ee/api/project_access_tokens.html#rotate-a-project-access-token
   163  func (s *ProjectAccessTokensService) RotateProjectAccessToken(pid interface{}, id int, opt *RotateProjectAccessTokenOptions, options ...RequestOptionFunc) (*ProjectAccessToken, *Response, error) {
   164  	projects, err := parseID(pid)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  	u := fmt.Sprintf("projects/%s/access_tokens/%d/rotate", PathEscape(projects), id)
   169  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   170  	if err != nil {
   171  		return nil, nil, err
   172  	}
   173  
   174  	pat := new(ProjectAccessToken)
   175  	resp, err := s.client.Do(req, pat)
   176  	if err != nil {
   177  		return nil, resp, err
   178  	}
   179  
   180  	return pat, resp, nil
   181  }
   182  
   183  // RevokeProjectAccessToken revokes a project access token.
   184  //
   185  // GitLab API docs:
   186  // https://docs.gitlab.com/ee/api/project_access_tokens.html#revoke-a-project-access-token
   187  func (s *ProjectAccessTokensService) RevokeProjectAccessToken(pid interface{}, id int, options ...RequestOptionFunc) (*Response, error) {
   188  	project, err := parseID(pid)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	u := fmt.Sprintf("projects/%s/access_tokens/%d", PathEscape(project), id)
   193  
   194  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	return s.client.Do(req, nil)
   200  }
   201  

View as plain text