diff options
author | 6543 <6543@obermui.de> | 2020-12-02 22:38:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-02 16:38:30 -0500 |
commit | 7ac8a770e1086a50d1fc0a194fe0d7060036fb30 (patch) | |
tree | f3c9e4e92bd158c8ef506ea46163c2dd95a57cfa /modules/convert/repository.go | |
parent | 2b4a08e9621e052845062ecd71ebfd37453c0383 (diff) | |
download | gitea-7ac8a770e1086a50d1fc0a194fe0d7060036fb30.tar.gz gitea-7ac8a770e1086a50d1fc0a194fe0d7060036fb30.zip |
Move Repo APIFormat to convert package (#13787)
* Move Repo APIFormat to convert package
* tweek
Diffstat (limited to 'modules/convert/repository.go')
-rw-r--r-- | modules/convert/repository.go | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/modules/convert/repository.go b/modules/convert/repository.go new file mode 100644 index 0000000000..f2e10447f7 --- /dev/null +++ b/modules/convert/repository.go @@ -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, + } + } + hasWiki := false + var externalWiki *api.ExternalWiki + if _, err := repo.GetUnit(models.UnitTypeWiki); err == nil { + hasWiki = true + } else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil { + hasWiki = true + config := unit.ExternalWikiConfig() + externalWiki = &api.ExternalWiki{ + ExternalWikiURL: config.ExternalWikiURL, + } + } + 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, + HasWiki: hasWiki, + HasProjects: hasProjects, + ExternalWiki: externalWiki, + HasPullRequests: hasPullRequests, + IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts, + AllowMerge: allowMerge, + AllowRebase: allowRebase, + AllowRebaseMerge: allowRebaseMerge, + AllowSquash: allowSquash, + AvatarURL: repo.AvatarLink(), + Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, + } +} |