55package convert
66
77import (
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}
0 commit comments