2015-12-04 22:16:42 +00:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2020-09-17 21:33:23 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2015-12-04 22:16:42 +00:00
|
|
|
|
2015-12-17 07:28:47 +00:00
|
|
|
package convert
|
2015-12-04 22:16:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-12-03 14:05:48 +00:00
|
|
|
"strconv"
|
2021-06-17 16:04:10 +00:00
|
|
|
"strings"
|
2020-12-03 14:05:48 +00:00
|
|
|
"time"
|
2015-12-04 22:16:42 +00:00
|
|
|
|
2021-12-10 08:14:24 +00:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2022-01-02 13:12:35 +00:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2022-04-28 11:48:48 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 15:51:54 +00:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 09:37:59 +00:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-03-29 06:29:02 +00:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 11:58:28 +00:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 10:09:36 +00:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 19:57:58 +00:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-11 07:03:30 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 05:13:16 +00:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2019-03-27 09:33:00 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2018-02-20 12:50:42 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-05-11 10:21:34 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2018-02-20 12:50:42 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-09-29 02:27:20 +00:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2021-11-10 05:13:16 +00:00
|
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
2015-12-04 22:16:42 +00:00
|
|
|
)
|
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToEmail convert models.EmailAddress to api.Email
|
2021-11-11 07:03:30 +00:00
|
|
|
func ToEmail(email *user_model.EmailAddress) *api.Email {
|
2015-12-16 03:57:18 +00:00
|
|
|
return &api.Email{
|
|
|
|
Email: email.Email,
|
|
|
|
Verified: email.IsActivated,
|
|
|
|
Primary: email.IsPrimary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// ToBranch convert a git.Commit and git.Branch to an api.Branch
|
2022-06-12 15:51:54 +00:00
|
|
|
func ToBranch(repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
|
2019-11-16 19:39:18 +00:00
|
|
|
if bp == nil {
|
2020-03-21 03:41:33 +00:00
|
|
|
var hasPerm bool
|
2022-04-28 15:45:33 +00:00
|
|
|
var canPush bool
|
2020-03-21 03:41:33 +00:00
|
|
|
var err error
|
|
|
|
if user != nil {
|
2022-05-11 10:09:36 +00:00
|
|
|
hasPerm, err = access_model.HasAccessUnit(db.DefaultContext, user, repo, unit.TypeCode, perm.AccessModeWrite)
|
2020-03-21 03:41:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-28 15:45:33 +00:00
|
|
|
|
2022-05-11 10:09:36 +00:00
|
|
|
perms, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2022-04-28 15:45:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-13 09:37:59 +00:00
|
|
|
canPush = issues_model.CanMaintainerWriteToBranch(perms, b.Name, user)
|
2019-11-16 19:39:18 +00:00
|
|
|
}
|
2020-03-21 03:41:33 +00:00
|
|
|
|
|
|
|
return &api.Branch{
|
|
|
|
Name: b.Name,
|
2020-09-18 12:09:26 +00:00
|
|
|
Commit: ToPayloadCommit(repo, c),
|
2020-03-21 03:41:33 +00:00
|
|
|
Protected: false,
|
|
|
|
RequiredApprovals: 0,
|
|
|
|
EnableStatusCheck: false,
|
|
|
|
StatusCheckContexts: []string{},
|
2022-04-28 15:45:33 +00:00
|
|
|
UserCanPush: canPush,
|
2020-03-21 03:41:33 +00:00
|
|
|
UserCanMerge: hasPerm,
|
|
|
|
}, nil
|
2020-02-12 23:19:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:39:08 +00:00
|
|
|
branch := &api.Branch{
|
2020-03-21 03:41:33 +00:00
|
|
|
Name: b.Name,
|
2020-09-18 12:09:26 +00:00
|
|
|
Commit: ToPayloadCommit(repo, c),
|
2020-03-21 03:41:33 +00:00
|
|
|
Protected: true,
|
|
|
|
RequiredApprovals: bp.RequiredApprovals,
|
|
|
|
EnableStatusCheck: bp.EnableStatusCheck,
|
|
|
|
StatusCheckContexts: bp.StatusCheckContexts,
|
|
|
|
}
|
|
|
|
|
|
|
|
if isRepoAdmin {
|
|
|
|
branch.EffectiveBranchProtectionName = bp.BranchName
|
2020-02-12 23:19:35 +00:00
|
|
|
}
|
2020-03-19 15:39:08 +00:00
|
|
|
|
|
|
|
if user != nil {
|
2022-05-11 10:09:36 +00:00
|
|
|
permission, err := access_model.GetUserRepoPermission(db.DefaultContext, repo, user)
|
2020-08-20 07:48:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-19 15:39:08 +00:00
|
|
|
branch.UserCanPush = bp.CanUserPush(user.ID)
|
2022-06-12 15:51:54 +00:00
|
|
|
branch.UserCanMerge = git_model.IsUserMergeWhitelisted(db.DefaultContext, bp, user.ID, permission)
|
2020-03-19 15:39:08 +00:00
|
|
|
}
|
2020-03-21 03:41:33 +00:00
|
|
|
|
|
|
|
return branch, nil
|
2020-02-12 23:19:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
|
2022-06-12 15:51:54 +00:00
|
|
|
func ToBranchProtection(bp *git_model.ProtectedBranch) *api.BranchProtection {
|
2021-11-24 09:49:20 +00:00
|
|
|
pushWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.WhitelistUserIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err)
|
|
|
|
}
|
2021-11-24 09:49:20 +00:00
|
|
|
mergeWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.MergeWhitelistUserIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserNamesByIDs (MergeWhitelistUserIDs): %v", err)
|
|
|
|
}
|
2021-11-24 09:49:20 +00:00
|
|
|
approvalsWhitelistUsernames, err := user_model.GetUserNamesByIDs(bp.ApprovalsWhitelistUserIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v", err)
|
|
|
|
}
|
2022-03-29 06:29:02 +00:00
|
|
|
pushWhitelistTeams, err := organization.GetTeamNamesByID(bp.WhitelistTeamIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetTeamNamesByID (WhitelistTeamIDs): %v", err)
|
|
|
|
}
|
2022-03-29 06:29:02 +00:00
|
|
|
mergeWhitelistTeams, err := organization.GetTeamNamesByID(bp.MergeWhitelistTeamIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetTeamNamesByID (MergeWhitelistTeamIDs): %v", err)
|
|
|
|
}
|
2022-03-29 06:29:02 +00:00
|
|
|
approvalsWhitelistTeams, err := organization.GetTeamNamesByID(bp.ApprovalsWhitelistTeamIDs)
|
2020-02-12 23:19:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetTeamNamesByID (ApprovalsWhitelistTeamIDs): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.BranchProtection{
|
2020-11-28 19:30:46 +00:00
|
|
|
BranchName: bp.BranchName,
|
|
|
|
EnablePush: bp.CanPush,
|
|
|
|
EnablePushWhitelist: bp.EnableWhitelist,
|
|
|
|
PushWhitelistUsernames: pushWhitelistUsernames,
|
|
|
|
PushWhitelistTeams: pushWhitelistTeams,
|
|
|
|
PushWhitelistDeployKeys: bp.WhitelistDeployKeys,
|
|
|
|
EnableMergeWhitelist: bp.EnableMergeWhitelist,
|
|
|
|
MergeWhitelistUsernames: mergeWhitelistUsernames,
|
|
|
|
MergeWhitelistTeams: mergeWhitelistTeams,
|
|
|
|
EnableStatusCheck: bp.EnableStatusCheck,
|
|
|
|
StatusCheckContexts: bp.StatusCheckContexts,
|
|
|
|
RequiredApprovals: bp.RequiredApprovals,
|
|
|
|
EnableApprovalsWhitelist: bp.EnableApprovalsWhitelist,
|
|
|
|
ApprovalsWhitelistUsernames: approvalsWhitelistUsernames,
|
|
|
|
ApprovalsWhitelistTeams: approvalsWhitelistTeams,
|
|
|
|
BlockOnRejectedReviews: bp.BlockOnRejectedReviews,
|
|
|
|
BlockOnOfficialReviewRequests: bp.BlockOnOfficialReviewRequests,
|
|
|
|
BlockOnOutdatedBranch: bp.BlockOnOutdatedBranch,
|
|
|
|
DismissStaleApprovals: bp.DismissStaleApprovals,
|
|
|
|
RequireSignedCommits: bp.RequireSignedCommits,
|
|
|
|
ProtectedFilePatterns: bp.ProtectedFilePatterns,
|
2021-09-11 14:21:17 +00:00
|
|
|
UnprotectedFilePatterns: bp.UnprotectedFilePatterns,
|
2020-11-28 19:30:46 +00:00
|
|
|
Created: bp.CreatedUnix.AsTime(),
|
|
|
|
Updated: bp.UpdatedUnix.AsTime(),
|
2016-03-14 03:20:22 +00:00
|
|
|
}
|
2016-01-28 19:49:05 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// ToTag convert a git.Tag to an api.Tag
|
2021-12-10 01:27:50 +00:00
|
|
|
func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag {
|
2019-02-07 12:00:52 +00:00
|
|
|
return &api.Tag{
|
2019-06-08 14:31:11 +00:00
|
|
|
Name: t.Name,
|
2021-06-17 16:04:10 +00:00
|
|
|
Message: strings.TrimSpace(t.Message),
|
2019-06-08 14:31:11 +00:00
|
|
|
ID: t.ID.String(),
|
|
|
|
Commit: ToCommitMeta(repo, t),
|
|
|
|
ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
|
|
|
|
TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
|
2019-02-07 12:00:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
|
|
|
|
func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
|
2021-12-10 08:14:24 +00:00
|
|
|
verif := asymkey_model.ParseCommitWithSignature(c)
|
2019-10-16 13:42:42 +00:00
|
|
|
commitVerification := &api.PayloadCommitVerification{
|
|
|
|
Verified: verif.Verified,
|
|
|
|
Reason: verif.Reason,
|
|
|
|
}
|
2019-06-08 14:31:11 +00:00
|
|
|
if c.Signature != nil {
|
2019-10-16 13:42:42 +00:00
|
|
|
commitVerification.Signature = c.Signature.Signature
|
|
|
|
commitVerification.Payload = c.Signature.Payload
|
2019-06-08 14:31:11 +00:00
|
|
|
}
|
2019-10-16 13:42:42 +00:00
|
|
|
if verif.SigningUser != nil {
|
2021-11-08 07:04:13 +00:00
|
|
|
commitVerification.Signer = &api.PayloadUser{
|
2019-10-16 13:42:42 +00:00
|
|
|
Name: verif.SigningUser.Name,
|
|
|
|
Email: verif.SigningUser.Email,
|
|
|
|
}
|
2016-01-28 19:49:05 +00:00
|
|
|
}
|
2019-10-16 13:42:42 +00:00
|
|
|
return commitVerification
|
2016-01-28 19:49:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 08:14:24 +00:00
|
|
|
// ToPublicKey convert asymkey_model.PublicKey to api.PublicKey
|
|
|
|
func ToPublicKey(apiLink string, key *asymkey_model.PublicKey) *api.PublicKey {
|
2015-12-04 22:16:42 +00:00
|
|
|
return &api.PublicKey{
|
2017-11-28 15:21:39 +00:00
|
|
|
ID: key.ID,
|
|
|
|
Key: key.Content,
|
2020-12-25 09:59:32 +00:00
|
|
|
URL: fmt.Sprintf("%s%d", apiLink, key.ID),
|
2017-11-28 15:21:39 +00:00
|
|
|
Title: key.Name,
|
|
|
|
Fingerprint: key.Fingerprint,
|
2017-12-11 04:37:04 +00:00
|
|
|
Created: key.CreatedUnix.AsTime(),
|
2015-12-04 22:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 01:27:35 +00:00
|
|
|
// ToGPGKey converts models.GPGKey to api.GPGKey
|
2021-12-10 08:14:24 +00:00
|
|
|
func ToGPGKey(key *asymkey_model.GPGKey) *api.GPGKey {
|
2017-03-16 01:27:35 +00:00
|
|
|
subkeys := make([]*api.GPGKey, len(key.SubsKey))
|
|
|
|
for id, k := range key.SubsKey {
|
|
|
|
subkeys[id] = &api.GPGKey{
|
|
|
|
ID: k.ID,
|
|
|
|
PrimaryKeyID: k.PrimaryKeyID,
|
|
|
|
KeyID: k.KeyID,
|
|
|
|
PublicKey: k.Content,
|
2017-12-11 04:37:04 +00:00
|
|
|
Created: k.CreatedUnix.AsTime(),
|
|
|
|
Expires: k.ExpiredUnix.AsTime(),
|
2017-03-16 01:27:35 +00:00
|
|
|
CanSign: k.CanSign,
|
|
|
|
CanEncryptComms: k.CanEncryptComms,
|
|
|
|
CanEncryptStorage: k.CanEncryptStorage,
|
|
|
|
CanCertify: k.CanSign,
|
2021-07-13 13:28:07 +00:00
|
|
|
Verified: k.Verified,
|
2017-03-16 01:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
emails := make([]*api.GPGKeyEmail, len(key.Emails))
|
|
|
|
for i, e := range key.Emails {
|
|
|
|
emails[i] = ToGPGKeyEmail(e)
|
|
|
|
}
|
|
|
|
return &api.GPGKey{
|
|
|
|
ID: key.ID,
|
|
|
|
PrimaryKeyID: key.PrimaryKeyID,
|
|
|
|
KeyID: key.KeyID,
|
|
|
|
PublicKey: key.Content,
|
2017-12-11 04:37:04 +00:00
|
|
|
Created: key.CreatedUnix.AsTime(),
|
|
|
|
Expires: key.ExpiredUnix.AsTime(),
|
2017-03-16 01:27:35 +00:00
|
|
|
Emails: emails,
|
|
|
|
SubsKey: subkeys,
|
|
|
|
CanSign: key.CanSign,
|
|
|
|
CanEncryptComms: key.CanEncryptComms,
|
|
|
|
CanEncryptStorage: key.CanEncryptStorage,
|
|
|
|
CanCertify: key.CanSign,
|
2021-07-13 13:28:07 +00:00
|
|
|
Verified: key.Verified,
|
2017-03-16 01:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail
|
2021-11-11 07:03:30 +00:00
|
|
|
func ToGPGKeyEmail(email *user_model.EmailAddress) *api.GPGKeyEmail {
|
2017-03-16 01:27:35 +00:00
|
|
|
return &api.GPGKeyEmail{
|
|
|
|
Email: email.Email,
|
|
|
|
Verified: email.IsActivated,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToHook convert models.Webhook to api.Hook
|
2022-11-03 18:23:20 +00:00
|
|
|
func ToHook(repoLink string, w *webhook.Webhook) (*api.Hook, error) {
|
2015-12-04 22:16:42 +00:00
|
|
|
config := map[string]string{
|
|
|
|
"url": w.URL,
|
|
|
|
"content_type": w.ContentType.Name(),
|
|
|
|
}
|
2021-11-10 05:13:16 +00:00
|
|
|
if w.Type == webhook.SLACK {
|
|
|
|
s := webhook_service.GetSlackHook(w)
|
2015-12-04 22:16:42 +00:00
|
|
|
config["channel"] = s.Channel
|
|
|
|
config["username"] = s.Username
|
|
|
|
config["icon_url"] = s.IconURL
|
|
|
|
config["color"] = s.Color
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:23:20 +00:00
|
|
|
authorizationHeader, err := w.HeaderAuthorization()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-04 22:16:42 +00:00
|
|
|
}
|
2022-11-03 18:23:20 +00:00
|
|
|
|
|
|
|
return &api.Hook{
|
|
|
|
ID: w.ID,
|
|
|
|
Type: w.Type,
|
|
|
|
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
|
|
|
|
Active: w.IsActive,
|
|
|
|
Config: config,
|
|
|
|
Events: w.EventsArray(),
|
|
|
|
AuthorizationHeader: authorizationHeader,
|
|
|
|
Updated: w.UpdatedUnix.AsTime(),
|
|
|
|
Created: w.CreatedUnix.AsTime(),
|
|
|
|
}, nil
|
2015-12-04 22:16:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 05:31:08 +00:00
|
|
|
// ToGitHook convert git.Hook to api.GitHook
|
|
|
|
func ToGitHook(h *git.Hook) *api.GitHook {
|
|
|
|
return &api.GitHook{
|
|
|
|
Name: h.Name(),
|
|
|
|
IsActive: h.IsActive,
|
|
|
|
Content: h.Content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 08:14:24 +00:00
|
|
|
// ToDeployKey convert asymkey_model.DeployKey to api.DeployKey
|
|
|
|
func ToDeployKey(apiLink string, key *asymkey_model.DeployKey) *api.DeployKey {
|
2015-12-04 22:16:42 +00:00
|
|
|
return &api.DeployKey{
|
2018-11-01 03:40:49 +00:00
|
|
|
ID: key.ID,
|
|
|
|
KeyID: key.KeyID,
|
|
|
|
Key: key.Content,
|
|
|
|
Fingerprint: key.Fingerprint,
|
2020-12-25 09:59:32 +00:00
|
|
|
URL: fmt.Sprintf("%s%d", apiLink, key.ID),
|
2018-11-01 03:40:49 +00:00
|
|
|
Title: key.Name,
|
|
|
|
Created: key.CreatedUnix.AsTime(),
|
2021-11-28 11:58:28 +00:00
|
|
|
ReadOnly: key.Mode == perm.AccessModeRead, // All deploy keys are read-only.
|
2015-12-04 22:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 07:28:47 +00:00
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
// ToOrganization convert user_model.User to api.Organization
|
2022-03-29 06:29:02 +00:00
|
|
|
func ToOrganization(org *organization.Organization) *api.Organization {
|
2015-12-17 07:28:47 +00:00
|
|
|
return &api.Organization{
|
2019-09-23 20:08:03 +00:00
|
|
|
ID: org.ID,
|
2021-11-19 11:41:40 +00:00
|
|
|
AvatarURL: org.AsUser().AvatarLink(),
|
2022-09-29 03:27:33 +00:00
|
|
|
Name: org.Name,
|
2019-09-23 20:08:03 +00:00
|
|
|
UserName: org.Name,
|
|
|
|
FullName: org.FullName,
|
|
|
|
Description: org.Description,
|
|
|
|
Website: org.Website,
|
|
|
|
Location: org.Location,
|
|
|
|
Visibility: org.Visibility.String(),
|
|
|
|
RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess,
|
2015-12-17 07:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 16:47:54 +00:00
|
|
|
|
2022-05-13 17:27:58 +00:00
|
|
|
// ToTeam convert models.Team to api.Team
|
|
|
|
func ToTeam(team *organization.Team, loadOrg ...bool) (*api.Team, error) {
|
|
|
|
teams, err := ToTeams([]*organization.Team{team}, len(loadOrg) != 0 && loadOrg[0])
|
|
|
|
if err != nil || len(teams) == 0 {
|
|
|
|
return nil, err
|
2020-10-20 18:18:25 +00:00
|
|
|
}
|
2022-05-13 17:27:58 +00:00
|
|
|
return teams[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToTeams convert models.Team list to api.Team list
|
|
|
|
func ToTeams(teams []*organization.Team, loadOrgs bool) ([]*api.Team, error) {
|
|
|
|
if len(teams) == 0 || teams[0] == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := make(map[int64]*api.Organization)
|
|
|
|
apiTeams := make([]*api.Team, len(teams))
|
|
|
|
for i := range teams {
|
|
|
|
if err := teams[i].GetUnits(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiTeams[i] = &api.Team{
|
|
|
|
ID: teams[i].ID,
|
|
|
|
Name: teams[i].Name,
|
|
|
|
Description: teams[i].Description,
|
|
|
|
IncludesAllRepositories: teams[i].IncludesAllRepositories,
|
|
|
|
CanCreateOrgRepo: teams[i].CanCreateOrgRepo,
|
|
|
|
Permission: teams[i].AccessMode.String(),
|
|
|
|
Units: teams[i].GetUnitNames(),
|
|
|
|
UnitsMap: teams[i].GetUnitsMap(),
|
|
|
|
}
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2022-05-13 17:27:58 +00:00
|
|
|
if loadOrgs {
|
|
|
|
apiOrg, ok := cache[teams[i].OrgID]
|
|
|
|
if !ok {
|
2022-05-20 14:08:52 +00:00
|
|
|
org, err := organization.GetOrgByID(db.DefaultContext, teams[i].OrgID)
|
2022-05-13 17:27:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apiOrg = ToOrganization(org)
|
|
|
|
cache[teams[i].OrgID] = apiOrg
|
|
|
|
}
|
|
|
|
apiTeams[i].Organization = apiOrg
|
|
|
|
}
|
2016-03-21 16:47:54 +00:00
|
|
|
}
|
2022-05-13 17:27:58 +00:00
|
|
|
return apiTeams, nil
|
2016-03-21 16:47:54 +00:00
|
|
|
}
|
2019-04-15 16:36:59 +00:00
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
|
2021-12-10 01:27:50 +00:00
|
|
|
func ToAnnotatedTag(repo *repo_model.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
2019-06-08 14:31:11 +00:00
|
|
|
return &api.AnnotatedTag{
|
|
|
|
Tag: t.Name,
|
|
|
|
SHA: t.ID.String(),
|
|
|
|
Object: ToAnnotatedTagObject(repo, c),
|
|
|
|
Message: t.Message,
|
|
|
|
URL: util.URLJoin(repo.APIURL(), "git/tags", t.ID.String()),
|
|
|
|
Tagger: ToCommitUser(t.Tagger),
|
|
|
|
Verification: ToVerification(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAnnotatedTagObject convert a git.Commit to an api.AnnotatedTagObject
|
2021-12-10 01:27:50 +00:00
|
|
|
func ToAnnotatedTagObject(repo *repo_model.Repository, commit *git.Commit) *api.AnnotatedTagObject {
|
2019-06-08 14:31:11 +00:00
|
|
|
return &api.AnnotatedTagObject{
|
|
|
|
SHA: commit.ID.String(),
|
|
|
|
Type: string(git.ObjectCommit),
|
|
|
|
URL: util.URLJoin(repo.APIURL(), "git/commits", commit.ID.String()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 15:46:24 +00:00
|
|
|
// ToTopicResponse convert from models.Topic to api.TopicResponse
|
2021-12-12 15:48:20 +00:00
|
|
|
func ToTopicResponse(topic *repo_model.Topic) *api.TopicResponse {
|
2019-09-03 15:46:24 +00:00
|
|
|
return &api.TopicResponse{
|
|
|
|
ID: topic.ID,
|
|
|
|
Name: topic.Name,
|
|
|
|
RepoCount: topic.RepoCount,
|
|
|
|
Created: topic.CreatedUnix.AsTime(),
|
|
|
|
Updated: topic.UpdatedUnix.AsTime(),
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 06:19:32 +00:00
|
|
|
|
2022-01-02 13:12:35 +00:00
|
|
|
// ToOAuth2Application convert from auth.OAuth2Application to api.OAuth2Application
|
|
|
|
func ToOAuth2Application(app *auth.OAuth2Application) *api.OAuth2Application {
|
2020-02-29 06:19:32 +00:00
|
|
|
return &api.OAuth2Application{
|
2022-10-24 07:59:24 +00:00
|
|
|
ID: app.ID,
|
|
|
|
Name: app.Name,
|
|
|
|
ClientID: app.ClientID,
|
|
|
|
ClientSecret: app.ClientSecret,
|
|
|
|
ConfidentialClient: app.ConfidentialClient,
|
|
|
|
RedirectURIs: app.RedirectURIs,
|
|
|
|
Created: app.CreatedUnix.AsTime(),
|
2020-02-29 06:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-17 04:23:08 +00:00
|
|
|
|
2020-12-03 14:05:48 +00:00
|
|
|
// ToLFSLock convert a LFSLock to api.LFSLock
|
2022-06-12 15:51:54 +00:00
|
|
|
func ToLFSLock(l *git_model.LFSLock) *api.LFSLock {
|
2021-11-24 09:49:20 +00:00
|
|
|
u, err := user_model.GetUserByID(l.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-03 14:05:48 +00:00
|
|
|
return &api.LFSLock{
|
|
|
|
ID: strconv.FormatInt(l.ID, 10),
|
|
|
|
Path: l.Path,
|
|
|
|
LockedAt: l.Created.Round(time.Second),
|
|
|
|
Owner: &api.LFSLockOwner{
|
2022-10-12 01:03:15 +00:00
|
|
|
Name: u.Name,
|
2020-12-03 14:05:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 02:27:20 +00:00
|
|
|
|
|
|
|
// ToChangedFile convert a gitdiff.DiffFile to api.ChangedFile
|
|
|
|
func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit string) *api.ChangedFile {
|
|
|
|
status := "changed"
|
|
|
|
if f.IsDeleted {
|
|
|
|
status = "deleted"
|
|
|
|
} else if f.IsCreated {
|
|
|
|
status = "added"
|
|
|
|
} else if f.IsRenamed && f.Type == gitdiff.DiffFileCopy {
|
|
|
|
status = "copied"
|
|
|
|
} else if f.IsRenamed && f.Type == gitdiff.DiffFileRename {
|
|
|
|
status = "renamed"
|
|
|
|
} else if f.Addition == 0 && f.Deletion == 0 {
|
|
|
|
status = "unchanged"
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &api.ChangedFile{
|
|
|
|
Filename: f.GetDiffFileName(),
|
|
|
|
Status: status,
|
|
|
|
Additions: f.Addition,
|
|
|
|
Deletions: f.Deletion,
|
|
|
|
Changes: f.Addition + f.Deletion,
|
|
|
|
HTMLURL: fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())),
|
|
|
|
ContentsURL: fmt.Sprint(repo.APIURL(), "/contents/", util.PathEscapeSegments(f.GetDiffFileName()), "?ref=", commit),
|
|
|
|
RawURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())),
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == "rename" {
|
|
|
|
file.PreviousFilename = f.OldName
|
|
|
|
}
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|