From c425bd646223978b464ec136405e5991fd8c1fc6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 27 Feb 2017 19:14:23 +0800 Subject: [PATCH 1/5] Add api url func. Signed-off-by: Bo-Yi Wu --- models/issue.go | 7 +++++++ models/repo.go | 5 +++++ models/repo_test.go | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/models/issue.go b/models/issue.go index ca01ffe2fac50..115cbc077796b 100644 --- a/models/issue.go +++ b/models/issue.go @@ -7,6 +7,7 @@ package models import ( "errors" "fmt" + "path" "sort" "strings" "time" @@ -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 path.Join(issue.Repo.APIURL(), "issues", string(issue.ID)) +} + // HTMLURL returns the absolute URL to this issue. func (issue *Issue) HTMLURL() string { var path string @@ -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, diff --git a/models/repo.go b/models/repo.go index d72d3d1da295c..8361780bfda83 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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() diff --git a/models/repo_test.go b/models/repo_test.go index 7c0e94a5aed59..603c1d18fd94f 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -125,3 +125,14 @@ func TestForkRepository(t *testing.T) { assert.Error(t, err) assert.True(t, IsErrRepoAlreadyExist(err)) } + +func TestRepoAPIURL(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + repo := &Repository{ + ID: int64(10), + OwnerID: int64(12), + Name: "repo10", + } + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) +} From 2d38a31a892e200d2f5fda12e09f84177b2e764b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 28 Feb 2017 00:15:55 +0800 Subject: [PATCH 2/5] fix: Add unit testing. --- models/issue.go | 2 +- models/issue_test.go | 15 ++++++++++++++- models/repo.go | 2 +- models/repo_test.go | 6 +----- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/models/issue.go b/models/issue.go index 115cbc077796b..30ffb4b9ef07e 100644 --- a/models/issue.go +++ b/models/issue.go @@ -203,7 +203,7 @@ func (issue *Issue) GetIsRead(userID int64) error { // APIURL returns the absolute APIURL to this issue. func (issue *Issue) APIURL() string { - return path.Join(issue.Repo.APIURL(), "issues", string(issue.ID)) + return strings.TrimRight(issue.Repo.APIURL(), "/") + "/" + path.Join("issues", fmt.Sprint(issue.ID)) } // HTMLURL returns the absolute URL to this issue. diff --git a/models/issue_test.go b/models/issue_test.go index 646a1a5a9ad58..93a59f62beafb 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -1,4 +1,8 @@ +<<<<<<< c67fc79f985bd7a2c077ec18cc5538ede3242489 // Copyright 2017 The Gitea Authors. All rights reserved. +======= +// Copyright 2017 The Gogs Authors. All rights reserved. +>>>>>>> fix: Add unit testing. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -10,7 +14,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestIssue_ReplaceLabels(t *testing.T) { +func TestIssueReplaceLabels(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) testSuccess := func(issueID int64, labelIDs []int64) { @@ -33,3 +37,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()) +} diff --git a/models/repo.go b/models/repo.go index 8361780bfda83..8930b12f1d731 100644 --- a/models/repo.go +++ b/models/repo.go @@ -266,7 +266,7 @@ func (repo *Repository) HTMLURL() string { // APIURL returns the repository API URL func (repo *Repository) APIURL() string { - return setting.AppURL + path.Join("api/v1/repos", repo.FullName()) + return strings.TrimRight(setting.AppURL, "/") + "/" + path.Join("api/v1/repos", repo.FullName()) } // APIFormat converts a Repository to api.Repository diff --git a/models/repo_test.go b/models/repo_test.go index 603c1d18fd94f..a538d44c0ea52 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -128,11 +128,7 @@ func TestForkRepository(t *testing.T) { func TestRepoAPIURL(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) + repo := AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) - repo := &Repository{ - ID: int64(10), - OwnerID: int64(12), - Name: "repo10", - } assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) } From 3bd9e8da2b004bf49034fbdc3fecf83f5657c216 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 2 Mar 2017 10:51:55 +0800 Subject: [PATCH 3/5] fix: conflicts --- models/issue_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/models/issue_test.go b/models/issue_test.go index 93a59f62beafb..5f82c38074f14 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -1,8 +1,4 @@ -<<<<<<< c67fc79f985bd7a2c077ec18cc5538ede3242489 // Copyright 2017 The Gitea Authors. All rights reserved. -======= -// Copyright 2017 The Gogs Authors. All rights reserved. ->>>>>>> fix: Add unit testing. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. From d1071d6b29d78518978b937abb4a94547a61bb1b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 3 Mar 2017 10:41:18 +0800 Subject: [PATCH 4/5] fix: remove trim --- models/issue.go | 2 +- models/repo.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/models/issue.go b/models/issue.go index 30ffb4b9ef07e..c740d8fec46dc 100644 --- a/models/issue.go +++ b/models/issue.go @@ -203,7 +203,7 @@ func (issue *Issue) GetIsRead(userID int64) error { // APIURL returns the absolute APIURL to this issue. func (issue *Issue) APIURL() string { - return strings.TrimRight(issue.Repo.APIURL(), "/") + "/" + path.Join("issues", fmt.Sprint(issue.ID)) + return issue.Repo.APIURL() + "/" + path.Join("issues", fmt.Sprint(issue.ID)) } // HTMLURL returns the absolute URL to this issue. diff --git a/models/repo.go b/models/repo.go index 8930b12f1d731..8361780bfda83 100644 --- a/models/repo.go +++ b/models/repo.go @@ -266,7 +266,7 @@ func (repo *Repository) HTMLURL() string { // APIURL returns the repository API URL func (repo *Repository) APIURL() string { - return strings.TrimRight(setting.AppURL, "/") + "/" + path.Join("api/v1/repos", repo.FullName()) + return setting.AppURL + path.Join("api/v1/repos", repo.FullName()) } // APIFormat converts a Repository to api.Repository From b5d1bd5a7c8f29acb5881db647015cf3110dfbdd Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 3 Mar 2017 16:18:03 +0800 Subject: [PATCH 5/5] fix: revert test function name. --- models/issue_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/issue_test.go b/models/issue_test.go index 5f82c38074f14..a6da80917e106 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestIssueReplaceLabels(t *testing.T) { +func TestIssue_ReplaceLabels(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) testSuccess := func(issueID int64, labelIDs []int64) {