Skip to content

Commit

Permalink
Added option to push to shared projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanHabic committed Nov 12, 2019
1 parent ea9287a commit 9999186
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
15 changes: 13 additions & 2 deletions commands/init.go
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"errors"
"fmt"
"github.com/manifoldco/promptui"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -79,7 +80,7 @@ var initCmd = &cobra.Command{
}

config.SetProjectConfig(config.ProjectSlug, project.Slug)
config.SetProjectConfig(config.AccountID, config.GetString(config.AccountID))
config.SetProjectConfig(config.AccountID, project.Owner)
WriteProjectConfig()

logrus.Info(colorizer.Sprintf("Project successfully initialized. "+
Expand Down Expand Up @@ -132,7 +133,17 @@ func promptProjectSelect(projects []*model.Project, rest *rest.Rest) *model.Proj
var projectNames []string
projectNames = append(projectNames, "Create new project")
for _, project := range projects {
projectNames = append(projectNames, project.Name)
var label string
if !project.IsShared {
label = project.Name
} else {
if project.Permissions == nil || !project.Permissions.AddContract {
continue
}
label = fmt.Sprintf("%s (shared project)", project.Name)
}

projectNames = append(projectNames, label)
}

promptProjects := promptui.Select{
Expand Down
12 changes: 10 additions & 2 deletions commands/push.go
Expand Up @@ -185,10 +185,17 @@ func uploadContracts(rest *rest.Rest) error {
)
}

username := config.GetString(config.Username)
if strings.Contains(projectSlug, "/") {
projectInfo := strings.Split(projectSlug, "/")
username = projectInfo[0]
projectSlug = projectInfo[1]
}

logrus.Info(colorizer.Sprintf(
"Successfully pushed Smart Contracts for project %s. You can view your contracts at %s\n",
colorizer.Bold(colorizer.Green(projectSlug)),
colorizer.Bold(colorizer.Green(fmt.Sprintf("https://dashboard.tenderly.dev/%s/%s/contracts", config.GetString(config.Username), projectSlug))),
colorizer.Bold(colorizer.Green(fmt.Sprintf("https://dashboard.tenderly.dev/%s/%s/contracts", username, projectSlug))),
))
}

Expand All @@ -212,7 +219,7 @@ func getProjectConfiguration() (ProjectConfigurationMap, error) {
singleConfigMap, ok := projectConfig.(map[string]interface{})
if !ok {
projectConfigurationMap[projectSlug] = &ProjectConfiguration{}
logrus.Debugf("Invalid configuration provided for project: %s", projectSlug)
logrus.Debugf("No configuration provided for project: %s", projectSlug)
continue
}

Expand All @@ -221,6 +228,7 @@ func getProjectConfiguration() (ProjectConfigurationMap, error) {
logrus.Debugf("failed extracting networks for project: %s", projectSlug)
continue
}

projectConfig := &ProjectConfiguration{}

for _, network := range networks {
Expand Down
11 changes: 10 additions & 1 deletion config/config.go
Expand Up @@ -99,6 +99,15 @@ func GetString(key string) string {
return getString(key)
}

func GetGlobalString(key string) string {
if !globalConfig.IsSet(key) {
fmt.Printf("Could not find value for config: %s\n", key)
os.Exit(1)
}

return globalConfig.GetString(key)
}

func MaybeGetString(key string) string {
return getString(key)
}
Expand All @@ -120,7 +129,7 @@ func IsLoggedIn() bool {
}

func IsProjectInit() bool {
return getString(ProjectSlug) != "" || len(MaybeGetMap(Projects)) >= 0
return getString(ProjectSlug) != "" || len(MaybeGetMap(Projects)) > 0
}

func SetProjectConfig(key string, value interface{}) {
Expand Down
12 changes: 12 additions & 0 deletions model/project.go
Expand Up @@ -4,15 +4,27 @@ import "time"

type AccountID string

func (a AccountID) String() string {
return string(a)
}

type ProjectID struct {
AccountID AccountID
Name string
}

type ProjectPermissions struct {
AddContract bool `json:"add_contract"`
}

type Project struct {
ID AccountID `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Owner AccountID `json:"owner_id"`
CreatedAt time.Time `json:"created_at"`

Permissions *ProjectPermissions `json:"permissions,omitempty"`

IsShared bool `json:"-"`
}
10 changes: 9 additions & 1 deletion rest/call/contract.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/tenderly/tenderly-cli/model"
"github.com/tenderly/tenderly-cli/rest/client"
"github.com/tenderly/tenderly-cli/rest/payloads"
"strings"
)

type ContractCalls struct {
Expand All @@ -21,11 +22,18 @@ func (rest *ContractCalls) UploadContracts(request payloads.UploadContractsReque
return nil, err
}

accountID := config.GetGlobalString(config.AccountID)
if strings.Contains(projectSlug, "/") {
projectInfo := strings.Split(projectSlug, "/")
accountID = projectInfo[0]
projectSlug = projectInfo[1]
}

var contracts *payloads.UploadContractsResponse

response := client.Request(
"POST",
"api/v1/account/"+config.GetString(config.AccountID)+"/project/"+projectSlug+"/contracts",
"api/v1/account/"+accountID+"/project/"+projectSlug+"/contracts",
uploadJson,
)

Expand Down
8 changes: 7 additions & 1 deletion rest/call/project.go
Expand Up @@ -55,7 +55,7 @@ func (rest *ProjectCalls) GetProjects(accountId string) (*payloads.GetProjectsRe
var getProjectsResponse payloads.GetProjectsResponse
response := client.Request(
"GET",
"api/v1/account/"+accountId+"/projects",
"api/v1/account/"+accountId+"/projects?withShared=true",
nil,
)

Expand All @@ -70,5 +70,11 @@ func (rest *ProjectCalls) GetProjects(accountId string) (*payloads.GetProjectsRe
return nil, fmt.Errorf("failed parsing get projects respose: %s", err)
}

for _, project := range getProjectsResponse.Projects {
if string(project.Owner) != accountId {
project.IsShared = true
}
}

return &getProjectsResponse, nil
}

0 comments on commit 9999186

Please sign in to comment.