Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repo webhook redelivery method #2305

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions github/repos_hooks_deliveries.go
Expand Up @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions github/repos_hooks_deliveries_test.go
Expand Up @@ -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{},
Expand Down