2015-12-05 03:46:42 +05:30
|
|
|
// 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 12:58:47 +05:30
|
|
|
package convert
|
2015-12-05 03:46:42 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Unknwon/com"
|
|
|
|
|
2016-01-15 23:54:03 +05:30
|
|
|
"github.com/gogits/git-module"
|
2016-03-14 08:50:22 +05:30
|
|
|
api "github.com/gogits/go-gogs-client"
|
2015-12-05 03:46:42 +05:30
|
|
|
|
|
|
|
"github.com/gogits/gogs/models"
|
2016-03-14 08:50:22 +05:30
|
|
|
"github.com/gogits/gogs/modules/log"
|
2015-12-05 03:46:42 +05:30
|
|
|
"github.com/gogits/gogs/modules/setting"
|
|
|
|
)
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToUser(u *models.User) *api.User {
|
|
|
|
if u == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-05 03:46:42 +05:30
|
|
|
return &api.User{
|
|
|
|
ID: u.Id,
|
|
|
|
UserName: u.Name,
|
|
|
|
FullName: u.FullName,
|
|
|
|
Email: u.Email,
|
|
|
|
AvatarUrl: u.AvatarLink(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToEmail(email *models.EmailAddress) *api.Email {
|
2015-12-16 09:27:18 +05:30
|
|
|
return &api.Email{
|
|
|
|
Email: email.Email,
|
|
|
|
Verified: email.IsActivated,
|
|
|
|
Primary: email.IsPrimary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
|
2015-12-05 03:46:42 +05:30
|
|
|
cl := repo.CloneLink()
|
|
|
|
return &api.Repository{
|
2016-03-14 08:50:22 +05:30
|
|
|
ID: repo.ID,
|
|
|
|
Owner: ToUser(owner),
|
2015-12-05 03:46:42 +05:30
|
|
|
FullName: owner.Name + "/" + repo.Name,
|
|
|
|
Private: repo.IsPrivate,
|
|
|
|
Fork: repo.IsFork,
|
|
|
|
HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
|
|
|
|
CloneUrl: cl.HTTPS,
|
|
|
|
SshUrl: cl.SSH,
|
|
|
|
Permissions: permission,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
|
2016-01-29 01:19:05 +05:30
|
|
|
return &api.Branch{
|
2016-03-14 08:50:22 +05:30
|
|
|
Name: b.Name,
|
|
|
|
Commit: ToCommit(c),
|
|
|
|
}
|
2016-01-29 01:19:05 +05:30
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToCommit(c *git.Commit) *api.PayloadCommit {
|
2016-01-29 01:19:05 +05:30
|
|
|
return &api.PayloadCommit{
|
2016-03-14 08:50:22 +05:30
|
|
|
ID: c.ID.String(),
|
2016-01-29 01:19:05 +05:30
|
|
|
Message: c.Message(),
|
2016-03-14 08:50:22 +05:30
|
|
|
URL: "Not implemented",
|
2016-01-29 01:19:05 +05:30
|
|
|
Author: &api.PayloadAuthor{
|
2016-03-14 08:50:22 +05:30
|
|
|
Name: c.Committer.Name,
|
2016-01-29 01:19:05 +05:30
|
|
|
Email: c.Committer.Email,
|
|
|
|
/* UserName: c.Committer.UserName, */
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
|
2015-12-05 03:46:42 +05:30
|
|
|
return &api.PublicKey{
|
|
|
|
ID: key.ID,
|
|
|
|
Key: key.Content,
|
|
|
|
URL: apiLink + com.ToStr(key.ID),
|
|
|
|
Title: key.Name,
|
|
|
|
Created: key.Created,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToHook(repoLink string, w *models.Webhook) *api.Hook {
|
2015-12-05 03:46:42 +05:30
|
|
|
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-03-14 08:50:22 +05:30
|
|
|
func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
|
2015-12-05 03:46:42 +05:30
|
|
|
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 12:58:47 +05:30
|
|
|
|
2016-03-14 08:50:22 +05:30
|
|
|
func ToLabel(label *models.Label) *api.Label {
|
|
|
|
return &api.Label{
|
|
|
|
Name: label.Name,
|
|
|
|
Color: label.Color,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToMilestone(milestone *models.Milestone) *api.Milestone {
|
|
|
|
if milestone == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
apiMilestone := &api.Milestone{
|
|
|
|
ID: milestone.ID,
|
|
|
|
State: milestone.State(),
|
|
|
|
Title: milestone.Name,
|
|
|
|
Description: milestone.Content,
|
|
|
|
OpenIssues: milestone.NumOpenIssues,
|
|
|
|
ClosedIssues: milestone.NumClosedIssues,
|
|
|
|
}
|
|
|
|
if milestone.IsClosed {
|
|
|
|
apiMilestone.Closed = &milestone.ClosedDate
|
|
|
|
}
|
|
|
|
if milestone.Deadline.Year() < 9999 {
|
|
|
|
apiMilestone.Deadline = &milestone.Deadline
|
|
|
|
}
|
|
|
|
return apiMilestone
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToIssue(issue *models.Issue) *api.Issue {
|
|
|
|
apiLabels := make([]*api.Label, len(issue.Labels))
|
|
|
|
for i := range issue.Labels {
|
|
|
|
apiLabels[i] = ToLabel(issue.Labels[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
apiIssue := &api.Issue{
|
|
|
|
ID: issue.ID,
|
|
|
|
Index: issue.Index,
|
|
|
|
State: issue.State(),
|
|
|
|
Title: issue.Name,
|
|
|
|
Body: issue.Content,
|
|
|
|
User: ToUser(issue.Poster),
|
|
|
|
Labels: apiLabels,
|
|
|
|
Assignee: ToUser(issue.Assignee),
|
|
|
|
Milestone: ToMilestone(issue.Milestone),
|
|
|
|
Comments: issue.NumComments,
|
|
|
|
Created: issue.Created,
|
|
|
|
Updated: issue.Updated,
|
|
|
|
}
|
|
|
|
if issue.IsPull {
|
|
|
|
if err := issue.GetPullRequest(); err != nil {
|
|
|
|
log.Error(4, "GetPullRequest", err)
|
|
|
|
} else {
|
|
|
|
apiIssue.PullRequest = &api.PullRequestMeta{
|
|
|
|
HasMerged: issue.PullRequest.HasMerged,
|
|
|
|
}
|
|
|
|
if issue.PullRequest.HasMerged {
|
|
|
|
apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiIssue
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToOrganization(org *models.User) *api.Organization {
|
2015-12-17 12:58:47 +05:30
|
|
|
return &api.Organization{
|
|
|
|
ID: org.Id,
|
|
|
|
AvatarUrl: org.AvatarLink(),
|
|
|
|
UserName: org.Name,
|
|
|
|
FullName: org.FullName,
|
|
|
|
Description: org.Description,
|
|
|
|
Website: org.Website,
|
|
|
|
Location: org.Location,
|
|
|
|
}
|
|
|
|
}
|