From 1b1270f69cd68895c2dbfbf09cb94eb0d04b7004 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 18:04:33 +0200 Subject: [PATCH 01/10] USER APIFormat -> ToUser --- integrations/api_issue_reaction_test.go | 7 ++- models/user_test.go | 5 +- models/userlist.go | 10 --- modules/convert/issue.go | 6 +- modules/convert/pull.go | 2 +- modules/convert/user.go | 3 + modules/notification/webhook/webhook.go | 76 +++++++++++------------ routers/api/v1/repo/issue_reaction.go | 13 ++-- routers/api/v1/repo/issue_subscription.go | 7 ++- routers/repo/webhook.go | 3 +- 10 files changed, 67 insertions(+), 65 deletions(-) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index 1906b8d09082c..20b83db2aab69 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -60,7 +61,7 @@ func TestAPIIssuesReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: user2.APIFormat(), + User: convert.ToUser(user2, true, true), Reaction: "eyes", Created: time.Unix(1573248003, 0), } @@ -120,12 +121,12 @@ func TestAPICommentReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: user2.APIFormat(), + User: convert.ToUser(user2, true, true), Reaction: "laugh", Created: time.Unix(1573248004, 0), } expectResponse[1] = api.Reaction{ - User: user1.APIFormat(), + User: convert.ToUser(user1, true, true), Reaction: "laugh", Created: time.Unix(1573248005, 0), } diff --git a/models/user_test.go b/models/user_test.go index d03ef4fad4dc0..b67cb1922ec29 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -10,6 +10,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -84,14 +85,14 @@ func TestUser_APIFormat(t *testing.T) { assert.NoError(t, err) assert.True(t, user.IsAdmin) - apiUser := user.APIFormat() + apiUser := convert.ToUser(user, true, true) assert.True(t, apiUser.IsAdmin) user, err = GetUserByID(2) assert.NoError(t, err) assert.False(t, user.IsAdmin) - apiUser = user.APIFormat() + apiUser = convert.ToUser(user, true, true) assert.False(t, apiUser.IsAdmin) } diff --git a/models/userlist.go b/models/userlist.go index 7e6cab50baf1d..a2a424848227b 100644 --- a/models/userlist.go +++ b/models/userlist.go @@ -8,7 +8,6 @@ import ( "fmt" "code.gitea.io/gitea/modules/log" - api "code.gitea.io/gitea/modules/structs" ) //UserList is a list of user. @@ -94,12 +93,3 @@ func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error } return tokenMaps, nil } - -//APIFormat return list of users in api format -func (users UserList) APIFormat() []*api.User { - result := make([]*api.User, 0, len(users)) - for _, u := range users { - result = append(result, u.APIFormat()) - } - return result -} diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 724ec8ffcf67f..f34656b47c1c7 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -31,7 +31,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { URL: issue.APIURL(), HTMLURL: issue.HTMLURL(), Index: issue.Index, - Poster: issue.Poster.APIFormat(), + Poster: ToUser(issue.Poster, false, false), Title: issue.Title, Body: issue.Content, Labels: ToLabelList(issue.Labels), @@ -65,9 +65,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { } if len(issue.Assignees) > 0 { for _, assignee := range issue.Assignees { - apiIssue.Assignees = append(apiIssue.Assignees, assignee.APIFormat()) + apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false)) } - apiIssue.Assignee = issue.Assignees[0].APIFormat() // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` + apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` } if issue.IsPull { if err := issue.LoadPullRequest(); err != nil { diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 2fa22efcb3888..e522bee787b3a 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -141,7 +141,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest { if pr.HasMerged { apiPullRequest.Merged = pr.MergedUnix.AsTimePtr() apiPullRequest.MergedCommitID = &pr.MergedCommitID - apiPullRequest.MergedBy = pr.Merger.APIFormat() + apiPullRequest.MergedBy = ToUser(pr.Merger, false, false) } return apiPullRequest diff --git a/modules/convert/user.go b/modules/convert/user.go index c75a8aac52504..010c92b969bb4 100644 --- a/modules/convert/user.go +++ b/modules/convert/user.go @@ -13,6 +13,9 @@ import ( // ToUser convert models.User to api.User // signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself func ToUser(user *models.User, signed, authed bool) *api.User { + if user == nil { + return nil + } result := &api.User{ ID: user.ID, UserName: user.Name, diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 625cf119a9ac1..6b49fa43de7b4 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -53,7 +53,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *model Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } else { err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ @@ -61,7 +61,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *model Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } if err != nil { @@ -77,7 +77,7 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo if err := webhook_module.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{ Forkee: oldRepo.APIFormat(oldMode), Repo: repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) } @@ -89,8 +89,8 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: repo.APIFormat(models.AccessModeOwner), - Organization: u.APIFormat(), - Sender: doer.APIFormat(), + Organization: convert.ToUser(u, false, false), + Sender: convert.ToUser(doer, false, false), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -103,8 +103,8 @@ func (m *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.Us if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: repo.APIFormat(models.AccessModeOwner), - Organization: u.APIFormat(), - Sender: doer.APIFormat(), + Organization: convert.ToUser(u, false, false), + Sender: convert.ToUser(doer, false, false), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -118,8 +118,8 @@ func (m *webhookNotifier) NotifyDeleteRepository(doer *models.User, repo *models if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoDeleted, Repository: repo.APIFormat(models.AccessModeOwner), - Organization: u.APIFormat(), - Sender: doer.APIFormat(), + Organization: convert.ToUser(u, false, false), + Sender: convert.ToUser(doer, false, false), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -139,7 +139,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), } if removed { apiPullRequest.Action = api.HookIssueUnassigned @@ -157,7 +157,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), } if removed { apiIssue.Action = api.HookIssueUnassigned @@ -191,7 +191,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } else { err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ @@ -204,7 +204,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model }, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: issue.Poster.APIFormat(), + Sender: convert.ToUser(issue.Poster, false, false), }) } @@ -226,7 +226,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), } if isClosed { apiPullRequest.Action = api.HookIssueClosed @@ -239,7 +239,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), } if isClosed { apiIssue.Action = api.HookIssueClosed @@ -269,7 +269,7 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue) { Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: issue.Poster.APIFormat(), + Sender: convert.ToUser(issue.Poster, false, false), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -295,7 +295,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest) { Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(pull), Repository: pull.Issue.Repo.APIFormat(mode), - Sender: pull.Issue.Poster.APIFormat(), + Sender: convert.ToUser(pull.Issue.Poster, false, false), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -316,7 +316,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } else { err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ @@ -329,7 +329,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod }, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } if err != nil { @@ -366,7 +366,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme }, }, Repository: c.Issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: true, }) } else { @@ -380,7 +380,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme }, }, Repository: c.Issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: false, }) } @@ -401,7 +401,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode Issue: convert.ToAPIIssue(issue), Comment: comment.APIFormat(), Repository: repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: true, }) } else { @@ -410,7 +410,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode Issue: convert.ToAPIIssue(issue), Comment: comment.APIFormat(), Repository: repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: false, }) } @@ -445,7 +445,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models Issue: convert.ToAPIIssue(comment.Issue), Comment: comment.APIFormat(), Repository: comment.Issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: true, }) } else { @@ -454,7 +454,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models Issue: convert.ToAPIIssue(comment.Issue), Comment: comment.APIFormat(), Repository: comment.Issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), IsPull: false, }) } @@ -494,7 +494,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(models.AccessModeNone), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } else { err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ @@ -502,7 +502,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } if err != nil { @@ -536,7 +536,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } else { err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{ @@ -544,7 +544,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) } if err != nil { @@ -553,7 +553,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m } func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { - apiPusher := pusher.APIFormat() + apiPusher := convert.ToUser(pusher, false, false) apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -602,7 +602,7 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mod Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: pr.Issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), Action: api.HookIssueClosed, } @@ -635,7 +635,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *models.User, }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: issue.Repo.APIFormat(mode), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }) if err != nil { @@ -674,7 +674,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: review.Issue.Repo.APIFormat(mode), - Sender: review.Reviewer.APIFormat(), + Sender: convert.ToUser(review.Reviewer, false, false), Review: &api.ReviewPayload{ Type: string(reviewHookType), Content: review.Content, @@ -685,7 +685,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review } func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { - apiPusher := pusher.APIFormat() + apiPusher := convert.ToUser(pusher, false, false) apiRepo := repo.APIFormat(models.AccessModeNone) refName := git.RefEndName(refFullName) @@ -729,14 +729,14 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *m Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: pr.Issue.Repo.APIFormat(models.AccessModeNone), - Sender: doer.APIFormat(), + Sender: convert.ToUser(doer, false, false), }); err != nil { log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err) } } func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { - apiPusher := pusher.APIFormat() + apiPusher := convert.ToUser(pusher, false, false) apiRepo := repo.APIFormat(models.AccessModeNone) refName := git.RefEndName(refFullName) @@ -762,7 +762,7 @@ func sendReleaseHook(doer *models.User, rel *models.Release, action api.HookRele Action: action, Release: rel.APIFormat(), Repository: rel.Repo.APIFormat(mode), - Sender: rel.Publisher.APIFormat(), + Sender: convert.ToUser(rel.Publisher, false, false), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -781,7 +781,7 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Rel } func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { - apiPusher := pusher.APIFormat() + apiPusher := convert.ToUser(pusher, false, false) apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index 564f67e49329c..f5387fdd1adf1 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -75,7 +76,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) { var result []api.Reaction for _, r := range reactions { result = append(result, api.Reaction{ - User: r.User.APIFormat(), + User: convert.ToUser(r.User, ctx.IsSigned, false), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), }) @@ -193,7 +194,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { ctx.JSON(http.StatusOK, api.Reaction{ - User: ctx.User.APIFormat(), + User: convert.ToUser(ctx.User, true, true), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -204,7 +205,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp } ctx.JSON(http.StatusCreated, api.Reaction{ - User: ctx.User.APIFormat(), + User: convert.ToUser(ctx.User, true, true), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -289,7 +290,7 @@ func GetIssueReactions(ctx *context.APIContext) { var result []api.Reaction for _, r := range reactions { result = append(result, api.Reaction{ - User: r.User.APIFormat(), + User: convert.ToUser(r.User, ctx.IsSigned, false), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), }) @@ -402,7 +403,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { ctx.JSON(http.StatusOK, api.Reaction{ - User: ctx.User.APIFormat(), + User: convert.ToUser(ctx.User, true, true), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -413,7 +414,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i } ctx.JSON(http.StatusCreated, api.Reaction{ - User: ctx.User.APIFormat(), + User: convert.ToUser(ctx.User, true, true), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index f0df0976fb329..2bbd72299ab59 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" ) @@ -276,6 +277,10 @@ func GetIssueSubscribers(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err) return } + apiUsers := make([]*api.User, 0, len(users)) + for i := range users { + apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, false) + } - ctx.JSON(http.StatusOK, users.APIFormat()) + ctx.JSON(http.StatusOK, apiUsers) } diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index bec401021cc5a..13b95c8076c29 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -1052,7 +1053,7 @@ func TestWebhook(ctx *context.Context) { } } - apiUser := ctx.User.APIFormat() + apiUser := convert.ToUser(ctx.User, true, true) p := &api.PushPayload{ Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, Before: commit.ID.String(), From e423b5a7e2c99d471f17f519d5f83965b014263a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 18:20:54 +0200 Subject: [PATCH 02/10] Migrate more and mark APIFormat deprecated --- models/user.go | 1 + modules/convert/git_commit.go | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/models/user.go b/models/user.go index 650d5a803aa9a..f88c5dbc2e67b 100644 --- a/models/user.go +++ b/models/user.go @@ -239,6 +239,7 @@ func (u *User) GetEmail() string { } // APIFormat converts a User to api.User +// Deprecated func (u *User) APIFormat() *api.User { if u == nil { return nil diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 55cbb3af80e39..0240113f61c21 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -85,13 +85,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } if ok { - apiAuthor = cacheAuthor.APIFormat() + apiAuthor = ToUser(cacheAuthor, false, false) } else { author, err := models.GetUserByEmail(commit.Author.Email) if err != nil && !models.IsErrUserNotExist(err) { return nil, err } else if err == nil { - apiAuthor = author.APIFormat() + apiAuthor = ToUser(author, false, false) if userCache != nil { userCache[commit.Author.Email] = author } @@ -107,13 +107,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } if ok { - apiCommitter = cacheCommitter.APIFormat() + apiCommitter = ToUser(cacheCommitter, false, false) } else { committer, err := models.GetUserByEmail(commit.Committer.Email) if err != nil && !models.IsErrUserNotExist(err) { return nil, err } else if err == nil { - apiCommitter = committer.APIFormat() + apiCommitter = ToUser(committer, false, false) if userCache != nil { userCache[commit.Committer.Email] = committer } From 109fa5ce6dbdc7db17e03c1ed24ae7ce17b481ed Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 18:32:13 +0200 Subject: [PATCH 03/10] models.Comment APIFormat() -> convert.ToComment --- integrations/api_comment_test.go | 3 ++- models/issue_comment.go | 15 --------------- modules/convert/issue_comment.go | 24 ++++++++++++++++++++++++ modules/notification/webhook/webhook.go | 12 ++++++------ routers/api/v1/repo/issue_comment.go | 11 ++++++----- 5 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 modules/convert/issue_comment.go diff --git a/integrations/api_comment_test.go b/integrations/api_comment_test.go index 2c754272e5687..f64f4a1c9c0f2 100644 --- a/integrations/api_comment_test.go +++ b/integrations/api_comment_test.go @@ -11,6 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -125,7 +126,7 @@ func TestAPIGetComment(t *testing.T) { DecodeJSON(t, resp, &apiComment) assert.NoError(t, comment.LoadPoster()) - expect := comment.APIFormat() + expect := convert.ToComment(comment) assert.Equal(t, expect.ID, apiComment.ID) assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName) diff --git a/models/issue_comment.go b/models/issue_comment.go index 726ed7472bcf5..f36a0ea1149fc 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -20,7 +20,6 @@ import ( "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/structs" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "github.com/unknwon/com" @@ -352,20 +351,6 @@ func (c *Comment) PRURL() string { return c.Issue.HTMLURL() } -// APIFormat converts a Comment to the api.Comment format -func (c *Comment) APIFormat() *api.Comment { - return &api.Comment{ - ID: c.ID, - Poster: c.Poster.APIFormat(), - HTMLURL: c.HTMLURL(), - IssueURL: c.IssueURL(), - PRURL: c.PRURL(), - Body: c.Content, - Created: c.CreatedUnix.AsTime(), - Updated: c.UpdatedUnix.AsTime(), - } -} - // CommentHashTag returns unique hash tag for comment id. func CommentHashTag(id int64) string { return fmt.Sprintf("issuecomment-%d", id) diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go new file mode 100644 index 0000000000000..cf65c876d3d08 --- /dev/null +++ b/modules/convert/issue_comment.go @@ -0,0 +1,24 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" +) + +// ToComment converts a models.Comment to the api.Comment format +func ToComment(c *models.Comment) *api.Comment { + return &api.Comment{ + ID: c.ID, + Poster: ToUser(c.Poster, false, false), + HTMLURL: c.HTMLURL(), + IssueURL: c.IssueURL(), + PRURL: c.PRURL(), + Body: c.Content, + Created: c.CreatedUnix.AsTime(), + Updated: c.UpdatedUnix.AsTime(), + } +} diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 6b49fa43de7b4..4ab4b421df7d9 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -359,7 +359,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: convert.ToAPIIssue(c.Issue), - Comment: c.APIFormat(), + Comment: convert.ToComment(c), Changes: &api.ChangesPayload{ Body: &api.ChangesFromPayload{ From: oldContent, @@ -373,7 +373,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentEdited, Issue: convert.ToAPIIssue(c.Issue), - Comment: c.APIFormat(), + Comment: convert.ToComment(c), Changes: &api.ChangesPayload{ Body: &api.ChangesFromPayload{ From: oldContent, @@ -399,7 +399,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(issue), - Comment: comment.APIFormat(), + Comment: convert.ToComment(comment), Repository: repo.APIFormat(mode), Sender: convert.ToUser(doer, false, false), IsPull: true, @@ -408,7 +408,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Issue: convert.ToAPIIssue(issue), - Comment: comment.APIFormat(), + Comment: convert.ToComment(comment), Repository: repo.APIFormat(mode), Sender: convert.ToUser(doer, false, false), IsPull: false, @@ -443,7 +443,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: convert.ToAPIIssue(comment.Issue), - Comment: comment.APIFormat(), + Comment: convert.ToComment(comment), Repository: comment.Issue.Repo.APIFormat(mode), Sender: convert.ToUser(doer, false, false), IsPull: true, @@ -452,7 +452,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ Action: api.HookIssueCommentDeleted, Issue: convert.ToAPIIssue(comment.Issue), - Comment: comment.APIFormat(), + Comment: convert.ToComment(comment), Repository: comment.Issue.Repo.APIFormat(mode), Sender: convert.ToUser(doer, false, false), IsPull: false, diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index bf86b42402f32..2da2f29063712 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" comment_service "code.gitea.io/gitea/services/comments" @@ -85,7 +86,7 @@ func ListIssueComments(ctx *context.APIContext) { apiComments := make([]*api.Comment, len(comments)) for i, comment := range comments { comment.Issue = issue - apiComments[i] = comments[i].APIFormat() + apiComments[i] = convert.ToComment(comments[i]) } ctx.JSON(http.StatusOK, &apiComments) } @@ -167,7 +168,7 @@ func ListRepoIssueComments(ctx *context.APIContext) { return } for i := range comments { - apiComments[i] = comments[i].APIFormat() + apiComments[i] = convert.ToComment(comments[i]) } ctx.JSON(http.StatusOK, &apiComments) } @@ -225,7 +226,7 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti return } - ctx.JSON(http.StatusCreated, comment.APIFormat()) + ctx.JSON(http.StatusCreated, convert.ToComment(comment)) } // GetIssueComment Get a comment by ID @@ -293,7 +294,7 @@ func GetIssueComment(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, comment.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToComment(comment)) } // EditIssueComment modify a comment of an issue @@ -414,7 +415,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) return } - ctx.JSON(http.StatusOK, comment.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToComment(comment)) } // DeleteIssueComment delete a comment from an issue From ab06cd6b3503503159168a6f1902b10388dc3074 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 18:40:29 +0200 Subject: [PATCH 04/10] models.Release APIFormat() -> convert.ToRelease --- models/attachment.go | 1 + models/release.go | 26 ----------------- modules/convert/release.go | 35 +++++++++++++++++++++++ modules/notification/webhook/webhook.go | 2 +- routers/api/v1/repo/release.go | 9 +++--- routers/api/v1/repo/release_attachment.go | 3 +- routers/api/v1/repo/release_tags.go | 3 +- 7 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 modules/convert/release.go diff --git a/models/attachment.go b/models/attachment.go index 26f466a4008b9..7c05839feca4a 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -44,6 +44,7 @@ func (a *Attachment) IncreaseDownloadCount() error { } // APIFormat converts models.Attachment to api.Attachment +// Deprecated func (a *Attachment) APIFormat() *api.Attachment { return &api.Attachment{ ID: a.ID, diff --git a/models/release.go b/models/release.go index f55341b86bb10..b8ffb257a29a2 100644 --- a/models/release.go +++ b/models/release.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "xorm.io/builder" @@ -86,31 +85,6 @@ func (r *Release) HTMLURL() string { return fmt.Sprintf("%s/releases/tag/%s", r.Repo.HTMLURL(), r.TagName) } -// APIFormat convert a Release to api.Release -func (r *Release) APIFormat() *api.Release { - assets := make([]*api.Attachment, 0) - for _, att := range r.Attachments { - assets = append(assets, att.APIFormat()) - } - return &api.Release{ - ID: r.ID, - TagName: r.TagName, - Target: r.Target, - Title: r.Title, - Note: r.Note, - URL: r.APIURL(), - HTMLURL: r.HTMLURL(), - TarURL: r.TarURL(), - ZipURL: r.ZipURL(), - IsDraft: r.IsDraft, - IsPrerelease: r.IsPrerelease, - CreatedAt: r.CreatedUnix.AsTime(), - PublishedAt: r.CreatedUnix.AsTime(), - Publisher: r.Publisher.APIFormat(), - Attachments: assets, - } -} - // IsReleaseExist returns true if release with given tag name already exists. func IsReleaseExist(repoID int64, tagName string) (bool, error) { if len(tagName) == 0 { diff --git a/modules/convert/release.go b/modules/convert/release.go new file mode 100644 index 0000000000000..3179c40af0e18 --- /dev/null +++ b/modules/convert/release.go @@ -0,0 +1,35 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" +) + +// ToRelease convert a models.Release to api.Release +func ToRelease(r *models.Release) *api.Release { + assets := make([]*api.Attachment, 0) + for _, att := range r.Attachments { + assets = append(assets, att.APIFormat()) + } + return &api.Release{ + ID: r.ID, + TagName: r.TagName, + Target: r.Target, + Title: r.Title, + Note: r.Note, + URL: r.APIURL(), + HTMLURL: r.HTMLURL(), + TarURL: r.TarURL(), + ZipURL: r.ZipURL(), + IsDraft: r.IsDraft, + IsPrerelease: r.IsPrerelease, + CreatedAt: r.CreatedUnix.AsTime(), + PublishedAt: r.CreatedUnix.AsTime(), + Publisher: ToUser(r.Publisher, false, false), + Attachments: assets, + } +} diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 4ab4b421df7d9..e778c45d584f9 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -760,7 +760,7 @@ func sendReleaseHook(doer *models.User, rel *models.Release, action api.HookRele mode, _ := models.AccessLevel(rel.Publisher, rel.Repo) if err := webhook_module.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{ Action: action, - Release: rel.APIFormat(), + Release: convert.ToRelease(rel), Repository: rel.Repo.APIFormat(mode), Sender: convert.ToUser(rel.Publisher, false, false), }); err != nil { diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go index c2ed1fe024fb0..358cc011433a3 100644 --- a/routers/api/v1/repo/release.go +++ b/routers/api/v1/repo/release.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" releaseservice "code.gitea.io/gitea/services/release" @@ -60,7 +61,7 @@ func GetRelease(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, release.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToRelease(release)) } // ListReleases list a repository's releases @@ -117,7 +118,7 @@ func ListReleases(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - rels[i] = release.APIFormat() + rels[i] = convert.ToRelease(release) } ctx.JSON(http.StatusOK, rels) } @@ -205,7 +206,7 @@ func CreateRelease(ctx *context.APIContext, form api.CreateReleaseOption) { return } } - ctx.JSON(http.StatusCreated, rel.APIFormat()) + ctx.JSON(http.StatusCreated, convert.ToRelease(rel)) } // EditRelease edit a release @@ -288,7 +289,7 @@ func EditRelease(ctx *context.APIContext, form api.EditReleaseOption) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, rel.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToRelease(rel)) } // DeleteRelease delete a release from a repository diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 3d1084f211fdc..9e1eed7f79e07 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -108,7 +109,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, release.APIFormat().Attachments) + ctx.JSON(http.StatusOK, convert.ToRelease(release).Attachments) } // CreateReleaseAttachment creates an attachment and saves the given file diff --git a/routers/api/v1/repo/release_tags.go b/routers/api/v1/repo/release_tags.go index bde3251ba28ca..2a72e0000e658 100644 --- a/routers/api/v1/repo/release_tags.go +++ b/routers/api/v1/repo/release_tags.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" ) // GetReleaseTag get a single release of a repository by its tagname @@ -56,5 +57,5 @@ func GetReleaseTag(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) return } - ctx.JSON(http.StatusOK, release.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToRelease(release)) } From 4296c399b3c883d7e837a8bc612d7fae34ebc945 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 18:48:19 +0200 Subject: [PATCH 05/10] models.Attachments APIFormat() -> convert.ToReleaseAttachments --- models/attachment.go | 15 --------------- modules/convert/release.go | 15 ++++++++++++++- routers/api/v1/repo/release_attachment.go | 6 +++--- routers/repo/issue.go | 5 +++-- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 7c05839feca4a..55a6cfc014b98 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" gouuid "github.com/google/uuid" @@ -43,20 +42,6 @@ func (a *Attachment) IncreaseDownloadCount() error { return nil } -// APIFormat converts models.Attachment to api.Attachment -// Deprecated -func (a *Attachment) APIFormat() *api.Attachment { - return &api.Attachment{ - ID: a.ID, - Name: a.Name, - Created: a.CreatedUnix.AsTime(), - DownloadCount: a.DownloadCount, - Size: a.Size, - UUID: a.UUID, - DownloadURL: a.DownloadURL(), - } -} - // AttachmentRelativePath returns the relative path func AttachmentRelativePath(uuid string) string { return path.Join(uuid[0:1], uuid[1:2], uuid) diff --git a/modules/convert/release.go b/modules/convert/release.go index 3179c40af0e18..d9def89637825 100644 --- a/modules/convert/release.go +++ b/modules/convert/release.go @@ -13,7 +13,7 @@ import ( func ToRelease(r *models.Release) *api.Release { assets := make([]*api.Attachment, 0) for _, att := range r.Attachments { - assets = append(assets, att.APIFormat()) + assets = append(assets, ToReleaseAttachment(att)) } return &api.Release{ ID: r.ID, @@ -33,3 +33,16 @@ func ToRelease(r *models.Release) *api.Release { Attachments: assets, } } + +// ToReleaseAttachment converts models.Attachment to api.Attachment +func ToReleaseAttachment(a *models.Attachment) *api.Attachment { + return &api.Attachment{ + ID: a.ID, + Name: a.Name, + Created: a.CreatedUnix.AsTime(), + DownloadCount: a.DownloadCount, + Size: a.Size, + UUID: a.UUID, + DownloadURL: a.DownloadURL(), + } +} diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 9e1eed7f79e07..75a05877151e5 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -64,7 +64,7 @@ func GetReleaseAttachment(ctx *context.APIContext) { return } // FIXME Should prove the existence of the given repo, but results in unnecessary database requests - ctx.JSON(http.StatusOK, attach.APIFormat()) + ctx.JSON(http.StatusOK, convert.ToReleaseAttachment(attach)) } // ListReleaseAttachments lists all attachments of the release @@ -205,7 +205,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { return } - ctx.JSON(http.StatusCreated, attach.APIFormat()) + ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach)) } // EditReleaseAttachment updates the given attachment @@ -269,7 +269,7 @@ func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptio if err := models.UpdateAttachment(attach); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach) } - ctx.JSON(http.StatusCreated, attach.APIFormat()) + ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach)) } // DeleteReleaseAttachment delete a given attachment diff --git a/routers/repo/issue.go b/routers/repo/issue.go index be46ddbeb93d7..8e356ad81834f 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" "code.gitea.io/gitea/modules/log" @@ -2124,7 +2125,7 @@ func GetIssueAttachments(ctx *context.Context) { issue := GetActionIssue(ctx) var attachments = make([]*api.Attachment, len(issue.Attachments)) for i := 0; i < len(issue.Attachments); i++ { - attachments[i] = issue.Attachments[i].APIFormat() + attachments[i] = convert.ToReleaseAttachment(issue.Attachments[i]) } ctx.JSON(200, attachments) } @@ -2143,7 +2144,7 @@ func GetCommentAttachments(ctx *context.Context) { return } for i := 0; i < len(comment.Attachments); i++ { - attachments = append(attachments, comment.Attachments[i].APIFormat()) + attachments = append(attachments, convert.ToReleaseAttachment(comment.Attachments[i])) } } ctx.JSON(200, attachments) From cf548d64d1c0094242c9dc7060959e297d0ca7e4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 19:02:32 +0200 Subject: [PATCH 06/10] models.CommitStatus APIFormat() -> convert.ToCommitStatus --- models/commit_status.go | 21 --------------------- modules/convert/convert.go | 21 +++++++++++++++++++++ routers/api/v1/repo/status.go | 7 ++++--- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index ed6f8702c5ce5..15fcbff6f9196 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -61,27 +61,6 @@ func (status *CommitStatus) APIURL() string { setting.AppURL, status.Repo.FullName(), status.SHA) } -// APIFormat assumes some fields assigned with values: -// Required - Repo, Creator -func (status *CommitStatus) APIFormat() *api.Status { - _ = status.loadRepo(x) - apiStatus := &api.Status{ - Created: status.CreatedUnix.AsTime(), - Updated: status.CreatedUnix.AsTime(), - State: api.StatusState(status.State), - TargetURL: status.TargetURL, - Description: status.Description, - ID: status.Index, - URL: status.APIURL(), - Context: status.Context, - } - if status.Creator != nil { - apiStatus.Creator = status.Creator.APIFormat() - } - - return apiStatus -} - // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus { var lastStatus *CommitStatus diff --git a/modules/convert/convert.go b/modules/convert/convert.go index ec676002b93f7..e81df0c0c3c70 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -339,3 +339,24 @@ func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application { Created: app.CreatedUnix.AsTime(), } } + +// ToCommitStatus converts models.CommitStatus to api.Status +func ToCommitStatus(status *models.CommitStatus) *api.Status { + apiStatus := &api.Status{ + Created: status.CreatedUnix.AsTime(), + Updated: status.CreatedUnix.AsTime(), + State: api.StatusState(status.State), + TargetURL: status.TargetURL, + Description: status.Description, + ID: status.Index, + URL: status.APIURL(), + Context: status.Context, + } + + if status.CreatorID != 0 { + creator, _ := models.GetUserByID(status.CreatorID) + apiStatus.Creator = ToUser(creator, false, false) + } + + return apiStatus +} diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 69661dca9177d..e7318edaa5176 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/repofiles" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" @@ -64,7 +65,7 @@ func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) { return } - ctx.JSON(http.StatusCreated, status.APIFormat()) + ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status)) } // GetCommitStatuses returns all statuses for any given commit hash @@ -222,7 +223,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { apiStatuses := make([]*api.Status, 0, len(statuses)) for _, status := range statuses { - apiStatuses = append(apiStatuses, status.APIFormat()) + apiStatuses = append(apiStatuses, convert.ToCommitStatus(status)) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) @@ -305,7 +306,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { retStatus.Statuses = make([]*api.Status, 0, len(statuses)) for _, status := range statuses { - retStatus.Statuses = append(retStatus.Statuses, status.APIFormat()) + retStatus.Statuses = append(retStatus.Statuses, convert.ToCommitStatus(status)) if status.State.NoBetterThan(retStatus.State) { retStatus.State = status.State } From 9f30914bc2445676498ba617ab1c6062e327f10d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 19:08:16 +0200 Subject: [PATCH 07/10] finish migration to convert.ToUser --- models/repo.go | 13 +++++++++++-- models/user.go | 20 -------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/models/repo.go b/models/repo.go index 46f91fc7df658..03a176d8a7608 100644 --- a/models/repo.go +++ b/models/repo.go @@ -413,8 +413,17 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true}) return &api.Repository{ - ID: repo.ID, - Owner: repo.Owner.APIFormat(), + ID: repo.ID, + // TODO use convert.ToUser(repo.Owner) + Owner: &api.User{ + ID: repo.Owner.ID, + UserName: repo.Owner.Name, + FullName: repo.Owner.FullName, + Email: repo.Owner.GetEmail(), + AvatarURL: repo.Owner.AvatarLink(), + LastLogin: repo.Owner.LastLoginUnix.AsTime(), + Created: repo.Owner.CreatedUnix.AsTime(), + }, Name: repo.Name, FullName: repo.FullName(), Description: repo.Description, diff --git a/models/user.go b/models/user.go index f88c5dbc2e67b..8db59324d19bc 100644 --- a/models/user.go +++ b/models/user.go @@ -32,7 +32,6 @@ import ( "code.gitea.io/gitea/modules/public" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -238,25 +237,6 @@ func (u *User) GetEmail() string { return u.Email } -// APIFormat converts a User to api.User -// Deprecated -func (u *User) APIFormat() *api.User { - if u == nil { - return nil - } - return &api.User{ - ID: u.ID, - UserName: u.Name, - FullName: u.FullName, - Email: u.GetEmail(), - AvatarURL: u.AvatarLink(), - Language: u.Language, - IsAdmin: u.IsAdmin, - LastLogin: u.LastLoginUnix.AsTime(), - Created: u.CreatedUnix.AsTime(), - } -} - // IsLocal returns true if user login type is LoginPlain. func (u *User) IsLocal() bool { return u.LoginType <= LoginPlain From 9bb5436eecf9dee8dc115e9a3adba2c0bddd30dc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 27 Sep 2020 19:16:04 +0200 Subject: [PATCH 08/10] Move Test --- models/user_test.go | 18 ------------------ modules/convert/user_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 modules/convert/user_test.go diff --git a/models/user_test.go b/models/user_test.go index b67cb1922ec29..7a6f5aa5122b7 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -10,7 +10,6 @@ import ( "strings" "testing" - "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -79,23 +78,6 @@ func TestGetUserEmailsByNames(t *testing.T) { assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user7"})) } -func TestUser_APIFormat(t *testing.T) { - - user, err := GetUserByID(1) - assert.NoError(t, err) - assert.True(t, user.IsAdmin) - - apiUser := convert.ToUser(user, true, true) - assert.True(t, apiUser.IsAdmin) - - user, err = GetUserByID(2) - assert.NoError(t, err) - assert.False(t, user.IsAdmin) - - apiUser = convert.ToUser(user, true, true) - assert.False(t, apiUser.IsAdmin) -} - func TestCanCreateOrganization(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go new file mode 100644 index 0000000000000..9e4c6dfae715d --- /dev/null +++ b/modules/convert/user_test.go @@ -0,0 +1,29 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "testing" + + "code.gitea.io/gitea/models" + "github.com/stretchr/testify/assert" +) + +func TestUser_ToUser(t *testing.T) { + + user, err := models.GetUserByID(1) + assert.NoError(t, err) + assert.True(t, user.IsAdmin) + + apiUser := ToUser(user, true, true) + assert.True(t, apiUser.IsAdmin) + + user, err = models.GetUserByID(2) + assert.NoError(t, err) + assert.False(t, user.IsAdmin) + + apiUser = ToUser(user, true, true) + assert.False(t, apiUser.IsAdmin) +} From d71e763a22a29b185a6c3db4faf857ca2f3ac62c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 1 Oct 2020 10:07:30 +0200 Subject: [PATCH 09/10] Imprufe Test --- modules/convert/user_test.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index 9e4c6dfae715d..cbda83bc92257 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -13,17 +13,16 @@ import ( func TestUser_ToUser(t *testing.T) { - user, err := models.GetUserByID(1) - assert.NoError(t, err) - assert.True(t, user.IsAdmin) + user1 := models.AssertExistsAndLoadBean(t, models.User{ID: 1, IsAdmin: true}).(*models.User) - apiUser := ToUser(user, true, true) + apiUser := ToUser(user1, true, true) assert.True(t, apiUser.IsAdmin) - user, err = models.GetUserByID(2) - assert.NoError(t, err) - assert.False(t, user.IsAdmin) + user2 := models.AssertExistsAndLoadBean(t, models.User{ID: 2, IsAdmin: false}).(*models.User) - apiUser = ToUser(user, true, true) + apiUser = ToUser(user2, true, true) + assert.False(t, apiUser.IsAdmin) + + apiUser = ToUser(user1, false, false) assert.False(t, apiUser.IsAdmin) } From 72c28002f1179709e725300682e0cfc35da4c6bd Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 1 Oct 2020 14:52:08 +0200 Subject: [PATCH 10/10] fix test --- modules/convert/user_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index cbda83bc92257..eff60d51830eb 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -13,12 +13,12 @@ import ( func TestUser_ToUser(t *testing.T) { - user1 := models.AssertExistsAndLoadBean(t, models.User{ID: 1, IsAdmin: true}).(*models.User) + user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) apiUser := ToUser(user1, true, true) assert.True(t, apiUser.IsAdmin) - user2 := models.AssertExistsAndLoadBean(t, models.User{ID: 2, IsAdmin: false}).(*models.User) + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) apiUser = ToUser(user2, true, true) assert.False(t, apiUser.IsAdmin)