2020-12-02 14:38:30 -07:00
|
|
|
// 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"
|
2021-11-28 04:58:28 -07:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-09 12:57:58 -07:00
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2020-12-02 14:38:30 -07:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2021-11-28 04:58:28 -07:00
|
|
|
func ToRepo(repo *models.Repository, mode perm.AccessMode) *api.Repository {
|
2020-12-02 14:38:30 -07:00
|
|
|
return innerToRepo(repo, mode, false)
|
|
|
|
}
|
|
|
|
|
2021-11-28 04:58:28 -07:00
|
|
|
func innerToRepo(repo *models.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
|
2020-12-02 14:38:30 -07:00
|
|
|
var parent *api.Repository
|
|
|
|
|
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2021-11-28 04:58:28 -07:00
|
|
|
Admin: mode >= perm.AccessModeAdmin,
|
|
|
|
Push: mode >= perm.AccessModeWrite,
|
|
|
|
Pull: mode >= perm.AccessModeRead,
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
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
|
2021-11-09 12:57:58 -07:00
|
|
|
if unit, err := repo.GetUnit(unit_model.TypeIssues); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2021-11-09 12:57:58 -07:00
|
|
|
} else if unit, err := repo.GetUnit(unit_model.TypeExternalTracker); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.ExternalTrackerConfig()
|
|
|
|
hasIssues = true
|
|
|
|
externalTracker = &api.ExternalTracker{
|
|
|
|
ExternalTrackerURL: config.ExternalTrackerURL,
|
|
|
|
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
|
|
|
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasWiki := false
|
|
|
|
var externalWiki *api.ExternalWiki
|
2021-11-09 12:57:58 -07:00
|
|
|
if _, err := repo.GetUnit(unit_model.TypeWiki); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasWiki = true
|
2021-11-09 12:57:58 -07:00
|
|
|
} else if unit, err := repo.GetUnit(unit_model.TypeExternalWiki); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasWiki = true
|
|
|
|
config := unit.ExternalWikiConfig()
|
|
|
|
externalWiki = &api.ExternalWiki{
|
|
|
|
ExternalWikiURL: config.ExternalWikiURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasPullRequests := false
|
|
|
|
ignoreWhitespaceConflicts := false
|
|
|
|
allowMerge := false
|
|
|
|
allowRebase := false
|
|
|
|
allowRebaseMerge := false
|
|
|
|
allowSquash := false
|
2021-03-27 08:55:40 -06:00
|
|
|
defaultMergeStyle := models.MergeStyleMerge
|
2021-11-09 12:57:58 -07:00
|
|
|
if unit, err := repo.GetUnit(unit_model.TypePullRequests); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2021-03-27 08:55:40 -06:00
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
hasProjects := false
|
2021-11-09 12:57:58 -07:00
|
|
|
if _, err := repo.GetUnit(unit_model.TypeProjects); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasProjects = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.GetOwner(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:24:55 -06:00
|
|
|
numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: false})
|
2020-12-02 14:38:30 -07:00
|
|
|
|
2021-01-02 16:47:47 -07:00
|
|
|
mirrorInterval := ""
|
|
|
|
if repo.IsMirror {
|
|
|
|
if err := repo.GetMirror(); err == nil {
|
|
|
|
mirrorInterval = repo.Mirror.Interval.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:38:30 -07:00
|
|
|
return &api.Repository{
|
|
|
|
ID: repo.ID,
|
2021-03-27 10:45:26 -06:00
|
|
|
Owner: ToUserWithAccessMode(repo.Owner, mode),
|
2020-12-02 14:38:30 -07:00
|
|
|
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,
|
2020-12-07 05:07:48 -07:00
|
|
|
OriginalURL: repo.SanitizedOriginalURL(),
|
2020-12-02 14:38:30 -07:00
|
|
|
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,
|
2021-03-27 08:55:40 -06:00
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2020-12-02 14:38:30 -07:00
|
|
|
AvatarURL: repo.AvatarLink(),
|
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
2021-01-02 16:47:47 -07:00
|
|
|
MirrorInterval: mirrorInterval,
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
}
|