From b09e3a2dc973aab94abbec1f9c335792e3d83955 Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Thu, 26 Jan 2023 12:19:52 -0500 Subject: [PATCH] Return error for non-existing runner group. (#2215) --- github/actions/client.go | 2 +- github/actions/client_runner_test.go | 44 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/github/actions/client.go b/github/actions/client.go index fd99dd21..48ab4a21 100644 --- a/github/actions/client.go +++ b/github/actions/client.go @@ -300,7 +300,7 @@ func (c *Client) GetRunnerGroupByName(ctx context.Context, runnerGroup string) ( } if runnerGroupList.Count == 0 { - return nil, nil + return nil, fmt.Errorf("no runner group found with name '%s'", runnerGroup) } if runnerGroupList.Count > 1 { diff --git a/github/actions/client_runner_test.go b/github/actions/client_runner_test.go index 9406425a..38d7b298 100644 --- a/github/actions/client_runner_test.go +++ b/github/actions/client_runner_test.go @@ -173,3 +173,47 @@ func TestDeleteRunner(t *testing.T) { assert.Equalf(t, actualRetry, expectedRetry, "A retry was expected after the first request but got: %v", actualRetry) }) } + +func TestGetRunnerGroupByName(t *testing.T) { + ctx := context.Background() + auth := &actions.ActionsAuth{ + Token: "token", + } + + t.Run("Get RunnerGroup by Name", func(t *testing.T) { + var runnerGroupID int64 = 1 + var runnerGroupName string = "test-runner-group" + want := &actions.RunnerGroup{ + ID: runnerGroupID, + Name: runnerGroupName, + } + response := []byte(`{"count": 1, "value": [{"id": 1, "name": "test-runner-group"}]}`) + + server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write(response) + })) + + client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth) + require.NoError(t, err) + + got, err := client.GetRunnerGroupByName(ctx, runnerGroupName) + require.NoError(t, err) + assert.Equal(t, want, got) + }) + + t.Run("Get RunnerGroup by name with not exist runner group", func(t *testing.T) { + var runnerGroupName string = "test-runner-group" + response := []byte(`{"count": 0, "value": []}`) + + server := newActionsServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write(response) + })) + + client, err := actions.NewClient(ctx, server.configURLForOrg("my-org"), auth) + require.NoError(t, err) + + got, err := client.GetRunnerGroupByName(ctx, runnerGroupName) + assert.ErrorContains(t, err, "no runner group found with name") + assert.Nil(t, got) + }) +}