Skip to content

Commit df27af0

Browse files
authored
Add Enterprise ListRunners/RegistrationToken APIs (google#1644)
1 parent 7c1a5f3 commit df27af0

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

github/enterprise.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2020 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+
// EnterpriseService provides access to the enterprise related functions
9+
// in the GitHub API.
10+
//
11+
// GitHub API docs: https://docs.github.com/en/rest/reference/enterprise-admin/
12+
type EnterpriseService service

github/enterprise_actions_runners.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020 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+
)
12+
13+
// CreateRegistrationToken creates a token that can be used to add a self-hosted runner.
14+
//
15+
// GitHub API docs: https://docs.github.com/en/rest/reference/enterprise-admin#create-a-registration-token-for-an-enterprise
16+
func (s *EnterpriseService) CreateRegistrationToken(ctx context.Context, enterprise string) (*RegistrationToken, *Response, error) {
17+
u := fmt.Sprintf("enterprises/%v/actions/runners/registration-token", enterprise)
18+
19+
req, err := s.client.NewRequest("POST", u, nil)
20+
if err != nil {
21+
return nil, nil, err
22+
}
23+
24+
registrationToken := new(RegistrationToken)
25+
resp, err := s.client.Do(ctx, req, registrationToken)
26+
if err != nil {
27+
return nil, resp, err
28+
}
29+
30+
return registrationToken, resp, nil
31+
}
32+
33+
// ListRunners lists all the self-hosted runners for a enterprise.
34+
//
35+
// GitHub API docs: https://docs.github.com/en/rest/reference/enterprise-admin/#list-self-hosted-runners-for-an-enterprise
36+
func (s *EnterpriseService) ListRunners(ctx context.Context, enterprise string, opts *ListOptions) (*Runners, *Response, error) {
37+
u := fmt.Sprintf("enterprises/%v/actions/runners", enterprise)
38+
u, err := addOptions(u, opts)
39+
if err != nil {
40+
return nil, nil, err
41+
}
42+
43+
req, err := s.client.NewRequest("GET", u, nil)
44+
if err != nil {
45+
return nil, nil, err
46+
}
47+
48+
runners := &Runners{}
49+
resp, err := s.client.Do(ctx, req, &runners)
50+
if err != nil {
51+
return nil, resp, err
52+
}
53+
54+
return runners, resp, nil
55+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020 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+
"reflect"
13+
"testing"
14+
"time"
15+
)
16+
17+
func TestEnterpriseService_CreateRegistrationToken(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/enterprises/e/actions/runners/registration-token", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "POST")
23+
fmt.Fprint(w, `{"token":"LLBF3JGZDX3P5PMEXLND6TS6FCWO6","expires_at":"2020-01-22T12:13:35.123Z"}`)
24+
})
25+
26+
token, _, err := client.Enterprise.CreateRegistrationToken(context.Background(), "e")
27+
if err != nil {
28+
t.Errorf("Enterprise.CreateRegistrationToken returned error: %v", err)
29+
}
30+
31+
want := &RegistrationToken{Token: String("LLBF3JGZDX3P5PMEXLND6TS6FCWO6"),
32+
ExpiresAt: &Timestamp{time.Date(2020, time.January, 22, 12, 13, 35,
33+
123000000, time.UTC)}}
34+
if !reflect.DeepEqual(token, want) {
35+
t.Errorf("Enterprise.CreateRegistrationToken returned %+v, want %+v", token, want)
36+
}
37+
}
38+
39+
func TestEnterpriseService_ListRunners(t *testing.T) {
40+
client, mux, _, teardown := setup()
41+
defer teardown()
42+
43+
mux.HandleFunc("/enterprises/e/actions/runners", func(w http.ResponseWriter, r *http.Request) {
44+
testMethod(t, r, "GET")
45+
testFormValues(t, r, values{"per_page": "2", "page": "2"})
46+
fmt.Fprint(w, `{"total_count":2,"runners":[{"id":23,"name":"MBP","os":"macos","status":"online"},{"id":24,"name":"iMac","os":"macos","status":"offline"}]}`)
47+
})
48+
49+
opts := &ListOptions{Page: 2, PerPage: 2}
50+
runners, _, err := client.Enterprise.ListRunners(context.Background(), "e", opts)
51+
if err != nil {
52+
t.Errorf("Enterprise.ListRunners returned error: %v", err)
53+
}
54+
55+
want := &Runners{
56+
TotalCount: 2,
57+
Runners: []*Runner{
58+
{ID: Int64(23), Name: String("MBP"), OS: String("macos"), Status: String("online")},
59+
{ID: Int64(24), Name: String("iMac"), OS: String("macos"), Status: String("offline")},
60+
},
61+
}
62+
if !reflect.DeepEqual(runners, want) {
63+
t.Errorf("Actions.ListRunners returned %+v, want %+v", runners, want)
64+
}
65+
}

github/github.go

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type Client struct {
161161
Authorizations *AuthorizationsService
162162
Checks *ChecksService
163163
CodeScanning *CodeScanningService
164+
Enterprise *EnterpriseService
164165
Gists *GistsService
165166
Git *GitService
166167
Gitignores *GitignoresService
@@ -269,6 +270,7 @@ func NewClient(httpClient *http.Client) *Client {
269270
c.Authorizations = (*AuthorizationsService)(&c.common)
270271
c.Checks = (*ChecksService)(&c.common)
271272
c.CodeScanning = (*CodeScanningService)(&c.common)
273+
c.Enterprise = (*EnterpriseService)(&c.common)
272274
c.Gists = (*GistsService)(&c.common)
273275
c.Git = (*GitService)(&c.common)
274276
c.Gitignores = (*GitignoresService)(&c.common)

0 commit comments

Comments
 (0)