From 744fd6a1c87f2ad39dbe282133fae1190df6e538 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Wed, 12 Jun 2019 15:20:43 -0500 Subject: [PATCH] Add error for fork already existing (#7185) --- models/error.go | 17 +++++++++++++++++ models/repo.go | 7 ++++--- models/repo_test.go | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/models/error.go b/models/error.go index fe0f05d36..11ca6e686 100644 --- a/models/error.go +++ b/models/error.go @@ -674,6 +674,23 @@ func (err ErrRepoAlreadyExist) Error() string { return fmt.Sprintf("repository already exists [uname: %s, name: %s]", err.Uname, err.Name) } +// ErrForkAlreadyExist represents a "ForkAlreadyExist" kind of error. +type ErrForkAlreadyExist struct { + Uname string + RepoName string + ForkName string +} + +// IsErrForkAlreadyExist checks if an error is an ErrForkAlreadyExist. +func IsErrForkAlreadyExist(err error) bool { + _, ok := err.(ErrForkAlreadyExist) + return ok +} + +func (err ErrForkAlreadyExist) Error() string { + return fmt.Sprintf("repository is already forked by user [uname: %s, repo path: %s, fork path: %s]", err.Uname, err.RepoName, err.ForkName) +} + // ErrRepoRedirectNotExist represents a "RepoRedirectNotExist" kind of error. type ErrRepoRedirectNotExist struct { OwnerID int64 diff --git a/models/repo.go b/models/repo.go index a4a7521aa..8819debd4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -2395,9 +2395,10 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R return nil, err } if forkedRepo != nil { - return nil, ErrRepoAlreadyExist{ - Uname: u.Name, - Name: forkedRepo.Name, + return nil, ErrForkAlreadyExist{ + Uname: u.Name, + RepoName: oldRepo.FullName(), + ForkName: forkedRepo.FullName(), } } diff --git a/models/repo_test.go b/models/repo_test.go index 8411536d7..02cb5ab99 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -131,7 +131,7 @@ func TestForkRepository(t *testing.T) { fork, err := ForkRepository(user, user, repo, "test", "test") assert.Nil(t, fork) assert.Error(t, err) - assert.True(t, IsErrRepoAlreadyExist(err)) + assert.True(t, IsErrForkAlreadyExist(err)) } func TestRepoAPIURL(t *testing.T) {