2020-12-03 00:38:30 +03: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 (
2022-01-18 16:18:30 +03:00
"time"
2020-12-03 00:38:30 +03:00
"code.gitea.io/gitea/models"
2021-12-10 04:27:50 +03:00
"code.gitea.io/gitea/models/db"
2021-11-28 14:58:28 +03:00
"code.gitea.io/gitea/models/perm"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-09 22:57:58 +03:00
unit_model "code.gitea.io/gitea/models/unit"
2021-12-24 07:26:52 +03:00
"code.gitea.io/gitea/modules/log"
2020-12-03 00:38:30 +03:00
api "code.gitea.io/gitea/modules/structs"
)
// ToRepo converts a Repository to api.Repository
2021-12-10 04:27:50 +03:00
func ToRepo ( repo * repo_model . Repository , mode perm . AccessMode ) * api . Repository {
2020-12-03 00:38:30 +03:00
return innerToRepo ( repo , mode , false )
}
2021-12-10 04:27:50 +03:00
func innerToRepo ( repo * repo_model . Repository , mode perm . AccessMode , isParent bool ) * api . Repository {
2020-12-03 00:38:30 +03:00
var parent * api . Repository
cloneLink := repo . CloneLink ( )
permission := & api . Permission {
2021-11-28 14:58:28 +03:00
Admin : mode >= perm . AccessModeAdmin ,
Push : mode >= perm . AccessModeWrite ,
Pull : mode >= perm . AccessModeRead ,
2020-12-03 00:38:30 +03:00
}
if ! isParent {
err := repo . GetBaseRepo ( )
if err != nil {
return nil
}
if repo . BaseRepo != nil {
parent = innerToRepo ( repo . BaseRepo , mode , true )
}
}
2022-01-20 20:46:10 +03:00
// check enabled/disabled units
2020-12-03 00:38:30 +03:00
hasIssues := false
var externalTracker * api . ExternalTracker
var internalTracker * api . InternalTracker
2021-11-09 22:57:58 +03:00
if unit , err := repo . GetUnit ( unit_model . TypeIssues ) ; err == nil {
2020-12-03 00:38:30 +03:00
config := unit . IssuesConfig ( )
hasIssues = true
internalTracker = & api . InternalTracker {
EnableTimeTracker : config . EnableTimetracker ,
AllowOnlyContributorsToTrackTime : config . AllowOnlyContributorsToTrackTime ,
EnableIssueDependencies : config . EnableDependencies ,
}
2021-11-09 22:57:58 +03:00
} else if unit , err := repo . GetUnit ( unit_model . TypeExternalTracker ) ; err == nil {
2020-12-03 00:38:30 +03: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 22:57:58 +03:00
if _ , err := repo . GetUnit ( unit_model . TypeWiki ) ; err == nil {
2020-12-03 00:38:30 +03:00
hasWiki = true
2021-11-09 22:57:58 +03:00
} else if unit , err := repo . GetUnit ( unit_model . TypeExternalWiki ) ; err == nil {
2020-12-03 00:38:30 +03:00
hasWiki = true
config := unit . ExternalWikiConfig ( )
externalWiki = & api . ExternalWiki {
ExternalWikiURL : config . ExternalWikiURL ,
}
}
hasPullRequests := false
ignoreWhitespaceConflicts := false
allowMerge := false
allowRebase := false
allowRebaseMerge := false
allowSquash := false
2022-07-15 11:00:01 +03:00
allowRebaseUpdate := false
defaultDeleteBranchAfterMerge := false
2021-12-10 04:27:50 +03:00
defaultMergeStyle := repo_model . MergeStyleMerge
2021-11-09 22:57:58 +03:00
if unit , err := repo . GetUnit ( unit_model . TypePullRequests ) ; err == nil {
2020-12-03 00:38:30 +03:00
config := unit . PullRequestsConfig ( )
hasPullRequests = true
ignoreWhitespaceConflicts = config . IgnoreWhitespaceConflicts
allowMerge = config . AllowMerge
allowRebase = config . AllowRebase
allowRebaseMerge = config . AllowRebaseMerge
allowSquash = config . AllowSquash
2022-07-15 11:00:01 +03:00
allowRebaseUpdate = config . AllowRebaseUpdate
defaultDeleteBranchAfterMerge = config . DefaultDeleteBranchAfterMerge
2021-03-27 17:55:40 +03:00
defaultMergeStyle = config . GetDefaultMergeStyle ( )
2020-12-03 00:38:30 +03:00
}
hasProjects := false
2021-11-09 22:57:58 +03:00
if _ , err := repo . GetUnit ( unit_model . TypeProjects ) ; err == nil {
2020-12-03 00:38:30 +03:00
hasProjects = true
}
2021-12-10 04:27:50 +03:00
if err := repo . GetOwner ( db . DefaultContext ) ; err != nil {
2020-12-03 00:38:30 +03:00
return nil
}
2022-08-25 05:31:57 +03:00
numReleases , _ := repo_model . GetReleaseCountByRepoID ( repo . ID , repo_model . FindReleasesOptions { IncludeDrafts : false , IncludeTags : false } )
2020-12-03 00:38:30 +03:00
2021-01-03 02:47:47 +03:00
mirrorInterval := ""
2022-01-18 16:18:30 +03:00
var mirrorUpdated time . Time
2021-01-03 02:47:47 +03:00
if repo . IsMirror {
2021-12-10 04:27:50 +03:00
var err error
2022-05-20 17:08:52 +03:00
repo . Mirror , err = repo_model . GetMirrorByRepoID ( db . DefaultContext , repo . ID )
2021-12-10 04:27:50 +03:00
if err == nil {
2021-01-03 02:47:47 +03:00
mirrorInterval = repo . Mirror . Interval . String ( )
2022-01-18 16:18:30 +03:00
mirrorUpdated = repo . Mirror . UpdatedUnix . AsTime ( )
2021-01-03 02:47:47 +03:00
}
}
2021-12-24 07:26:52 +03:00
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 )
}
}
}
2022-01-25 09:33:40 +03:00
var language string
if repo . PrimaryLanguage != nil {
language = repo . PrimaryLanguage . Language
}
repoAPIURL := repo . APIURL ( )
2020-12-03 00:38:30 +03:00
return & api . Repository {
2022-07-15 11:00:01 +03:00
ID : repo . ID ,
Owner : ToUserWithAccessMode ( repo . Owner , mode ) ,
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 ,
OriginalURL : repo . SanitizedOriginalURL ( ) ,
Website : repo . Website ,
Language : language ,
LanguagesURL : repoAPIURL + "/languages" ,
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 ,
AllowRebaseUpdate : allowRebaseUpdate ,
DefaultDeleteBranchAfterMerge : defaultDeleteBranchAfterMerge ,
DefaultMergeStyle : string ( defaultMergeStyle ) ,
AvatarURL : repo . AvatarLink ( ) ,
Internal : ! repo . IsPrivate && repo . Owner . Visibility == api . VisibleTypePrivate ,
MirrorInterval : mirrorInterval ,
MirrorUpdated : mirrorUpdated ,
RepoTransfer : transfer ,
2021-12-24 07:26:52 +03:00
}
}
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
func ToRepoTransfer ( t * models . RepoTransfer ) * api . RepoTransfer {
2022-05-13 20:27:58 +03:00
teams , _ := ToTeams ( t . Teams , false )
2021-12-24 07:26:52 +03:00
return & api . RepoTransfer {
Doer : ToUser ( t . Doer , nil ) ,
Recipient : ToUser ( t . Recipient , nil ) ,
Teams : teams ,
2020-12-03 00:38:30 +03:00
}
}