Skip to content

Commit b2ae94a

Browse files
authored
Support code scanning default setup configuration (#2808)
Fixes: #2751 .
1 parent 6a57598 commit b2ae94a

File tree

4 files changed

+283
-0
lines changed

4 files changed

+283
-0
lines changed

github/code-scanning.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,76 @@ func (s *CodeScanningService) GetAnalysis(ctx context.Context, owner, repo strin
378378

379379
return analysis, resp, nil
380380
}
381+
382+
// DefaultSetupConfiguration represents a code scanning default setup configuration.
383+
type DefaultSetupConfiguration struct {
384+
State *string `json:"state,omitempty"`
385+
Languages []string `json:"languages,omitempty"`
386+
QuerySuite *string `json:"query_suite,omitempty"`
387+
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
388+
}
389+
390+
// GetDefaultSetupConfiguration gets a code scanning default setup configuration.
391+
//
392+
// You must use an access token with the repo scope to use this
393+
// endpoint with private repos or the public_repo scope for public repos. GitHub Apps must have the repo write
394+
// permission to use this endpoint.
395+
//
396+
// GitHub API docs: https://docs.github.com/en/rest/code-scanning#get-a-code-scanning-default-setup-configuration
397+
func (s *CodeScanningService) GetDefaultSetupConfiguration(ctx context.Context, owner, repo string) (*DefaultSetupConfiguration, *Response, error) {
398+
u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
399+
400+
req, err := s.client.NewRequest("GET", u, nil)
401+
if err != nil {
402+
return nil, nil, err
403+
}
404+
405+
cfg := new(DefaultSetupConfiguration)
406+
resp, err := s.client.Do(ctx, req, cfg)
407+
if err != nil {
408+
return nil, resp, err
409+
}
410+
411+
return cfg, resp, nil
412+
}
413+
414+
// UpdateDefaultSetupConfigurationOptions specifies parameters to the CodeScanningService.UpdateDefaultSetupConfiguration
415+
// method.
416+
type UpdateDefaultSetupConfigurationOptions struct {
417+
State string `json:"state"`
418+
QuerySuite *string `json:"query_suite,omitempty"`
419+
Languages []string `json:"languages,omitempty"`
420+
}
421+
422+
// UpdateDefaultSetupConfigurationResponse represents a response from updating a code scanning default setup configuration.
423+
type UpdateDefaultSetupConfigurationResponse struct {
424+
RunID *int64 `json:"run_id,omitempty"`
425+
RunURL *string `json:"run_url,omitempty"`
426+
}
427+
428+
// UpdateDefaultSetupConfiguration updates a code scanning default setup configuration.
429+
//
430+
// You must use an access token with the repo scope to use this
431+
// endpoint with private repos or the public_repo scope for public repos. GitHub Apps must have the repo write
432+
// permission to use this endpoint.
433+
//
434+
// This method might return an AcceptedError and a status code of 202. This is because this is the status that GitHub
435+
// returns to signify that it has now scheduled the update of the pull request branch in a background task.
436+
//
437+
// GitHub API docs: https://docs.github.com/en/rest/code-scanning#update-a-code-scanning-default-setup-configuration
438+
func (s *CodeScanningService) UpdateDefaultSetupConfiguration(ctx context.Context, owner, repo string, options *UpdateDefaultSetupConfigurationOptions) (*UpdateDefaultSetupConfigurationResponse, *Response, error) {
439+
u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
440+
441+
req, err := s.client.NewRequest("PATCH", u, options)
442+
if err != nil {
443+
return nil, nil, err
444+
}
445+
446+
a := new(UpdateDefaultSetupConfigurationResponse)
447+
resp, err := s.client.Do(ctx, req, a)
448+
if err != nil {
449+
return nil, resp, err
450+
}
451+
452+
return a, resp, nil
453+
}

github/code-scanning_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,105 @@ func TestCodeScanningService_GetAnalysis(t *testing.T) {
11771177
return resp, err
11781178
})
11791179
}
1180+
1181+
func TestCodeScanningService_GetDefaultSetupConfiguration(t *testing.T) {
1182+
client, mux, _, teardown := setup()
1183+
defer teardown()
1184+
1185+
mux.HandleFunc("/repos/o/r/code-scanning/default-setup", func(w http.ResponseWriter, r *http.Request) {
1186+
testMethod(t, r, "GET")
1187+
_, err := fmt.Fprint(w, `{
1188+
"state": "configured",
1189+
"languages": [
1190+
"javascript",
1191+
"javascript-typescript",
1192+
"typescript"
1193+
],
1194+
"query_suite": "default",
1195+
"updated_at": "2006-01-02T15:04:05Z"
1196+
}`)
1197+
if err != nil {
1198+
t.Fatal(err)
1199+
}
1200+
})
1201+
1202+
ctx := context.Background()
1203+
cfg, _, err := client.CodeScanning.GetDefaultSetupConfiguration(ctx, "o", "r")
1204+
if err != nil {
1205+
t.Errorf("CodeScanning.GetDefaultSetupConfiguration returned error: %v", err)
1206+
}
1207+
1208+
date := &Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}
1209+
want := &DefaultSetupConfiguration{
1210+
State: String("configured"),
1211+
Languages: []string{"javascript", "javascript-typescript", "typescript"},
1212+
QuerySuite: String("default"),
1213+
UpdatedAt: date,
1214+
}
1215+
if !cmp.Equal(cfg, want) {
1216+
t.Errorf("CodeScanning.GetDefaultSetupConfiguration returned %+v, want %+v", cfg, want)
1217+
}
1218+
1219+
const methodName = "GetDefaultSetupConfiguration"
1220+
testBadOptions(t, methodName, func() (err error) {
1221+
_, _, err = client.CodeScanning.GetDefaultSetupConfiguration(ctx, "\n", "\n")
1222+
return err
1223+
})
1224+
1225+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1226+
got, resp, err := client.CodeScanning.GetDefaultSetupConfiguration(ctx, "o", "r")
1227+
if got != nil {
1228+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1229+
}
1230+
return resp, err
1231+
})
1232+
}
1233+
1234+
func TestCodeScanningService_UpdateDefaultSetupConfiguration(t *testing.T) {
1235+
client, mux, _, teardown := setup()
1236+
defer teardown()
1237+
1238+
mux.HandleFunc("/repos/o/r/code-scanning/default-setup", func(w http.ResponseWriter, r *http.Request) {
1239+
testMethod(t, r, "PATCH")
1240+
_, err := fmt.Fprint(w, `{
1241+
"run_id": 5301214200,
1242+
"run_url": "https://api.github.com/repos/o/r/actions/runs/5301214200"
1243+
}`)
1244+
if err != nil {
1245+
t.Fatal(err)
1246+
}
1247+
})
1248+
1249+
ctx := context.Background()
1250+
options := &UpdateDefaultSetupConfigurationOptions{
1251+
State: "configured",
1252+
Languages: []string{"go"},
1253+
QuerySuite: String("default"),
1254+
}
1255+
got, _, err := client.CodeScanning.UpdateDefaultSetupConfiguration(ctx, "o", "r", options)
1256+
if err != nil {
1257+
t.Errorf("CodeScanning.UpdateDefaultSetupConfiguration returned error: %v", err)
1258+
}
1259+
1260+
want := &UpdateDefaultSetupConfigurationResponse{
1261+
RunID: Int64(5301214200),
1262+
RunURL: String("https://api.github.com/repos/o/r/actions/runs/5301214200"),
1263+
}
1264+
if !cmp.Equal(got, want) {
1265+
t.Errorf("CodeScanning.UpdateDefaultSetupConfiguration returned %+v, want %+v", got, want)
1266+
}
1267+
1268+
const methodName = "UpdateDefaultSetupConfiguration"
1269+
testBadOptions(t, methodName, func() (err error) {
1270+
_, _, err = client.CodeScanning.UpdateDefaultSetupConfiguration(ctx, "\n", "\n", nil)
1271+
return err
1272+
})
1273+
1274+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1275+
got, resp, err := client.CodeScanning.UpdateDefaultSetupConfiguration(ctx, "o", "r", nil)
1276+
if got != nil {
1277+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1278+
}
1279+
return resp, err
1280+
})
1281+
}

github/github-accessors.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)