Skip to content

Move Repo APIFormat to convert package #13787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 0 additions & 134 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,6 @@ func (repo *Repository) APIURL() string {
return setting.AppURL + path.Join("api/v1/repos", repo.FullName())
}

// APIFormat converts a Repository to api.Repository
func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
return repo.innerAPIFormat(x, mode, false)
}

// GetCommitsCountCacheKey returns cache key used for commits count caching.
func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool) string {
var prefix string
Expand All @@ -338,135 +333,6 @@ func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool)
return fmt.Sprintf("commits-count-%d-%s-%s", repo.ID, prefix, contextName)
}

func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) *api.Repository {
var parent *api.Repository

cloneLink := repo.cloneLink(false)
permission := &api.Permission{
Admin: mode >= AccessModeAdmin,
Push: mode >= AccessModeWrite,
Pull: mode >= AccessModeRead,
}
if !isParent {
err := repo.getBaseRepo(e)
if err != nil {
log.Error("APIFormat: %v", err)
}
if repo.BaseRepo != nil {
parent = repo.BaseRepo.innerAPIFormat(e, mode, true)
}
}

//check enabled/disabled units
hasIssues := false
var externalTracker *api.ExternalTracker
var internalTracker *api.InternalTracker
if unit, err := repo.getUnit(e, UnitTypeIssues); err == nil {
config := unit.IssuesConfig()
hasIssues = true
internalTracker = &api.InternalTracker{
EnableTimeTracker: config.EnableTimetracker,
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
EnableIssueDependencies: config.EnableDependencies,
}
} else if unit, err := repo.getUnit(e, UnitTypeExternalTracker); err == nil {
config := unit.ExternalTrackerConfig()
hasIssues = true
externalTracker = &api.ExternalTracker{
ExternalTrackerURL: config.ExternalTrackerURL,
ExternalTrackerFormat: config.ExternalTrackerFormat,
ExternalTrackerStyle: config.ExternalTrackerStyle,
}
}
hasUncyclo := false
var externalUncyclo *api.ExternalUncyclo
if _, err := repo.getUnit(e, UnitTypeUncyclo); err == nil {
hasUncyclo = true
} else if unit, err := repo.getUnit(e, UnitTypeExternalUncyclo); err == nil {
hasUncyclo = true
config := unit.ExternalUncycloConfig()
externalUncyclo = &api.ExternalUncyclo{
ExternalUncycloURL: config.ExternalUncycloURL,
}
}
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
if unit, err := repo.getUnit(e, UnitTypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
allowMerge = config.AllowMerge
allowRebase = config.AllowRebase
allowRebaseMerge = config.AllowRebaseMerge
allowSquash = config.AllowSquash
}
hasProjects := false
if _, err := repo.getUnit(e, UnitTypeProjects); err == nil {
hasProjects = true
}

repo.mustOwner(e)

numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})

return &api.Repository{
ID: repo.ID,
// TODO use convert.ToUser(repo.Owner)
Owner: &api.User{
ID: repo.Owner.ID,
UserName: repo.Owner.Name,
FullName: repo.Owner.FullName,
Email: repo.Owner.GetEmail(),
AvatarURL: repo.Owner.AvatarLink(),
LastLogin: repo.Owner.LastLoginUnix.AsTime(),
Created: repo.Owner.CreatedUnix.AsTime(),
},
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
Private: repo.IsPrivate,
Template: repo.IsTemplate,
Empty: repo.IsEmpty,
Archived: repo.IsArchived,
Size: int(repo.Size / 1024),
Fork: repo.IsFork,
Parent: parent,
Mirror: repo.IsMirror,
HTMLURL: repo.HTMLURL(),
SSHURL: cloneLink.SSH,
CloneURL: cloneLink.HTTPS,
Website: repo.Website,
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
OpenIssues: repo.NumOpenIssues,
OpenPulls: repo.NumOpenPulls,
Releases: int(numReleases),
DefaultBranch: repo.DefaultBranch,
Created: repo.CreatedUnix.AsTime(),
Updated: repo.UpdatedUnix.AsTime(),
Permissions: permission,
HasIssues: hasIssues,
ExternalTracker: externalTracker,
InternalTracker: internalTracker,
HasUncyclo: hasUncyclo,
HasProjects: hasProjects,
ExternalUncyclo: externalUncyclo,
HasPullRequests: hasPullRequests,
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
AllowMerge: allowMerge,
AllowRebase: allowRebase,
AllowRebaseMerge: allowRebaseMerge,
AllowSquash: allowSquash,
AvatarURL: repo.avatarLink(e),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
}
}

func (repo *Repository) getUnits(e Engine) (err error) {
if repo.Units != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread {

//since user only get notifications when he has access to use minimal access mode
if n.Repository != nil {
result.Repository = n.Repository.APIFormat(models.AccessModeRead)
result.Repository = ToRepo(n.Repository, models.AccessModeRead)
}

//handle Subject
Expand Down
4 changes: 2 additions & 2 deletions modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
Name: pr.BaseBranch,
Ref: pr.BaseBranch,
RepoID: pr.BaseRepoID,
Repository: pr.BaseRepo.APIFormat(models.AccessModeNone),
Repository: ToRepo(pr.BaseRepo, models.AccessModeNone),
},
Head: &api.PRBranchInfo{
Name: pr.HeadBranch,
Expand Down Expand Up @@ -97,7 +97,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {

if pr.HeadRepo != nil {
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = pr.HeadRepo.APIFormat(models.AccessModeNone)
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, models.AccessModeNone)

headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion modules/convert/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
Ref: "refs/pull/2/head",
Sha: "4a357436d925b5c974181ff12a994538ddc5a269",
RepoID: 1,
Repository: headRepo.APIFormat(models.AccessModeNone),
Repository: ToRepo(headRepo, models.AccessModeNone),
}, apiPullRequest.Head)

//withOut HeadRepo
Expand Down
137 changes: 137 additions & 0 deletions modules/convert/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package convert

import (
"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
)

// ToRepo converts a Repository to api.Repository
func ToRepo(repo *models.Repository, mode models.AccessMode) *api.Repository {
return innerToRepo(repo, mode, false)
}

func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) *api.Repository {
var parent *api.Repository

cloneLink := repo.CloneLink()
permission := &api.Permission{
Admin: mode >= models.AccessModeAdmin,
Push: mode >= models.AccessModeWrite,
Pull: mode >= models.AccessModeRead,
}
if !isParent {
err := repo.GetBaseRepo()
if err != nil {
return nil
}
if repo.BaseRepo != nil {
parent = innerToRepo(repo.BaseRepo, mode, true)
}
}

//check enabled/disabled units
hasIssues := false
var externalTracker *api.ExternalTracker
var internalTracker *api.InternalTracker
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
config := unit.IssuesConfig()
hasIssues = true
internalTracker = &api.InternalTracker{
EnableTimeTracker: config.EnableTimetracker,
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
EnableIssueDependencies: config.EnableDependencies,
}
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
config := unit.ExternalTrackerConfig()
hasIssues = true
externalTracker = &api.ExternalTracker{
ExternalTrackerURL: config.ExternalTrackerURL,
ExternalTrackerFormat: config.ExternalTrackerFormat,
ExternalTrackerStyle: config.ExternalTrackerStyle,
}
}
hasUncyclo := false
var externalUncyclo *api.ExternalUncyclo
if _, err := repo.GetUnit(models.UnitTypeUncyclo); err == nil {
hasUncyclo = true
} else if unit, err := repo.GetUnit(models.UnitTypeExternalUncyclo); err == nil {
hasUncyclo = true
config := unit.ExternalUncycloConfig()
externalUncyclo = &api.ExternalUncyclo{
ExternalUncycloURL: config.ExternalUncycloURL,
}
}
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
config := unit.PullRequestsConfig()
hasPullRequests = true
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
allowMerge = config.AllowMerge
allowRebase = config.AllowRebase
allowRebaseMerge = config.AllowRebaseMerge
allowSquash = config.AllowSquash
}
hasProjects := false
if _, err := repo.GetUnit(models.UnitTypeProjects); err == nil {
hasProjects = true
}

if err := repo.GetOwner(); err != nil {
return nil
}

numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})

return &api.Repository{
ID: repo.ID,
Owner: ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin),
Name: repo.Name,
FullName: repo.FullName(),
Description: repo.Description,
Private: repo.IsPrivate,
Template: repo.IsTemplate,
Empty: repo.IsEmpty,
Archived: repo.IsArchived,
Size: int(repo.Size / 1024),
Fork: repo.IsFork,
Parent: parent,
Mirror: repo.IsMirror,
HTMLURL: repo.HTMLURL(),
SSHURL: cloneLink.SSH,
CloneURL: cloneLink.HTTPS,
Website: repo.Website,
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
OpenIssues: repo.NumOpenIssues,
OpenPulls: repo.NumOpenPulls,
Releases: int(numReleases),
DefaultBranch: repo.DefaultBranch,
Created: repo.CreatedUnix.AsTime(),
Updated: repo.UpdatedUnix.AsTime(),
Permissions: permission,
HasIssues: hasIssues,
ExternalTracker: externalTracker,
InternalTracker: internalTracker,
HasUncyclo: hasUncyclo,
HasProjects: hasProjects,
ExternalUncyclo: externalUncyclo,
HasPullRequests: hasPullRequests,
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
AllowMerge: allowMerge,
AllowRebase: allowRebase,
AllowRebaseMerge: allowRebaseMerge,
AllowSquash: allowSquash,
AvatarURL: repo.AvatarLink(),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
}
}
Loading