Skip to content

Commit

Permalink
Testing a wrapper for github (#166)
Browse files Browse the repository at this point in the history
* Testing a wrapper for github

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* Testing a wrapper for github

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* Testing a wrapper for github

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

* Testing a wrapper for github

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>
  • Loading branch information
6za committed Aug 2, 2022
1 parent d6d5dee commit c694c12
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
36 changes: 3 additions & 33 deletions cmd/createGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
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
Expand All @@ -29,14 +25,15 @@ var createGithubCmd = &cobra.Command{
progressPrinter.GetInstance()
progressPrinter.SetupProgress(4)
config := configs.ReadConfig()
gitWrapper := githubWrapper.New()
log.Printf(config.AwsProfile)
infoCmd.Run(cmd, args)

progressPrinter.AddTracker("step-0", "Test Installer ", 4)
//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")
Expand Down Expand Up @@ -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())
}
66 changes: 66 additions & 0 deletions internal/githubWrapper/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package githubWrapper

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
}

// Create a new client for github wrapper
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

}

// 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")
}
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 {
return fmt.Errorf("error creating private repo: %s - %s", name, err)
}
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
}

0 comments on commit c694c12

Please sign in to comment.