2020-12-03 03:08:30 +05:30
|
|
|
// 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-12-10 06:57:50 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2021-12-24 09:56:52 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-03 03:08:30 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2021-12-10 06:57:50 +05:30
|
|
|
func ToRepo(repo *repo_model.Repository, mode perm.AccessMode) *api.Repository {
|
2020-12-03 03:08:30 +05:30
|
|
|
return innerToRepo(repo, mode, false)
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
|
2020-12-03 03:08:30 +05:30
|
|
|
var parent *api.Repository
|
|
|
|
|
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2021-11-28 17:28:28 +05:30
|
|
|
Admin: mode >= perm.AccessModeAdmin,
|
|
|
|
Push: mode >= perm.AccessModeWrite,
|
|
|
|
Pull: mode >= perm.AccessModeRead,
|
2020-12-03 03:08:30 +05:30
|
|
|
}
|
|
|
|
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-10 01:27:58 +05:30
|
|
|
if unit, err := repo.GetUnit(unit_model.TypeIssues); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2021-11-10 01:27:58 +05:30
|
|
|
} else if unit, err := repo.GetUnit(unit_model.TypeExternalTracker); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
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-10 01:27:58 +05:30
|
|
|
if _, err := repo.GetUnit(unit_model.TypeWiki); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
hasWiki = true
|
2021-11-10 01:27:58 +05:30
|
|
|
} else if unit, err := repo.GetUnit(unit_model.TypeExternalWiki); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
hasWiki = true
|
|
|
|
config := unit.ExternalWikiConfig()
|
|
|
|
externalWiki = &api.ExternalWiki{
|
|
|
|
ExternalWikiURL: config.ExternalWikiURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasPullRequests := false
|
|
|
|
ignoreWhitespaceConflicts := false
|
|
|
|
allowMerge := false
|
|
|
|
allowRebase := false
|
|
|
|
allowRebaseMerge := false
|
|
|
|
allowSquash := false
|
2021-12-10 06:57:50 +05:30
|
|
|
defaultMergeStyle := repo_model.MergeStyleMerge
|
2021-11-10 01:27:58 +05:30
|
|
|
if unit, err := repo.GetUnit(unit_model.TypePullRequests); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2021-03-27 20:25:40 +05:30
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2020-12-03 03:08:30 +05:30
|
|
|
}
|
|
|
|
hasProjects := false
|
2021-11-10 01:27:58 +05:30
|
|
|
if _, err := repo.GetUnit(unit_model.TypeProjects); err == nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
hasProjects = true
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := repo.GetOwner(db.DefaultContext); err != nil {
|
2020-12-03 03:08:30 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:54:55 +05:30
|
|
|
numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: false})
|
2020-12-03 03:08:30 +05:30
|
|
|
|
2021-01-03 05:17:47 +05:30
|
|
|
mirrorInterval := ""
|
|
|
|
if repo.IsMirror {
|
2021-12-10 06:57:50 +05:30
|
|
|
var err error
|
|
|
|
repo.Mirror, err = repo_model.GetMirrorByRepoID(repo.ID)
|
|
|
|
if err == nil {
|
2021-01-03 05:17:47 +05:30
|
|
|
mirrorInterval = repo.Mirror.Interval.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 09:56:52 +05:30
|
|
|
var transfer *api.RepoTransfer
|
|
|
|
if repo.Status == repo_model.RepositoryPendingTransfer {
|
|
|
|
t, err := models.GetPendingRepositoryTransfer(repo)
|
|
|
|
if err != nil && !models.IsErrNoPendingTransfer(err) {
|
|
|
|
log.Warn("GetPendingRepositoryTransfer: %v", err)
|
|
|
|
} else {
|
|
|
|
if err := t.LoadAttributes(); err != nil {
|
|
|
|
log.Warn("LoadAttributes of RepoTransfer: %v", err)
|
|
|
|
} else {
|
|
|
|
transfer = ToRepoTransfer(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 03:08:30 +05:30
|
|
|
return &api.Repository{
|
|
|
|
ID: repo.ID,
|
2021-03-27 22:15:26 +05:30
|
|
|
Owner: ToUserWithAccessMode(repo.Owner, mode),
|
2020-12-03 03:08:30 +05:30
|
|
|
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 17:37:48 +05:30
|
|
|
OriginalURL: repo.SanitizedOriginalURL(),
|
2020-12-03 03:08:30 +05:30
|
|
|
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 20:25:40 +05:30
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2020-12-03 03:08:30 +05:30
|
|
|
AvatarURL: repo.AvatarLink(),
|
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
2021-01-03 05:17:47 +05:30
|
|
|
MirrorInterval: mirrorInterval,
|
2021-12-24 09:56:52 +05:30
|
|
|
RepoTransfer: transfer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
|
|
|
|
func ToRepoTransfer(t *models.RepoTransfer) *api.RepoTransfer {
|
|
|
|
var teams []*api.Team
|
|
|
|
for _, v := range t.Teams {
|
|
|
|
teams = append(teams, ToTeam(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.RepoTransfer{
|
|
|
|
Doer: ToUser(t.Doer, nil),
|
|
|
|
Recipient: ToUser(t.Recipient, nil),
|
|
|
|
Teams: teams,
|
2020-12-03 03:08:30 +05:30
|
|
|
}
|
|
|
|
}
|