diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 10 | ||||
-rw-r--r-- | models/issue.go | 9 | ||||
-rw-r--r-- | models/repo.go | 31 |
3 files changed, 28 insertions, 22 deletions
diff --git a/models/error.go b/models/error.go index 50d9cba171..fceee21fdf 100644 --- a/models/error.go +++ b/models/error.go @@ -572,9 +572,10 @@ func (err ErrLFSLockAlreadyExist) Error() string { // ErrRepoNotExist represents a "RepoNotExist" kind of error. type ErrRepoNotExist struct { - ID int64 - UID int64 - Name string + ID int64 + UID int64 + OwnerName string + Name string } // IsErrRepoNotExist checks if an error is a ErrRepoNotExist. @@ -584,7 +585,8 @@ func IsErrRepoNotExist(err error) bool { } func (err ErrRepoNotExist) Error() string { - return fmt.Sprintf("repository does not exist [id: %d, uid: %d, name: %s]", err.ID, err.UID, err.Name) + return fmt.Sprintf("repository does not exist [id: %d, uid: %d, owner_name: %s, name: %s]", + err.ID, err.UID, err.OwnerName, err.Name) } // ErrRepoAlreadyExist represents a "RepoAlreadyExist" kind of error. diff --git a/models/issue.go b/models/issue.go index 17aef10553..00e0bf8027 100644 --- a/models/issue.go +++ b/models/issue.go @@ -964,7 +964,7 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) // GetIssueByRef returns an Issue specified by a GFM reference. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax. func GetIssueByRef(ref string) (*Issue, error) { - n := strings.IndexByte(ref, byte('#')) + n := strings.IndexByte(ref, '#') if n == -1 { return nil, errMissingIssueNumber } @@ -974,7 +974,12 @@ func GetIssueByRef(ref string) (*Issue, error) { return nil, errInvalidIssueNumber } - repo, err := GetRepositoryByRef(ref[:n]) + i := strings.IndexByte(ref[:n], '/') + if i < 2 { + return nil, ErrInvalidReference + } + + repo, err := GetRepositoryByOwnerAndName(ref[:i], ref[i+1:n]) if err != nil { return nil, err } diff --git a/models/repo.go b/models/repo.go index 8d57ae51a5..a9f0d93031 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1740,13 +1740,13 @@ func DeleteRepository(doer *User, uid, repoID int64) error { if err != nil { return err } else if !has { - return ErrRepoNotExist{repoID, uid, ""} + return ErrRepoNotExist{repoID, uid, "", ""} } if cnt, err := sess.ID(repoID).Delete(&Repository{}); err != nil { return err } else if cnt != 1 { - return ErrRepoNotExist{repoID, uid, ""} + return ErrRepoNotExist{repoID, uid, "", ""} } if org.IsOrganization() { @@ -1891,21 +1891,20 @@ func DeleteRepository(doer *User, uid, repoID int64) error { return nil } -// GetRepositoryByRef returns a Repository specified by a GFM reference. -// See https://help.github.com/articles/writing-on-github#references for more information on the syntax. -func GetRepositoryByRef(ref string) (*Repository, error) { - n := strings.IndexByte(ref, byte('/')) - if n < 2 { - return nil, ErrInvalidReference - } - - userName, repoName := ref[:n], ref[n+1:] - user, err := GetUserByName(userName) +// GetRepositoryByOwnerAndName returns the repository by given ownername and reponame. +func GetRepositoryByOwnerAndName(ownerName, repoName string) (*Repository, error) { + var repo Repository + has, err := x.Select("repository.*"). + Join("INNER", "user", "`user`.id = repository.owner_id"). + Where("repository.lower_name = ?", strings.ToLower(repoName)). + And("`user`.lower_name = ?", strings.ToLower(ownerName)). + Get(&repo) if err != nil { return nil, err + } else if !has { + return nil, ErrRepoNotExist{0, 0, ownerName, repoName} } - - return GetRepositoryByName(user.ID, repoName) + return &repo, nil } // GetRepositoryByName returns the repository by given name under user if exists. @@ -1918,7 +1917,7 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) { if err != nil { return nil, err } else if !has { - return nil, ErrRepoNotExist{0, ownerID, name} + return nil, ErrRepoNotExist{0, ownerID, "", name} } return repo, err } @@ -1929,7 +1928,7 @@ func getRepositoryByID(e Engine, id int64) (*Repository, error) { if err != nil { return nil, err } else if !has { - return nil, ErrRepoNotExist{id, 0, ""} + return nil, ErrRepoNotExist{id, 0, "", ""} } return repo, nil } |