Skip to content

Commit 5e5389f

Browse files
Copilotjasonrclark
andauthored
feat: add --org flag to create command for selected_orgs visibility (#21)
Part of https://github.com/github/spark/issues/817 ⚠️ Depends on https://github.com/github/github/pull/426192 ## Summary Adds `--org` argument to the `gh runtime create` command to support the upcoming GitHub API `organization_login` parameter. ## Changes - Added `org` field to `createCmdFlags` struct - Added `OrganizationLogin` field (JSON: `organization_login`) to `createReq` struct - Added `--org` / `-o` flag registration with descriptive help text - Updated `--visibility` flag description to include `selected_orgs` - Updated command `Example` to document the new flag - Added validation in `runCreate`: - `--org` is rejected unless `--visibility=selected_orgs` - `--org` is required when `--visibility=selected_orgs` - Passes the `org` value as `organization_login` in the API request body ## Tests - `TestRunCreate_OrgWithoutSelectedOrgsVisibility` – verifies error when `--org` is used with a non-`selected_orgs` visibility - `TestRunCreate_SelectedOrgsVisibilityWithoutOrg` – verifies error when `--visibility=selected_orgs` is used without `--org` - `TestRunCreate_WithOrgAndSelectedOrgsVisibility` – verifies successful request with correct `organization_login` in the body All existing tests continue to pass. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jasonrclark <130504+jasonrclark@users.noreply.github.com>
1 parent 554e547 commit 5e5389f

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

cmd/create.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type createCmdFlags struct {
1616
app string
1717
name string
1818
visibility string
19+
org string
1920
environmentVariables []string
2021
secrets []string
2122
revisionName string
@@ -25,6 +26,7 @@ type createCmdFlags struct {
2526
type createReq struct {
2627
Name string `json:"friendly_name,omitempty"`
2728
Visibility string `json:"visibility,omitempty"`
29+
OrganizationLogin string `json:"organization_login,omitempty"`
2830
EnvironmentVariables map[string]string `json:"environment_variables"`
2931
Secrets map[string]string `json:"secrets"`
3032
}
@@ -40,14 +42,23 @@ func init() {
4042
Use: "create",
4143
Short: "Create a GitHub Runtime app",
4244
Long: heredoc.Doc(`
43-
Create a GitHub Runtime app
45+
Create a GitHub Runtime app.
4446
`),
4547
Example: heredoc.Doc(`
4648
$ gh runtime create --app my-app --env key1=value1 --env key2=value2 --secret key3=value3 --secret key4=value4
4749
# => Creates the app with the ID 'my-app'
4850
4951
$ gh runtime create --name my-new-app
5052
# => Creates a new app with the given name
53+
54+
$ gh runtime create --app my-app --visibility only_owner
55+
# => Creates the app visible only to the owner
56+
57+
$ gh runtime create --app my-app --visibility github
58+
# => Creates the app visible to all GitHub users
59+
60+
$ gh runtime create --app my-app --visibility selected_orgs --org my-org
61+
# => Creates the app visible to 'my-org' organization
5162
`),
5263
RunE: func(cmd *cobra.Command, args []string) error {
5364
client, err := api.DefaultRESTClient()
@@ -70,7 +81,8 @@ func init() {
7081

7182
createCmd.Flags().StringVarP(&createCmdFlags.app, "app", "a", "", "The app ID to create")
7283
createCmd.Flags().StringVarP(&createCmdFlags.name, "name", "n", "", "The name for the app")
73-
createCmd.Flags().StringVarP(&createCmdFlags.visibility, "visibility", "v", "", "The visibility of the app (e.g. 'only_owner' or 'github')")
84+
createCmd.Flags().StringVarP(&createCmdFlags.visibility, "visibility", "v", "", "The visibility of the app (e.g. 'only_owner', 'github', or 'selected_orgs')")
85+
createCmd.Flags().StringVarP(&createCmdFlags.org, "org", "o", "", "The organization login to grant access (only valid with --visibility=selected_orgs)")
7486
createCmd.Flags().StringSliceVarP(&createCmdFlags.environmentVariables, "env", "e", []string{}, "Environment variables to set on the app in the form 'key=value'")
7587
createCmd.Flags().StringSliceVarP(&createCmdFlags.secrets, "secret", "s", []string{}, "Secrets to set on the app in the form 'key=value'")
7688
createCmd.Flags().StringVarP(&createCmdFlags.revisionName, "revision-name", "r", "", "The revision name to use for the app")
@@ -83,9 +95,18 @@ func runCreate(client restClient, flags createCmdFlags) (createResp, error) {
8395
return createResp{}, fmt.Errorf("either --app or --name flag is required")
8496
}
8597

98+
if flags.org != "" && flags.visibility != "selected_orgs" {
99+
return createResp{}, fmt.Errorf("--org can only be used with --visibility=selected_orgs")
100+
}
101+
102+
if flags.visibility == "selected_orgs" && flags.org == "" {
103+
return createResp{}, fmt.Errorf("--org is required when --visibility=selected_orgs")
104+
}
105+
86106
requestBody := createReq{
87107
Name: flags.name,
88108
Visibility: flags.visibility,
109+
OrganizationLogin: flags.org,
89110
EnvironmentVariables: map[string]string{},
90111
Secrets: map[string]string{},
91112
}

cmd/create_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,40 @@ func TestRunCreate_WithVisibility(t *testing.T) {
220220
assert.Equal(t, "github", req.Visibility)
221221
}
222222

223+
func TestRunCreate_OrgWithoutSelectedOrgsVisibility(t *testing.T) {
224+
client := &mockRESTClient{}
225+
_, err := runCreate(client, createCmdFlags{app: "my-app", org: "my-org"})
226+
require.ErrorContains(t, err, "--org can only be used with --visibility=selected_orgs")
227+
228+
_, err = runCreate(client, createCmdFlags{app: "my-app", visibility: "github", org: "my-org"})
229+
require.ErrorContains(t, err, "--org can only be used with --visibility=selected_orgs")
230+
}
231+
232+
func TestRunCreate_SelectedOrgsVisibilityWithoutOrg(t *testing.T) {
233+
client := &mockRESTClient{}
234+
_, err := runCreate(client, createCmdFlags{app: "my-app", visibility: "selected_orgs"})
235+
require.ErrorContains(t, err, "--org is required when --visibility=selected_orgs")
236+
}
237+
238+
func TestRunCreate_WithOrgAndSelectedOrgsVisibility(t *testing.T) {
239+
var capturedBody []byte
240+
client := &mockRESTClient{
241+
putFunc: func(_ string, body io.Reader, resp interface{}) error {
242+
capturedBody, _ = io.ReadAll(body)
243+
buildCreateResponse(createResp{AppUrl: "https://my-app.example.com"}, resp)
244+
return nil
245+
},
246+
}
247+
248+
_, err := runCreate(client, createCmdFlags{app: "my-app", visibility: "selected_orgs", org: "my-org"})
249+
require.NoError(t, err)
250+
251+
var req createReq
252+
json.Unmarshal(capturedBody, &req)
253+
assert.Equal(t, "selected_orgs", req.Visibility)
254+
assert.Equal(t, "my-org", req.OrganizationLogin)
255+
}
256+
223257
func TestRunCreate_ResponseWithID(t *testing.T) {
224258
client := &mockRESTClient{
225259
putFunc: func(_ string, _ io.Reader, resp interface{}) error {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/MakeNowJust/heredoc v1.0.0
77
github.com/cli/go-gh/v2 v2.12.2
88
github.com/spf13/cobra v1.10.1
9+
github.com/stretchr/testify v1.10.0
910
)
1011

1112
require (
@@ -23,7 +24,6 @@ require (
2324
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
2425
github.com/rivo/uniseg v0.4.7 // indirect
2526
github.com/spf13/pflag v1.0.9 // indirect
26-
github.com/stretchr/testify v1.10.0 // indirect
2727
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
2828
golang.org/x/sys v0.31.0 // indirect
2929
golang.org/x/term v0.30.0 // indirect

0 commit comments

Comments
 (0)