From f661249f076599822249d68bcd1739980b16ab50 Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Wed, 29 Jun 2022 17:53:03 +0900 Subject: [PATCH] Use the go-github impl of ListRunnerGroups with visible_to_repository (#1578) Ref #1402 --- github/github.go | 43 +++++-------------------------------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/github/github.go b/github/github.go index e918933a..f38a0e51 100644 --- a/github/github.go +++ b/github/github.go @@ -272,9 +272,12 @@ func (c *Client) ListOrganizationRunnerGroups(ctx context.Context, org string) ( func (c *Client) ListOrganizationRunnerGroupsForRepository(ctx context.Context, org, repo string) ([]*github.RunnerGroup, error) { var runnerGroups []*github.RunnerGroup - opts := github.ListOptions{PerPage: 100} + var opts github.ListOrgRunnerGroupOptions + opts.PerPage = 100 + opts.VisibleToRepository = repo + for { - list, res, err := c.listOrganizationRunnerGroupsVisibleToRepo(ctx, org, repo, &opts) + list, res, err := c.Actions.ListOrganizationRunnerGroups(ctx, org, &opts) if err != nil { return runnerGroups, fmt.Errorf("failed to list organization runner groups: %w", err) } @@ -310,42 +313,6 @@ func (c *Client) ListRunnerGroupRepositoryAccesses(ctx context.Context, org stri return repos, nil } -// listOrganizationRunnerGroupsVisibleToRepo lists all self-hosted runner groups configured in an organization which can be used by the repository. -// -// GitHub API docs: https://docs.github.com/en/rest/reference/actions#list-self-hosted-runner-groups-for-an-organization -func (c *Client) listOrganizationRunnerGroupsVisibleToRepo(ctx context.Context, org, repo string, opts *github.ListOptions) (*github.RunnerGroups, *github.Response, error) { - repoName := repo - parts := strings.Split(repo, "/") - if len(parts) == 2 { - repoName = parts[1] - } - - u := fmt.Sprintf("orgs/%v/actions/runner-groups?visible_to_repository=%v", org, repoName) - - if opts != nil { - if opts.PerPage > 0 { - u = fmt.Sprintf("%v&per_page=%v", u, opts.PerPage) - } - - if opts.Page > 0 { - u = fmt.Sprintf("%v&page=%v", u, opts.Page) - } - } - - req, err := c.Client.NewRequest("GET", u, nil) - if err != nil { - return nil, nil, err - } - - groups := &github.RunnerGroups{} - resp, err := c.Client.Do(ctx, req, &groups) - if err != nil { - return nil, resp, err - } - - return groups, resp, nil -} - // cleanup removes expired registration tokens. func (c *Client) cleanup() { c.mu.Lock()