Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Use latest version of repository in rit add repo stdin (#685)
Browse files Browse the repository at this point in the history
* Create a new function to get latest tag

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix tests

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Added interface to detail

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Added new detail to list repo

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Removes unusable function

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Add function to use latest version when version not informed on stdin

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix lint

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Add initial tests to stdin

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix tests

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix review

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix tests and remove unesed file

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>

* Fix lint

Signed-off-by: Bruna Tavares <bruna.silva@zup.com.br>
  • Loading branch information
brunasilvazup authored Nov 30, 2020
1 parent 13275a3 commit 89883c0
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 46 deletions.
11 changes: 11 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package mocks

import (
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/rcontext"
"github.com/stretchr/testify/mock"
)
Expand All @@ -30,3 +31,13 @@ func (cf *ContextFinderMock) Find() (rcontext.ContextHolder, error) {

return args.Get(0).(rcontext.ContextHolder), args.Error(1)
}

type DetailManagerMock struct {
mock.Mock
}

func (d *DetailManagerMock) LatestTag(repo formula.Repo) string {
args := d.Called(repo)

return args.String(0)
}
12 changes: 9 additions & 3 deletions pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"

Expand All @@ -45,6 +44,7 @@ type addRepoCmd struct {
prompt.InputInt
tutorial rtutorial.Finder
tree tree.CheckerManager
detail formula.RepositoryDetail
}

func NewAddRepoCmd(
Expand All @@ -58,6 +58,7 @@ func NewAddRepoCmd(
inInt prompt.InputInt,
rtf rtutorial.Finder,
treeChecker tree.CheckerManager,
rd formula.RepositoryDetail,
) *cobra.Command {
addRepo := addRepoCmd{
repo: repo,
Expand All @@ -70,6 +71,7 @@ func NewAddRepoCmd(
InputPassword: inPass,
tutorial: rtf,
tree: treeChecker,
detail: rd,
}
cmd := &cobra.Command{
Use: "repo",
Expand Down Expand Up @@ -199,14 +201,18 @@ func (ad addRepoCmd) runPrompt() CommandRunnerFunc {

func (ad addRepoCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

r := formula.Repo{}

err := stdin.ReadJson(os.Stdin, &r)
err := stdin.ReadJson(cmd.InOrStdin(), &r)
if err != nil {
return err
}

if r.Version.String() == "" {
latestTag := ad.detail.LatestTag(r)
r.Version = formula.RepoVersion(latestTag)
}

if err := ad.repo.Add(r); err != nil {
return err
}
Expand Down
66 changes: 61 additions & 5 deletions pkg/cmd/add_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ package cmd

import (
"errors"
"strings"
"testing"

"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func Test_addRepoCmd_runPrompt(t *testing.T) {
func TestAddRepoCmd(t *testing.T) {
repoProviders := formula.NewRepoProviders()
repoProviders.Add("Github", formula.Git{Repos: defaultGitRepositoryMock, NewRepoInfo: github.NewRepoInfo})

Expand All @@ -39,6 +43,8 @@ func Test_addRepoCmd_runPrompt(t *testing.T) {
InputList prompt.InputList
InputBool prompt.InputBool
InputInt prompt.InputInt
stdin string
detailLatestTag string
}
tests := []struct {
name string
Expand Down Expand Up @@ -163,12 +169,53 @@ func Test_addRepoCmd_runPrompt(t *testing.T) {
},
wantErr: true,
},
{
name: "Run with success when input is stdin",
fields: fields{
repo: defaultRepoAdderMock,
repoProviders: repoProviders,
InputTextValidator: inputTextValidatorMock{},
InputPassword: inputPasswordMock{},
InputURL: inputURLMock{},
InputBool: inputTrueMock{},
InputInt: inputIntMock{},
InputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
return "Github", nil
},
},
stdin: "{\"provider\": \"github\", \"name\": \"repo-name\", \"version\": \"0.0.0\", \"url\": \"https://url.com/repo\", \"token,omitempty\": \"\", \"priority\": 5, \"isLocal\": false}\n",
},
wantErr: false,
},
{
name: "Run with success when input is stdin and version is not informed",
fields: fields{
repo: defaultRepoAdderMock,
repoProviders: repoProviders,
InputTextValidator: inputTextValidatorMock{},
InputPassword: inputPasswordMock{},
InputURL: inputURLMock{},
InputBool: inputTrueMock{},
InputInt: inputIntMock{},
InputList: inputListCustomMock{
list: func(name string, items []string) (string, error) {
return "Github", nil
},
},
stdin: "{\"provider\": \"github\", \"name\": \"repo-name\", \"version\": \"\", \"url\": \"https://url.com/repo\", \"token,omitempty\": \"\", \"priority\": 5, \"isLocal\": false}\n",
detailLatestTag: "1.0.0",
},
wantErr: false,
},
}
checkerManager := tree.NewChecker(treeMock{})

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := NewAddRepoCmd(
detailMock := new(mocks.DetailManagerMock)
detailMock.On("LatestTag", mock.Anything).Return(tt.fields.detailLatestTag)
cmd := NewAddRepoCmd(
tt.fields.repo,
tt.fields.repoProviders,
tt.fields.InputTextValidator,
Expand All @@ -179,11 +226,20 @@ func Test_addRepoCmd_runPrompt(t *testing.T) {
tt.fields.InputInt,
TutorialFinderMock{},
checkerManager,
detailMock,
)
o.PersistentFlags().Bool("stdin", false, "input by stdin")
if err := o.Execute(); (err != nil) != tt.wantErr {
t.Errorf("init_runPrompt() error = %v, wantErr %v", err, tt.wantErr)

if tt.fields.stdin != "" {
newReader := strings.NewReader(tt.fields.stdin)
cmd.SetIn(newReader)
cmd.PersistentFlags().Bool("stdin", true, "input by stdin")
} else {
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
}

err := cmd.Execute()

assert.Equal(t, tt.wantErr, (err != nil))
})
}
}
22 changes: 7 additions & 15 deletions pkg/cmd/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ type listRepoCmd struct {
formula.RepositoryLister
repoProviders formula.RepoProviders
rt rtutorial.Finder
detail formula.RepositoryDetail
}

func NewListRepoCmd(rl formula.RepositoryLister, rp formula.RepoProviders, rtf rtutorial.Finder) *cobra.Command {
lr := listRepoCmd{rl, rp, rtf}
func NewListRepoCmd(rl formula.RepositoryLister, rp formula.RepoProviders, rtf rtutorial.Finder, rd formula.RepositoryDetail) *cobra.Command {
lr := listRepoCmd{rl, rp, rtf, rd}
cmd := &cobra.Command{
Use: "repo",
Short: "Show a list with all your available repositories",
Expand Down Expand Up @@ -81,7 +82,10 @@ func (lr listRepoCmd) printRepos(repos formula.Repos) {
for _, repo := range repos {
latestTag := "0.0.0"
if !repo.IsLocal {
latestTag = lr.getLatestTag(repo)
latestTag = lr.detail.LatestTag(repo)
if len(latestTag) == 0 {
latestTag = "Couldn't get that information"
}
}

table.AddRow(repo.Provider, repo.Name, repo.Version, repo.Priority, repo.Url, latestTag)
Expand All @@ -92,18 +96,6 @@ func (lr listRepoCmd) printRepos(repos formula.Repos) {

}

func (lr listRepoCmd) getLatestTag(repo formula.Repo) string {
formulaGit := lr.repoProviders.Resolve(repo.Provider)

repoInfo := formulaGit.NewRepoInfo(repo.Url, repo.Token)
tag, err := formulaGit.Repos.LatestTag(repoInfo)
if err != nil {
return "Couldn't get that information"
}

return tag.Name
}

func tutorialListRepo(tutorialStatus string) {
const tagTutorial = "\n[TUTORIAL]"
const MessageTitle = "To update all repositories or delete an repository:"
Expand Down
27 changes: 19 additions & 8 deletions pkg/cmd/list_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"errors"
"testing"

"github.com/ZupIT/ritchie-cli/internal/mocks"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
"github.com/stretchr/testify/mock"
)

func TestListRepoRunFunc(t *testing.T) {
Expand All @@ -32,6 +34,7 @@ func TestListRepoRunFunc(t *testing.T) {
RepositoryLister formula.RepositoryLister
Repos git.Repositories
Tutorial rtutorial.Finder
detailLatestTag string
}
tests := []struct {
name string
Expand All @@ -53,8 +56,9 @@ func TestListRepoRunFunc(t *testing.T) {
}, nil
},
},
Tutorial: TutorialFinderMockReturnDisabled{},
Repos: defaultGitRepositoryMock,
Tutorial: TutorialFinderMockReturnDisabled{},
Repos: defaultGitRepositoryMock,
detailLatestTag: "2.0.0",
},
wantErr: false,
},
Expand All @@ -79,8 +83,9 @@ func TestListRepoRunFunc(t *testing.T) {
}, nil
},
},
Tutorial: TutorialFinderMockReturnDisabled{},
Repos: defaultGitRepositoryMock,
Tutorial: TutorialFinderMockReturnDisabled{},
Repos: defaultGitRepositoryMock,
detailLatestTag: "2.0.0",
},
wantErr: false,
},
Expand All @@ -99,8 +104,9 @@ func TestListRepoRunFunc(t *testing.T) {
}, nil
},
},
Tutorial: TutorialFinderMock{},
Repos: defaultGitRepositoryMock,
Tutorial: TutorialFinderMock{},
Repos: defaultGitRepositoryMock,
detailLatestTag: "2.0.0",
},
wantErr: false,
},
Expand Down Expand Up @@ -138,6 +144,7 @@ func TestListRepoRunFunc(t *testing.T) {
return git.Tag{}, someError
},
},
detailLatestTag: "",
},
wantErr: false,
},
Expand All @@ -161,17 +168,21 @@ func TestListRepoRunFunc(t *testing.T) {
return rtutorial.TutorialHolder{}, someError
},
},
Repos: defaultGitRepositoryMock,
Repos: defaultGitRepositoryMock,
detailLatestTag: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
detailMock := new(mocks.DetailManagerMock)
detailMock.On("LatestTag", mock.Anything).Return(tt.in.detailLatestTag)

repoProviders := formula.NewRepoProviders()
repoProviders.Add("Github", formula.Git{Repos: tt.in.Repos, NewRepoInfo: github.NewRepoInfo})

lr := NewListRepoCmd(tt.in.RepositoryLister, repoProviders, tt.in.Tutorial)
lr := NewListRepoCmd(tt.in.RepositoryLister, repoProviders, tt.in.Tutorial, detailMock)
lr.PersistentFlags().Bool("stdin", false, "input by stdin")
if err := lr.Execute(); (err != nil) != tt.wantErr {
t.Errorf("setCredentialCmd_runPrompt() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
6 changes: 4 additions & 2 deletions pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func Build() *cobra.Command {
repoDeleter := repo.NewDeleter(ritchieHomeDir, repoListWriter, dirManager)
repoPrioritySetter := repo.NewPrioritySetter(repoListWriter)

detailRepo := repo.NewDetail(repoProviders)

tplManager := template.NewManager(api.RitchieHomeDir(), dirManager)
ctxFinder := rcontext.NewFinder(ritchieHomeDir, fileManager)
ctxSetter := rcontext.NewSetter(ritchieHomeDir, ctxFinder)
Expand Down Expand Up @@ -220,9 +222,9 @@ func Build() *cobra.Command {
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, repoProviders, inputTextValidator, inputPassword, inputURL, inputList, inputBool, inputInt, tutorialFinder, treeChecker)
addRepoCmd := cmd.NewAddRepoCmd(repoAddLister, repoProviders, inputTextValidator, inputPassword, inputURL, inputList, inputBool, inputInt, tutorialFinder, treeChecker, detailRepo)
updateRepoCmd := cmd.NewUpdateRepoCmd(http.DefaultClient, repoListUpdater, repoProviders, inputText, inputPassword, inputURL, inputList, inputBool, inputInt)
listRepoCmd := cmd.NewListRepoCmd(repoLister, repoProviders, tutorialFinder)
listRepoCmd := cmd.NewListRepoCmd(repoLister, repoProviders, tutorialFinder, detailRepo)
deleteRepoCmd := cmd.NewDeleteRepoCmd(repoLister, inputList, repoDeleter)
listWorkspaceCmd := cmd.NewListWorkspaceCmd(formulaWorkspace, tutorialFinder)
setPriorityCmd := cmd.NewSetPriorityCmd(inputList, inputInt, repoLister, repoPrioritySetter)
Expand Down
4 changes: 4 additions & 0 deletions pkg/formula/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ type RepositoryWriter interface {
Write(repos Repos) error
}

type RepositoryDetail interface {
LatestTag(repo Repo) string
}

type RepositoryAddLister interface {
RepositoryAdder
RepositoryLister
Expand Down
41 changes: 41 additions & 0 deletions pkg/formula/repo/detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package repo

import (
"github.com/ZupIT/ritchie-cli/pkg/formula"
)

type DetailManager struct {
repoProviders formula.RepoProviders
}

func NewDetail(repoProviders formula.RepoProviders) DetailManager {
return DetailManager{repoProviders}
}

func (dm DetailManager) LatestTag(repo formula.Repo) string {
formulaGit := dm.repoProviders.Resolve(repo.Provider)

repoInfo := formulaGit.NewRepoInfo(repo.Url, repo.Token)
tag, err := formulaGit.Repos.LatestTag(repoInfo)
if err != nil {
return ""
}

return tag.Name
}
Loading

0 comments on commit 89883c0

Please sign in to comment.