1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

Replace convert.To with APIFormat calls

This commit is contained in:
Unknwon
2016-08-14 04:17:26 -07:00
parent 3f7f4852ef
commit dccb0c15b9
21 changed files with 79 additions and 225 deletions

View File

@@ -11,7 +11,6 @@ import (
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers/api/v1/convert"
"github.com/gogits/gogs/routers/api/v1/user"
)
@@ -69,7 +68,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
models.SendRegisterNotifyMail(ctx.Context.Context, u)
}
ctx.JSON(201, convert.ToUser(u))
ctx.JSON(201, u.APIFormat())
}
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
@@ -121,7 +120,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
}
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
ctx.JSON(200, convert.ToUser(u))
ctx.JSON(200, u.APIFormat())
}
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user

View File

@@ -13,23 +13,8 @@ import (
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/setting"
)
func ToUser(u *models.User) *api.User {
if u == nil {
return nil
}
return &api.User{
ID: u.ID,
UserName: u.Name,
FullName: u.FullName,
Email: u.Email,
AvatarUrl: u.AvatarLink(),
}
}
func ToEmail(email *models.EmailAddress) *api.Email {
return &api.Email{
Email: email.Email,
@@ -38,28 +23,6 @@ func ToEmail(email *models.EmailAddress) *api.Email {
}
}
func ToRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
cl := repo.CloneLink()
return &api.Repository{
ID: repo.ID,
Owner: ToUser(owner),
FullName: owner.Name + "/" + repo.Name,
Description: repo.Description,
Private: repo.IsPrivate,
Fork: repo.IsFork,
HTMLURL: setting.AppUrl + owner.Name + "/" + repo.Name,
CloneURL: cl.HTTPS,
SSHURL: cl.SSH,
OpenIssues: repo.NumOpenIssues,
Stars: repo.NumStars,
Forks: repo.NumForks,
Watchers: repo.NumWatches,
Created: repo.Created,
Updated: repo.Updated,
Permissions: &permission,
}
}
func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
return &api.Branch{
Name: b.Name,
@@ -82,12 +45,12 @@ func ToCommit(c *git.Commit) *api.PayloadCommit {
ID: c.ID.String(),
Message: c.Message(),
URL: "Not implemented",
Author: &api.PayloadAuthor{
Author: &api.PayloadUser{
Name: c.Author.Name,
Email: c.Author.Email,
UserName: authorUsername,
},
Committer: &api.PayloadCommitter{
Committer: &api.PayloadUser{
Name: c.Committer.Name,
Email: c.Committer.Email,
UserName: committerUsername,
@@ -142,68 +105,6 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
}
}
func ToLabel(label *models.Label) *api.Label {
return &api.Label{
ID: label.ID,
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.Title,
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 {
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 {
return &api.Organization{
ID: org.ID,

View File

@@ -13,7 +13,6 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/routers/api/v1/convert"
)
func ListIssues(ctx *context.APIContext) {
@@ -28,7 +27,12 @@ func ListIssues(ctx *context.APIContext) {
apiIssues := make([]*api.Issue, len(issues))
for i := range issues {
apiIssues[i] = convert.ToIssue(issues[i])
// FIXME: use IssueList to improve performance.
if err = issues[i].LoadAttributes(); err != nil {
ctx.Error(500, "LoadAttributes", err)
return
}
apiIssues[i] = issues[i].APIFormat()
}
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
@@ -46,13 +50,13 @@ func GetIssue(ctx *context.APIContext) {
return
}
ctx.JSON(200, convert.ToIssue(issue))
ctx.JSON(200, issue.APIFormat())
}
func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
issue := &models.Issue{
RepoID: ctx.Repo.Repository.ID,
Title: form.Title,
Title: form.Title,
PosterID: ctx.User.ID,
Poster: ctx.User,
Content: form.Body,
@@ -83,7 +87,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
if form.Closed {
if err := issue.ChangeStatus(ctx.User, ctx.Repo.Repository, true); err != nil {
ctx.Error(500, "issue.ChangeStatus", err)
ctx.Error(500, "ChangeStatus", err)
return
}
}
@@ -95,7 +99,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
ctx.Error(500, "GetIssueByID", err)
return
}
ctx.JSON(201, convert.ToIssue(issue))
ctx.JSON(201, issue.APIFormat())
}
func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
@@ -164,5 +168,5 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
ctx.Error(500, "GetIssueByID", err)
return
}
ctx.JSON(201, convert.ToIssue(issue))
ctx.JSON(201, issue.APIFormat())
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/routers/api/v1/convert"
)
func ListIssueLabels(ctx *context.APIContext) {
@@ -25,7 +24,7 @@ func ListIssueLabels(ctx *context.APIContext) {
apiLabels := make([]*api.Label, len(issue.Labels))
for i := range issue.Labels {
apiLabels[i] = convert.ToLabel(issue.Labels[i])
apiLabels[i] = issue.Labels[i].APIFormat()
}
ctx.JSON(200, &apiLabels)
}
@@ -65,7 +64,7 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
apiLabels := make([]*api.Label, len(labels))
for i := range labels {
apiLabels[i] = convert.ToLabel(labels[i])
apiLabels[i] = issue.Labels[i].APIFormat()
}
ctx.JSON(200, &apiLabels)
}
@@ -139,7 +138,7 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
apiLabels := make([]*api.Label, len(labels))
for i := range labels {
apiLabels[i] = convert.ToLabel(labels[i])
apiLabels[i] = issue.Labels[i].APIFormat()
}
ctx.JSON(200, &apiLabels)
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/routers/api/v1/convert"
)
func ListLabels(ctx *context.APIContext) {
@@ -21,7 +20,7 @@ func ListLabels(ctx *context.APIContext) {
apiLabels := make([]*api.Label, len(labels))
for i := range labels {
apiLabels[i] = convert.ToLabel(labels[i])
apiLabels[i] = labels[i].APIFormat()
}
ctx.JSON(200, &apiLabels)
}
@@ -37,7 +36,7 @@ func GetLabel(ctx *context.APIContext) {
return
}
ctx.JSON(200, convert.ToLabel(label))
ctx.JSON(200, label.APIFormat())
}
func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
@@ -55,7 +54,7 @@ func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) {
ctx.Error(500, "NewLabel", err)
return
}
ctx.JSON(201, convert.ToLabel(label))
ctx.JSON(201, label.APIFormat())
}
func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
@@ -84,7 +83,7 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) {
ctx.Handle(500, "UpdateLabel", err)
return
}
ctx.JSON(200, convert.ToLabel(label))
ctx.JSON(200, label.APIFormat())
}
func DeleteLabel(ctx *context.APIContext) {

View File

@@ -93,12 +93,12 @@ func ListMyRepos(ctx *context.APIContext) {
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = convert.ToRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
}
i := numOwnRepos
for repo, access := range accessibleRepos {
repos[i] = convert.ToRepository(repo.Owner, repo, api.Permission{
repos[i] = repo.APIFormat(&api.Permission{
Admin: access >= models.ACCESS_MODE_ADMIN,
Push: access >= models.ACCESS_MODE_WRITE,
Pull: true,
@@ -135,7 +135,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
return
}
ctx.JSON(201, convert.ToRepository(owner, repo, api.Permission{true, true, true}))
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create
@@ -235,7 +235,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
}
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
ctx.JSON(201, convert.ToRepository(ctxUser, repo, api.Permission{true, true, true}))
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
}
func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
@@ -264,12 +264,12 @@ func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repositor
// https://github.com/gogits/go-gogs-client/wiki/Repositories#get
func Get(ctx *context.APIContext) {
owner, repo := parseOwnerAndRepo(ctx)
_, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
ctx.JSON(200, convert.ToRepository(owner, repo, api.Permission{true, true, true}))
ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true}))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete

View File

@@ -9,13 +9,12 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/routers/api/v1/convert"
)
func responseApiUsers(ctx *context.APIContext, users []*models.User) {
apiUsers := make([]*api.User, len(users))
for i := range users {
apiUsers[i] = convert.ToUser(users[i])
apiUsers[i] = users[i].APIFormat()
}
ctx.JSON(200, &apiUsers)
}

View File

@@ -11,7 +11,6 @@ import (
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/context"
"github.com/gogits/gogs/routers/api/v1/convert"
)
func Search(ctx *context.APIContext) {
@@ -67,9 +66,9 @@ func GetInfo(ctx *context.APIContext) {
if !ctx.IsSigned {
u.Email = ""
}
ctx.JSON(200, convert.ToUser(u))
ctx.JSON(200, u.APIFormat())
}
func GetAuthenticatedUser(ctx *context.APIContext) {
ctx.JSON(200, convert.ToUser(ctx.User))
ctx.JSON(200, ctx.User.APIFormat())
}