Skip to content

Commit 437b7bf

Browse files
author
cointem
committed
credentials: implement POST /credentials/revoke and tests
1 parent 3afe183 commit 437b7bf

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

github/credentials.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
)
11+
12+
// CredentialsService handles credentials related methods of the GitHub API.
13+
type CredentialsService service
14+
15+
// RevokeCredentialsRequest represents the request body for revoking credentials.
16+
type RevokeCredentialsRequest struct {
17+
// The list of credential strings (tokens) to revoke.
18+
Credentials []string `json:"credentials"`
19+
}
20+
21+
// Revoke revokes a list of credentials.
22+
//
23+
// GitHub API docs: https://docs.github.com/rest/credentials/revoke#revoke-a-list-of-credentials
24+
//
25+
//meta:operation POST /credentials/revoke
26+
func (s *CredentialsService) Revoke(ctx context.Context, credentials []string) (*Response, error) {
27+
u := "credentials/revoke"
28+
29+
reqBody := &RevokeCredentialsRequest{Credentials: credentials}
30+
31+
req, err := s.client.NewRequest("POST", u, reqBody)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return s.client.Do(ctx, req, nil)
37+
}

github/credentials_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"encoding/json"
10+
"errors"
11+
"net/http"
12+
"testing"
13+
)
14+
15+
func TestCredentialsService_Revoke(t *testing.T) {
16+
t.Parallel()
17+
client, mux, _ := setup(t)
18+
19+
creds := []string{
20+
"ghp_1234567890abcdef1234567890abcdef12345678",
21+
"ghp_abcdef1234567890abcdef1234567890abcdef12",
22+
}
23+
expectedBodyBytes, _ := json.Marshal(map[string][]string{"credentials": creds})
24+
expectedBody := string(expectedBodyBytes) + "\n"
25+
26+
mux.HandleFunc("/credentials/revoke", func(w http.ResponseWriter, r *http.Request) {
27+
testMethod(t, r, "POST")
28+
testBody(t, r, expectedBody)
29+
w.WriteHeader(http.StatusAccepted)
30+
})
31+
32+
ctx := t.Context()
33+
resp, err := client.Credentials.Revoke(ctx, creds)
34+
if !errors.As(err, new(*AcceptedError)) {
35+
t.Errorf("Credentials.Revoke returned error: %v (want AcceptedError)", err)
36+
}
37+
if resp == nil {
38+
t.Fatalf("Credentials.Revoke returned nil response")
39+
}
40+
if resp.StatusCode != http.StatusAccepted {
41+
t.Errorf("Credentials.Revoke returned status %d, want %d", resp.StatusCode, http.StatusAccepted)
42+
}
43+
44+
const methodName = "Revoke"
45+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
46+
return client.Credentials.Revoke(ctx, []string{"a"})
47+
})
48+
}

github/github.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ type Client struct {
197197
Apps *AppsService
198198
Authorizations *AuthorizationsService
199199
Billing *BillingService
200+
Credentials *CredentialsService
200201
Checks *ChecksService
201202
Classroom *ClassroomService
202203
CodeScanning *CodeScanningService
@@ -439,6 +440,7 @@ func (c *Client) initialize() {
439440
c.Apps = (*AppsService)(&c.common)
440441
c.Authorizations = (*AuthorizationsService)(&c.common)
441442
c.Billing = (*BillingService)(&c.common)
443+
c.Credentials = (*CredentialsService)(&c.common)
442444
c.Checks = (*ChecksService)(&c.common)
443445
c.Classroom = (*ClassroomService)(&c.common)
444446
c.CodeScanning = (*CodeScanningService)(&c.common)

0 commit comments

Comments
 (0)