From 2c4880f96337a7d577120ac893a16ff270292286 Mon Sep 17 00:00:00 2001 From: Guillaume Winter Date: Mon, 23 Sep 2024 13:12:33 +0200 Subject: [PATCH] add CancelInvite method to cancel an org invitation by ID --- github/orgs_members.go | 14 ++++++++++++++ github/orgs_members_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/github/orgs_members.go b/github/orgs_members.go index 5bc23657fcd..d7967949641 100644 --- a/github/orgs_members.go +++ b/github/orgs_members.go @@ -151,6 +151,20 @@ func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user strin return s.client.Do(ctx, req, nil) } +// CancelInvite cancels an organization invitation. +// +// GitHub API docs: https://docs.github.com/rest/orgs/members#cancel-an-organization-invitation +// +//meta:operation DELETE /orgs/{org}/invitations/{invitation_id} +func (s *OrganizationsService) CancelInvite(ctx context.Context, org string, invitationID int64) (*Response, error) { + u := fmt.Sprintf("orgs/%v/invitations/%v", org, invitationID) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + return s.client.Do(ctx, req, nil) +} + // PublicizeMembership publicizes a user's membership in an organization. (A // user cannot publicize the membership for another user.) // diff --git a/github/orgs_members_test.go b/github/orgs_members_test.go index 9526442cda4..3717802a584 100644 --- a/github/orgs_members_test.go +++ b/github/orgs_members_test.go @@ -284,6 +284,32 @@ func TestOrganizationsService_RemoveMember(t *testing.T) { }) } +func TestOrganizationsService_CancelInvite(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/invitations/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + _, err := client.Organizations.CancelInvite(ctx, "o", 1) + if err != nil { + t.Errorf("Organizations.CancelInvite returned error: %v", err) + } + + const methodName = "CancelInvite" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Organizations.CancelInvite(ctx, "\n", 1) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Organizations.CancelInvite(ctx, "o", 1) + }) +} + func TestOrganizationsService_RemoveMember_invalidOrg(t *testing.T) { client, _, _, teardown := setup() defer teardown()