diff --git a/models/issues/issue.go b/models/issues/issue.go index a90686eb50..e73dc57b6b 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -386,7 +386,14 @@ func (issue *Issue) APIURL(ctx context.Context) string { return "" } } - return fmt.Sprintf("%s/issues/%d", issue.Repo.APIURL(), issue.Index) + + var path string + if issue.IsPull { + path = "pulls" + } else { + path = "issues" + } + return fmt.Sprintf("%s/%s/%d", issue.Repo.APIURL(), path, issue.Index) } // HTMLURL returns the absolute URL to this issue. diff --git a/models/issues/issue_test.go b/models/issues/issue_test.go index 0c5da6a2aa..f21df067a2 100644 --- a/models/issues/issue_test.go +++ b/models/issues/issue_test.go @@ -67,6 +67,10 @@ func TestIssueAPIURL(t *testing.T) { require.NoError(t, err) assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/issues/1", issue.APIURL(db.DefaultContext)) + + pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) + require.NoError(t, pr.LoadIssue(db.DefaultContext)) + assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/pulls/2", pr.Issue.APIURL(db.DefaultContext)) } func TestGetIssuesByIDs(t *testing.T) { diff --git a/services/convert/pull.go b/services/convert/pull.go index 4856c58736..38ab42048a 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -67,7 +67,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u apiPullRequest := &api.PullRequest{ ID: pr.ID, - URL: pr.Issue.HTMLURL(), + URL: pr.Issue.APIURL(ctx), Index: pr.Index, Poster: apiIssue.Poster, Title: apiIssue.Title, diff --git a/tests/integration/actions_trust_test.go b/tests/integration/actions_trust_test.go index a990529e2e..3c334e0d8c 100644 --- a/tests/integration/actions_trust_test.go +++ b/tests/integration/actions_trust_test.go @@ -534,7 +534,7 @@ func TestActionsPullRequestTrustCancelOnClose(t *testing.T) { regularUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}) regularSession := loginUser(t, regularUser.Name) - token := getTokenForLoggedInUser(t, regularSession, auth_model.AccessTokenScopeWriteIssue) + token := getTokenForLoggedInUser(t, regularSession, auth_model.AccessTokenScopeWriteRepository) baseRepo, f := actionsTrustTestCreateBaseRepo(t, ownerUser) defer f() diff --git a/tests/integration/api_pull_test.go b/tests/integration/api_pull_test.go index 17fab07f9e..98fb100b76 100644 --- a/tests/integration/api_pull_test.go +++ b/tests/integration/api_pull_test.go @@ -299,6 +299,7 @@ func TestAPICreatePullSuccess(t *testing.T) { DecodeJSON(t, res, pull) assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", pull.MergeBase) + assert.Contains(t, pull.URL, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner10.Name, repo10.Name, pull.Index)) } func TestAPICreatePullSameRepoSuccess(t *testing.T) {