diff --git a/github/dependabot_alerts.go b/github/dependabot_alerts.go index 177316b1dc8..69296dc4c4d 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 + 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 } 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()