Skip to content

Commit 3c24b3b

Browse files
committed
apply patch by @zeripath
1 parent 7351a1e commit 3c24b3b

File tree

4 files changed

+75
-48
lines changed

4 files changed

+75
-48
lines changed

models/issues/issue.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (issue *Issue) IsOverdue() bool {
189189

190190
// LoadRepo loads issue's repository
191191
func (issue *Issue) LoadRepo(ctx context.Context) (err error) {
192-
if issue.Repo == nil {
192+
if issue.Repo == nil && issue.RepoID != 0 {
193193
issue.Repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID)
194194
if err != nil {
195195
return fmt.Errorf("getRepositoryByID [%d]: %w", issue.RepoID, err)
@@ -223,7 +223,7 @@ func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
223223

224224
// LoadLabels loads labels
225225
func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
226-
if issue.Labels == nil {
226+
if issue.Labels == nil && issue.ID != 0 {
227227
issue.Labels, err = GetLabelsByIssueID(ctx, issue.ID)
228228
if err != nil {
229229
return fmt.Errorf("getLabelsByIssueID [%d]: %w", issue.ID, err)
@@ -234,7 +234,7 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
234234

235235
// LoadPoster loads poster
236236
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
237-
if issue.Poster == nil {
237+
if issue.Poster == nil && issue.PosterID != 0 {
238238
issue.Poster, err = user_model.GetPossibleUserByID(ctx, issue.PosterID)
239239
if err != nil {
240240
issue.PosterID = -1
@@ -252,7 +252,7 @@ func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
252252
// LoadPullRequest loads pull request info
253253
func (issue *Issue) LoadPullRequest(ctx context.Context) (err error) {
254254
if issue.IsPull {
255-
if issue.PullRequest == nil {
255+
if issue.PullRequest == nil && issue.ID != 0 {
256256
issue.PullRequest, err = GetPullRequestByIssueID(ctx, issue.ID)
257257
if err != nil {
258258
if IsErrPullRequestNotExist(err) {
@@ -261,7 +261,9 @@ func (issue *Issue) LoadPullRequest(ctx context.Context) (err error) {
261261
return fmt.Errorf("getPullRequestByIssueID [%d]: %w", issue.ID, err)
262262
}
263263
}
264-
issue.PullRequest.Issue = issue
264+
if issue.PullRequest != nil {
265+
issue.PullRequest.Issue = issue
266+
}
265267
}
266268
return nil
267269
}
@@ -2128,15 +2130,18 @@ func (issue *Issue) GetParticipantIDsByIssue(ctx context.Context) ([]int64, erro
21282130
}
21292131

21302132
// BlockedByDependencies finds all Dependencies an issue is blocked by
2131-
func (issue *Issue) BlockedByDependencies(ctx context.Context) (issueDeps []*DependencyInfo, err error) {
2132-
err = db.GetEngine(ctx).
2133+
func (issue *Issue) BlockedByDependencies(ctx context.Context, opts db.ListOptions) (issueDeps []*DependencyInfo, err error) {
2134+
sess := db.GetEngine(ctx).
21332135
Table("issue").
21342136
Join("INNER", "repository", "repository.id = issue.repo_id").
21352137
Join("INNER", "issue_dependency", "issue_dependency.dependency_id = issue.id").
21362138
Where("issue_id = ?", issue.ID).
21372139
// sort by repo id then created date, with the issues of the same repo at the beginning of the list
2138-
OrderBy("CASE WHEN issue.repo_id = ? THEN 0 ELSE issue.repo_id END, issue.created_unix DESC", issue.RepoID).
2139-
Find(&issueDeps)
2140+
OrderBy("CASE WHEN issue.repo_id = ? THEN 0 ELSE issue.repo_id END, issue.created_unix DESC", issue.RepoID)
2141+
if opts.Page != 0 {
2142+
sess = db.SetSessionPagination(sess, &opts)
2143+
}
2144+
err = sess.Find(&issueDeps)
21402145

21412146
for _, depInfo := range issueDeps {
21422147
depInfo.Issue.Repo = &depInfo.Repository

routers/api/v1/repo/issue_dependency.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
"net/http"
99

10+
"code.gitea.io/gitea/models/db"
1011
issues_model "code.gitea.io/gitea/models/issues"
1112
access_model "code.gitea.io/gitea/models/perm/access"
1213
repo_model "code.gitea.io/gitea/models/repo"
@@ -74,37 +75,36 @@ func GetIssueDependencies(ctx *context.APIContext) {
7475
return
7576
}
7677

77-
canWrite := ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull)
78-
79-
// 2. Get everything `issue` depends on, i.e. All `<#b>`: `<issue> <- <#b>`
80-
blockersInfo, err := issue.BlockedByDependencies(ctx)
81-
if err != nil {
82-
ctx.Error(http.StatusInternalServerError, "BlockedByDependencies", err)
83-
return
84-
}
85-
8678
page := ctx.FormInt("page")
8779
if page <= 1 {
8880
page = 1
8981
}
9082
limit := ctx.FormInt("limit")
91-
if limit <= 1 {
83+
if limit == 0 {
9284
limit = setting.API.DefaultPagingNum
85+
} else if limit > setting.API.MaxResponseItems {
86+
limit = setting.API.MaxResponseItems
9387
}
9488

95-
skip := (page - 1) * limit
96-
max := page * limit
89+
canWrite := ctx.Repo.Permission.CanWriteIssuesOrPulls(issue.IsPull)
9790

98-
var blockerIssues []*issues_model.Issue
91+
blockerIssues := make([]*issues_model.Issue, 0, limit)
9992

10093
perms := map[int64]access_model.Permission{}
10194
perms[ctx.Repo.Repository.ID] = ctx.Repo.Permission
102-
// FIXME: the i here should be the number of issues displayed to the user!
103-
for i, blocker := range blockersInfo {
104-
if i < skip || i >= max {
105-
continue
106-
}
10795

96+
// 2. Get the issues this issue depends on, i.e. the `<#b>`: `<issue> <- <#b>`
97+
blockersInfo, err := issue.BlockedByDependencies(ctx, db.ListOptions{
98+
Page: page,
99+
PageSize: limit,
100+
})
101+
if err != nil {
102+
ctx.Error(http.StatusInternalServerError, "BlockedByDependencies", err)
103+
return
104+
}
105+
for _, blocker := range blockersInfo {
106+
107+
// Get the permissions for this repository
108108
perm, has := perms[blocker.Repository.ID]
109109
if !has {
110110
perm, err = access_model.GetUserRepoPermission(ctx, &blocker.Repository, ctx.Doer)
@@ -114,16 +114,34 @@ func GetIssueDependencies(ctx *context.APIContext) {
114114
}
115115
}
116116

117+
// check permission
117118
if !perm.CanReadIssuesOrPulls(blocker.Issue.IsPull) {
118119
if !canWrite {
119-
// We can't read this issue/pull so we shouldn't know about it
120-
// FIXME: the UI is broken here!
121-
continue
120+
hiddenBlocker := &issues_model.DependencyInfo{
121+
Issue: issues_model.Issue{
122+
Title: "HIDDEN",
123+
},
124+
}
125+
blocker = hiddenBlocker
126+
} else {
127+
confidentialBlocker := &issues_model.DependencyInfo{
128+
Issue: issues_model.Issue{
129+
RepoID: blocker.Issue.RepoID,
130+
Index: blocker.Index,
131+
Title: blocker.Title,
132+
IsClosed: blocker.IsClosed,
133+
IsPull: blocker.IsPull,
134+
},
135+
Repository: repo_model.Repository{
136+
ID: blocker.Issue.Repo.ID,
137+
Name: blocker.Issue.Repo.Name,
138+
OwnerName: blocker.Issue.Repo.OwnerName,
139+
},
140+
}
141+
confidentialBlocker.Issue.Repo = &confidentialBlocker.Repository
142+
blocker = confidentialBlocker
122143
}
123-
// FIXME: We shouldn't display the whole issue here
124144
}
125-
126-
blocker.Issue.Repo = &blocker.Repository
127145
blockerIssues = append(blockerIssues, &blocker.Issue)
128146
}
129147

routers/web/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ func ViewIssue(ctx *context.Context) {
18121812
}
18131813

18141814
// Get Dependencies
1815-
ctx.Data["BlockedByDependencies"], err = issue.BlockedByDependencies(ctx)
1815+
ctx.Data["BlockedByDependencies"], err = issue.BlockedByDependencies(ctx, db.ListOptions{})
18161816
if err != nil {
18171817
ctx.ServerError("BlockedByDependencies", err)
18181818
return

services/convert/issue.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
3232
if err := issue.LoadRepo(ctx); err != nil {
3333
return &api.Issue{}
3434
}
35-
if err := issue.Repo.LoadOwner(ctx); err != nil {
36-
return &api.Issue{}
37-
}
3835

3936
apiIssue := &api.Issue{
4037
ID: issue.ID,
@@ -46,19 +43,24 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
4643
Body: issue.Content,
4744
Attachments: ToAttachments(issue.Attachments),
4845
Ref: issue.Ref,
49-
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
5046
State: issue.State(),
5147
IsLocked: issue.IsLocked,
5248
Comments: issue.NumComments,
5349
Created: issue.CreatedUnix.AsTime(),
5450
Updated: issue.UpdatedUnix.AsTime(),
5551
}
5652

57-
apiIssue.Repo = &api.RepositoryMeta{
58-
ID: issue.Repo.ID,
59-
Name: issue.Repo.Name,
60-
Owner: issue.Repo.OwnerName,
61-
FullName: issue.Repo.FullName(),
53+
if issue.Repo != nil {
54+
if err := issue.Repo.LoadOwner(ctx); err != nil {
55+
return &api.Issue{}
56+
}
57+
apiIssue.Labels = ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner)
58+
apiIssue.Repo = &api.RepositoryMeta{
59+
ID: issue.Repo.ID,
60+
Name: issue.Repo.Name,
61+
Owner: issue.Repo.OwnerName,
62+
FullName: issue.Repo.FullName(),
63+
}
6264
}
6365

6466
if issue.ClosedUnix != 0 {
@@ -85,11 +87,13 @@ func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
8587
if err := issue.LoadPullRequest(ctx); err != nil {
8688
return &api.Issue{}
8789
}
88-
apiIssue.PullRequest = &api.PullRequestMeta{
89-
HasMerged: issue.PullRequest.HasMerged,
90-
}
91-
if issue.PullRequest.HasMerged {
92-
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
90+
if issue.PullRequest != nil {
91+
apiIssue.PullRequest = &api.PullRequestMeta{
92+
HasMerged: issue.PullRequest.HasMerged,
93+
}
94+
if issue.PullRequest.HasMerged {
95+
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
96+
}
9397
}
9498
}
9599
if issue.DeadlineUnix != 0 {

0 commit comments

Comments
 (0)