Files
Atay-Makhzan/routers/api/v1/convert/convert.go
T

136 lines
3.3 KiB
Go
Raw Normal View History

2015-12-04 17:16:42 -05: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 02:28:47 -05:00
package convert
2015-12-04 17:16:42 -05:00
import (
"fmt"
"github.com/Unknwon/com"
2016-11-11 10:39:44 +01:00
api "code.gitea.io/sdk/gitea"
2015-12-04 17:16:42 -05:00
2016-11-24 15:04:31 +08:00
"code.gitea.io/git"
"code.gitea.io/gitea/models"
2015-12-04 17:16:42 -05:00
)
2016-11-24 15:04:31 +08:00
// ToEmail convert models.EmailAddress to api.Email
2016-03-13 23:20:22 -04:00
func ToEmail(email *models.EmailAddress) *api.Email {
2015-12-15 22:57:18 -05:00
return &api.Email{
Email: email.Email,
Verified: email.IsActivated,
Primary: email.IsPrimary,
}
}
2016-11-24 15:04:31 +08:00
// ToBranch convert a commit and branch to an api.Branch
2016-03-13 23:20:22 -04:00
func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
2016-01-28 20:49:05 +01:00
return &api.Branch{
2016-03-13 23:20:22 -04:00
Name: b.Name,
Commit: ToCommit(c),
}
2016-01-28 20:49:05 +01:00
}
2016-11-24 15:04:31 +08:00
// ToCommit convert a commit to api.PayloadCommit
2016-03-13 23:20:22 -04:00
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
}
2016-01-28 20:49:05 +01:00
return &api.PayloadCommit{
2016-03-13 23:20:22 -04:00
ID: c.ID.String(),
2016-01-28 20:49:05 +01:00
Message: c.Message(),
2016-03-13 23:20:22 -04:00
URL: "Not implemented",
2016-08-14 04:17:26 -07:00
Author: &api.PayloadUser{
Name: c.Author.Name,
Email: c.Author.Email,
UserName: authorUsername,
},
2016-08-14 04:17:26 -07:00
Committer: &api.PayloadUser{
Name: c.Committer.Name,
Email: c.Committer.Email,
UserName: committerUsername,
2016-01-28 20:49:05 +01:00
},
Timestamp: c.Author.When,
2016-01-28 20:49:05 +01:00
}
}
2016-11-24 15:04:31 +08:00
// ToPublicKey convert models.PublicKey to api.PublicKey
2016-03-13 23:20:22 -04:00
func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
2015-12-04 17:16:42 -05:00
return &api.PublicKey{
ID: key.ID,
Key: key.Content,
URL: apiLink + com.ToStr(key.ID),
Title: key.Name,
Created: key.Created,
}
}
2016-11-24 15:04:31 +08:00
// ToHook convert models.Webhook to api.Hook
2016-03-13 23:20:22 -04:00
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
2015-12-04 17:16:42 -05: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(),
Updated: w.Updated,
Created: w.Created,
}
}
2016-11-24 15:04:31 +08:00
// ToDeployKey convert models.DeployKey to api.DeployKey
2016-03-13 23:20:22 -04:00
func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
2015-12-04 17:16:42 -05:00
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 02:28:47 -05:00
2016-11-24 15:04:31 +08:00
// ToOrganization convert models.User to api.Organization
2016-03-13 23:20:22 -04:00
func ToOrganization(org *models.User) *api.Organization {
2015-12-17 02:28:47 -05:00
return &api.Organization{
2016-07-24 01:08:22 +08:00
ID: org.ID,
2015-12-17 02:28:47 -05:00
AvatarUrl: org.AvatarLink(),
UserName: org.Name,
FullName: org.FullName,
Description: org.Description,
Website: org.Website,
Location: org.Location,
}
}
2016-11-24 15:04:31 +08: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(),
}
}