Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions github/github-iterators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions github/github-iterators_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 92 additions & 5 deletions github/orgs_personal_access_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)

Expand Down Expand Up @@ -83,6 +84,9 @@ type ListFineGrainedPATOptions struct {
// This is a timestamp in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
LastUsedAfter string `url:"last_used_after,omitempty"`

// TokenID filters results by the given fine-grained personal access token IDs.
TokenID []int64 `url:"-"`

ListOptions
}

Expand Down Expand Up @@ -115,6 +119,75 @@ func (s *OrganizationsService) ListFineGrainedPersonalAccessTokens(ctx context.C
return pats, resp, nil
}

// FineGrainedPersonalAccessTokenRequest represents the details of a request to access organization resources via a fine-grained personal access token.
type FineGrainedPersonalAccessTokenRequest struct {
// Unique identifier of the request for access via fine-grained personal access token.
ID int64 `json:"id"`

// Reason is the reason for the request.
Reason string `json:"reason"`

// Owner is the GitHub user associated with the token.
Owner User `json:"owner"`

// RepositorySelection is the type of repository selection requested.
// Possible values are: "none", "all", "subset".
RepositorySelection string `json:"repository_selection"`

// URL to the list of repositories the fine-grained personal access token can access.
// Only follow when `repository_selection` is `subset`.
RepositoriesURL string `json:"repositories_url"`

// Permissions are the permissions requested, categorized by type.
Permissions PersonalAccessTokenPermissions `json:"permissions"`

// Date and time when the request was created.
CreatedAt *Timestamp `json:"created_at"`

// Whether the associated fine-grained personal access token has expired.
TokenExpired bool `json:"token_expired"`

// Date and time when the associated fine-grained personal access token expires.
TokenExpiresAt *Timestamp `json:"token_expires_at"`

// TokenID
TokenID int64 `json:"token_id"`

// TokenName
TokenName string `json:"token_name"`

// Date and time when the associated fine-grained personal access token was last used for authentication.
TokenLastUsedAt *Timestamp `json:"token_last_used_at"`
}

// ListFineGrainedPersonalAccessTokenRequests lists requests to access organization resources via fine-grained personal access tokens.
// Only GitHub Apps can call this API, using the `Personal access tokens` organization permissions (read).
//
// GitHub API docs: https://docs.github.com/rest/orgs/personal-access-tokens#list-requests-to-access-organization-resources-with-fine-grained-personal-access-tokens
//
//meta:operation GET /orgs/{org}/personal-access-token-requests
func (s *OrganizationsService) ListFineGrainedPersonalAccessTokenRequests(ctx context.Context, org string, opts *ListFineGrainedPATOptions) ([]*FineGrainedPersonalAccessTokenRequest, *Response, error) {
u := fmt.Sprintf("orgs/%v/personal-access-token-requests", org)
// The `owner` parameter is a special case that uses the `owner[]=...` format and needs a custom function to format it correctly.
u, err := addListFineGrainedPATOptions(u, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest("GET", u, opts)
if err != nil {
return nil, nil, err
}

var pats []*FineGrainedPersonalAccessTokenRequest
resp, err := s.client.Do(ctx, req, &pats)
if err != nil {
return nil, resp, err
}

return pats, resp, nil
}

// ReviewPersonalAccessTokenRequestOptions specifies the parameters to the ReviewPersonalAccessTokenRequest method.
type ReviewPersonalAccessTokenRequestOptions struct {
Action string `json:"action"`
Expand All @@ -139,16 +212,16 @@ func (s *OrganizationsService) ReviewPersonalAccessTokenRequest(ctx context.Cont
return s.client.Do(ctx, req, nil)
}

// addListFineGrainedPATOptions adds the owner parameter to the URL query string with the correct format if it is set.
// addListFineGrainedPATOptions adds the owner and token_id parameters to the URL query string with the correct format if they are set.
//
// GitHub API expects the owner parameter to be a list of strings in the `owner[]=...` format.
// For multiple owner values, the owner parameter is repeated in the query string.
// For multiple owner and token_id values, the owner and token_id parameters are repeated in the query string.
//
// Example:
// owner[]=user1&owner[]=user2
// This will filter the results to only include fine-grained personal access tokens owned by `user1` and `user2`.
// owner[]=user1&owner[]=user2&token_id[]=123&token_id[]=456
// This will filter the results to only include fine-grained personal access tokens owned by `user1` and `user2` and with token IDs `123` and `456`.
//
// This function ensures the owner parameter is formatted correctly in the URL query string.
// This function ensures the owner and token_id parameters are formatted correctly in the URL query string.
func addListFineGrainedPATOptions(s string, opts *ListFineGrainedPATOptions) (string, error) {
u, err := addOptions(s, opts)
if err != nil {
Expand All @@ -172,6 +245,20 @@ func addListFineGrainedPATOptions(s string, opts *ListFineGrainedPATOptions) (st
u += "?" + ownerQuery
}
}
if len(opts.TokenID) > 0 {
tokenIDVals := make([]string, len(opts.TokenID))
for i, tokenID := range opts.TokenID {
tokenIDVals[i] = fmt.Sprintf("token_id[]=%v", url.QueryEscape(strconv.FormatInt(tokenID, 10)))
}
tokenIDQuery := strings.Join(tokenIDVals, "&")

if strings.Contains(u, "?") {
u += "&" + tokenIDQuery
} else {
u += "?" + tokenIDQuery
}
return u, nil
}

return u, nil
}
Loading
Loading