From c17fa898767527b61d885a44fc04017c9ca79ce4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Jun 2021 18:56:16 +0200 Subject: [PATCH 1/7] [API] lable object calculate URL (fix #8028) --- modules/convert/issue.go | 60 +++++++++++++++++++++++++++--- modules/convert/issue_test.go | 5 ++- routers/api/v1/org/label.go | 18 ++++++--- routers/api/v1/repo/issue_label.go | 12 ++++-- routers/api/v1/repo/label.go | 18 +++++++-- 5 files changed, 95 insertions(+), 18 deletions(-) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index da09aeaca41e..55017b242702 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -5,9 +5,12 @@ package convert import ( + "fmt" "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" ) @@ -26,6 +29,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { return &api.Issue{} } + repoCache := make(map[int64]*models.Repository) + repoCache[issue.RepoID] = issue.Repo + apiIssue := &api.Issue{ ID: issue.ID, URL: issue.APIURL(), @@ -35,7 +41,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { Title: issue.Title, Body: issue.Content, Ref: issue.Ref, - Labels: ToLabelList(issue.Labels), + Labels: ToLabelList(issue.Labels, repoCache, nil), State: issue.State(), IsLocked: issue.IsLocked, Comments: issue.NumComments, @@ -168,20 +174,64 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList { } // ToLabel converts Label to API format -func ToLabel(label *models.Label) *api.Label { - return &api.Label{ +func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCache map[int64]*models.User) *api.Label { + result := &api.Label{ ID: label.ID, Name: label.Name, Color: strings.TrimLeft(label.Color, "#"), Description: label.Description, } + + // calculate URL + if label.BelongsToRepo() { + if repoCache == nil { + repoCache = make(map[int64]*models.Repository) + } + _, ok := repoCache[label.RepoID] + if !ok { + var err error + repoCache[label.RepoID], err = models.GetRepositoryByID(label.RepoID) + if err != nil { + log.Error("cant load repo of label [%d]: %v", label.ID, err) + } + } + + if repo := repoCache[label.RepoID]; repo != nil { + result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID) + } + } + if label.BelongsToOrg() { + if orgCache == nil { + orgCache = make(map[int64]*models.User) + } + _, ok := orgCache[label.OrgID] + if !ok { + var err error + orgCache[label.OrgID], err = models.GetUserByID(label.OrgID) + if err != nil { + log.Error("cant load org of label [%d]: %v", label.ID, err) + } + } + + if org := orgCache[label.RepoID]; org != nil { + result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID) + } + } + + return result } // ToLabelList converts list of Label to API format -func ToLabelList(labels []*models.Label) []*api.Label { +func ToLabelList(labels []*models.Label, repoCache map[int64]*models.Repository, orgCache map[int64]*models.User) []*api.Label { result := make([]*api.Label, len(labels)) + if repoCache == nil { + repoCache = make(map[int64]*models.Repository) + } + if orgCache == nil { + orgCache = make(map[int64]*models.User) + } for i := range labels { - result[i] = ToLabel(labels[i]) + result[i] = ToLabel(labels[i], repoCache, orgCache) } return result } diff --git a/modules/convert/issue_test.go b/modules/convert/issue_test.go index 2f8f56e99a64..2baedfdbbb82 100644 --- a/modules/convert/issue_test.go +++ b/modules/convert/issue_test.go @@ -5,10 +5,12 @@ package convert import ( + "fmt" "testing" "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -22,7 +24,8 @@ func TestLabel_ToLabel(t *testing.T) { ID: label.ID, Name: label.Name, Color: "abcdef", - }, ToLabel(label)) + URL: fmt.Sprintf("%sapi/v1/repos/user2/repo1/labels/%d", setting.AppURL, label.ID), + }, ToLabel(label, nil, nil)) } func TestMilestone_APIFormat(t *testing.T) { diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index 76061f163aac..b6c62411e46f 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -48,8 +48,9 @@ func ListLabels(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetLabelsByOrgID", err) return } - - ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) + orgCache := make(map[int64]*models.User) + orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, orgCache)) } // CreateLabel create a label for a repository @@ -96,7 +97,9 @@ func CreateLabel(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "NewLabel", err) return } - ctx.JSON(http.StatusCreated, convert.ToLabel(label)) + orgCache := make(map[int64]*models.User) + orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization + ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, orgCache)) } // GetLabel get label by organization and label id @@ -141,7 +144,9 @@ func GetLabel(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabel(label)) + orgCache := make(map[int64]*models.User) + orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization + ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, orgCache)) } // EditLabel modify a label for an Organization @@ -205,7 +210,10 @@ func EditLabel(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "UpdateLabel", err) return } - ctx.JSON(http.StatusOK, convert.ToLabel(label)) + + orgCache := make(map[int64]*models.User) + orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization + ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, orgCache)) } // DeleteLabel delete a label for an organization diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index d7f64b2d995e..34a73aff0414 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -61,7 +61,9 @@ func ListIssueLabels(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels)) + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels, repoCache, nil)) } // AddIssueLabels add labels for an issue @@ -117,7 +119,9 @@ func AddIssueLabels(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) } // DeleteIssueLabel delete a label for an issue @@ -243,7 +247,9 @@ func ReplaceIssueLabels(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) } // ClearIssueLabels delete all the labels for an issue diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index f71683f6ece1..9d5375708ebf 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -55,7 +55,9 @@ func ListLabels(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabelList(labels)) + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) } // GetLabel get label by repository and label id @@ -105,7 +107,9 @@ func GetLabel(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToLabel(label)) + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabel(label, repoCache, nil)) } // CreateLabel create a label for a repository @@ -158,7 +162,10 @@ func CreateLabel(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "NewLabel", err) return } - ctx.JSON(http.StatusCreated, convert.ToLabel(label)) + + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusCreated, convert.ToLabel(label, repoCache, nil)) } // EditLabel modify a label for a repository @@ -228,7 +235,10 @@ func EditLabel(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "UpdateLabel", err) return } - ctx.JSON(http.StatusOK, convert.ToLabel(label)) + + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.JSON(http.StatusOK, convert.ToLabel(label, repoCache, nil)) } // DeleteLabel delete a label for a repository From f7da4a1abb32eac239dac6b9ca31c0108c6cafba Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Jun 2021 22:30:51 +0200 Subject: [PATCH 2/7] Fix cp bug Co-authored-by: zeripath --- modules/convert/issue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 55017b242702..3af114b94466 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -213,7 +213,7 @@ func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCac } } - if org := orgCache[label.RepoID]; org != nil { + if org := orgCache[label.OrgID]; org != nil { result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID) } } From 3c61190d7dbe25c81dc7ee597e56c71c81aaa23d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 18 Jun 2021 00:23:13 +0200 Subject: [PATCH 3/7] } else { --- modules/convert/issue.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 3af114b94466..f060cc544dff 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -199,8 +199,7 @@ func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCac if repo := repoCache[label.RepoID]; repo != nil { result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID) } - } - if label.BelongsToOrg() { + } else { // BelongsToOrg if orgCache == nil { orgCache = make(map[int64]*models.User) } From c0c49270c2cc72e4971173f75eaf263385258954 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 29 Aug 2021 16:02:51 +0200 Subject: [PATCH 4/7] fmt --- routers/api/v1/org/label.go | 7 ++++--- routers/api/v1/repo/label.go | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index c3338344612b..f24bb3dbab73 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -49,15 +49,15 @@ func ListLabels(ctx *context.APIContext) { return } - orgCache := make(map[int64]*models.User) - orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization - count, err := models.CountLabelsByOrgID(ctx.Org.Organization.ID) if err != nil { ctx.InternalServerError(err) return } + orgCache := make(map[int64]*models.User) + orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, orgCache)) } @@ -106,6 +106,7 @@ func CreateLabel(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "NewLabel", err) return } + orgCache := make(map[int64]*models.User) orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, orgCache)) diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 94d2c9f6428d..a6bea6343b8c 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -55,15 +55,15 @@ func ListLabels(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - count, err := models.CountLabelsByRepoID(ctx.Repo.Repository.ID) if err != nil { ctx.InternalServerError(err) return } + repoCache := make(map[int64]*models.Repository) + repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository + ctx.SetTotalCountHeader(count) ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) } From 68abf1422c1b0a46c8894411510ec6f47021dd73 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 29 Aug 2021 16:05:48 +0200 Subject: [PATCH 5/7] as per @lunny --- modules/convert/issue.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index f060cc544dff..1338e64a5056 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -187,32 +187,36 @@ func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCac if repoCache == nil { repoCache = make(map[int64]*models.Repository) } - _, ok := repoCache[label.RepoID] + repo, ok := repoCache[label.RepoID] if !ok { var err error - repoCache[label.RepoID], err = models.GetRepositoryByID(label.RepoID) + repo, err = models.GetRepositoryByID(label.RepoID) if err != nil { log.Error("cant load repo of label [%d]: %v", label.ID, err) + } else { + repoCache[label.RepoID] = repo } } - if repo := repoCache[label.RepoID]; repo != nil { + if repo != nil { result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID) } } else { // BelongsToOrg if orgCache == nil { orgCache = make(map[int64]*models.User) } - _, ok := orgCache[label.OrgID] + org, ok := orgCache[label.OrgID] if !ok { var err error - orgCache[label.OrgID], err = models.GetUserByID(label.OrgID) + org, err = models.GetUserByID(label.OrgID) if err != nil { log.Error("cant load org of label [%d]: %v", label.ID, err) + } else { + orgCache[label.OrgID] = org } } - if org := orgCache[label.OrgID]; org != nil { + if org != nil { result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID) } } From c49726a221ead082800e5c6cd8b79f0cb2fea6dc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 29 Aug 2021 16:29:46 +0200 Subject: [PATCH 6/7] as per @zeripath --- modules/convert/issue.go | 54 +++++++----------------------- routers/api/v1/org/label.go | 17 +++------- routers/api/v1/repo/issue_label.go | 12 ++----- routers/api/v1/repo/label.go | 17 +++------- 4 files changed, 23 insertions(+), 77 deletions(-) diff --git a/modules/convert/issue.go b/modules/convert/issue.go index 1338e64a5056..3974d460e0e5 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -28,9 +28,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { if err := issue.LoadRepo(); err != nil { return &api.Issue{} } - - repoCache := make(map[int64]*models.Repository) - repoCache[issue.RepoID] = issue.Repo + if err := issue.Repo.GetOwner(); err != nil { + return &api.Issue{} + } apiIssue := &api.Issue{ ID: issue.ID, @@ -41,7 +41,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { Title: issue.Title, Body: issue.Content, Ref: issue.Ref, - Labels: ToLabelList(issue.Labels, repoCache, nil), + Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner), State: issue.State(), IsLocked: issue.IsLocked, Comments: issue.NumComments, @@ -174,7 +174,7 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList { } // ToLabel converts Label to API format -func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCache map[int64]*models.User) *api.Label { +func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *api.Label { result := &api.Label{ ID: label.ID, Name: label.Name, @@ -183,41 +183,17 @@ func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCac } // calculate URL - if label.BelongsToRepo() { - if repoCache == nil { - repoCache = make(map[int64]*models.Repository) - } - repo, ok := repoCache[label.RepoID] - if !ok { - var err error - repo, err = models.GetRepositoryByID(label.RepoID) - if err != nil { - log.Error("cant load repo of label [%d]: %v", label.ID, err) - } else { - repoCache[label.RepoID] = repo - } - } - + if label.BelongsToRepo() && repo != nil { if repo != nil { result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID) + } else { + log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID) } } else { // BelongsToOrg - if orgCache == nil { - orgCache = make(map[int64]*models.User) - } - org, ok := orgCache[label.OrgID] - if !ok { - var err error - org, err = models.GetUserByID(label.OrgID) - if err != nil { - log.Error("cant load org of label [%d]: %v", label.ID, err) - } else { - orgCache[label.OrgID] = org - } - } - if org != nil { result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID) + } else { + log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID) } } @@ -225,16 +201,10 @@ func ToLabel(label *models.Label, repoCache map[int64]*models.Repository, orgCac } // ToLabelList converts list of Label to API format -func ToLabelList(labels []*models.Label, repoCache map[int64]*models.Repository, orgCache map[int64]*models.User) []*api.Label { +func ToLabelList(labels []*models.Label, repo *models.Repository, org *models.User) []*api.Label { result := make([]*api.Label, len(labels)) - if repoCache == nil { - repoCache = make(map[int64]*models.Repository) - } - if orgCache == nil { - orgCache = make(map[int64]*models.User) - } for i := range labels { - result[i] = ToLabel(labels[i], repoCache, orgCache) + result[i] = ToLabel(labels[i], repo, org) } return result } diff --git a/routers/api/v1/org/label.go b/routers/api/v1/org/label.go index f24bb3dbab73..b3752841898d 100644 --- a/routers/api/v1/org/label.go +++ b/routers/api/v1/org/label.go @@ -55,11 +55,8 @@ func ListLabels(ctx *context.APIContext) { return } - orgCache := make(map[int64]*models.User) - orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization - ctx.SetTotalCountHeader(count) - ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, orgCache)) + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization)) } // CreateLabel create a label for a repository @@ -107,9 +104,7 @@ func CreateLabel(ctx *context.APIContext) { return } - orgCache := make(map[int64]*models.User) - orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization - ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, orgCache)) + ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization)) } // GetLabel get label by organization and label id @@ -154,9 +149,7 @@ func GetLabel(ctx *context.APIContext) { return } - orgCache := make(map[int64]*models.User) - orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization - ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, orgCache)) + ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization)) } // EditLabel modify a label for an Organization @@ -221,9 +214,7 @@ func EditLabel(ctx *context.APIContext) { return } - orgCache := make(map[int64]*models.User) - orgCache[ctx.Org.Organization.ID] = ctx.Org.Organization - ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, orgCache)) + ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization)) } // DeleteLabel delete a label for an organization diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index 34a73aff0414..0469ae247c3b 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -61,9 +61,7 @@ func ListIssueLabels(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels, ctx.Repo.Repository, ctx.Repo.Owner)) } // AddIssueLabels add labels for an issue @@ -119,9 +117,7 @@ func AddIssueLabels(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner)) } // DeleteIssueLabel delete a label for an issue @@ -247,9 +243,7 @@ func ReplaceIssueLabels(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner)) } // ClearIssueLabels delete all the labels for an issue diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index a6bea6343b8c..67682fc60da9 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -61,11 +61,8 @@ func ListLabels(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.SetTotalCountHeader(count) - ctx.JSON(http.StatusOK, convert.ToLabelList(labels, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, nil)) } // GetLabel get label by repository and label id @@ -115,9 +112,7 @@ func GetLabel(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusOK, convert.ToLabel(label, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil)) } // CreateLabel create a label for a repository @@ -171,9 +166,7 @@ func CreateLabel(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusCreated, convert.ToLabel(label, repoCache, nil)) + ctx.JSON(http.StatusCreated, convert.ToLabel(label, ctx.Repo.Repository, nil)) } // EditLabel modify a label for a repository @@ -244,9 +237,7 @@ func EditLabel(ctx *context.APIContext) { return } - repoCache := make(map[int64]*models.Repository) - repoCache[ctx.Repo.Repository.ID] = ctx.Repo.Repository - ctx.JSON(http.StatusOK, convert.ToLabel(label, repoCache, nil)) + ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil)) } // DeleteLabel delete a label for a repository From 7abb499112d36bfd8474247c6d3c21838433fe6d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 29 Aug 2021 20:50:51 +0200 Subject: [PATCH 7/7] fix test --- modules/convert/issue_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/convert/issue_test.go b/modules/convert/issue_test.go index 2baedfdbbb82..f3c5b50c8cf3 100644 --- a/modules/convert/issue_test.go +++ b/modules/convert/issue_test.go @@ -20,12 +20,13 @@ import ( func TestLabel_ToLabel(t *testing.T) { assert.NoError(t, models.PrepareTestDatabase()) label := models.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) assert.Equal(t, &api.Label{ ID: label.ID, Name: label.Name, Color: "abcdef", URL: fmt.Sprintf("%sapi/v1/repos/user2/repo1/labels/%d", setting.AppURL, label.ID), - }, ToLabel(label, nil, nil)) + }, ToLabel(label, repo, nil)) } func TestMilestone_APIFormat(t *testing.T) {