From 36d42fb43e5ed1b8ab58f38d565c66e6e348a6bc Mon Sep 17 00:00:00 2001 From: Joe Mann Date: Tue, 10 Oct 2023 16:49:59 +0200 Subject: [PATCH 1/3] Implement UpdateRepoAlert. --- github/dependabot_alerts.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/github/dependabot_alerts.go b/github/dependabot_alerts.go index 177316b1dc8..2d767194d15 100644 --- a/github/dependabot_alerts.go +++ b/github/dependabot_alerts.go @@ -82,6 +82,13 @@ type ListAlertsOptions struct { ListCursorOptions } +// AlertUpdate specifies the optional parameters to the DependabotService.UpdateRepoAlert method. +type AlertUpdate struct { + State string `json:"state"` + DismissedReason *string `json:"dismissed_reason,omitempty"` + DismissedComment *string `json:"dismissed_comment,omitempty"` +} + func (s *DependabotService) listAlerts(ctx context.Context, url string, opts *ListAlertsOptions) ([]*DependabotAlert, *Response, error) { u, err := addOptions(url, opts) if err != nil { @@ -128,11 +135,27 @@ func (s *DependabotService) GetRepoAlert(ctx context.Context, owner, repo string return nil, nil, err } - alert := new(DependabotAlert) + var alert DependabotAlert + resp, err := s.client.Do(ctx, req, alert) + if err != nil { + return nil, resp, err + } + + return &alert, resp, nil +} + +func (s *DependabotService) UpdateRepoAlert(ctx context.Context, owner, repo string, number int, update *AlertUpdate) (*DependabotAlert, *Response, error) { + url := fmt.Sprintf("repos/%v/%v/dependabot/alerts/%v", owner, repo, number) + req, err := s.client.NewRequest("PATCH", url, update) + if err != nil { + return nil, nil, err + } + + var alert DependabotAlert resp, err := s.client.Do(ctx, req, alert) if err != nil { return nil, resp, err } - return alert, resp, nil + return &alert, resp, nil } From a012a3d4d91446fea1d167a5cfe56da3ed2a9ac9 Mon Sep 17 00:00:00 2001 From: Joe Mann Date: Sat, 14 Oct 2023 12:27:25 +0200 Subject: [PATCH 2/3] Fix UpdateRepoAlert. --- github/dependabot_alerts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/dependabot_alerts.go b/github/dependabot_alerts.go index 2d767194d15..69296dc4c4d 100644 --- a/github/dependabot_alerts.go +++ b/github/dependabot_alerts.go @@ -152,7 +152,7 @@ func (s *DependabotService) UpdateRepoAlert(ctx context.Context, owner, repo str } var alert DependabotAlert - resp, err := s.client.Do(ctx, req, alert) + resp, err := s.client.Do(ctx, req, &alert) if err != nil { return nil, resp, err } From bb3337b90c5efdec3d634cdf99b5e51ae28f5d76 Mon Sep 17 00:00:00 2001 From: Joe Mann Date: Sat, 14 Oct 2023 12:29:45 +0200 Subject: [PATCH 3/3] Add test for UpdateRepoAlert. --- github/dependabot_alerts_test.go | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/github/dependabot_alerts_test.go b/github/dependabot_alerts_test.go index a7c3b14788b..ec308bfa02e 100644 --- a/github/dependabot_alerts_test.go +++ b/github/dependabot_alerts_test.go @@ -92,6 +92,47 @@ func TestDependabotService_GetRepoAlert(t *testing.T) { }) } +func TestDependabotService_UpdateRepoAlert(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/dependabot/alerts/42", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + fmt.Fprint(w, `{"number":42,"state":"fixed"}`) + }) + + update := AlertUpdate{ + State: "fixed", + } + ctx := context.Background() + alert, _, err := client.Dependabot.UpdateRepoAlert(ctx, "o", "r", 42, &update) + if err != nil { + t.Errorf("Dependabot.GetRepoAlert returned error: %v", err) + } + + want := &DependabotAlert{ + Number: Int(42), + State: String("fixed"), + } + if !cmp.Equal(alert, want) { + t.Errorf("Dependabot.UpdateRepoAlert returned %+v, want %+v", alert, want) + } + + const methodName = "UpdateRepoAlert" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Dependabot.UpdateRepoAlert(ctx, "\n", "\n", 0, &update) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Dependabot.UpdateRepoAlert(ctx, "o", "r", 42, &update) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + func TestDependabotService_ListOrgAlerts(t *testing.T) { client, mux, _, teardown := setup() defer teardown()