Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose url field on issue api. #982

Merged
merged 5 commits into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models
import (
"errors"
"fmt"
"path"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -200,6 +201,11 @@ func (issue *Issue) GetIsRead(userID int64) error {
return nil
}

// APIURL returns the absolute APIURL to this issue.
func (issue *Issue) APIURL() string {
return issue.Repo.APIURL() + "/" + path.Join("issues", fmt.Sprint(issue.ID))
}

// HTMLURL returns the absolute URL to this issue.
func (issue *Issue) HTMLURL() string {
var path string
Expand Down Expand Up @@ -246,6 +252,7 @@ func (issue *Issue) APIFormat() *api.Issue {

apiIssue := &api.Issue{
ID: issue.ID,
URL: issue.APIURL(),
Index: issue.Index,
Poster: issue.Poster.APIFormat(),
Title: issue.Title,
Expand Down
9 changes: 9 additions & 0 deletions models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ func TestIssue_ReplaceLabels(t *testing.T) {
testSuccess(1, []int64{1, 2})
testSuccess(1, []int64{})
}

func TestIssueAPIURL(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
err := issue.LoadAttributes()

assert.NoError(t, err)
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL())
}
5 changes: 5 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ func (repo *Repository) HTMLURL() string {
return setting.AppURL + repo.FullName()
}

// APIURL returns the repository API URL
func (repo *Repository) APIURL() string {
return setting.AppURL + path.Join("api/v1/repos", repo.FullName())
}

// APIFormat converts a Repository to api.Repository
func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
cloneLink := repo.CloneLink()
Expand Down
7 changes: 7 additions & 0 deletions models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ func TestForkRepository(t *testing.T) {
assert.Error(t, err)
assert.True(t, IsErrRepoAlreadyExist(err))
}

func TestRepoAPIURL(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing output lint errors after execute make lint command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linting doesn't apply to *_test.go 🙂

assert.NoError(t, PrepareTestDatabase())
repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository)

assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL())
}