Skip to content

Commit 4b55d56

Browse files
Add GenerateEnterpriseJITConfig
With version 3.10 of GitHub Enterprise server, the new just-in-time runner configuration is available for enterprises. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 7ceef94 commit 4b55d56

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

github/enterprise_actions_runners.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ func (s *EnterpriseService) ListRunnerApplicationDownloads(ctx context.Context,
2929
return rads, resp, nil
3030
}
3131

32+
// GenerateEnterpriseJITConfig generates a just-in-time configuration for an enterprise.
33+
//
34+
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-configuration-for-a-just-in-time-runner-for-an-enterprise
35+
func (s *EnterpriseService) GenerateEnterpriseJITConfig(ctx context.Context, enterprise string, request *GenerateJITConfigRequest) (*JITRunnerConfig, *Response, error) {
36+
u := fmt.Sprintf("enterprises/%v/actions/runners/generate-jitconfig", enterprise)
37+
38+
req, err := s.client.NewRequest("POST", u, request)
39+
if err != nil {
40+
return nil, nil, err
41+
}
42+
43+
jitConfig := new(JITRunnerConfig)
44+
resp, err := s.client.Do(ctx, req, jitConfig)
45+
if err != nil {
46+
return nil, resp, err
47+
}
48+
49+
return jitConfig, resp, nil
50+
}
51+
3252
// CreateRegistrationToken creates a token that can be used to add a self-hosted runner.
3353
//
3454
// GitHub API docs: https://docs.github.com/en/rest/actions/self-hosted-runners#create-a-registration-token-for-an-enterprise

github/enterprise_actions_runners_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package github
77

88
import (
99
"context"
10+
"encoding/json"
1011
"fmt"
1112
"net/http"
1213
"testing"
@@ -15,6 +16,50 @@ import (
1516
"github.com/google/go-cmp/cmp"
1617
)
1718

19+
func TestEnterpriseService_GenerateEnterpriseJITConfig(t *testing.T) {
20+
client, mux, _, teardown := setup()
21+
defer teardown()
22+
23+
input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}}
24+
25+
mux.HandleFunc("/enterprises/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) {
26+
v := new(GenerateJITConfigRequest)
27+
json.NewDecoder(r.Body).Decode(v)
28+
29+
testMethod(t, r, "POST")
30+
if !cmp.Equal(v, input) {
31+
t.Errorf("Request body = %+v, want %+v", v, input)
32+
}
33+
34+
fmt.Fprint(w, `{"encoded_jit_config":"foo"}`)
35+
})
36+
37+
ctx := context.Background()
38+
jitConfig, _, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
39+
if err != nil {
40+
t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned error: %v", err)
41+
}
42+
43+
want := &JITRunnerConfig{EncodedJITConfig: String("foo")}
44+
if !cmp.Equal(jitConfig, want) {
45+
t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned %+v, want %+v", jitConfig, want)
46+
}
47+
48+
const methodName = "GenerateEnterpriseJITConfig"
49+
testBadOptions(t, methodName, func() (err error) {
50+
_, _, err = client.Enterprise.GenerateEnterpriseJITConfig(ctx, "\n", input)
51+
return err
52+
})
53+
54+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
55+
got, resp, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
56+
if got != nil {
57+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
58+
}
59+
return resp, err
60+
})
61+
}
62+
1863
func TestEnterpriseService_CreateRegistrationToken(t *testing.T) {
1964
client, mux, _, teardown := setup()
2065
defer teardown()

0 commit comments

Comments
 (0)