Skip to content

Commit 0431b55

Browse files
authored
feat: Add ListFineGrainedPersonalAccessTokenRequests for org (#4022)
1 parent 92a3830 commit 0431b55

File tree

6 files changed

+471
-5
lines changed

6 files changed

+471
-5
lines changed

github/github-accessors.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-iterators.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-iterators_test.go

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/orgs_personal_access_tokens.go

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"net/url"
13+
"strconv"
1314
"strings"
1415
)
1516

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

87+
// TokenID filters results by the given fine-grained personal access token IDs.
88+
TokenID []int64 `url:"-"`
89+
8690
ListOptions
8791
}
8892

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

122+
// FineGrainedPersonalAccessTokenRequest represents the details of a request to access organization resources via a fine-grained personal access token.
123+
type FineGrainedPersonalAccessTokenRequest struct {
124+
// Unique identifier of the request for access via fine-grained personal access token.
125+
ID int64 `json:"id"`
126+
127+
// Reason is the reason for the request.
128+
Reason string `json:"reason"`
129+
130+
// Owner is the GitHub user associated with the token.
131+
Owner User `json:"owner"`
132+
133+
// RepositorySelection is the type of repository selection requested.
134+
// Possible values are: "none", "all", "subset".
135+
RepositorySelection string `json:"repository_selection"`
136+
137+
// URL to the list of repositories the fine-grained personal access token can access.
138+
// Only follow when `repository_selection` is `subset`.
139+
RepositoriesURL string `json:"repositories_url"`
140+
141+
// Permissions are the permissions requested, categorized by type.
142+
Permissions PersonalAccessTokenPermissions `json:"permissions"`
143+
144+
// Date and time when the request was created.
145+
CreatedAt *Timestamp `json:"created_at"`
146+
147+
// Whether the associated fine-grained personal access token has expired.
148+
TokenExpired bool `json:"token_expired"`
149+
150+
// Date and time when the associated fine-grained personal access token expires.
151+
TokenExpiresAt *Timestamp `json:"token_expires_at"`
152+
153+
// TokenID
154+
TokenID int64 `json:"token_id"`
155+
156+
// TokenName
157+
TokenName string `json:"token_name"`
158+
159+
// Date and time when the associated fine-grained personal access token was last used for authentication.
160+
TokenLastUsedAt *Timestamp `json:"token_last_used_at"`
161+
}
162+
163+
// ListFineGrainedPersonalAccessTokenRequests lists requests to access organization resources via fine-grained personal access tokens.
164+
// Only GitHub Apps can call this API, using the `Personal access tokens` organization permissions (read).
165+
//
166+
// GitHub API docs: https://docs.github.com/rest/orgs/personal-access-tokens#list-requests-to-access-organization-resources-with-fine-grained-personal-access-tokens
167+
//
168+
//meta:operation GET /orgs/{org}/personal-access-token-requests
169+
func (s *OrganizationsService) ListFineGrainedPersonalAccessTokenRequests(ctx context.Context, org string, opts *ListFineGrainedPATOptions) ([]*FineGrainedPersonalAccessTokenRequest, *Response, error) {
170+
u := fmt.Sprintf("orgs/%v/personal-access-token-requests", org)
171+
// The `owner` parameter is a special case that uses the `owner[]=...` format and needs a custom function to format it correctly.
172+
u, err := addListFineGrainedPATOptions(u, opts)
173+
if err != nil {
174+
return nil, nil, err
175+
}
176+
177+
req, err := s.client.NewRequest("GET", u, opts)
178+
if err != nil {
179+
return nil, nil, err
180+
}
181+
182+
var pats []*FineGrainedPersonalAccessTokenRequest
183+
resp, err := s.client.Do(ctx, req, &pats)
184+
if err != nil {
185+
return nil, resp, err
186+
}
187+
188+
return pats, resp, nil
189+
}
190+
118191
// ReviewPersonalAccessTokenRequestOptions specifies the parameters to the ReviewPersonalAccessTokenRequest method.
119192
type ReviewPersonalAccessTokenRequestOptions struct {
120193
Action string `json:"action"`
@@ -139,16 +212,16 @@ func (s *OrganizationsService) ReviewPersonalAccessTokenRequest(ctx context.Cont
139212
return s.client.Do(ctx, req, nil)
140213
}
141214

142-
// addListFineGrainedPATOptions adds the owner parameter to the URL query string with the correct format if it is set.
215+
// addListFineGrainedPATOptions adds the owner and token_id parameters to the URL query string with the correct format if they are set.
143216
//
144217
// GitHub API expects the owner parameter to be a list of strings in the `owner[]=...` format.
145-
// For multiple owner values, the owner parameter is repeated in the query string.
218+
// For multiple owner and token_id values, the owner and token_id parameters are repeated in the query string.
146219
//
147220
// Example:
148-
// owner[]=user1&owner[]=user2
149-
// This will filter the results to only include fine-grained personal access tokens owned by `user1` and `user2`.
221+
// owner[]=user1&owner[]=user2&token_id[]=123&token_id[]=456
222+
// 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`.
150223
//
151-
// This function ensures the owner parameter is formatted correctly in the URL query string.
224+
// This function ensures the owner and token_id parameters are formatted correctly in the URL query string.
152225
func addListFineGrainedPATOptions(s string, opts *ListFineGrainedPATOptions) (string, error) {
153226
u, err := addOptions(s, opts)
154227
if err != nil {
@@ -172,6 +245,20 @@ func addListFineGrainedPATOptions(s string, opts *ListFineGrainedPATOptions) (st
172245
u += "?" + ownerQuery
173246
}
174247
}
248+
if len(opts.TokenID) > 0 {
249+
tokenIDVals := make([]string, len(opts.TokenID))
250+
for i, tokenID := range opts.TokenID {
251+
tokenIDVals[i] = fmt.Sprintf("token_id[]=%v", url.QueryEscape(strconv.FormatInt(tokenID, 10)))
252+
}
253+
tokenIDQuery := strings.Join(tokenIDVals, "&")
254+
255+
if strings.Contains(u, "?") {
256+
u += "&" + tokenIDQuery
257+
} else {
258+
u += "?" + tokenIDQuery
259+
}
260+
return u, nil
261+
}
175262

176263
return u, nil
177264
}

0 commit comments

Comments
 (0)