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] [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