From a99dd03aadfa51ae2db519e35ee3d405fc40f43e Mon Sep 17 00:00:00 2001 From: DeviousLab Date: Tue, 24 Aug 2021 00:21:19 +0400 Subject: [PATCH 1/4] Added function for redelivering webhooks #2060 Signed-off-by: DeviousLab --- github/orgs_hooks_deliveries.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/github/orgs_hooks_deliveries.go b/github/orgs_hooks_deliveries.go index 6ab2d7aa244..6d91315060d 100644 --- a/github/orgs_hooks_deliveries.go +++ b/github/orgs_hooks_deliveries.go @@ -52,3 +52,22 @@ func (s *OrganizationsService) GetHookDelivery(ctx context.Context, owner string return h, resp, nil } + +// RedeliverHookDelivery redelivers a delivery for a webhook configured in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/orgs#redeliver-a-delivery-for-an-organization-webhook +func (s *OrganizationsService) RedeliverHookDelivery(ctx context.Context, owner string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { + u := fmt.Sprintf("orgs/%v/hooks/%v/deliveries/%v/attempts", owner, hookID, deliveryID) + req, err := s.client.NewRequest("GET", 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 +} \ No newline at end of file From 54fc1fb29492410450f052f69bd1c3d451bd9bd9 Mon Sep 17 00:00:00 2001 From: DeviousLab Date: Wed, 25 Aug 2021 06:55:48 +0400 Subject: [PATCH 2/4] Switched request type from GET to POST As suggested by gmlewis Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/orgs_hooks_deliveries.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_hooks_deliveries.go b/github/orgs_hooks_deliveries.go index 6d91315060d..521221803c6 100644 --- a/github/orgs_hooks_deliveries.go +++ b/github/orgs_hooks_deliveries.go @@ -58,7 +58,7 @@ func (s *OrganizationsService) GetHookDelivery(ctx context.Context, owner string // GitHub API docs: https://docs.github.com/en/rest/reference/orgs#redeliver-a-delivery-for-an-organization-webhook func (s *OrganizationsService) RedeliverHookDelivery(ctx context.Context, owner string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { u := fmt.Sprintf("orgs/%v/hooks/%v/deliveries/%v/attempts", owner, hookID, deliveryID) - req, err := s.client.NewRequest("GET", u, nil) + req, err := s.client.NewRequest("POST", u, nil) if err != nil { return nil, nil, err } From c46d29b25c540d0b50aeac095de92d35fabe38ba Mon Sep 17 00:00:00 2001 From: DeviousLab Date: Wed, 25 Aug 2021 07:10:28 +0400 Subject: [PATCH 3/4] Applied gofmt linter Signed-off-by: DeviousLab --- github/orgs_hooks_deliveries.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_hooks_deliveries.go b/github/orgs_hooks_deliveries.go index 6d91315060d..64c63406111 100644 --- a/github/orgs_hooks_deliveries.go +++ b/github/orgs_hooks_deliveries.go @@ -70,4 +70,4 @@ func (s *OrganizationsService) RedeliverHookDelivery(ctx context.Context, owner } return h, resp, nil -} \ No newline at end of file +} From 3e4a880ad89445d9739ec0ca430db906e65874b0 Mon Sep 17 00:00:00 2001 From: DeviousLab Date: Wed, 25 Aug 2021 16:46:43 +0400 Subject: [PATCH 4/4] First draft of unit test of redeliver webhook Signed-off-by: DeviousLab --- github/orgs_hooks_deliveries_test.go | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/github/orgs_hooks_deliveries_test.go b/github/orgs_hooks_deliveries_test.go index c1efc527aa8..cd0bd109a91 100644 --- a/github/orgs_hooks_deliveries_test.go +++ b/github/orgs_hooks_deliveries_test.go @@ -104,3 +104,47 @@ func TestOrganizationsService_GetHookDelivery_invalidOwner(t *testing.T) { _, _, err := client.Organizations.GetHookDelivery(ctx, "%", 1, 1) testURLParseError(t, err) } + +func TestOrganizationsService_RedeliverHookDelivery(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/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.Organizations.RedeliverHookDelivery(ctx, "o", 1, 1) + if err != nil { + t.Errorf("Organizations.RedeliverHookDelivery returned error: %v", err) + } + + want := &HookDelivery{ID: Int64(1)} + if !cmp.Equal(hook, want) { + t.Errorf("Organizations.RedeliverHookDelivery returned %+v, want %+v", hook, want) + } + + const methodName = "Rede;overHookDelivery" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.RedeliverHookDelivery(ctx, "\n", -1, -1) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.RedeliverHookDelivery(ctx, "o", 1, 1) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestOrganizationsService_RedeliverHookDelivery_invalidOwner(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Organizations.RedeliverHookDelivery(ctx, "%", 1, 1) + testURLParseError(t, err) +}