From 5a4019ec44de32670dc32b0140983e22b4a34578 Mon Sep 17 00:00:00 2001 From: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Date: Thu, 3 Mar 2022 11:19:52 -0700 Subject: [PATCH] Add repo webhook redelivery method (#2305) Fixes: #2304. --- github/repos_hooks_deliveries.go | 19 +++++++++++++++ github/repos_hooks_deliveries_test.go | 35 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/github/repos_hooks_deliveries.go b/github/repos_hooks_deliveries.go index 1713269519..7420b59e7c 100644 --- a/github/repos_hooks_deliveries.go +++ b/github/repos_hooks_deliveries.go @@ -104,6 +104,25 @@ func (s *RepositoriesService) GetHookDelivery(ctx context.Context, owner, repo s return h, resp, nil } +// RedeliverHookDelivery redelivers a delivery for a webhook configured in a repository. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/webhooks#redeliver-a-delivery-for-a-repository-webhook +func (s *RepositoriesService) RedeliverHookDelivery(ctx context.Context, owner, repo string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v/attempts", owner, repo, hookID, deliveryID) + req, err := s.client.NewRequest("POST", u, nil) + if err != nil { + return nil, nil, err + } + + h := new(HookDelivery) + resp, err := s.client.Do(ctx, req, h) + if err != nil { + return nil, resp, err + } + + return h, resp, nil +} + // ParseRequestPayload parses the request payload. For recognized event types, // a value of the corresponding struct type will be returned. func (d *HookDelivery) ParseRequestPayload() (interface{}, error) { diff --git a/github/repos_hooks_deliveries_test.go b/github/repos_hooks_deliveries_test.go index bdcaabecfa..1a27685961 100644 --- a/github/repos_hooks_deliveries_test.go +++ b/github/repos_hooks_deliveries_test.go @@ -107,6 +107,41 @@ func TestRepositoriesService_GetHookDelivery_invalidOwner(t *testing.T) { testURLParseError(t, err) } +func TestRepositoriesService_RedeliverHookDelivery(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/hooks/1/deliveries/1/attempts", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + fmt.Fprint(w, `{"id":1}`) + }) + + ctx := context.Background() + hook, _, err := client.Repositories.RedeliverHookDelivery(ctx, "o", "r", 1, 1) + if err != nil { + t.Errorf("Repositories.RedeliverHookDelivery returned error: %v", err) + } + + want := &HookDelivery{ID: Int64(1)} + if !cmp.Equal(hook, want) { + t.Errorf("Repositories.RedeliverHookDelivery returned %+v, want %+v", hook, want) + } + + const methodName = "RedeliverHookDelivery" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Repositories.RedeliverHookDelivery(ctx, "\n", "\n", -1, -1) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.RedeliverHookDelivery(ctx, "o", "r", 1, 1) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + var hookDeliveryPayloadTypeToStruct = map[string]interface{}{ "check_run": &CheckRunEvent{}, "check_suite": &CheckSuiteEvent{},