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

Commit

Permalink
Friendlier error message on update repo (#903)
Browse files Browse the repository at this point in the history
* Changing error message when repository version cannot be retrieved

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* added not found status for bitbucket repos

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* adding @fernandobelettizup to code owners list

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* moved error message to git package

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* added tests simulating 404 errors for each provider

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* extracted status code check to a single function

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>

* changes made to CODEOWNERS

Signed-off-by: fernandobelettizup <fernando.beletti@zup.com.br>
  • Loading branch information
fernandobelettizup committed Apr 9, 2021
1 parent e60dbf5 commit e669ace
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
@@ -1 +1 @@
* @kaduartur @victor-schumacher @victorschumacherzup @brunasilvazup @lucasdittrichzup @henriquemoraeszup @rodrigomedeirosf @ernelio
* @kaduartur @brunasilvazup @lucasdittrichzup @henriquemoraeszup @rodrigomedeirosf @ernelio @fernandobelettizup
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -2,6 +2,7 @@

Bruna Tavares <silvatavares.bruna@gmail.com>
Ernélio Júnior <erneliojunior@gmail.com>
Fernando Beletti <fernando.beletti@zup.com.br>
Gabriel Pinheiro <gabrielctpinheiro@gmail.com>
Guillaume Falourd <guillaume.falourd@zup.com.br>
Harirai Mahajan <harim1709@gmail.com>
Expand Down
5 changes: 2 additions & 3 deletions pkg/git/bitbucket/repository.go
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions pkg/git/bitbucket/repository_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 23 additions & 1 deletion pkg/git/git.go
Expand Up @@ -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"`
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/git/github/repository.go
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/git/github/repository_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/git/gitlab/repository.go
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/git/gitlab/repository_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e669ace

Please sign in to comment.