2015-12-04 23:16:42 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-12-17 08:28:47 +01:00
|
|
|
package convert
|
2015-12-04 23:16:42 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
|
|
|
|
2016-11-11 10:39:44 +01:00
|
|
|
api "code.gitea.io/sdk/gitea"
|
2015-12-04 23:16:42 +01:00
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
"code.gitea.io/git"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/models"
|
2018-02-20 13:50:42 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2015-12-04 23:16:42 +01:00
|
|
|
)
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToEmail convert models.EmailAddress to api.Email
|
2016-03-14 04:20:22 +01:00
|
|
|
func ToEmail(email *models.EmailAddress) *api.Email {
|
2015-12-16 04:57:18 +01:00
|
|
|
return &api.Email{
|
|
|
|
Email: email.Email,
|
|
|
|
Verified: email.IsActivated,
|
|
|
|
Primary: email.IsPrimary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToBranch convert a commit and branch to an api.Branch
|
2018-02-20 13:50:42 +01:00
|
|
|
func ToBranch(repo *models.Repository, b *models.Branch, c *git.Commit) *api.Branch {
|
2016-01-28 20:49:05 +01:00
|
|
|
return &api.Branch{
|
2016-03-14 04:20:22 +01:00
|
|
|
Name: b.Name,
|
2018-02-20 13:50:42 +01:00
|
|
|
Commit: ToCommit(repo, c),
|
2016-03-14 04:20:22 +01:00
|
|
|
}
|
2016-01-28 20:49:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 13:00:52 +01:00
|
|
|
// ToTag convert a tag to an api.Tag
|
|
|
|
func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
|
|
|
|
return &api.Tag{
|
|
|
|
Name: t.Name,
|
|
|
|
Commit: struct {
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}{
|
|
|
|
SHA: t.ID.String(),
|
|
|
|
URL: util.URLJoin(repo.Link(), "commit", t.ID.String()),
|
|
|
|
},
|
|
|
|
ZipballURL: util.URLJoin(repo.Link(), "archive", t.Name+".zip"),
|
|
|
|
TarballURL: util.URLJoin(repo.Link(), "archive", t.Name+".tar.gz"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToCommit convert a commit to api.PayloadCommit
|
2018-02-20 13:50:42 +01:00
|
|
|
func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
|
2016-08-10 07:01:57 +02:00
|
|
|
authorUsername := ""
|
2018-02-20 13:50:42 +01:00
|
|
|
if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
|
2016-08-10 07:01:57 +02:00
|
|
|
authorUsername = author.Name
|
2018-02-20 13:50:42 +01:00
|
|
|
} else if !models.IsErrUserNotExist(err) {
|
|
|
|
log.Error(4, "GetUserByEmail: %v", err)
|
2016-08-10 07:01:57 +02:00
|
|
|
}
|
2018-02-20 13:50:42 +01:00
|
|
|
|
2016-08-10 07:01:57 +02:00
|
|
|
committerUsername := ""
|
2018-02-20 13:50:42 +01:00
|
|
|
if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
|
2016-08-10 07:01:57 +02:00
|
|
|
committerUsername = committer.Name
|
2018-02-20 13:50:42 +01:00
|
|
|
} else if !models.IsErrUserNotExist(err) {
|
|
|
|
log.Error(4, "GetUserByEmail: %v", err)
|
2016-08-10 07:01:57 +02:00
|
|
|
}
|
2018-02-20 13:50:42 +01:00
|
|
|
|
2017-03-22 11:43:54 +01:00
|
|
|
verif := models.ParseCommitWithSignature(c)
|
2017-04-07 03:40:05 +02:00
|
|
|
var signature, payload string
|
|
|
|
if c.Signature != nil {
|
|
|
|
signature = c.Signature.Signature
|
|
|
|
payload = c.Signature.Payload
|
|
|
|
}
|
2018-02-20 13:50:42 +01:00
|
|
|
|
2016-01-28 20:49:05 +01:00
|
|
|
return &api.PayloadCommit{
|
2016-03-14 04:20:22 +01:00
|
|
|
ID: c.ID.String(),
|
2016-01-28 20:49:05 +01:00
|
|
|
Message: c.Message(),
|
2018-02-20 13:50:42 +01:00
|
|
|
URL: util.URLJoin(repo.Link(), "commit", c.ID.String()),
|
2016-08-14 13:17:26 +02:00
|
|
|
Author: &api.PayloadUser{
|
2016-08-10 07:01:57 +02:00
|
|
|
Name: c.Author.Name,
|
|
|
|
Email: c.Author.Email,
|
|
|
|
UserName: authorUsername,
|
|
|
|
},
|
2016-08-14 13:17:26 +02:00
|
|
|
Committer: &api.PayloadUser{
|
2016-08-10 07:01:57 +02:00
|
|
|
Name: c.Committer.Name,
|
|
|
|
Email: c.Committer.Email,
|
|
|
|
UserName: committerUsername,
|
2016-01-28 20:49:05 +01:00
|
|
|
},
|
2016-08-10 07:01:57 +02:00
|
|
|
Timestamp: c.Author.When,
|
2017-03-22 11:43:54 +01:00
|
|
|
Verification: &api.PayloadCommitVerification{
|
|
|
|
Verified: verif.Verified,
|
|
|
|
Reason: verif.Reason,
|
2017-04-07 03:40:05 +02:00
|
|
|
Signature: signature,
|
|
|
|
Payload: payload,
|
2017-03-22 11:43:54 +01:00
|
|
|
},
|
2016-01-28 20:49:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToPublicKey convert models.PublicKey to api.PublicKey
|
2016-03-14 04:20:22 +01:00
|
|
|
func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
|
2015-12-04 23:16:42 +01:00
|
|
|
return &api.PublicKey{
|
2017-11-28 16:21:39 +01:00
|
|
|
ID: key.ID,
|
|
|
|
Key: key.Content,
|
|
|
|
URL: apiLink + com.ToStr(key.ID),
|
|
|
|
Title: key.Name,
|
|
|
|
Fingerprint: key.Fingerprint,
|
2017-12-11 05:37:04 +01:00
|
|
|
Created: key.CreatedUnix.AsTime(),
|
2015-12-04 23:16:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 02:27:35 +01:00
|
|
|
// ToGPGKey converts models.GPGKey to api.GPGKey
|
|
|
|
func ToGPGKey(key *models.GPGKey) *api.GPGKey {
|
|
|
|
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 05:37:04 +01:00
|
|
|
Created: k.CreatedUnix.AsTime(),
|
|
|
|
Expires: k.ExpiredUnix.AsTime(),
|
2017-03-16 02:27:35 +01:00
|
|
|
CanSign: k.CanSign,
|
|
|
|
CanEncryptComms: k.CanEncryptComms,
|
|
|
|
CanEncryptStorage: k.CanEncryptStorage,
|
|
|
|
CanCertify: k.CanSign,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 05:37:04 +01:00
|
|
|
Created: key.CreatedUnix.AsTime(),
|
|
|
|
Expires: key.ExpiredUnix.AsTime(),
|
2017-03-16 02:27:35 +01:00
|
|
|
Emails: emails,
|
|
|
|
SubsKey: subkeys,
|
|
|
|
CanSign: key.CanSign,
|
|
|
|
CanEncryptComms: key.CanEncryptComms,
|
|
|
|
CanEncryptStorage: key.CanEncryptStorage,
|
|
|
|
CanCertify: key.CanSign,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail
|
|
|
|
func ToGPGKeyEmail(email *models.EmailAddress) *api.GPGKeyEmail {
|
|
|
|
return &api.GPGKeyEmail{
|
|
|
|
Email: email.Email,
|
|
|
|
Verified: email.IsActivated,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToHook convert models.Webhook to api.Hook
|
2016-03-14 04:20:22 +01:00
|
|
|
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
|
2015-12-04 23:16:42 +01:00
|
|
|
config := map[string]string{
|
|
|
|
"url": w.URL,
|
|
|
|
"content_type": w.ContentType.Name(),
|
|
|
|
}
|
|
|
|
if w.HookTaskType == models.SLACK {
|
|
|
|
s := w.GetSlackHook()
|
|
|
|
config["channel"] = s.Channel
|
|
|
|
config["username"] = s.Username
|
|
|
|
config["icon_url"] = s.IconURL
|
|
|
|
config["color"] = s.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.Hook{
|
|
|
|
ID: w.ID,
|
|
|
|
Type: w.HookTaskType.Name(),
|
|
|
|
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
|
|
|
|
Active: w.IsActive,
|
|
|
|
Config: config,
|
|
|
|
Events: w.EventsArray(),
|
2017-12-11 05:37:04 +01:00
|
|
|
Updated: w.UpdatedUnix.AsTime(),
|
|
|
|
Created: w.CreatedUnix.AsTime(),
|
2015-12-04 23:16:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToDeployKey convert models.DeployKey to api.DeployKey
|
2016-03-14 04:20:22 +01:00
|
|
|
func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
|
2015-12-04 23:16:42 +01:00
|
|
|
return &api.DeployKey{
|
2018-11-01 04:40:49 +01:00
|
|
|
ID: key.ID,
|
|
|
|
KeyID: key.KeyID,
|
|
|
|
Key: key.Content,
|
|
|
|
Fingerprint: key.Fingerprint,
|
|
|
|
URL: apiLink + com.ToStr(key.ID),
|
|
|
|
Title: key.Name,
|
|
|
|
Created: key.CreatedUnix.AsTime(),
|
|
|
|
ReadOnly: key.Mode == models.AccessModeRead, // All deploy keys are read-only.
|
2015-12-04 23:16:42 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 08:28:47 +01:00
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToOrganization convert models.User to api.Organization
|
2016-03-14 04:20:22 +01:00
|
|
|
func ToOrganization(org *models.User) *api.Organization {
|
2015-12-17 08:28:47 +01:00
|
|
|
return &api.Organization{
|
2016-07-23 19:08:22 +02:00
|
|
|
ID: org.ID,
|
2016-11-29 09:25:47 +01:00
|
|
|
AvatarURL: org.AvatarLink(),
|
2015-12-17 08:28:47 +01:00
|
|
|
UserName: org.Name,
|
|
|
|
FullName: org.FullName,
|
|
|
|
Description: org.Description,
|
|
|
|
Website: org.Website,
|
|
|
|
Location: org.Location,
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 17:47:54 +01:00
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// ToTeam convert models.Team to api.Team
|
2016-03-21 17:47:54 +01:00
|
|
|
func ToTeam(team *models.Team) *api.Team {
|
|
|
|
return &api.Team{
|
|
|
|
ID: team.ID,
|
|
|
|
Name: team.Name,
|
|
|
|
Description: team.Description,
|
|
|
|
Permission: team.Authorize.String(),
|
2018-11-10 20:45:32 +01:00
|
|
|
Units: team.GetUnitNames(),
|
2016-03-21 17:47:54 +01:00
|
|
|
}
|
|
|
|
}
|