diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 76a9ad955..a01b5aed7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @kaduartur @victor-schumacher @victorschumacherzup @brunasilvazup @lucasdittrichzup @henriquemoraeszup @rodrigomedeirosf @ernelio +* @kaduartur @brunasilvazup @lucasdittrichzup @henriquemoraeszup @rodrigomedeirosf @ernelio @fernandobelettizup diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 4c4c854ab..b95441df7 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -2,6 +2,7 @@ Bruna Tavares Ernélio Júnior +Fernando Beletti Gabriel Pinheiro Guillaume Falourd Harirai Mahajan diff --git a/pkg/git/bitbucket/repository.go b/pkg/git/bitbucket/repository.go index 872d0c6de..43154fd83 100644 --- a/pkg/git/bitbucket/repository.go +++ b/pkg/git/bitbucket/repository.go @@ -69,9 +69,8 @@ func (re RepoManager) Tags(info git.RepoInfo) (git.Tags, error) { defer res.Body.Close() - if res.StatusCode != http.StatusOK { - all, _ := ioutil.ReadAll(res.Body) - return git.Tags{}, errors.New(res.Status + "-" + string(all)) + if err := git.CheckStatusCode(res); err != nil { + return git.Tags{}, err } var bTags bitbucketTags diff --git a/pkg/git/bitbucket/repository_test.go b/pkg/git/bitbucket/repository_test.go index ae8a3e238..be0b095d8 100644 --- a/pkg/git/bitbucket/repository_test.go +++ b/pkg/git/bitbucket/repository_test.go @@ -33,6 +33,12 @@ func TestTags(t *testing.T) { mockServerThatFail := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { writer.WriteHeader(http.StatusBadRequest) })) + mockServerNotFound := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusNotFound) + })) + mockServerForbidden := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusForbidden) + })) tests := []struct { name string @@ -74,6 +80,24 @@ func TestTags(t *testing.T) { want: git.Tags{}, wantErr: "Get \"\": unsupported protocol scheme \"\"", }, + { + name: "Return err when repo not found", + client: mockServerNotFound.Client(), + info: info{ + tagsUrl: mockServerNotFound.URL, + }, + want: git.Tags{}, + wantErr: git.ErrRepoNotFound.Error(), + }, + { + name: "Return err when repo access denied", + client: mockServerForbidden.Client(), + info: info{ + tagsUrl: mockServerForbidden.URL, + }, + want: git.Tags{}, + wantErr: git.ErrRepoNotFound.Error(), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/git/git.go b/pkg/git/git.go index a09b49bca..24bafd299 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -15,7 +15,18 @@ */ package git -import "io" +import ( + "errors" + "io" + "io/ioutil" + "net/http" +) + +var ErrRepoNotFound = errors.New( + `could not retrieve new versions for selected repository +Please check if it still exists or changed visiblity +Try adding it again using: +rit add repo`) type Tag struct { Name string `json:"tag_name"` @@ -33,6 +44,17 @@ func (t Tags) Names() []string { return tags } +func CheckStatusCode(res *http.Response) (err error) { + if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusForbidden { + return ErrRepoNotFound + } else if res.StatusCode != http.StatusOK { + all, _ := ioutil.ReadAll(res.Body) + return errors.New(res.Status + "-" + string(all)) + } + + return nil +} + type RepoInfo interface { ZipUrl(version string) string TagsUrl() string diff --git a/pkg/git/github/repository.go b/pkg/git/github/repository.go index b33784851..4cfc33eac 100644 --- a/pkg/git/github/repository.go +++ b/pkg/git/github/repository.go @@ -60,9 +60,9 @@ func (re RepoManager) Tags(info git.RepoInfo) (git.Tags, error) { } defer res.Body.Close() - if res.StatusCode != http.StatusOK { - all, _ := ioutil.ReadAll(res.Body) - return git.Tags{}, errors.New(res.Status + "-" + string(all)) + + if err := git.CheckStatusCode(res); err != nil { + return git.Tags{}, err } var tags git.Tags diff --git a/pkg/git/github/repository_test.go b/pkg/git/github/repository_test.go index da741bf11..42840a1e9 100644 --- a/pkg/git/github/repository_test.go +++ b/pkg/git/github/repository_test.go @@ -34,6 +34,9 @@ func TestTags(t *testing.T) { mockServerThatFail := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { writer.WriteHeader(http.StatusBadRequest) })) + mockServerNotFound := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusNotFound) + })) tests := []struct { name string @@ -74,6 +77,15 @@ func TestTags(t *testing.T) { want: git.Tags{}, wantErr: "Get \"htttp://yourhost.com/username/repo/\": unsupported protocol scheme \"htttp\"", }, + { + name: "Return err when repo not found", + client: mockServerNotFound.Client(), + info: info{ + tagsUrl: mockServerNotFound.URL, + }, + want: git.Tags{}, + wantErr: git.ErrRepoNotFound.Error(), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/git/gitlab/repository.go b/pkg/git/gitlab/repository.go index 51bcc6c3c..9a4c2b920 100644 --- a/pkg/git/gitlab/repository.go +++ b/pkg/git/gitlab/repository.go @@ -60,9 +60,8 @@ func (re RepoManager) Tags(info git.RepoInfo) (git.Tags, error) { defer res.Body.Close() - if res.StatusCode != http.StatusOK { - all, _ := ioutil.ReadAll(res.Body) - return git.Tags{}, errors.New(res.Status + "-" + string(all)) + if err := git.CheckStatusCode(res); err != nil { + return git.Tags{}, err } var tags git.Tags diff --git a/pkg/git/gitlab/repository_test.go b/pkg/git/gitlab/repository_test.go index 5ab583a26..d194f2836 100644 --- a/pkg/git/gitlab/repository_test.go +++ b/pkg/git/gitlab/repository_test.go @@ -33,6 +33,9 @@ func TestTags(t *testing.T) { mockServerThatFail := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { writer.WriteHeader(http.StatusBadRequest) })) + mockServerNotFound := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusNotFound) + })) tests := []struct { name string @@ -74,6 +77,15 @@ func TestTags(t *testing.T) { want: git.Tags{}, wantErr: "Get \"htttp://yourhost.com/username/repo/\": unsupported protocol scheme \"htttp\"", }, + { + name: "Return err when repo not found", + client: mockServerNotFound.Client(), + info: info{ + tagsUrl: mockServerNotFound.URL, + }, + want: git.Tags{}, + wantErr: git.ErrRepoNotFound.Error(), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {