From cb1af0c3e928500d6f3f33164386c4ee0faec024 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:52:09 +0000 Subject: [PATCH 1/4] Testing a wrapper for github Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- internal/githubWrapper/github.go | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 internal/githubWrapper/github.go diff --git a/internal/githubWrapper/github.go b/internal/githubWrapper/github.go new file mode 100644 index 000000000..237278391 --- /dev/null +++ b/internal/githubWrapper/github.go @@ -0,0 +1,50 @@ +package githupWrapper + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + + "github.com/google/go-github/v45/github" + "golang.org/x/oauth2" +) + +type githubSession struct { + context context.Context + staticToken oauth2.TokenSource + oauthClient *http.Client + gitClient *github.Client +} + +func New() githubSession { + token := os.Getenv("GITHUB_AUTH_TOKEN") + if token == "" { + log.Fatal("Unauthorized: No token present") + } + var gSession githubSession + gSession.context = context.Background() + gSession.staticToken = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) + gSession.oauthClient = oauth2.NewClient(gSession.context, gSession.staticToken) + gSession.gitClient = github.NewClient(gSession.oauthClient) + return gSession + +} + +func (g githubSession) createPrivateRepo(org string, name string, description string) { + if name == "" { + log.Fatal("No name: New repos must be given a name") + } + isPrivate := true + autoInit := true + r := &github.Repository{Name: &name, + Private: &isPrivate, + Description: &description, + AutoInit: &autoInit} + repo, _, err := g.gitClient.Repositories.Create(g.context, org, r) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) +} From 8b1121e8ac3d684beeb9457649c171ce6feff21f Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:57:28 +0000 Subject: [PATCH 2/4] Testing a wrapper for github Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- internal/githubWrapper/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/githubWrapper/github.go b/internal/githubWrapper/github.go index 237278391..f034e9224 100644 --- a/internal/githubWrapper/github.go +++ b/internal/githubWrapper/github.go @@ -1,4 +1,4 @@ -package githupWrapper +package githubWrapper import ( "context" From b987fa4ee9d7c78e72458d37edcc53f6e1f98879 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 27 Jul 2022 19:10:03 +0000 Subject: [PATCH 3/4] Testing a wrapper for github Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- cmd/createGithub.go | 36 +++----------------------------- internal/githubWrapper/github.go | 13 ++++++------ 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/cmd/createGithub.go b/cmd/createGithub.go index 0ec17bbff..f3744b030 100644 --- a/cmd/createGithub.go +++ b/cmd/createGithub.go @@ -5,18 +5,14 @@ Copyright © 2022 NAME HERE package cmd import ( - "context" "fmt" "log" - "os" "time" "github.com/kubefirst/kubefirst/configs" + "github.com/kubefirst/kubefirst/internal/githubWrapper" "github.com/kubefirst/kubefirst/internal/progressPrinter" "github.com/spf13/cobra" - "golang.org/x/oauth2" - - "github.com/google/go-github/v45/github" ) // createGithubCmd represents the createGithub command @@ -29,6 +25,7 @@ var createGithubCmd = &cobra.Command{ progressPrinter.GetInstance() progressPrinter.SetupProgress(4) config := configs.ReadConfig() + gitWrapper := githubWrapper.New() log.Printf(config.AwsProfile) infoCmd.Run(cmd, args) @@ -36,7 +33,7 @@ var createGithubCmd = &cobra.Command{ //sendStartedInstallTelemetry(dryRun, useTelemetry) informUser("Create Github Org") informUser("Create Github Repo - gitops") - createRepo("gitops") + gitWrapper.CreatePrivateRepo("", "sample", "My First App") //gitlab.PushGitRepo(dryRun, config, "gitlab", "metaphor") // make a github version of it informUser("Create Github Repo - metaphor") @@ -74,30 +71,3 @@ func init() { clusterCmd.AddCommand(createGithubCmd) } - -func createRepo(name string) { - token := os.Getenv("GITHUB_AUTH_TOKEN") - if token == "" { - log.Fatal("Unauthorized: No token present") - } - if name == "" { - log.Fatal("No name: New repos must be given a name") - } - ctx := context.Background() - ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) - tc := oauth2.NewClient(ctx, ts) - client := github.NewClient(tc) - isPrivate := true - autoInit := true - description := "sample" - organization := os.Getenv("ORG") - r := &github.Repository{Name: &name, - Private: &isPrivate, - Description: &description, - AutoInit: &autoInit} - repo, _, err := client.Repositories.Create(ctx, organization, r) - if err != nil { - log.Fatal(err) - } - fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) -} diff --git a/internal/githubWrapper/github.go b/internal/githubWrapper/github.go index f034e9224..6f5b1327d 100644 --- a/internal/githubWrapper/github.go +++ b/internal/githubWrapper/github.go @@ -11,19 +11,19 @@ import ( "golang.org/x/oauth2" ) -type githubSession struct { +type GithubSession struct { context context.Context staticToken oauth2.TokenSource oauthClient *http.Client gitClient *github.Client } -func New() githubSession { +func New() GithubSession { token := os.Getenv("GITHUB_AUTH_TOKEN") if token == "" { log.Fatal("Unauthorized: No token present") } - var gSession githubSession + var gSession GithubSession gSession.context = context.Background() gSession.staticToken = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) gSession.oauthClient = oauth2.NewClient(gSession.context, gSession.staticToken) @@ -32,7 +32,7 @@ func New() githubSession { } -func (g githubSession) createPrivateRepo(org string, name string, description string) { +func (g GithubSession) CreatePrivateRepo(org string, name string, description string) error { if name == "" { log.Fatal("No name: New repos must be given a name") } @@ -44,7 +44,8 @@ func (g githubSession) createPrivateRepo(org string, name string, description st AutoInit: &autoInit} repo, _, err := g.gitClient.Repositories.Create(g.context, org, r) if err != nil { - log.Fatal(err) + return fmt.Errorf("error creating private repo: %s - %s", name, err) } - fmt.Printf("Successfully created new repo: %v\n", repo.GetName()) + log.Printf("Successfully created new repo: %v\n", repo.GetName()) + return nil } From ceb4b885929bd16109f63d0625b1528a02313dc2 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 27 Jul 2022 19:16:18 +0000 Subject: [PATCH 4/4] Testing a wrapper for github Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- internal/githubWrapper/github.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/githubWrapper/github.go b/internal/githubWrapper/github.go index 6f5b1327d..0bdd0a33e 100644 --- a/internal/githubWrapper/github.go +++ b/internal/githubWrapper/github.go @@ -18,6 +18,7 @@ type GithubSession struct { gitClient *github.Client } +// Create a new client for github wrapper func New() GithubSession { token := os.Getenv("GITHUB_AUTH_TOKEN") if token == "" { @@ -32,6 +33,7 @@ func New() GithubSession { } +// Use github API to create a private repo func (g GithubSession) CreatePrivateRepo(org string, name string, description string) error { if name == "" { log.Fatal("No name: New repos must be given a name") @@ -49,3 +51,16 @@ func (g GithubSession) CreatePrivateRepo(org string, name string, description st log.Printf("Successfully created new repo: %v\n", repo.GetName()) return nil } + +// Add ssh keys to a user account to allow kubefirst installer +// to use its own token during installation +func (g GithubSession) AddSSHKey(publicKey string) error { + log.Printf("Add SSH key to user account on behalf of kubefrist") + return nil +} + +// Verify if a repo exists +func (g GithubSession) IsRepoInUse(org string, name string) (bool, error) { + log.Printf("Add SSH key to user account on behalf of kubefrist") + return false, nil +}