Skip to content

Commit 16a7393

Browse files
committed
feat: add ListCampaignChecks to support check removal from campaigns
Adds a lightweight query for fetching a campaign's checks (id + name only) to enable matching and deleting campaign checks when rubric check IDs are removed from the Terraform resource's check_ids list. Made-with: Cursor
1 parent e8c2c9f commit 16a7393

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

campaign.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,45 @@ func (client *Client) UnscheduleCampaign(id ID) (*Campaign, error) {
9393
return &m.Payload.Campaign, HandleErrors(err, m.Payload.Errors)
9494
}
9595

96+
// CampaignCheckNode is a lightweight representation of a check belonging to a campaign,
97+
// used when listing campaign checks without needing the full Check interface fragments.
98+
type CampaignCheckNode struct {
99+
Id ID `graphql:"id"`
100+
Name string `graphql:"name"`
101+
}
102+
103+
type campaignCheckConnection struct {
104+
Nodes []CampaignCheckNode `graphql:"nodes"`
105+
PageInfo PageInfo `graphql:"pageInfo"`
106+
}
107+
108+
func (client *Client) ListCampaignChecks(campaignId ID) ([]CampaignCheckNode, error) {
109+
var q struct {
110+
Account struct {
111+
Campaign struct {
112+
Checks campaignCheckConnection `graphql:"checks(first: $first, after: $after)"`
113+
} `graphql:"campaign(id: $id)"`
114+
}
115+
}
116+
117+
pages := client.InitialPageVariablesPointer()
118+
(*pages)["id"] = campaignId
119+
120+
if err := client.Query(&q, *pages, WithName("CampaignChecksList")); err != nil {
121+
return nil, err
122+
}
123+
124+
allChecks := q.Account.Campaign.Checks.Nodes
125+
for q.Account.Campaign.Checks.PageInfo.HasNextPage {
126+
(*pages)["after"] = q.Account.Campaign.Checks.PageInfo.End
127+
if err := client.Query(&q, *pages, WithName("CampaignChecksList")); err != nil {
128+
return nil, err
129+
}
130+
allChecks = append(allChecks, q.Account.Campaign.Checks.Nodes...)
131+
}
132+
return allChecks, nil
133+
}
134+
96135
func (client *Client) CopyChecksToCampaign(input ChecksCopyToCampaignInput) (*Campaign, error) {
97136
var m struct {
98137
Payload ChecksCopyToCampaignPayload `graphql:"checksCopyToCampaign(input: $input)"`

campaign_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ func TestCopyChecksToCampaign(t *testing.T) {
163163
autopilot.Equals(t, 2, campaign.CheckStats.Total)
164164
}
165165

166+
func TestListCampaignChecks(t *testing.T) {
167+
// Arrange
168+
testRequest := autopilot.NewTestRequest(
169+
`{{ template "campaign_list_checks_request" }}`,
170+
`{{ template "campaign_list_checks_request_vars" }}`,
171+
`{{ template "campaign_list_checks_response" }}`,
172+
)
173+
client := BestTestClient(t, "campaign/list_checks", testRequest)
174+
175+
// Act
176+
checks, err := client.ListCampaignChecks(id1)
177+
178+
// Assert
179+
autopilot.Ok(t, err)
180+
autopilot.Equals(t, 2, len(checks))
181+
autopilot.Equals(t, id2, checks[0].Id)
182+
autopilot.Equals(t, "Secret Rotation", checks[0].Name)
183+
autopilot.Equals(t, id3, checks[1].Id)
184+
autopilot.Equals(t, "Dependency Scanning", checks[1].Name)
185+
}
186+
166187
func TestListCampaigns(t *testing.T) {
167188
// Arrange
168189
testRequestOne := autopilot.NewTestRequest(

testdata/templates/campaigns.tpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ mutation CampaignUnschedule($input:DeleteInput!){campaignUnschedule(input: $inpu
225225
},"errors":[]}}
226226
}{{ end }}
227227

228+
{{- define "campaign_list_checks_request" }}
229+
query CampaignChecksList($after:String!$first:Int!$id:ID!){account{campaign(id: $id){checks(first: $first, after: $after){nodes{id,name},pageInfo{hasNextPage,hasPreviousPage,startCursor,endCursor}}}}}
230+
{{ end }}
231+
232+
{{- define "campaign_list_checks_request_vars" }}
233+
{"after":"","first":500,"id":"{{ template "id1_string" }}"}
234+
{{ end }}
235+
236+
{{- define "campaign_list_checks_response" }}{
237+
"data":{"account":{"campaign":{"checks":{"nodes":[
238+
{"id":"{{ template "id2_string" }}","name":"Secret Rotation"},
239+
{"id":"{{ template "id3_string" }}","name":"Dependency Scanning"}
240+
],"pageInfo":{"hasNextPage":false,"hasPreviousPage":false,"startCursor":null,"endCursor":null}}}}}
241+
}{{ end }}
242+
228243
{{- define "campaign_copy_checks_request" }}
229244
mutation ChecksCopyToCampaign($input:ChecksCopyToCampaignInput!){checksCopyToCampaign(input: $input){campaign{checkStats{total,totalSuccessful},endedDate,filter{id,name},htmlUrl,id,name,owner{alias,id},projectBrief,rawProjectBrief,reminder{channels,daysOfWeek,defaultSlackChannel,frequency,frequencyUnit,message,nextOccurrence,timeOfDay,timezone},serviceStats{total,totalSuccessful},startDate,status,targetDate},errors{message,path}}}
230245
{{ end }}

0 commit comments

Comments
 (0)