...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     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  // InvitesService handles communication with the invitation related
    26  // methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/invitations.html
    29  type InvitesService struct {
    30  	client *Client
    31  }
    32  
    33  // PendingInvite represents a pending invite.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/invitations.html
    36  type PendingInvite struct {
    37  	ID            int              `json:"id"`
    38  	InviteEmail   string           `json:"invite_email"`
    39  	CreatedAt     *time.Time       `json:"created_at"`
    40  	AccessLevel   AccessLevelValue `json:"access_level"`
    41  	ExpiresAt     *time.Time       `json:"expires_at"`
    42  	UserName      string           `json:"user_name"`
    43  	CreatedByName string           `json:"created_by_name"`
    44  }
    45  
    46  // ListPendingInvitationsOptions represents the available
    47  // ListPendingInvitations() options.
    48  //
    49  // GitLab API docs:
    50  // https://docs.gitlab.com/ee/api/invitations.html#list-all-invitations-pending-for-a-group-or-project
    51  type ListPendingInvitationsOptions struct {
    52  	ListOptions
    53  	Query *string `url:"query,omitempty" json:"query,omitempty"`
    54  }
    55  
    56  // ListPendingGroupInvitations gets a list of invited group members.
    57  //
    58  // GitLab API docs:
    59  // https://docs.gitlab.com/ee/api/invitations.html#list-all-invitations-pending-for-a-group-or-project
    60  func (s *InvitesService) ListPendingGroupInvitations(gid interface{}, opt *ListPendingInvitationsOptions, options ...RequestOptionFunc) ([]*PendingInvite, *Response, error) {
    61  	group, err := parseID(gid)
    62  	if err != nil {
    63  		return nil, nil, err
    64  	}
    65  	u := fmt.Sprintf("groups/%s/invitations", PathEscape(group))
    66  
    67  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	var pis []*PendingInvite
    73  	resp, err := s.client.Do(req, &pis)
    74  	if err != nil {
    75  		return nil, resp, err
    76  	}
    77  
    78  	return pis, resp, nil
    79  }
    80  
    81  // ListPendingProjectInvitations gets a list of invited project members.
    82  //
    83  // GitLab API docs:
    84  // https://docs.gitlab.com/ee/api/invitations.html#list-all-invitations-pending-for-a-group-or-project
    85  func (s *InvitesService) ListPendingProjectInvitations(pid interface{}, opt *ListPendingInvitationsOptions, options ...RequestOptionFunc) ([]*PendingInvite, *Response, error) {
    86  	project, err := parseID(pid)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  	u := fmt.Sprintf("projects/%s/invitations", PathEscape(project))
    91  
    92  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  
    97  	var pis []*PendingInvite
    98  	resp, err := s.client.Do(req, &pis)
    99  	if err != nil {
   100  		return nil, resp, err
   101  	}
   102  
   103  	return pis, resp, nil
   104  }
   105  
   106  // InvitesOptions represents the available GroupInvites() and ProjectInvites()
   107  // options.
   108  //
   109  // GitLab API docs:
   110  // https://docs.gitlab.com/ee/api/invitations.html#add-a-member-to-a-group-or-project
   111  type InvitesOptions struct {
   112  	ID          interface{}       `url:"id,omitempty" json:"id,omitempty"`
   113  	Email       *string           `url:"email,omitempty" json:"email,omitempty"`
   114  	UserID      interface{}       `url:"user_id,omitempty" json:"user_id,omitempty"`
   115  	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
   116  	ExpiresAt   *ISOTime          `url:"expires_at,omitempty" json:"expires_at,omitempty"`
   117  }
   118  
   119  // InvitesResult represents an invitations result.
   120  //
   121  // GitLab API docs:
   122  // https://docs.gitlab.com/ee/api/invitations.html#add-a-member-to-a-group-or-project
   123  type InvitesResult struct {
   124  	Status  string            `json:"status"`
   125  	Message map[string]string `json:"message,omitempty"`
   126  }
   127  
   128  // GroupInvites invites new users by email to join a group.
   129  //
   130  // GitLab API docs:
   131  // https://docs.gitlab.com/ee/api/invitations.html#add-a-member-to-a-group-or-project
   132  func (s *InvitesService) GroupInvites(gid interface{}, opt *InvitesOptions, options ...RequestOptionFunc) (*InvitesResult, *Response, error) {
   133  	group, err := parseID(gid)
   134  	if err != nil {
   135  		return nil, nil, err
   136  	}
   137  	u := fmt.Sprintf("groups/%s/invitations", PathEscape(group))
   138  
   139  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   140  	if err != nil {
   141  		return nil, nil, err
   142  	}
   143  
   144  	ir := new(InvitesResult)
   145  	resp, err := s.client.Do(req, ir)
   146  	if err != nil {
   147  		return nil, resp, err
   148  	}
   149  
   150  	return ir, resp, nil
   151  }
   152  
   153  // ProjectInvites invites new users by email to join a project.
   154  //
   155  // GitLab API docs:
   156  // https://docs.gitlab.com/ee/api/invitations.html#add-a-member-to-a-group-or-project
   157  func (s *InvitesService) ProjectInvites(pid interface{}, opt *InvitesOptions, options ...RequestOptionFunc) (*InvitesResult, *Response, error) {
   158  	project, err := parseID(pid)
   159  	if err != nil {
   160  		return nil, nil, err
   161  	}
   162  	u := fmt.Sprintf("projects/%s/invitations", PathEscape(project))
   163  
   164  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   165  	if err != nil {
   166  		return nil, nil, err
   167  	}
   168  
   169  	ir := new(InvitesResult)
   170  	resp, err := s.client.Do(req, ir)
   171  	if err != nil {
   172  		return nil, resp, err
   173  	}
   174  
   175  	return ir, resp, nil
   176  }
   177  

View as plain text