...

Source file src/github.com/google/go-github/v55/github/actions_runner_groups_test.go

Documentation: github.com/google/go-github/v55/github

     1  // Copyright 2021 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestActionsService_ListOrganizationRunnerGroups(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/orgs/o/actions/runner-groups", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    24  		fmt.Fprint(w, `{"total_count":3,"runner_groups":[{"id":1,"name":"Default","visibility":"all","default":true,"runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/1/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":true,"selected_workflows":["a","b"]},{"id":2,"name":"octo-runner-group","visibility":"selected","default":false,"selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories","runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners","inherited":true,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]},{"id":3,"name":"expensive-hardware","visibility":"private","default":false,"runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/3/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]}]}`)
    25  	})
    26  
    27  	opts := &ListOrgRunnerGroupOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
    28  	ctx := context.Background()
    29  	groups, _, err := client.Actions.ListOrganizationRunnerGroups(ctx, "o", opts)
    30  	if err != nil {
    31  		t.Errorf("Actions.ListOrganizationRunnerGroups returned error: %v", err)
    32  	}
    33  
    34  	want := &RunnerGroups{
    35  		TotalCount: 3,
    36  		RunnerGroups: []*RunnerGroup{
    37  			{ID: Int64(1), Name: String("Default"), Visibility: String("all"), Default: Bool(true), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/1/runners"), Inherited: Bool(false), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(true), SelectedWorkflows: []string{"a", "b"}},
    38  			{ID: Int64(2), Name: String("octo-runner-group"), Visibility: String("selected"), Default: Bool(false), SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories"), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners"), Inherited: Bool(true), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(false), SelectedWorkflows: []string{}},
    39  			{ID: Int64(3), Name: String("expensive-hardware"), Visibility: String("private"), Default: Bool(false), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/3/runners"), Inherited: Bool(false), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(false), SelectedWorkflows: []string{}},
    40  		},
    41  	}
    42  	if !cmp.Equal(groups, want) {
    43  		t.Errorf("Actions.ListOrganizationRunnerGroups returned %+v, want %+v", groups, want)
    44  	}
    45  
    46  	const methodName = "ListOrganizationRunnerGroups"
    47  	testBadOptions(t, methodName, func() (err error) {
    48  		_, _, err = client.Actions.ListOrganizationRunnerGroups(ctx, "\n", opts)
    49  		return err
    50  	})
    51  
    52  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    53  		got, resp, err := client.Actions.ListOrganizationRunnerGroups(ctx, "o", opts)
    54  		if got != nil {
    55  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    56  		}
    57  		return resp, err
    58  	})
    59  }
    60  
    61  func TestActionsService_ListOrganizationRunnerGroupsVisibleToRepo(t *testing.T) {
    62  	client, mux, _, teardown := setup()
    63  	defer teardown()
    64  
    65  	mux.HandleFunc("/orgs/o/actions/runner-groups", func(w http.ResponseWriter, r *http.Request) {
    66  		testMethod(t, r, "GET")
    67  		testFormValues(t, r, values{"per_page": "2", "page": "2", "visible_to_repository": "github"})
    68  		fmt.Fprint(w, `{"total_count":3,"runner_groups":[{"id":1,"name":"Default","visibility":"all","default":true,"runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/1/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]},{"id":2,"name":"octo-runner-group","visibility":"selected","default":false,"selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories","runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners","inherited":true,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]},{"id":3,"name":"expensive-hardware","visibility":"private","default":false,"runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/3/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]}]}`)
    69  	})
    70  
    71  	opts := &ListOrgRunnerGroupOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}, VisibleToRepository: "github"}
    72  	ctx := context.Background()
    73  	groups, _, err := client.Actions.ListOrganizationRunnerGroups(ctx, "o", opts)
    74  	if err != nil {
    75  		t.Errorf("Actions.ListOrganizationRunnerGroups returned error: %v", err)
    76  	}
    77  
    78  	want := &RunnerGroups{
    79  		TotalCount: 3,
    80  		RunnerGroups: []*RunnerGroup{
    81  			{ID: Int64(1), Name: String("Default"), Visibility: String("all"), Default: Bool(true), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/1/runners"), Inherited: Bool(false), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(false), SelectedWorkflows: []string{}},
    82  			{ID: Int64(2), Name: String("octo-runner-group"), Visibility: String("selected"), Default: Bool(false), SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories"), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners"), Inherited: Bool(true), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(false), SelectedWorkflows: []string{}},
    83  			{ID: Int64(3), Name: String("expensive-hardware"), Visibility: String("private"), Default: Bool(false), RunnersURL: String("https://api.github.com/orgs/octo-org/actions/runner_groups/3/runners"), Inherited: Bool(false), AllowsPublicRepositories: Bool(true), RestrictedToWorkflows: Bool(false), SelectedWorkflows: []string{}},
    84  		},
    85  	}
    86  	if !cmp.Equal(groups, want) {
    87  		t.Errorf("Actions.ListOrganizationRunnerGroups returned %+v, want %+v", groups, want)
    88  	}
    89  
    90  	const methodName = "ListOrganizationRunnerGroups"
    91  	testBadOptions(t, methodName, func() (err error) {
    92  		_, _, err = client.Actions.ListOrganizationRunnerGroups(ctx, "\n", opts)
    93  		return err
    94  	})
    95  
    96  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    97  		got, resp, err := client.Actions.ListOrganizationRunnerGroups(ctx, "o", opts)
    98  		if got != nil {
    99  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   100  		}
   101  		return resp, err
   102  	})
   103  }
   104  
   105  func TestActionsService_GetOrganizationRunnerGroup(t *testing.T) {
   106  	client, mux, _, teardown := setup()
   107  	defer teardown()
   108  
   109  	mux.HandleFunc("/orgs/o/actions/runner-groups/2", func(w http.ResponseWriter, r *http.Request) {
   110  		testMethod(t, r, "GET")
   111  		fmt.Fprint(w, `{"id":2,"name":"octo-runner-group","visibility":"selected","default":false,"selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories","runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]}`)
   112  	})
   113  
   114  	ctx := context.Background()
   115  	group, _, err := client.Actions.GetOrganizationRunnerGroup(ctx, "o", 2)
   116  	if err != nil {
   117  		t.Errorf("Actions.ListOrganizationRunnerGroups returned error: %v", err)
   118  	}
   119  
   120  	want := &RunnerGroup{
   121  		ID:                       Int64(2),
   122  		Name:                     String("octo-runner-group"),
   123  		Visibility:               String("selected"),
   124  		Default:                  Bool(false),
   125  		SelectedRepositoriesURL:  String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories"),
   126  		RunnersURL:               String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners"),
   127  		Inherited:                Bool(false),
   128  		AllowsPublicRepositories: Bool(true),
   129  		RestrictedToWorkflows:    Bool(false),
   130  		SelectedWorkflows:        []string{},
   131  	}
   132  
   133  	if !cmp.Equal(group, want) {
   134  		t.Errorf("Actions.GetOrganizationRunnerGroup returned %+v, want %+v", group, want)
   135  	}
   136  
   137  	const methodName = "GetOrganizationRunnerGroup"
   138  	testBadOptions(t, methodName, func() (err error) {
   139  		_, _, err = client.Actions.GetOrganizationRunnerGroup(ctx, "\n", 2)
   140  		return err
   141  	})
   142  
   143  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   144  		got, resp, err := client.Actions.GetOrganizationRunnerGroup(ctx, "o", 2)
   145  		if got != nil {
   146  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   147  		}
   148  		return resp, err
   149  	})
   150  }
   151  
   152  func TestActionsService_DeleteOrganizationRunnerGroup(t *testing.T) {
   153  	client, mux, _, teardown := setup()
   154  	defer teardown()
   155  
   156  	mux.HandleFunc("/orgs/o/actions/runner-groups/2", func(w http.ResponseWriter, r *http.Request) {
   157  		testMethod(t, r, "DELETE")
   158  	})
   159  
   160  	ctx := context.Background()
   161  	_, err := client.Actions.DeleteOrganizationRunnerGroup(ctx, "o", 2)
   162  	if err != nil {
   163  		t.Errorf("Actions.DeleteOrganizationRunnerGroup returned error: %v", err)
   164  	}
   165  
   166  	const methodName = "DeleteOrganizationRunnerGroup"
   167  	testBadOptions(t, methodName, func() (err error) {
   168  		_, err = client.Actions.DeleteOrganizationRunnerGroup(ctx, "\n", 2)
   169  		return err
   170  	})
   171  
   172  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   173  		return client.Actions.DeleteOrganizationRunnerGroup(ctx, "o", 2)
   174  	})
   175  }
   176  
   177  func TestActionsService_CreateOrganizationRunnerGroup(t *testing.T) {
   178  	client, mux, _, teardown := setup()
   179  	defer teardown()
   180  
   181  	mux.HandleFunc("/orgs/o/actions/runner-groups", func(w http.ResponseWriter, r *http.Request) {
   182  		testMethod(t, r, "POST")
   183  		fmt.Fprint(w, `{"id":2,"name":"octo-runner-group","visibility":"selected","default":false,"selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories","runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]}`)
   184  	})
   185  
   186  	ctx := context.Background()
   187  	req := CreateRunnerGroupRequest{
   188  		Name:                     String("octo-runner-group"),
   189  		Visibility:               String("selected"),
   190  		AllowsPublicRepositories: Bool(true),
   191  		RestrictedToWorkflows:    Bool(false),
   192  		SelectedWorkflows:        []string{},
   193  	}
   194  	group, _, err := client.Actions.CreateOrganizationRunnerGroup(ctx, "o", req)
   195  	if err != nil {
   196  		t.Errorf("Actions.CreateOrganizationRunnerGroup returned error: %v", err)
   197  	}
   198  
   199  	want := &RunnerGroup{
   200  		ID:                       Int64(2),
   201  		Name:                     String("octo-runner-group"),
   202  		Visibility:               String("selected"),
   203  		Default:                  Bool(false),
   204  		SelectedRepositoriesURL:  String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories"),
   205  		RunnersURL:               String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners"),
   206  		Inherited:                Bool(false),
   207  		AllowsPublicRepositories: Bool(true),
   208  		RestrictedToWorkflows:    Bool(false),
   209  		SelectedWorkflows:        []string{},
   210  	}
   211  
   212  	if !cmp.Equal(group, want) {
   213  		t.Errorf("Actions.CreateOrganizationRunnerGroup returned %+v, want %+v", group, want)
   214  	}
   215  
   216  	const methodName = "CreateOrganizationRunnerGroup"
   217  	testBadOptions(t, methodName, func() (err error) {
   218  		_, _, err = client.Actions.CreateOrganizationRunnerGroup(ctx, "\n", req)
   219  		return err
   220  	})
   221  
   222  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   223  		got, resp, err := client.Actions.CreateOrganizationRunnerGroup(ctx, "o", req)
   224  		if got != nil {
   225  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   226  		}
   227  		return resp, err
   228  	})
   229  }
   230  
   231  func TestActionsService_UpdateOrganizationRunnerGroup(t *testing.T) {
   232  	client, mux, _, teardown := setup()
   233  	defer teardown()
   234  
   235  	mux.HandleFunc("/orgs/o/actions/runner-groups/2", func(w http.ResponseWriter, r *http.Request) {
   236  		testMethod(t, r, "PATCH")
   237  		fmt.Fprint(w, `{"id":2,"name":"octo-runner-group","visibility":"selected","default":false,"selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories","runners_url":"https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners","inherited":false,"allows_public_repositories":true,"restricted_to_workflows":false,"selected_workflows":[]}`)
   238  	})
   239  
   240  	ctx := context.Background()
   241  	req := UpdateRunnerGroupRequest{
   242  		Name:                     String("octo-runner-group"),
   243  		Visibility:               String("selected"),
   244  		AllowsPublicRepositories: Bool(true),
   245  		RestrictedToWorkflows:    Bool(false),
   246  		SelectedWorkflows:        []string{},
   247  	}
   248  	group, _, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, "o", 2, req)
   249  	if err != nil {
   250  		t.Errorf("Actions.UpdateOrganizationRunnerGroup returned error: %v", err)
   251  	}
   252  
   253  	want := &RunnerGroup{
   254  		ID:                       Int64(2),
   255  		Name:                     String("octo-runner-group"),
   256  		Visibility:               String("selected"),
   257  		Default:                  Bool(false),
   258  		SelectedRepositoriesURL:  String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/repositories"),
   259  		RunnersURL:               String("https://api.github.com/orgs/octo-org/actions/runner_groups/2/runners"),
   260  		Inherited:                Bool(false),
   261  		AllowsPublicRepositories: Bool(true),
   262  		RestrictedToWorkflows:    Bool(false),
   263  		SelectedWorkflows:        []string{},
   264  	}
   265  
   266  	if !cmp.Equal(group, want) {
   267  		t.Errorf("Actions.UpdateOrganizationRunnerGroup returned %+v, want %+v", group, want)
   268  	}
   269  
   270  	const methodName = "UpdateOrganizationRunnerGroup"
   271  	testBadOptions(t, methodName, func() (err error) {
   272  		_, _, err = client.Actions.UpdateOrganizationRunnerGroup(ctx, "\n", 2, req)
   273  		return err
   274  	})
   275  
   276  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   277  		got, resp, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, "o", 2, req)
   278  		if got != nil {
   279  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   280  		}
   281  		return resp, err
   282  	})
   283  }
   284  
   285  func TestActionsService_ListRepositoryAccessRunnerGroup(t *testing.T) {
   286  	client, mux, _, teardown := setup()
   287  	defer teardown()
   288  
   289  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/repositories", func(w http.ResponseWriter, r *http.Request) {
   290  		testMethod(t, r, "GET")
   291  		testFormValues(t, r, values{"per_page": "1", "page": "1"})
   292  		fmt.Fprint(w, `{"total_count": 1, "repositories": [{"id": 43, "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", "name": "Hello-World", "full_name": "octocat/Hello-World"}]}`)
   293  	})
   294  
   295  	ctx := context.Background()
   296  	opts := &ListOptions{Page: 1, PerPage: 1}
   297  	groups, _, err := client.Actions.ListRepositoryAccessRunnerGroup(ctx, "o", 2, opts)
   298  	if err != nil {
   299  		t.Errorf("Actions.ListRepositoryAccessRunnerGroup returned error: %v", err)
   300  	}
   301  
   302  	want := &ListRepositories{
   303  		TotalCount: Int(1),
   304  		Repositories: []*Repository{
   305  			{ID: Int64(43), NodeID: String("MDEwOlJlcG9zaXRvcnkxMjk2MjY5"), Name: String("Hello-World"), FullName: String("octocat/Hello-World")},
   306  		},
   307  	}
   308  	if !cmp.Equal(groups, want) {
   309  		t.Errorf("Actions.ListRepositoryAccessRunnerGroup returned %+v, want %+v", groups, want)
   310  	}
   311  
   312  	const methodName = "ListRepositoryAccessRunnerGroup"
   313  	testBadOptions(t, methodName, func() (err error) {
   314  		_, _, err = client.Actions.ListRepositoryAccessRunnerGroup(ctx, "\n", 2, opts)
   315  		return err
   316  	})
   317  
   318  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   319  		got, resp, err := client.Actions.ListRepositoryAccessRunnerGroup(ctx, "o", 2, opts)
   320  		if got != nil {
   321  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   322  		}
   323  		return resp, err
   324  	})
   325  }
   326  
   327  func TestActionsService_SetRepositoryAccessRunnerGroup(t *testing.T) {
   328  	client, mux, _, teardown := setup()
   329  	defer teardown()
   330  
   331  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/repositories", func(w http.ResponseWriter, r *http.Request) {
   332  		testMethod(t, r, "PUT")
   333  	})
   334  
   335  	req := SetRepoAccessRunnerGroupRequest{
   336  		SelectedRepositoryIDs: []int64{
   337  			1,
   338  			2,
   339  		},
   340  	}
   341  
   342  	ctx := context.Background()
   343  	_, err := client.Actions.SetRepositoryAccessRunnerGroup(ctx, "o", 2, req)
   344  	if err != nil {
   345  		t.Errorf("Actions.SetRepositoryAccessRunnerGroup returned error: %v", err)
   346  	}
   347  
   348  	const methodName = "SetRepositoryAccessRunnerGroup"
   349  	testBadOptions(t, methodName, func() (err error) {
   350  		_, err = client.Actions.SetRepositoryAccessRunnerGroup(ctx, "\n", 2, req)
   351  		return err
   352  	})
   353  
   354  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   355  		return client.Actions.SetRepositoryAccessRunnerGroup(ctx, "o", 2, req)
   356  	})
   357  }
   358  
   359  func TestActionsService_AddRepositoryAccessRunnerGroup(t *testing.T) {
   360  	client, mux, _, teardown := setup()
   361  	defer teardown()
   362  
   363  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/repositories/42", func(w http.ResponseWriter, r *http.Request) {
   364  		testMethod(t, r, "PUT")
   365  	})
   366  
   367  	ctx := context.Background()
   368  	_, err := client.Actions.AddRepositoryAccessRunnerGroup(ctx, "o", 2, 42)
   369  	if err != nil {
   370  		t.Errorf("Actions.AddRepositoryAccessRunnerGroup returned error: %v", err)
   371  	}
   372  
   373  	const methodName = "AddRepositoryAccessRunnerGroup"
   374  	testBadOptions(t, methodName, func() (err error) {
   375  		_, err = client.Actions.AddRepositoryAccessRunnerGroup(ctx, "\n", 2, 42)
   376  		return err
   377  	})
   378  
   379  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   380  		return client.Actions.AddRepositoryAccessRunnerGroup(ctx, "o", 2, 42)
   381  	})
   382  }
   383  
   384  func TestActionsService_RemoveRepositoryAccessRunnerGroup(t *testing.T) {
   385  	client, mux, _, teardown := setup()
   386  	defer teardown()
   387  
   388  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/repositories/42", func(w http.ResponseWriter, r *http.Request) {
   389  		testMethod(t, r, "DELETE")
   390  	})
   391  
   392  	ctx := context.Background()
   393  	_, err := client.Actions.RemoveRepositoryAccessRunnerGroup(ctx, "o", 2, 42)
   394  	if err != nil {
   395  		t.Errorf("Actions.RemoveRepositoryAccessRunnerGroup returned error: %v", err)
   396  	}
   397  
   398  	const methodName = "RemoveRepositoryAccessRunnerGroup"
   399  	testBadOptions(t, methodName, func() (err error) {
   400  		_, err = client.Actions.RemoveRepositoryAccessRunnerGroup(ctx, "\n", 2, 42)
   401  		return err
   402  	})
   403  
   404  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   405  		return client.Actions.RemoveRepositoryAccessRunnerGroup(ctx, "o", 2, 42)
   406  	})
   407  }
   408  
   409  func TestActionsService_ListRunnerGroupRunners(t *testing.T) {
   410  	client, mux, _, teardown := setup()
   411  	defer teardown()
   412  
   413  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/runners", func(w http.ResponseWriter, r *http.Request) {
   414  		testMethod(t, r, "GET")
   415  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
   416  		fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
   417  	})
   418  
   419  	opts := &ListOptions{Page: 2, PerPage: 2}
   420  	ctx := context.Background()
   421  	runners, _, err := client.Actions.ListRunnerGroupRunners(ctx, "o", 2, opts)
   422  	if err != nil {
   423  		t.Errorf("Actions.ListRunnerGroupRunners returned error: %v", err)
   424  	}
   425  
   426  	want := &Runners{
   427  		TotalCount: 2,
   428  		Runners: []*Runner{
   429  			{ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
   430  			{ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
   431  		},
   432  	}
   433  	if !cmp.Equal(runners, want) {
   434  		t.Errorf("Actions.ListRunnerGroupRunners returned %+v, want %+v", runners, want)
   435  	}
   436  
   437  	const methodName = "ListRunnerGroupRunners"
   438  	testBadOptions(t, methodName, func() (err error) {
   439  		_, _, err = client.Actions.ListRunnerGroupRunners(ctx, "\n", 2, opts)
   440  		return err
   441  	})
   442  
   443  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   444  		got, resp, err := client.Actions.ListRunnerGroupRunners(ctx, "o", 2, opts)
   445  		if got != nil {
   446  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   447  		}
   448  		return resp, err
   449  	})
   450  }
   451  
   452  func TestActionsService_SetRunnerGroupRunners(t *testing.T) {
   453  	client, mux, _, teardown := setup()
   454  	defer teardown()
   455  
   456  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/runners", func(w http.ResponseWriter, r *http.Request) {
   457  		testMethod(t, r, "PUT")
   458  	})
   459  
   460  	req := SetRunnerGroupRunnersRequest{
   461  		Runners: []int64{
   462  			1,
   463  			2,
   464  		},
   465  	}
   466  
   467  	ctx := context.Background()
   468  	_, err := client.Actions.SetRunnerGroupRunners(ctx, "o", 2, req)
   469  	if err != nil {
   470  		t.Errorf("Actions.SetRunnerGroupRunners returned error: %v", err)
   471  	}
   472  
   473  	const methodName = "SetRunnerGroupRunners"
   474  	testBadOptions(t, methodName, func() (err error) {
   475  		_, err = client.Actions.SetRunnerGroupRunners(ctx, "\n", 2, req)
   476  		return err
   477  	})
   478  
   479  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   480  		return client.Actions.SetRunnerGroupRunners(ctx, "o", 2, req)
   481  	})
   482  }
   483  
   484  func TestActionsService_AddRunnerGroupRunners(t *testing.T) {
   485  	client, mux, _, teardown := setup()
   486  	defer teardown()
   487  
   488  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/runners/42", func(w http.ResponseWriter, r *http.Request) {
   489  		testMethod(t, r, "PUT")
   490  	})
   491  
   492  	ctx := context.Background()
   493  	_, err := client.Actions.AddRunnerGroupRunners(ctx, "o", 2, 42)
   494  	if err != nil {
   495  		t.Errorf("Actions.AddRunnerGroupRunners returned error: %v", err)
   496  	}
   497  
   498  	const methodName = "AddRunnerGroupRunners"
   499  	testBadOptions(t, methodName, func() (err error) {
   500  		_, err = client.Actions.AddRunnerGroupRunners(ctx, "\n", 2, 42)
   501  		return err
   502  	})
   503  
   504  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   505  		return client.Actions.AddRunnerGroupRunners(ctx, "o", 2, 42)
   506  	})
   507  }
   508  
   509  func TestActionsService_RemoveRunnerGroupRunners(t *testing.T) {
   510  	client, mux, _, teardown := setup()
   511  	defer teardown()
   512  
   513  	mux.HandleFunc("/orgs/o/actions/runner-groups/2/runners/42", func(w http.ResponseWriter, r *http.Request) {
   514  		testMethod(t, r, "DELETE")
   515  	})
   516  
   517  	ctx := context.Background()
   518  	_, err := client.Actions.RemoveRunnerGroupRunners(ctx, "o", 2, 42)
   519  	if err != nil {
   520  		t.Errorf("Actions.RemoveRunnerGroupRunners returned error: %v", err)
   521  	}
   522  
   523  	const methodName = "RemoveRunnerGroupRunners"
   524  	testBadOptions(t, methodName, func() (err error) {
   525  		_, err = client.Actions.RemoveRunnerGroupRunners(ctx, "\n", 2, 42)
   526  		return err
   527  	})
   528  
   529  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   530  		return client.Actions.RemoveRunnerGroupRunners(ctx, "o", 2, 42)
   531  	})
   532  }
   533  
   534  func TestRunnerGroup_Marshal(t *testing.T) {
   535  	testJSONMarshal(t, &RunnerGroup{}, "{}")
   536  
   537  	u := &RunnerGroup{
   538  		ID:                       Int64(1),
   539  		Name:                     String("n"),
   540  		Visibility:               String("v"),
   541  		Default:                  Bool(true),
   542  		SelectedRepositoriesURL:  String("s"),
   543  		RunnersURL:               String("r"),
   544  		Inherited:                Bool(true),
   545  		AllowsPublicRepositories: Bool(true),
   546  		RestrictedToWorkflows:    Bool(false),
   547  		SelectedWorkflows:        []string{},
   548  	}
   549  
   550  	want := `{
   551  		"id": 1,
   552  		"name": "n",
   553  		"visibility": "v",
   554  		"default": true,
   555  		"selected_repositories_url": "s",
   556  		"runners_url": "r",
   557  		"inherited": true,
   558  		"allows_public_repositories": true,
   559  		"restricted_to_workflows": false,
   560  		"selected_workflows": []
   561  	}`
   562  
   563  	testJSONMarshal(t, u, want)
   564  }
   565  
   566  func TestRunnerGroups_Marshal(t *testing.T) {
   567  	testJSONMarshal(t, &RunnerGroups{}, "{}")
   568  
   569  	u := &RunnerGroups{
   570  		TotalCount: int(1),
   571  		RunnerGroups: []*RunnerGroup{
   572  			{
   573  				ID:                       Int64(1),
   574  				Name:                     String("n"),
   575  				Visibility:               String("v"),
   576  				Default:                  Bool(true),
   577  				SelectedRepositoriesURL:  String("s"),
   578  				RunnersURL:               String("r"),
   579  				Inherited:                Bool(true),
   580  				AllowsPublicRepositories: Bool(true),
   581  				RestrictedToWorkflows:    Bool(false),
   582  				SelectedWorkflows:        []string{},
   583  			},
   584  		},
   585  	}
   586  
   587  	want := `{
   588  		"total_count": 1,
   589  		"runner_groups": [{
   590  			"id": 1,
   591  			"name": "n",
   592  			"visibility": "v",
   593  			"default": true,
   594  			"selected_repositories_url": "s",
   595  			"runners_url": "r",
   596  			"inherited": true,
   597  			"allows_public_repositories": true,
   598  			"restricted_to_workflows": false,
   599  			"selected_workflows": []
   600  		}]		
   601  	}`
   602  
   603  	testJSONMarshal(t, u, want)
   604  }
   605  
   606  func TestCreateRunnerGroupRequest_Marshal(t *testing.T) {
   607  	testJSONMarshal(t, &CreateRunnerGroupRequest{}, "{}")
   608  
   609  	u := &CreateRunnerGroupRequest{
   610  		Name:                     String("n"),
   611  		Visibility:               String("v"),
   612  		SelectedRepositoryIDs:    []int64{1},
   613  		Runners:                  []int64{1},
   614  		AllowsPublicRepositories: Bool(true),
   615  		RestrictedToWorkflows:    Bool(true),
   616  		SelectedWorkflows:        []string{"a", "b"},
   617  	}
   618  
   619  	want := `{
   620  		"name": "n",
   621  		"visibility": "v",
   622  		"selected_repository_ids": [1],
   623  		"runners": [1],
   624  		"allows_public_repositories": true,
   625  		"restricted_to_workflows": true,
   626  		"selected_workflows": ["a","b"]
   627  	}`
   628  
   629  	testJSONMarshal(t, u, want)
   630  }
   631  
   632  func TestUpdateRunnerGroupRequest_Marshal(t *testing.T) {
   633  	testJSONMarshal(t, &UpdateRunnerGroupRequest{}, "{}")
   634  
   635  	u := &UpdateRunnerGroupRequest{
   636  		Name:                     String("n"),
   637  		Visibility:               String("v"),
   638  		AllowsPublicRepositories: Bool(true),
   639  		RestrictedToWorkflows:    Bool(false),
   640  		SelectedWorkflows:        []string{},
   641  	}
   642  
   643  	want := `{
   644  		"name": "n",
   645  		"visibility": "v",
   646  		"allows_public_repositories": true,
   647  		"restricted_to_workflows": false,
   648  		"selected_workflows": []
   649  	}`
   650  
   651  	testJSONMarshal(t, u, want)
   652  }
   653  
   654  func TestSetRepoAccessRunnerGroupRequest_Marshal(t *testing.T) {
   655  	testJSONMarshal(t, &SetRepoAccessRunnerGroupRequest{}, "{}")
   656  
   657  	u := &SetRepoAccessRunnerGroupRequest{
   658  		SelectedRepositoryIDs: []int64{1},
   659  	}
   660  
   661  	want := `{
   662  		"selected_repository_ids": [1]
   663  	}`
   664  
   665  	testJSONMarshal(t, u, want)
   666  }
   667  
   668  func TestSetRunnerGroupRunnersRequest_Marshal(t *testing.T) {
   669  	testJSONMarshal(t, &SetRunnerGroupRunnersRequest{}, "{}")
   670  
   671  	u := &SetRunnerGroupRunnersRequest{
   672  		Runners: []int64{1},
   673  	}
   674  
   675  	want := `{
   676  		"runners": [1]
   677  	}`
   678  
   679  	testJSONMarshal(t, u, want)
   680  }
   681  

View as plain text