From 38d7b16d53c396178eeb836b7cb287304dab66ae Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sun, 16 Oct 2022 19:51:34 +0300 Subject: [PATCH] Add enterprise list runner applications download (#2496) Fixes: #2495 . --- github/enterprise_actions_runners.go | 19 +++++++++++ github/enterprise_actions_runners_test.go | 41 +++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/github/enterprise_actions_runners.go b/github/enterprise_actions_runners.go index f2ba166360..daafc5e628 100644 --- a/github/enterprise_actions_runners.go +++ b/github/enterprise_actions_runners.go @@ -10,6 +10,25 @@ import ( "fmt" ) +// ListRunnerApplicationDownloads lists self-hosted runner application binaries that can be downloaded and run. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#list-runner-applications-for-an-enterprise +func (s *EnterpriseService) ListRunnerApplicationDownloads(ctx context.Context, enterprise string) ([]*RunnerApplicationDownload, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/runners/downloads", enterprise) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var rads []*RunnerApplicationDownload + resp, err := s.client.Do(ctx, req, &rads) + if err != nil { + return nil, resp, err + } + + return rads, resp, nil +} + // CreateRegistrationToken creates a token that can be used to add a self-hosted runner. // // GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-an-enterprise diff --git a/github/enterprise_actions_runners_test.go b/github/enterprise_actions_runners_test.go index a854b482ec..b7c3f78b2d 100644 --- a/github/enterprise_actions_runners_test.go +++ b/github/enterprise_actions_runners_test.go @@ -119,3 +119,44 @@ func TestEnterpriseService_RemoveRunner(t *testing.T) { return client.Enterprise.RemoveRunner(ctx, "o", 21) }) } + +func TestEnterpriseService_ListRunnerApplicationDownloads(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/o/actions/runners/downloads", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"os":"osx","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-osx-x64-2.164.0.tar.gz","filename":"actions-runner-osx-x64-2.164.0.tar.gz"},{"os":"linux","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-x64-2.164.0.tar.gz","filename":"actions-runner-linux-x64-2.164.0.tar.gz"},{"os": "linux","architecture":"arm","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm-2.164.0.tar.gz","filename":"actions-runner-linux-arm-2.164.0.tar.gz"},{"os":"win","architecture":"x64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-win-x64-2.164.0.zip","filename":"actions-runner-win-x64-2.164.0.zip"},{"os":"linux","architecture":"arm64","download_url":"https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm64-2.164.0.tar.gz","filename":"actions-runner-linux-arm64-2.164.0.tar.gz"}]`) + }) + + ctx := context.Background() + downloads, _, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o") + if err != nil { + t.Errorf("Enterprise.ListRunnerApplicationDownloads returned error: %v", err) + } + + want := []*RunnerApplicationDownload{ + {OS: String("osx"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-osx-x64-2.164.0.tar.gz"), Filename: String("actions-runner-osx-x64-2.164.0.tar.gz")}, + {OS: String("linux"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-x64-2.164.0.tar.gz"), Filename: String("actions-runner-linux-x64-2.164.0.tar.gz")}, + {OS: String("linux"), Architecture: String("arm"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm-2.164.0.tar.gz"), Filename: String("actions-runner-linux-arm-2.164.0.tar.gz")}, + {OS: String("win"), Architecture: String("x64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-win-x64-2.164.0.zip"), Filename: String("actions-runner-win-x64-2.164.0.zip")}, + {OS: String("linux"), Architecture: String("arm64"), DownloadURL: String("https://github.com/actions/runner/releases/download/v2.164.0/actions-runner-linux-arm64-2.164.0.tar.gz"), Filename: String("actions-runner-linux-arm64-2.164.0.tar.gz")}, + } + if !cmp.Equal(downloads, want) { + t.Errorf("Enterprise.ListRunnerApplicationDownloads returned %+v, want %+v", downloads, want) + } + + const methodName = "ListRunnerApplicationDownloads" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Enterprise.ListRunnerApplicationDownloads(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Enterprise.ListRunnerApplicationDownloads(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +}