Skip to content

Commit 51578d6

Browse files
authored
Calculate label URL on API (#16186)
close #8028
1 parent 9a938dc commit 51578d6

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

modules/convert/issue.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
package convert
66

77
import (
8+
"fmt"
89
"strings"
910

1011
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
1114
api "code.gitea.io/gitea/modules/structs"
1215
)
1316

@@ -25,6 +28,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
2528
if err := issue.LoadRepo(); err != nil {
2629
return &api.Issue{}
2730
}
31+
if err := issue.Repo.GetOwner(); err != nil {
32+
return &api.Issue{}
33+
}
2834

2935
apiIssue := &api.Issue{
3036
ID: issue.ID,
@@ -35,7 +41,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
3541
Title: issue.Title,
3642
Body: issue.Content,
3743
Ref: issue.Ref,
38-
Labels: ToLabelList(issue.Labels),
44+
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
3945
State: issue.State(),
4046
IsLocked: issue.IsLocked,
4147
Comments: issue.NumComments,
@@ -168,20 +174,37 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList {
168174
}
169175

170176
// ToLabel converts Label to API format
171-
func ToLabel(label *models.Label) *api.Label {
172-
return &api.Label{
177+
func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *api.Label {
178+
result := &api.Label{
173179
ID: label.ID,
174180
Name: label.Name,
175181
Color: strings.TrimLeft(label.Color, "#"),
176182
Description: label.Description,
177183
}
184+
185+
// calculate URL
186+
if label.BelongsToRepo() && repo != nil {
187+
if repo != nil {
188+
result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID)
189+
} else {
190+
log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID)
191+
}
192+
} else { // BelongsToOrg
193+
if org != nil {
194+
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID)
195+
} else {
196+
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
197+
}
198+
}
199+
200+
return result
178201
}
179202

180203
// ToLabelList converts list of Label to API format
181-
func ToLabelList(labels []*models.Label) []*api.Label {
204+
func ToLabelList(labels []*models.Label, repo *models.Repository, org *models.User) []*api.Label {
182205
result := make([]*api.Label, len(labels))
183206
for i := range labels {
184-
result[i] = ToLabel(labels[i])
207+
result[i] = ToLabel(labels[i], repo, org)
185208
}
186209
return result
187210
}

modules/convert/issue_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package convert
66

77
import (
8+
"fmt"
89
"testing"
910
"time"
1011

1112
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/setting"
1214
api "code.gitea.io/gitea/modules/structs"
1315
"code.gitea.io/gitea/modules/timeutil"
1416

@@ -18,11 +20,13 @@ import (
1820
func TestLabel_ToLabel(t *testing.T) {
1921
assert.NoError(t, models.PrepareTestDatabase())
2022
label := models.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label)
23+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository)
2124
assert.Equal(t, &api.Label{
2225
ID: label.ID,
2326
Name: label.Name,
2427
Color: "abcdef",
25-
}, ToLabel(label))
28+
URL: fmt.Sprintf("%sapi/v1/repos/user2/repo1/labels/%d", setting.AppURL, label.ID),
29+
}, ToLabel(label, repo, nil))
2630
}
2731

2832
func TestMilestone_APIFormat(t *testing.T) {

routers/api/v1/org/label.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func ListLabels(ctx *context.APIContext) {
5656
}
5757

5858
ctx.SetTotalCountHeader(count)
59-
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
59+
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization))
6060
}
6161

6262
// CreateLabel create a label for a repository
@@ -103,7 +103,8 @@ func CreateLabel(ctx *context.APIContext) {
103103
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
104104
return
105105
}
106-
ctx.JSON(http.StatusCreated, convert.ToLabel(label))
106+
107+
ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization))
107108
}
108109

109110
// GetLabel get label by organization and label id
@@ -148,7 +149,7 @@ func GetLabel(ctx *context.APIContext) {
148149
return
149150
}
150151

151-
ctx.JSON(http.StatusOK, convert.ToLabel(label))
152+
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
152153
}
153154

154155
// EditLabel modify a label for an Organization
@@ -212,7 +213,8 @@ func EditLabel(ctx *context.APIContext) {
212213
ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
213214
return
214215
}
215-
ctx.JSON(http.StatusOK, convert.ToLabel(label))
216+
217+
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
216218
}
217219

218220
// DeleteLabel delete a label for an organization

routers/api/v1/repo/issue_label.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func ListIssueLabels(ctx *context.APIContext) {
6161
return
6262
}
6363

64-
ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels))
64+
ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels, ctx.Repo.Repository, ctx.Repo.Owner))
6565
}
6666

6767
// AddIssueLabels add labels for an issue
@@ -117,7 +117,7 @@ func AddIssueLabels(ctx *context.APIContext) {
117117
return
118118
}
119119

120-
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
120+
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner))
121121
}
122122

123123
// DeleteIssueLabel delete a label for an issue
@@ -243,7 +243,7 @@ func ReplaceIssueLabels(ctx *context.APIContext) {
243243
return
244244
}
245245

246-
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
246+
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner))
247247
}
248248

249249
// ClearIssueLabels delete all the labels for an issue

routers/api/v1/repo/label.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func ListLabels(ctx *context.APIContext) {
6262
}
6363

6464
ctx.SetTotalCountHeader(count)
65-
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
65+
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, nil))
6666
}
6767

6868
// GetLabel get label by repository and label id
@@ -112,7 +112,7 @@ func GetLabel(ctx *context.APIContext) {
112112
return
113113
}
114114

115-
ctx.JSON(http.StatusOK, convert.ToLabel(label))
115+
ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil))
116116
}
117117

118118
// CreateLabel create a label for a repository
@@ -165,7 +165,8 @@ func CreateLabel(ctx *context.APIContext) {
165165
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
166166
return
167167
}
168-
ctx.JSON(http.StatusCreated, convert.ToLabel(label))
168+
169+
ctx.JSON(http.StatusCreated, convert.ToLabel(label, ctx.Repo.Repository, nil))
169170
}
170171

171172
// EditLabel modify a label for a repository
@@ -235,7 +236,8 @@ func EditLabel(ctx *context.APIContext) {
235236
ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
236237
return
237238
}
238-
ctx.JSON(http.StatusOK, convert.ToLabel(label))
239+
240+
ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil))
239241
}
240242

241243
// DeleteLabel delete a label for a repository

0 commit comments

Comments
 (0)