Skip to content

Commit

Permalink
🔖 Connector & Harness Code Added
Browse files Browse the repository at this point in the history
  • Loading branch information
ritek01 committed May 28, 2024
1 parent 730103b commit ba238d1
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 1 deletion.
21 changes: 20 additions & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"harness/defaults"
"harness/globals"
. "harness/utils"
"os"
"strings"
Expand Down Expand Up @@ -76,8 +77,26 @@ func DeployProject(c *cli.Context) error {
return err
}

//CheckConnectorExistsAndCreate(c, orgName, projectName)
globals.OrgId = orgName
globals.ProjectId = projectName

_, err = DockerConnector(c)
if err != nil {
return err
}

fmt.Print("Do you want to use the Harness Code Repository for code hosting? (y/n): ")
var useHarnessRepo string
fmt.Scanln(&useHarnessRepo)

if useHarnessRepo == "y" {
err = UploadToHarnessCodeRepo()
if err != nil {
return err
}
} else {
fmt.Println("Feature not supported yet.")
}
fmt.Println("Deployment process initialized.")

return nil
Expand Down
2 changes: 2 additions & 0 deletions globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ var (
AccountId string
BaseURL string
UserId string
OrgId string
ProjectId string
)
36 changes: 36 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,39 @@ type ConnectorResponse struct {
type ConnectorBody struct {
Conn Connector `json:"connector"`
}

type CreateRepoRequest struct {
DefaultBranch string `json:"default_branch"`
Description string `json:"description"`
GitIgnore string `json:"git_ignore"`
IsPublic bool `json:"is_public"`
License string `json:"license"`
Uid string `json:"uid"`
Readme bool `json:"readme"`
ParentRef string `json:"parent_ref"`
}

type CreateRepoResponse struct {
ID int `json:"id"`
ParentID int `json:"parent_id"`
Identifier string `json:"identifier"`
Path string `json:"path"`
Description string `json:"description"`
IsPublic bool `json:"is_public"`
CreatedBy int `json:"created_by"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Size int `json:"size"`
SizeUpdated int `json:"size_updated"`
DefaultBranch string `json:"default_branch"`
ForkID int `json:"fork_id"`
NumForks int `json:"num_forks"`
NumPulls int `json:"num_pulls"`
NumClosedPulls int `json:"num_closed_pulls"`
NumOpenPulls int `json:"num_open_pulls"`
NumMergedPulls int `json:"num_merged_pulls"`
Importing bool `json:"importing"`
IsEmpty bool `json:"is_empty"`
GitURL string `json:"git_url"`
UID string `json:"uid"`
}
53 changes: 53 additions & 0 deletions utils/connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package utils

import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"os"
"strings"
"time"
)

func DockerConnector(c *cli.Context) (bool, error) {
dockerConnName := promptUser("Enter Docker Connector Name", "DockerConnector")
progressBar(dockerConnName)
color.Set(color.FgGreen)
fmt.Printf("\nDocker Connector : %s is present in the account\n", dockerConnName)
color.Unset()
return true, nil
}

func K8sConnector(c *cli.Context) (bool, error) {
K8sConnName := promptUser("Enter K8s Connector Name", "K8sConnector")
progressBar(K8sConnName)
color.Set(color.FgGreen)
fmt.Printf("\nK8s Connector %s is present in the account\n", K8sConnName)
color.Unset()
return true, nil
}

func promptUser(prompt, defaultValue string) string {
fmt.Printf("%s: ", prompt)
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)

if input == "" {
return defaultValue
}
return input
}
func progressBar(connectorName string) {
barLength := 20
spinChars := []string{"|", "/", "-", "\\"}
for i := 0; i <= barLength; i++ {
spinner := spinChars[i%len(spinChars)]
progress := strings.Repeat("=", i) + strings.Repeat(" ", barLength-i)
color.Set(color.FgCyan)
fmt.Printf("\r[%s] %s %s", progress, spinner, "Checking... `"+connectorName+"` is present in the account...")
color.Unset()
time.Sleep(time.Millisecond * 65)
}
}
137 changes: 137 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package utils

import (
"fmt"
"harness/globals"
"harness/netclient"
. "harness/types"
"os"
"os/exec"
"path/filepath"
)

func UploadToHarnessCodeRepo() error {
if !isGitInitialized() {
fmt.Println("Git is not initialized. Initializing...")
if err := initializeGitRepo(); err != nil {
return fmt.Errorf("failed to initialize git repository: %v", err)
}
} else {
fmt.Println("Git is already initialized.")
}

branch, err := getCurrentBranch()
if err != nil {
return err
}
folderName, err := getCurrentDirectoryName()
spacePath := globals.AccountId + "/" + globals.OrgId + "/" + globals.ProjectId
// Define repo details
repoDetails := CreateRepoRequest{
DefaultBranch: branch,
Description: "My new Harness repo",
GitIgnore: "",
IsPublic: false,
License: "",
Uid: folderName,
Readme: false,
ParentRef: spacePath,
}

respBody, err := createHarnessRepo(spacePath, repoDetails)
if err != nil {
fmt.Println("Error creating Harness repository:", err)
return nil
}
fmt.Println("Repository created successfully:", respBody)

repoURL := "https://git.harness.io/" + spacePath + "/" + folderName + ".git"
if err := addRemoteAndPush(repoURL, branch); err != nil {
return fmt.Errorf("failed to push to Harness repository: %v", err)
}

fmt.Println("Project successfully pushed to Harness Code Repository.")
return nil
}

func isGitInitialized() bool {
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
err := cmd.Run()
return err == nil
}

func initializeGitRepo() error {
cmds := [][]string{
{"git", "init"},
{"git", "add", "."},
{"git", "commit", "-m", "Project Init"},
}

for _, cmd := range cmds {
if err := runCommand(cmd[0], cmd[1:]...); err != nil {
return err
}
}

return nil
}

func getCurrentBranch() (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get current branch: %v", err)
}
return string(output), nil
}

func addRemoteAndPush(repoURL, branch string) error {
cmds := [][]string{
{"git", "remote", "add", "code", repoURL},
{"git", "push", "code", branch},
}

for _, cmd := range cmds {
if err := runCommand(cmd[0], cmd[1:]...); err != nil {
return err
}
}

return nil
}

func runCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if name == "git" && (args[0] == "push" || args[0] == "pull") {
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_USERNAME=%s", globals.UserId))
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_PASSWORD=%s", globals.ApiKey))
}

if err := cmd.Run(); err != nil {
return fmt.Errorf("command %s %v failed: %v", name, args, err)
}
return nil
}

func createHarnessRepo(spacePath string, repoDetails CreateRepoRequest) (ResponseBody, error) {
reqUrl := fmt.Sprintf("https://app.harness.io/gateway/code/api/v1/repos?routingId=%s&space_path=%s", globals.AccountId, spacePath)
contentType := "application/json"

respBody, err := netclient.Post(reqUrl, globals.ApiKey, repoDetails, contentType, nil)
if err != nil {
return ResponseBody{}, fmt.Errorf("failed to create Harness repository: %v", err)
}

return respBody, nil
}

func getCurrentDirectoryName() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Base(dir), nil
}

0 comments on commit ba238d1

Please sign in to comment.