Skip to content

Commit 7ac8a77

Browse files
authored
Move Repo APIFormat to convert package (#13787)
* Move Repo APIFormat to convert package * tweek
1 parent 2b4a08e commit 7ac8a77

File tree

18 files changed

+201
-193
lines changed

18 files changed

+201
-193
lines changed

models/repo.go

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ func (repo *Repository) APIURL() string {
322322
return setting.AppURL + path.Join("api/v1/repos", repo.FullName())
323323
}
324324

325-
// APIFormat converts a Repository to api.Repository
326-
func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
327-
return repo.innerAPIFormat(x, mode, false)
328-
}
329-
330325
// GetCommitsCountCacheKey returns cache key used for commits count caching.
331326
func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool) string {
332327
var prefix string
@@ -338,135 +333,6 @@ func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool)
338333
return fmt.Sprintf("commits-count-%d-%s-%s", repo.ID, prefix, contextName)
339334
}
340335

341-
func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) *api.Repository {
342-
var parent *api.Repository
343-
344-
cloneLink := repo.cloneLink(false)
345-
permission := &api.Permission{
346-
Admin: mode >= AccessModeAdmin,
347-
Push: mode >= AccessModeWrite,
348-
Pull: mode >= AccessModeRead,
349-
}
350-
if !isParent {
351-
err := repo.getBaseRepo(e)
352-
if err != nil {
353-
log.Error("APIFormat: %v", err)
354-
}
355-
if repo.BaseRepo != nil {
356-
parent = repo.BaseRepo.innerAPIFormat(e, mode, true)
357-
}
358-
}
359-
360-
//check enabled/disabled units
361-
hasIssues := false
362-
var externalTracker *api.ExternalTracker
363-
var internalTracker *api.InternalTracker
364-
if unit, err := repo.getUnit(e, UnitTypeIssues); err == nil {
365-
config := unit.IssuesConfig()
366-
hasIssues = true
367-
internalTracker = &api.InternalTracker{
368-
EnableTimeTracker: config.EnableTimetracker,
369-
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
370-
EnableIssueDependencies: config.EnableDependencies,
371-
}
372-
} else if unit, err := repo.getUnit(e, UnitTypeExternalTracker); err == nil {
373-
config := unit.ExternalTrackerConfig()
374-
hasIssues = true
375-
externalTracker = &api.ExternalTracker{
376-
ExternalTrackerURL: config.ExternalTrackerURL,
377-
ExternalTrackerFormat: config.ExternalTrackerFormat,
378-
ExternalTrackerStyle: config.ExternalTrackerStyle,
379-
}
380-
}
381-
hasUncyclo := false
382-
var externalUncyclo *api.ExternalUncyclo
383-
if _, err := repo.getUnit(e, UnitTypeUncyclo); err == nil {
384-
hasUncyclo = true
385-
} else if unit, err := repo.getUnit(e, UnitTypeExternalUncyclo); err == nil {
386-
hasUncyclo = true
387-
config := unit.ExternalUncycloConfig()
388-
externalUncyclo = &api.ExternalUncyclo{
389-
ExternalUncycloURL: config.ExternalUncycloURL,
390-
}
391-
}
392-
hasPullRequests := false
393-
ignoreWhitespaceConflicts := false
394-
allowMerge := false
395-
allowRebase := false
396-
allowRebaseMerge := false
397-
allowSquash := false
398-
if unit, err := repo.getUnit(e, UnitTypePullRequests); err == nil {
399-
config := unit.PullRequestsConfig()
400-
hasPullRequests = true
401-
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
402-
allowMerge = config.AllowMerge
403-
allowRebase = config.AllowRebase
404-
allowRebaseMerge = config.AllowRebaseMerge
405-
allowSquash = config.AllowSquash
406-
}
407-
hasProjects := false
408-
if _, err := repo.getUnit(e, UnitTypeProjects); err == nil {
409-
hasProjects = true
410-
}
411-
412-
repo.mustOwner(e)
413-
414-
numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})
415-
416-
return &api.Repository{
417-
ID: repo.ID,
418-
// TODO use convert.ToUser(repo.Owner)
419-
Owner: &api.User{
420-
ID: repo.Owner.ID,
421-
UserName: repo.Owner.Name,
422-
FullName: repo.Owner.FullName,
423-
Email: repo.Owner.GetEmail(),
424-
AvatarURL: repo.Owner.AvatarLink(),
425-
LastLogin: repo.Owner.LastLoginUnix.AsTime(),
426-
Created: repo.Owner.CreatedUnix.AsTime(),
427-
},
428-
Name: repo.Name,
429-
FullName: repo.FullName(),
430-
Description: repo.Description,
431-
Private: repo.IsPrivate,
432-
Template: repo.IsTemplate,
433-
Empty: repo.IsEmpty,
434-
Archived: repo.IsArchived,
435-
Size: int(repo.Size / 1024),
436-
Fork: repo.IsFork,
437-
Parent: parent,
438-
Mirror: repo.IsMirror,
439-
HTMLURL: repo.HTMLURL(),
440-
SSHURL: cloneLink.SSH,
441-
CloneURL: cloneLink.HTTPS,
442-
Website: repo.Website,
443-
Stars: repo.NumStars,
444-
Forks: repo.NumForks,
445-
Watchers: repo.NumWatches,
446-
OpenIssues: repo.NumOpenIssues,
447-
OpenPulls: repo.NumOpenPulls,
448-
Releases: int(numReleases),
449-
DefaultBranch: repo.DefaultBranch,
450-
Created: repo.CreatedUnix.AsTime(),
451-
Updated: repo.UpdatedUnix.AsTime(),
452-
Permissions: permission,
453-
HasIssues: hasIssues,
454-
ExternalTracker: externalTracker,
455-
InternalTracker: internalTracker,
456-
HasUncyclo: hasUncyclo,
457-
HasProjects: hasProjects,
458-
ExternalUncyclo: externalUncyclo,
459-
HasPullRequests: hasPullRequests,
460-
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
461-
AllowMerge: allowMerge,
462-
AllowRebase: allowRebase,
463-
AllowRebaseMerge: allowRebaseMerge,
464-
AllowSquash: allowSquash,
465-
AvatarURL: repo.avatarLink(e),
466-
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
467-
}
468-
}
469-
470336
func (repo *Repository) getUnits(e Engine) (err error) {
471337
if repo.Units != nil {
472338
return nil

modules/convert/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread {
2121

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

2727
//handle Subject

modules/convert/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
6868
Name: pr.BaseBranch,
6969
Ref: pr.BaseBranch,
7070
RepoID: pr.BaseRepoID,
71-
Repository: pr.BaseRepo.APIFormat(models.AccessModeNone),
71+
Repository: ToRepo(pr.BaseRepo, models.AccessModeNone),
7272
},
7373
Head: &api.PRBranchInfo{
7474
Name: pr.HeadBranch,
@@ -97,7 +97,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
9797

9898
if pr.HeadRepo != nil {
9999
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
100-
apiPullRequest.Head.Repository = pr.HeadRepo.APIFormat(models.AccessModeNone)
100+
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, models.AccessModeNone)
101101

102102
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
103103
if err != nil {

modules/convert/pull_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
2727
Ref: "refs/pull/2/head",
2828
Sha: "4a357436d925b5c974181ff12a994538ddc5a269",
2929
RepoID: 1,
30-
Repository: headRepo.APIFormat(models.AccessModeNone),
30+
Repository: ToRepo(headRepo, models.AccessModeNone),
3131
}, apiPullRequest.Head)
3232

3333
//withOut HeadRepo

modules/convert/repository.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package convert
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
api "code.gitea.io/gitea/modules/structs"
10+
)
11+
12+
// ToRepo converts a Repository to api.Repository
13+
func ToRepo(repo *models.Repository, mode models.AccessMode) *api.Repository {
14+
return innerToRepo(repo, mode, false)
15+
}
16+
17+
func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) *api.Repository {
18+
var parent *api.Repository
19+
20+
cloneLink := repo.CloneLink()
21+
permission := &api.Permission{
22+
Admin: mode >= models.AccessModeAdmin,
23+
Push: mode >= models.AccessModeWrite,
24+
Pull: mode >= models.AccessModeRead,
25+
}
26+
if !isParent {
27+
err := repo.GetBaseRepo()
28+
if err != nil {
29+
return nil
30+
}
31+
if repo.BaseRepo != nil {
32+
parent = innerToRepo(repo.BaseRepo, mode, true)
33+
}
34+
}
35+
36+
//check enabled/disabled units
37+
hasIssues := false
38+
var externalTracker *api.ExternalTracker
39+
var internalTracker *api.InternalTracker
40+
if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil {
41+
config := unit.IssuesConfig()
42+
hasIssues = true
43+
internalTracker = &api.InternalTracker{
44+
EnableTimeTracker: config.EnableTimetracker,
45+
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
46+
EnableIssueDependencies: config.EnableDependencies,
47+
}
48+
} else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil {
49+
config := unit.ExternalTrackerConfig()
50+
hasIssues = true
51+
externalTracker = &api.ExternalTracker{
52+
ExternalTrackerURL: config.ExternalTrackerURL,
53+
ExternalTrackerFormat: config.ExternalTrackerFormat,
54+
ExternalTrackerStyle: config.ExternalTrackerStyle,
55+
}
56+
}
57+
hasUncyclo := false
58+
var externalUncyclo *api.ExternalUncyclo
59+
if _, err := repo.GetUnit(models.UnitTypeUncyclo); err == nil {
60+
hasUncyclo = true
61+
} else if unit, err := repo.GetUnit(models.UnitTypeExternalUncyclo); err == nil {
62+
hasUncyclo = true
63+
config := unit.ExternalUncycloConfig()
64+
externalUncyclo = &api.ExternalUncyclo{
65+
ExternalUncycloURL: config.ExternalUncycloURL,
66+
}
67+
}
68+
hasPullRequests := false
69+
ignoreWhitespaceConflicts := false
70+
allowMerge := false
71+
allowRebase := false
72+
allowRebaseMerge := false
73+
allowSquash := false
74+
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
75+
config := unit.PullRequestsConfig()
76+
hasPullRequests = true
77+
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
78+
allowMerge = config.AllowMerge
79+
allowRebase = config.AllowRebase
80+
allowRebaseMerge = config.AllowRebaseMerge
81+
allowSquash = config.AllowSquash
82+
}
83+
hasProjects := false
84+
if _, err := repo.GetUnit(models.UnitTypeProjects); err == nil {
85+
hasProjects = true
86+
}
87+
88+
if err := repo.GetOwner(); err != nil {
89+
return nil
90+
}
91+
92+
numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})
93+
94+
return &api.Repository{
95+
ID: repo.ID,
96+
Owner: ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin),
97+
Name: repo.Name,
98+
FullName: repo.FullName(),
99+
Description: repo.Description,
100+
Private: repo.IsPrivate,
101+
Template: repo.IsTemplate,
102+
Empty: repo.IsEmpty,
103+
Archived: repo.IsArchived,
104+
Size: int(repo.Size / 1024),
105+
Fork: repo.IsFork,
106+
Parent: parent,
107+
Mirror: repo.IsMirror,
108+
HTMLURL: repo.HTMLURL(),
109+
SSHURL: cloneLink.SSH,
110+
CloneURL: cloneLink.HTTPS,
111+
Website: repo.Website,
112+
Stars: repo.NumStars,
113+
Forks: repo.NumForks,
114+
Watchers: repo.NumWatches,
115+
OpenIssues: repo.NumOpenIssues,
116+
OpenPulls: repo.NumOpenPulls,
117+
Releases: int(numReleases),
118+
DefaultBranch: repo.DefaultBranch,
119+
Created: repo.CreatedUnix.AsTime(),
120+
Updated: repo.UpdatedUnix.AsTime(),
121+
Permissions: permission,
122+
HasIssues: hasIssues,
123+
ExternalTracker: externalTracker,
124+
InternalTracker: internalTracker,
125+
HasUncyclo: hasUncyclo,
126+
HasProjects: hasProjects,
127+
ExternalUncyclo: externalUncyclo,
128+
HasPullRequests: hasPullRequests,
129+
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
130+
AllowMerge: allowMerge,
131+
AllowRebase: allowRebase,
132+
AllowRebaseMerge: allowRebaseMerge,
133+
AllowSquash: allowSquash,
134+
AvatarURL: repo.AvatarLink(),
135+
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
136+
}
137+
}

0 commit comments

Comments
 (0)