Skip to content

Commit d76d67d

Browse files
appleboylunny
authored andcommitted
feat: expose url field on issue api. (#982)
* Add api url func. Signed-off-by: Bo-Yi Wu <[email protected]> * fix: Add unit testing. * fix: conflicts * fix: remove trim * fix: revert test function name.
1 parent 0afab87 commit d76d67d

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

Diff for: models/issue.go

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"errors"
99
"fmt"
10+
"path"
1011
"sort"
1112
"strings"
1213
"time"
@@ -200,6 +201,11 @@ func (issue *Issue) GetIsRead(userID int64) error {
200201
return nil
201202
}
202203

204+
// APIURL returns the absolute APIURL to this issue.
205+
func (issue *Issue) APIURL() string {
206+
return issue.Repo.APIURL() + "/" + path.Join("issues", fmt.Sprint(issue.ID))
207+
}
208+
203209
// HTMLURL returns the absolute URL to this issue.
204210
func (issue *Issue) HTMLURL() string {
205211
var path string
@@ -246,6 +252,7 @@ func (issue *Issue) APIFormat() *api.Issue {
246252

247253
apiIssue := &api.Issue{
248254
ID: issue.ID,
255+
URL: issue.APIURL(),
249256
Index: issue.Index,
250257
Poster: issue.Poster.APIFormat(),
251258
Title: issue.Title,

Diff for: models/issue_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ func TestIssue_ReplaceLabels(t *testing.T) {
3333
testSuccess(1, []int64{1, 2})
3434
testSuccess(1, []int64{})
3535
}
36+
37+
func TestIssueAPIURL(t *testing.T) {
38+
assert.NoError(t, PrepareTestDatabase())
39+
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
40+
err := issue.LoadAttributes()
41+
42+
assert.NoError(t, err)
43+
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
44+
}

Diff for: models/repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ func (repo *Repository) HTMLURL() string {
264264
return setting.AppURL + repo.FullName()
265265
}
266266

267+
// APIURL returns the repository API URL
268+
func (repo *Repository) APIURL() string {
269+
return setting.AppURL + path.Join("api/v1/repos", repo.FullName())
270+
}
271+
267272
// APIFormat converts a Repository to api.Repository
268273
func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
269274
cloneLink := repo.CloneLink()

Diff for: models/repo_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,10 @@ func TestForkRepository(t *testing.T) {
125125
assert.Error(t, err)
126126
assert.True(t, IsErrRepoAlreadyExist(err))
127127
}
128+
129+
func TestRepoAPIURL(t *testing.T) {
130+
assert.NoError(t, PrepareTestDatabase())
131+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)
132+
133+
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
134+
}

0 commit comments

Comments
 (0)