Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cf3400180 | ||
|
|
0e230791b4 | ||
|
|
61eacd00a4 |
@@ -6,7 +6,7 @@ require (
|
||||
github.com/BurntSushi/toml v1.6.0
|
||||
github.com/pulumi/pulumi-github/sdk/v6 v6.14.0
|
||||
github.com/pulumi/pulumi/sdk/v3 v3.243.0
|
||||
golang.org/x/term v0.42.0
|
||||
golang.org/x/term v0.43.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -106,13 +106,13 @@ require (
|
||||
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/crypto v0.50.0 // indirect
|
||||
golang.org/x/crypto v0.51.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect
|
||||
golang.org/x/mod v0.35.0 // indirect
|
||||
golang.org/x/net v0.53.0 // indirect
|
||||
golang.org/x/net v0.55.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
golang.org/x/sys v0.43.0 // indirect
|
||||
golang.org/x/text v0.36.0 // indirect
|
||||
golang.org/x/sys v0.45.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/tools v0.44.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 // indirect
|
||||
|
||||
@@ -289,8 +289,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
|
||||
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
|
||||
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
||||
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM=
|
||||
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80=
|
||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
||||
@@ -305,8 +305,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
|
||||
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
|
||||
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
|
||||
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -328,16 +328,16 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
||||
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.42.0 h1:UiKe+zDFmJobeJ5ggPwOshJIVt6/Ft0rcfrXZDLWAWY=
|
||||
golang.org/x/term v0.42.0/go.mod h1:Dq/D+snpsbazcBG5+F9Q1n2rXV8Ma+71xEjTRufARgY=
|
||||
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
|
||||
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
||||
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
||||
@@ -208,7 +208,44 @@ func Run(ctx context.Context, opts *cli.Options) error {
|
||||
default:
|
||||
return fmt.Errorf("unknown action: %s", opts.Action)
|
||||
}
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.RepoMode == cli.RepoModeCreate && opts.Action == cli.ActionApply {
|
||||
if err := setGitRemoteAndPush(opts.Owner, opts.Repo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// setGitRemoteAndPush wires the newly created GitHub repo as the "origin"
|
||||
// remote of the current git working tree and pushes all local commits.
|
||||
// If the working directory is not inside a git repo the step is skipped.
|
||||
func setGitRemoteAndPush(owner, repo string) error {
|
||||
if err := execCommand("git", "rev-parse", "--is-inside-work-tree").Run(); err != nil {
|
||||
fmt.Println(">>> Not inside a git repository; skipping remote setup.")
|
||||
return nil
|
||||
}
|
||||
|
||||
remoteURL := fmt.Sprintf("https://github.com/%s/%s.git", owner, repo)
|
||||
fmt.Printf(">>> Setting git remote origin → %s\n", remoteURL)
|
||||
|
||||
if execCommand("git", "remote", "get-url", "origin").Run() == nil {
|
||||
if out, err := execCommand("git", "remote", "set-url", "origin", remoteURL).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("updating git remote: %s: %w", strings.TrimSpace(string(out)), err)
|
||||
}
|
||||
} else {
|
||||
if out, err := execCommand("git", "remote", "add", "origin", remoteURL).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("adding git remote: %s: %w", strings.TrimSpace(string(out)), err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(">>> Pushing local commits to origin...")
|
||||
if out, err := execCommand("git", "push", "-u", "origin", "HEAD").CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("pushing to remote: %s: %w", strings.TrimSpace(string(out)), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runCreatePrompts asks the user for any missing required --create values
|
||||
|
||||
+178
-25
@@ -40,35 +40,59 @@ func TestHelperProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
command := args[0]
|
||||
if command != "gh" {
|
||||
fmt.Fprintf(os.Stderr, "expected 'gh', got: %s\n", command)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
subCmd := args[1]
|
||||
switch subCmd {
|
||||
case "auth":
|
||||
if len(args) >= 3 && args[2] == "token" {
|
||||
fmt.Print("gh_mock_token\n")
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "unknown auth subcommand\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
case "api":
|
||||
if len(args) >= 3 {
|
||||
path := args[2]
|
||||
switch {
|
||||
case path == "users/octocat":
|
||||
fmt.Print(`{"id":583234}`)
|
||||
case path == "orgs/JMR-dev/teams/release":
|
||||
fmt.Print(`{"id":98765}`)
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown path: %s\n", path)
|
||||
switch command {
|
||||
case "gh":
|
||||
subCmd := args[1]
|
||||
switch subCmd {
|
||||
case "auth":
|
||||
if len(args) >= 3 && args[2] == "token" {
|
||||
fmt.Print("gh_mock_token\n")
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "unknown auth subcommand\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
case "api":
|
||||
if len(args) >= 3 {
|
||||
path := args[2]
|
||||
switch {
|
||||
case path == "users/octocat":
|
||||
fmt.Print(`{"id":583234}`)
|
||||
case path == "orgs/JMR-dev/teams/release":
|
||||
fmt.Print(`{"id":98765}`)
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown path: %s\n", path)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown gh command: %s\n", subCmd)
|
||||
os.Exit(2)
|
||||
}
|
||||
case "git":
|
||||
if os.Getenv("MOCK_GIT_ENABLED") != "1" {
|
||||
fmt.Fprintf(os.Stderr, "git not mocked in this test\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
subCmd := args[1]
|
||||
switch subCmd {
|
||||
case "rev-parse":
|
||||
// success — we're in a mock git repo
|
||||
case "remote":
|
||||
if len(args) >= 3 && args[2] == "get-url" {
|
||||
// Simulate "no origin" by default; set MOCK_GIT_ORIGIN_EXISTS=1 to override.
|
||||
if os.Getenv("MOCK_GIT_ORIGIN_EXISTS") != "1" {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
// add / set-url: exit 0 (success)
|
||||
case "push":
|
||||
// success
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown git subcommand: %s\n", subCmd)
|
||||
os.Exit(2)
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown command: %s\n", subCmd)
|
||||
fmt.Fprintf(os.Stderr, "unknown command: %s\n", command)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
@@ -81,6 +105,24 @@ func mockExec(command string, args ...string) *exec.Cmd {
|
||||
return cmd
|
||||
}
|
||||
|
||||
// mockExecWithGit is like mockExec but also enables the git mock.
|
||||
func mockExecWithGit(command string, args ...string) *exec.Cmd {
|
||||
cs := []string{"-test.run=TestHelperProcess", "--", command}
|
||||
cs = append(cs, args...)
|
||||
cmd := exec.Command(os.Args[0], cs...)
|
||||
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1", "MOCK_GIT_ENABLED=1")
|
||||
return cmd
|
||||
}
|
||||
|
||||
// makeRecordingExec wraps a delegate exec function and records every call.
|
||||
func makeRecordingExec(delegate func(string, ...string) *exec.Cmd) (func(string, ...string) *exec.Cmd, *[][]string) {
|
||||
calls := &[][]string{}
|
||||
return func(name string, args ...string) *exec.Cmd {
|
||||
*calls = append(*calls, append([]string{name}, args...))
|
||||
return delegate(name, args...)
|
||||
}, calls
|
||||
}
|
||||
|
||||
// mockStack implements stackInterface.
|
||||
type mockStack struct {
|
||||
setConfigCalls map[string]string
|
||||
@@ -449,3 +491,114 @@ func TestRun_CreateModeInteractive(t *testing.T) {
|
||||
t.Errorf("expected Description to be 'my repo description', got: %v", opts.RepoSettings.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_CreateMode_SetsRemoteAndPushes(t *testing.T) {
|
||||
oldExec := execCommand
|
||||
oldGithubExec := githubapi.ExecCommand
|
||||
oldUpsert := upsertStack
|
||||
defer func() {
|
||||
execCommand = oldExec
|
||||
githubapi.ExecCommand = oldGithubExec
|
||||
upsertStack = oldUpsert
|
||||
}()
|
||||
|
||||
githubapi.ExecCommand = mockExec
|
||||
|
||||
recorder, calls := makeRecordingExec(mockExecWithGit)
|
||||
execCommand = recorder
|
||||
|
||||
mStack := &mockStack{setConfigCalls: make(map[string]string)}
|
||||
upsertStack = func(ctx context.Context, stackName, projectName string, program pulumi.RunFunc, opts ...auto.LocalWorkspaceOption) (stackInterface, error) {
|
||||
return mStack, nil
|
||||
}
|
||||
|
||||
stateDir := t.TempDir()
|
||||
oldToken := os.Getenv("GITHUB_TOKEN")
|
||||
os.Setenv("GITHUB_TOKEN", "test-token")
|
||||
defer os.Setenv("GITHUB_TOKEN", oldToken)
|
||||
|
||||
desc := "test repo"
|
||||
opts := &cli.Options{
|
||||
Owner: "JMR-dev",
|
||||
Repo: "new-repo",
|
||||
Branch: "main",
|
||||
Action: cli.ActionApply,
|
||||
RepoMode: cli.RepoModeCreate,
|
||||
StateDir: stateDir,
|
||||
RepoSettings: cli.RepoSettings{
|
||||
Visibility: "private",
|
||||
Description: &desc,
|
||||
},
|
||||
}
|
||||
|
||||
if err := Run(context.Background(), opts); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify the expected git command sequence was issued.
|
||||
wantRemoteURL := "https://github.com/JMR-dev/new-repo.git"
|
||||
checkCmd := func(want []string) {
|
||||
t.Helper()
|
||||
for _, c := range *calls {
|
||||
if len(c) == len(want) {
|
||||
match := true
|
||||
for i := range want {
|
||||
if c[i] != want[i] {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if match {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Errorf("expected command %v not found in recorded calls: %v", want, *calls)
|
||||
}
|
||||
|
||||
checkCmd([]string{"git", "rev-parse", "--is-inside-work-tree"})
|
||||
checkCmd([]string{"git", "remote", "get-url", "origin"})
|
||||
checkCmd([]string{"git", "remote", "add", "origin", wantRemoteURL})
|
||||
checkCmd([]string{"git", "push", "-u", "origin", "HEAD"})
|
||||
}
|
||||
|
||||
func TestRun_NonCreateMode_NoRemoteSetup(t *testing.T) {
|
||||
oldExec := execCommand
|
||||
oldUpsert := upsertStack
|
||||
defer func() {
|
||||
execCommand = oldExec
|
||||
upsertStack = oldUpsert
|
||||
}()
|
||||
|
||||
recorder, calls := makeRecordingExec(mockExec)
|
||||
execCommand = recorder
|
||||
|
||||
mStack := &mockStack{setConfigCalls: make(map[string]string)}
|
||||
upsertStack = func(ctx context.Context, stackName, projectName string, program pulumi.RunFunc, opts ...auto.LocalWorkspaceOption) (stackInterface, error) {
|
||||
return mStack, nil
|
||||
}
|
||||
|
||||
stateDir := t.TempDir()
|
||||
oldToken := os.Getenv("GITHUB_TOKEN")
|
||||
os.Setenv("GITHUB_TOKEN", "test-token")
|
||||
defer os.Setenv("GITHUB_TOKEN", oldToken)
|
||||
|
||||
opts := &cli.Options{
|
||||
Owner: "JMR-dev",
|
||||
Repo: "existing-repo",
|
||||
Branch: "main",
|
||||
Action: cli.ActionApply,
|
||||
RepoMode: cli.RepoModeData,
|
||||
StateDir: stateDir,
|
||||
}
|
||||
|
||||
if err := Run(context.Background(), opts); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
for _, c := range *calls {
|
||||
if len(c) > 0 && c[0] == "git" {
|
||||
t.Errorf("expected no git commands for non-create mode, got: %v", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user