Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions github/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
)

// CredentialsService handles credentials related methods of the GitHub API.
type CredentialsService service

// RevokeCredentialsRequest represents the request body for revoking credentials.
type RevokeCredentialsRequest struct {
// The list of credential strings (tokens) to revoke.
Credentials []string `json:"credentials"`
}

// Revoke revokes a list of credentials.
//
// GitHub API docs: https://docs.github.com/rest/credentials/revoke#revoke-a-list-of-credentials
//
//meta:operation POST /credentials/revoke
func (s *CredentialsService) Revoke(ctx context.Context, credentials []string) (*Response, error) {
u := "credentials/revoke"

reqBody := &RevokeCredentialsRequest{Credentials: credentials}

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

return s.client.Do(ctx, req, nil)
}
48 changes: 48 additions & 0 deletions github/credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2025 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"encoding/json"
"errors"
"net/http"
"testing"
)

func TestCredentialsService_Revoke(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

creds := []string{
"ghp_1234567890abcdef1234567890abcdef12345678",
"ghp_abcdef1234567890abcdef1234567890abcdef12",
}
expectedBodyBytes, _ := json.Marshal(map[string][]string{"credentials": creds})
expectedBody := string(expectedBodyBytes) + "\n"

mux.HandleFunc("/credentials/revoke", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testBody(t, r, expectedBody)
w.WriteHeader(http.StatusAccepted)
})

ctx := t.Context()
resp, err := client.Credentials.Revoke(ctx, creds)
if !errors.As(err, new(*AcceptedError)) {
t.Errorf("Credentials.Revoke returned error: %v (want AcceptedError)", err)
}
if resp == nil {
t.Fatal("Credentials.Revoke returned nil response")
}
if resp.StatusCode != http.StatusAccepted {
t.Errorf("Credentials.Revoke returned status %v, want %v", resp.StatusCode, http.StatusAccepted)
}

const methodName = "Revoke"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Credentials.Revoke(ctx, []string{"a"})
})
}
2 changes: 2 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Client struct {
Apps *AppsService
Authorizations *AuthorizationsService
Billing *BillingService
Credentials *CredentialsService
Checks *ChecksService
Classroom *ClassroomService
CodeScanning *CodeScanningService
Expand Down Expand Up @@ -439,6 +440,7 @@ func (c *Client) initialize() {
c.Apps = (*AppsService)(&c.common)
c.Authorizations = (*AuthorizationsService)(&c.common)
c.Billing = (*BillingService)(&c.common)
c.Credentials = (*CredentialsService)(&c.common)
c.Checks = (*ChecksService)(&c.common)
c.Classroom = (*ClassroomService)(&c.common)
c.CodeScanning = (*CodeScanningService)(&c.common)
Expand Down
Loading