2015-12-04 22:16:42 +00: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 07:28:47 +00:00
|
|
|
package convert
|
2015-12-04 22:16:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-08 14:31:11 +00:00
|
|
|
"time"
|
2015-12-04 22:16:42 +00:00
|
|
|
|
2016-11-10 16:24:48 +00:00
|
|
|
"code.gitea.io/gitea/models"
|
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-04-15 16:36:59 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
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"
|
2019-03-27 09:33:00 +00:00
|
|
|
|
2019-08-23 16:40:30 +00:00
|
|
|
"github.com/unknwon/com"
|
2015-12-04 22:16:42 +00:00
|
|
|
)
|
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToEmail convert models.EmailAddress to api.Email
|
2016-03-14 03:20:22 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:11 +00:00
|
|
|
// ToBranch convert a git.Commit and git.Branch to an api.Branch
|
2019-04-19 12:17:27 +00:00
|
|
|
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit) *api.Branch {
|
2016-01-28 19:49:05 +00:00
|
|
|
return &api.Branch{
|
2016-03-14 03:20:22 +00:00
|
|
|
Name: b.Name,
|
2018-02-20 12:50:42 +00:00
|
|
|
Commit: ToCommit(repo, c),
|
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
|
2019-02-07 12:00:52 +00:00
|
|
|
func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
|
|
|
|
return &api.Tag{
|
2019-06-08 14:31:11 +00:00
|
|
|
Name: t.Name,
|
|
|
|
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
|
|
|
// ToCommit convert a git.Commit to api.PayloadCommit
|
2018-02-20 12:50:42 +00:00
|
|
|
func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
|
2016-08-10 05:01:57 +00:00
|
|
|
authorUsername := ""
|
2018-02-20 12:50:42 +00:00
|
|
|
if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
|
2016-08-10 05:01:57 +00:00
|
|
|
authorUsername = author.Name
|
2018-02-20 12:50:42 +00:00
|
|
|
} else if !models.IsErrUserNotExist(err) {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("GetUserByEmail: %v", err)
|
2016-08-10 05:01:57 +00:00
|
|
|
}
|
2018-02-20 12:50:42 +00:00
|
|
|
|
2016-08-10 05:01:57 +00:00
|
|
|
committerUsername := ""
|
2018-02-20 12:50:42 +00:00
|
|
|
if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
|
2016-08-10 05:01:57 +00:00
|
|
|
committerUsername = committer.Name
|
2018-02-20 12:50:42 +00:00
|
|
|
} else if !models.IsErrUserNotExist(err) {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("GetUserByEmail: %v", err)
|
2016-08-10 05:01:57 +00:00
|
|
|
}
|
2018-02-20 12:50:42 +00:00
|
|
|
|
2016-01-28 19:49:05 +00:00
|
|
|
return &api.PayloadCommit{
|
2016-03-14 03:20:22 +00:00
|
|
|
ID: c.ID.String(),
|
2016-01-28 19:49:05 +00:00
|
|
|
Message: c.Message(),
|
2019-06-08 14:31:11 +00:00
|
|
|
URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()),
|
2016-08-14 11:17:26 +00:00
|
|
|
Author: &api.PayloadUser{
|
2016-08-10 05:01:57 +00:00
|
|
|
Name: c.Author.Name,
|
|
|
|
Email: c.Author.Email,
|
|
|
|
UserName: authorUsername,
|
|
|
|
},
|
2016-08-14 11:17:26 +00:00
|
|
|
Committer: &api.PayloadUser{
|
2016-08-10 05:01:57 +00:00
|
|
|
Name: c.Committer.Name,
|
|
|
|
Email: c.Committer.Email,
|
|
|
|
UserName: committerUsername,
|
2016-01-28 19:49:05 +00:00
|
|
|
},
|
2019-06-08 14:31:11 +00:00
|
|
|
Timestamp: c.Author.When,
|
|
|
|
Verification: ToVerification(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
|
|
|
|
func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
|
|
|
|
verif := models.ParseCommitWithSignature(c)
|
|
|
|
var signature, payload string
|
|
|
|
if c.Signature != nil {
|
|
|
|
signature = c.Signature.Signature
|
|
|
|
payload = c.Signature.Payload
|
|
|
|
}
|
|
|
|
return &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
|
2016-03-14 03:20:22 +00:00
|
|
|
func ToPublicKey(apiLink string, key *models.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,
|
|
|
|
URL: apiLink + com.ToStr(key.ID),
|
|
|
|
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
|
|
|
|
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 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-03-14 03:20:22 +00:00
|
|
|
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
|
2015-12-04 22:16:42 +00: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 04:37:04 +00:00
|
|
|
Updated: w.UpdatedUnix.AsTime(),
|
|
|
|
Created: w.CreatedUnix.AsTime(),
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToDeployKey convert models.DeployKey to api.DeployKey
|
2016-03-14 03:20:22 +00:00
|
|
|
func ToDeployKey(apiLink string, key *models.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,
|
|
|
|
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 22:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 07:28:47 +00:00
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToOrganization convert models.User to api.Organization
|
2016-03-14 03:20:22 +00:00
|
|
|
func ToOrganization(org *models.User) *api.Organization {
|
2015-12-17 07:28:47 +00:00
|
|
|
return &api.Organization{
|
2019-09-23 20:08:03 +00:00
|
|
|
ID: org.ID,
|
|
|
|
AvatarURL: org.AvatarLink(),
|
|
|
|
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
|
|
|
|
2016-11-24 07:04:31 +00:00
|
|
|
// ToTeam convert models.Team to api.Team
|
2016-03-21 16:47:54 +00: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 19:45:32 +00:00
|
|
|
Units: team.GetUnitNames(),
|
2016-03-21 16:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 16:36:59 +00:00
|
|
|
|
|
|
|
// ToUser convert models.User to api.User
|
2019-07-27 13:15:30 +00:00
|
|
|
func ToUser(user *models.User, signed, authed bool) *api.User {
|
2019-04-15 16:36:59 +00:00
|
|
|
result := &api.User{
|
|
|
|
ID: user.ID,
|
|
|
|
UserName: user.Name,
|
|
|
|
AvatarURL: user.AvatarLink(),
|
|
|
|
FullName: markup.Sanitize(user.FullName),
|
|
|
|
IsAdmin: user.IsAdmin,
|
2019-06-16 03:28:32 +00:00
|
|
|
LastLogin: user.LastLoginUnix.AsTime(),
|
|
|
|
Created: user.CreatedUnix.AsTime(),
|
2019-04-15 16:36:59 +00:00
|
|
|
}
|
2019-07-27 13:15:30 +00:00
|
|
|
// hide primary email if API caller isn't user itself or an admin
|
|
|
|
if !signed {
|
|
|
|
result.Email = ""
|
|
|
|
} else if user.KeepEmailPrivate && !authed {
|
|
|
|
result.Email = user.GetEmail()
|
|
|
|
} else {
|
2019-04-15 16:36:59 +00:00
|
|
|
result.Email = user.Email
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2019-06-08 14:31:11 +00:00
|
|
|
|
|
|
|
// ToAnnotatedTag convert git.Tag to api.AnnotatedTag
|
|
|
|
func ToAnnotatedTag(repo *models.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
|
|
|
|
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
|
|
|
|
func ToAnnotatedTagObject(repo *models.Repository, commit *git.Commit) *api.AnnotatedTagObject {
|
|
|
|
return &api.AnnotatedTagObject{
|
|
|
|
SHA: commit.ID.String(),
|
|
|
|
Type: string(git.ObjectCommit),
|
|
|
|
URL: util.URLJoin(repo.APIURL(), "git/commits", commit.ID.String()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToCommitUser convert a git.Signature to an api.CommitUser
|
|
|
|
func ToCommitUser(sig *git.Signature) *api.CommitUser {
|
|
|
|
return &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
|
|
|
Name: sig.Name,
|
|
|
|
Email: sig.Email,
|
|
|
|
},
|
|
|
|
Date: sig.When.UTC().Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToCommitMeta convert a git.Tag to an api.CommitMeta
|
|
|
|
func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
|
|
|
|
return &api.CommitMeta{
|
2019-06-29 10:44:17 +00:00
|
|
|
SHA: tag.Object.String(),
|
2019-06-08 14:31:11 +00:00
|
|
|
// TODO: Add the /commits API endpoint and use it here (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
|
|
|
|
URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 15:46:24 +00:00
|
|
|
|
|
|
|
// ToTopicResponse convert from models.Topic to api.TopicResponse
|
|
|
|
func ToTopicResponse(topic *models.Topic) *api.TopicResponse {
|
|
|
|
return &api.TopicResponse{
|
|
|
|
ID: topic.ID,
|
|
|
|
Name: topic.Name,
|
|
|
|
RepoCount: topic.RepoCount,
|
|
|
|
Created: topic.CreatedUnix.AsTime(),
|
|
|
|
Updated: topic.UpdatedUnix.AsTime(),
|
|
|
|
}
|
|
|
|
}
|