1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-14 14:14:25 +00:00
gitea/routers/api/v1/convert/convert.go

193 lines
4.9 KiB
Go
Raw Normal View History

// 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 07:28:47 +00:00
package convert
import (
"fmt"
"github.com/Unknwon/com"
api "code.gitea.io/sdk/gitea"
2016-11-24 07:04:31 +00:00
"code.gitea.io/git"
"code.gitea.io/gitea/models"
)
2016-11-24 07:04:31 +00:00
// ToEmail convert models.EmailAddress to api.Email
func ToEmail(email *models.EmailAddress) *api.Email {
2015-12-16 03:57:18 +00:00
return &api.Email{
Email: email.Email,
Verified: email.IsActivated,
Primary: email.IsPrimary,
}
}
2016-11-24 07:04:31 +00:00
// ToBranch convert a commit and branch to an api.Branch
func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
2016-01-28 19:49:05 +00:00
return &api.Branch{
Name: b.Name,
Commit: ToCommit(c),
}
2016-01-28 19:49:05 +00:00
}
2016-11-24 07:04:31 +00:00
// ToCommit convert a commit to api.PayloadCommit
func ToCommit(c *git.Commit) *api.PayloadCommit {
authorUsername := ""
author, err := models.GetUserByEmail(c.Author.Email)
if err == nil {
authorUsername = author.Name
}
committerUsername := ""
committer, err := models.GetUserByEmail(c.Committer.Email)
if err == nil {
committerUsername = committer.Name
}
verif := models.ParseCommitWithSignature(c)
var signature, payload string
if c.Signature != nil {
signature = c.Signature.Signature
payload = c.Signature.Payload
}
2016-01-28 19:49:05 +00:00
return &api.PayloadCommit{
ID: c.ID.String(),
2016-01-28 19:49:05 +00:00
Message: c.Message(),
URL: "Not implemented",
Author: &api.PayloadUser{
Name: c.Author.Name,
Email: c.Author.Email,
UserName: authorUsername,
},
Committer: &api.PayloadUser{
Name: c.Committer.Name,
Email: c.Committer.Email,
UserName: committerUsername,
2016-01-28 19:49:05 +00:00
},
Timestamp: c.Author.When,
Verification: &api.PayloadCommitVerification{
Verified: verif.Verified,
Reason: verif.Reason,
Signature: signature,
Payload: payload,
},
2016-01-28 19:49:05 +00:00
}
}
2016-11-24 07:04:31 +00:00
// ToPublicKey convert models.PublicKey to api.PublicKey
func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
return &api.PublicKey{
ID: key.ID,
Key: key.Content,
URL: apiLink + com.ToStr(key.ID),
Title: key.Name,
Created: key.Created,
}
}
// 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,
Created: k.Created,
Expires: k.Expired,
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,
Created: key.Created,
Expires: key.Expired,
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 07:04:31 +00:00
// ToHook convert models.Webhook to api.Hook
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
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(),
Updated: w.Updated,
Created: w.Created,
}
}
2016-11-24 07:04:31 +00:00
// ToDeployKey convert models.DeployKey to api.DeployKey
func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
return &api.DeployKey{
ID: key.ID,
Key: key.Content,
URL: apiLink + com.ToStr(key.ID),
Title: key.Name,
Created: key.Created,
ReadOnly: true, // All deploy keys are read-only.
}
}
2015-12-17 07:28:47 +00:00
2016-11-24 07:04:31 +00:00
// ToOrganization convert models.User to api.Organization
func ToOrganization(org *models.User) *api.Organization {
2015-12-17 07:28:47 +00:00
return &api.Organization{
2016-07-23 17:08:22 +00:00
ID: org.ID,
2016-11-29 08:25:47 +00:00
AvatarURL: org.AvatarLink(),
2015-12-17 07:28:47 +00:00
UserName: org.Name,
FullName: org.FullName,
Description: org.Description,
Website: org.Website,
Location: org.Location,
}
}
2016-11-24 07:04:31 +00:00
// ToTeam convert models.Team to api.Team
func ToTeam(team *models.Team) *api.Team {
return &api.Team{
ID: team.ID,
Name: team.Name,
Description: team.Description,
Permission: team.Authorize.String(),
}
}