From dccb0c15b996ac4dc0307cbfed140ce1558d7e3c Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Sun, 14 Aug 2016 04:17:26 -0700
Subject: Replace convert.To with APIFormat calls

---
 routers/api/v1/admin/user.go       |   5 +-
 routers/api/v1/convert/convert.go  | 103 +------------------------------------
 routers/api/v1/repo/issue.go       |  18 ++++---
 routers/api/v1/repo/issue_label.go |   7 ++-
 routers/api/v1/repo/label.go       |   9 ++--
 routers/api/v1/repo/repo.go        |  12 ++---
 routers/api/v1/user/follower.go    |   3 +-
 routers/api/v1/user/user.go        |   5 +-
 routers/repo/http.go               |   4 +-
 routers/repo/webhook.go            |  19 +++----
 10 files changed, 40 insertions(+), 145 deletions(-)

(limited to 'routers')

diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go
index bbf069108c..ca3d67adbf 100644
--- a/routers/api/v1/admin/user.go
+++ b/routers/api/v1/admin/user.go
@@ -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
diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go
index c3ca9fd5d1..fcadb51fd4 100644
--- a/routers/api/v1/convert/convert.go
+++ b/routers/api/v1/convert/convert.go
@@ -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,
diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go
index 90dc6e78e3..1440a17cdc 100644
--- a/routers/api/v1/repo/issue.go
+++ b/routers/api/v1/repo/issue.go
@@ -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())
 }
diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go
index b471d4a08d..f1eed2112a 100644
--- a/routers/api/v1/repo/issue_label.go
+++ b/routers/api/v1/repo/issue_label.go
@@ -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)
 }
diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go
index dfd0b1ddda..5c34849385 100644
--- a/routers/api/v1/repo/label.go
+++ b/routers/api/v1/repo/label.go
@@ -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) {
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index e1c1b5760d..cc75f5bf4e 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -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
diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go
index 8d103c9e14..58a162444b 100644
--- a/routers/api/v1/user/follower.go
+++ b/routers/api/v1/user/follower.go
@@ -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)
 }
diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go
index 87a9c08623..b526af5da7 100644
--- a/routers/api/v1/user/user.go
+++ b/routers/api/v1/user/user.go
@@ -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())
 }
diff --git a/routers/repo/http.go b/routers/repo/http.go
index 0615143e38..89a9b1b9ee 100644
--- a/routers/repo/http.go
+++ b/routers/repo/http.go
@@ -20,6 +20,8 @@ import (
 	"strings"
 	"time"
 
+	git "github.com/gogits/git-module"
+
 	"github.com/gogits/gogs/models"
 	"github.com/gogits/gogs/modules/base"
 	"github.com/gogits/gogs/modules/context"
@@ -206,7 +208,7 @@ func HTTP(ctx *context.Context) {
 						RepoName:     reponame,
 					}); err == nil {
 						go models.HookQueue.Add(repo.ID)
-						go models.AddTestPullRequestTask(authUser, repo.ID, strings.TrimPrefix(refName, "refs/heads/"), true)
+						go models.AddTestPullRequestTask(authUser, repo.ID, strings.TrimPrefix(refName, git.BRANCH_PREFIX), true)
 					}
 
 				}
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index 9b4c33e4b6..8f6ee3f06c 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -347,6 +347,7 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
 }
 
 func TestWebhook(ctx *context.Context) {
+	apiUser := ctx.User.APIFormat()
 	p := &api.PushPayload{
 		Ref:    git.BRANCH_PREFIX + ctx.Repo.Repository.DefaultBranch,
 		Before: ctx.Repo.CommitID,
@@ -356,27 +357,19 @@ func TestWebhook(ctx *context.Context) {
 				ID:      ctx.Repo.CommitID,
 				Message: ctx.Repo.Commit.Message(),
 				URL:     ctx.Repo.Repository.FullLink() + "/commit/" + ctx.Repo.CommitID,
-				Author: &api.PayloadAuthor{
+				Author: &api.PayloadUser{
 					Name:  ctx.Repo.Commit.Author.Name,
 					Email: ctx.Repo.Commit.Author.Email,
 				},
-				Committer: &api.PayloadCommitter{
+				Committer: &api.PayloadUser{
 					Name:  ctx.Repo.Commit.Committer.Name,
 					Email: ctx.Repo.Commit.Committer.Email,
 				},
 			},
 		},
-		Repo: ctx.Repo.Repository.ComposePayload(),
-		Pusher: &api.PayloadAuthor{
-			Name:     ctx.User.Name,
-			Email:    ctx.User.Email,
-			UserName: ctx.User.Name,
-		},
-		Sender: &api.PayloadUser{
-			UserName:  ctx.User.Name,
-			ID:        ctx.User.ID,
-			AvatarUrl: ctx.User.AvatarLink(),
-		},
+		Repo:   ctx.Repo.Repository.APIFormat(nil),
+		Pusher: apiUser,
+		Sender: apiUser,
 	}
 	if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p); err != nil {
 		ctx.Flash.Error("PrepareWebhooks: " + err.Error())
-- 
cgit v1.2.3