Skip to content

Commit

Permalink
Add GenerateEnterpriseJITConfig (google#2890)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-samfira authored and gmlewis committed Sep 18, 2023
1 parent dde920a commit 1d84f43
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
20 changes: 20 additions & 0 deletions github/enterprise_actions_runners.go
Expand Up @@ -29,6 +29,26 @@ func (s *EnterpriseService) ListRunnerApplicationDownloads(ctx context.Context,
return rads, resp, nil
}

// GenerateEnterpriseJITConfig generates a just-in-time configuration for an enterprise.
//
// 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
func (s *EnterpriseService) GenerateEnterpriseJITConfig(ctx context.Context, enterprise string, request *GenerateJITConfigRequest) (*JITRunnerConfig, *Response, error) {
u := fmt.Sprintf("enterprises/%v/actions/runners/generate-jitconfig", enterprise)

req, err := s.client.NewRequest("POST", u, request)
if err != nil {
return nil, nil, err
}

jitConfig := new(JITRunnerConfig)
resp, err := s.client.Do(ctx, req, jitConfig)
if err != nil {
return nil, resp, err
}

return jitConfig, 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
Expand Down
45 changes: 45 additions & 0 deletions github/enterprise_actions_runners_test.go
Expand Up @@ -7,6 +7,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
Expand All @@ -15,6 +16,50 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestEnterpriseService_GenerateEnterpriseJITConfig(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}}

mux.HandleFunc("/enterprises/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) {
v := new(GenerateJITConfigRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "POST")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

fmt.Fprint(w, `{"encoded_jit_config":"foo"}`)
})

ctx := context.Background()
jitConfig, _, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
if err != nil {
t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned error: %v", err)
}

want := &JITRunnerConfig{EncodedJITConfig: String("foo")}
if !cmp.Equal(jitConfig, want) {
t.Errorf("Enterprise.GenerateEnterpriseJITConfig returned %+v, want %+v", jitConfig, want)
}

const methodName = "GenerateEnterpriseJITConfig"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Enterprise.GenerateEnterpriseJITConfig(ctx, "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Enterprise.GenerateEnterpriseJITConfig(ctx, "o", input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestEnterpriseService_CreateRegistrationToken(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 1d84f43

Please sign in to comment.