Skip to content

Commit f540d0a

Browse files
authored
Fix issues/pulls dependencies problems (#9842)
* Fix issues/pulls dependencies problems * fix swagger and api param name * fix js
1 parent 0641965 commit f540d0a

File tree

8 files changed

+57
-26
lines changed

8 files changed

+57
-26
lines changed

modules/context/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b
138138
}
139139

140140
// CanCreateIssueDependencies returns whether or not a user can create dependencies.
141-
func (r *Repository) CanCreateIssueDependencies(user *models.User) bool {
142-
return r.Permission.CanWrite(models.UnitTypeIssues) && r.Repository.IsDependenciesEnabled()
141+
func (r *Repository) CanCreateIssueDependencies(user *models.User, isPull bool) bool {
142+
return r.Repository.IsDependenciesEnabled() && r.Permission.CanWriteIssuesOrPulls(isPull)
143143
}
144144

145145
// GetCommitsCount returns cached commit count for current view

routers/api/v1/repo/issue.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func SearchIssues(ctx *context.APIContext) {
5151
// description: repository to prioritize in the results
5252
// type: integer
5353
// format: int64
54+
// - name: type
55+
// in: query
56+
// description: filter by type (issues / pulls) if set
57+
// type: string
5458
// responses:
5559
// "200":
5660
// "$ref": "#/responses/IssueList"
@@ -128,6 +132,16 @@ func SearchIssues(ctx *context.APIContext) {
128132
}
129133
}
130134

135+
var isPull util.OptionalBool
136+
switch ctx.Query("type") {
137+
case "pulls":
138+
isPull = util.OptionalBoolTrue
139+
case "issues":
140+
isPull = util.OptionalBoolFalse
141+
default:
142+
isPull = util.OptionalBoolNone
143+
}
144+
131145
// Only fetch the issues if we either don't have a keyword or the search returned issues
132146
// This would otherwise return all issues if no issues were found by the search.
133147
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
@@ -140,6 +154,7 @@ func SearchIssues(ctx *context.APIContext) {
140154
LabelIDs: labelIDs,
141155
SortType: "priorityrepo",
142156
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
157+
IsPull: isPull,
143158
})
144159
}
145160

routers/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func CompareDiff(ctx *context.Context) {
413413

414414
if !nothingToCompare {
415415
// Setup information for new form.
416-
RetrieveRepoMetas(ctx, ctx.Repo.Repository)
416+
RetrieveRepoMetas(ctx, ctx.Repo.Repository, true)
417417
if ctx.Written() {
418418
return
419419
}

routers/repo/issue.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos
343343
}
344344

345345
// RetrieveRepoMetas find all the meta information of a repository
346-
func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
346+
func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull bool) []*models.Label {
347347
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
348348
return nil
349349
}
@@ -368,7 +368,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.
368368
ctx.Data["Branches"] = brs
369369

370370
// Contains true if the user can create issue dependencies
371-
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
371+
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User, isPull)
372372

373373
return labels
374374
}
@@ -438,7 +438,7 @@ func NewIssue(ctx *context.Context) {
438438
setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
439439
renderAttachmentSettings(ctx)
440440

441-
RetrieveRepoMetas(ctx, ctx.Repo.Repository)
441+
RetrieveRepoMetas(ctx, ctx.Repo.Repository, false)
442442
if ctx.Written() {
443443
return
444444
}
@@ -453,7 +453,7 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull b
453453
err error
454454
)
455455

456-
labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
456+
labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository, isPull)
457457
if ctx.Written() {
458458
return nil, nil, 0
459459
}
@@ -665,6 +665,14 @@ func ViewIssue(ctx *context.Context) {
665665
ctx.Data["PageIsIssueList"] = true
666666
}
667667

668+
if issue.IsPull && !ctx.Repo.CanRead(models.UnitTypeIssues) {
669+
ctx.Data["IssueType"] = "pulls"
670+
} else if !issue.IsPull && !ctx.Repo.CanRead(models.UnitTypePullRequests) {
671+
ctx.Data["IssueType"] = "issues"
672+
} else {
673+
ctx.Data["IssueType"] = "all"
674+
}
675+
668676
ctx.Data["RequireHighlightJS"] = true
669677
ctx.Data["RequireDropzone"] = true
670678
ctx.Data["RequireTribute"] = true
@@ -802,7 +810,7 @@ func ViewIssue(ctx *context.Context) {
802810
}
803811

804812
// Check if the user can use the dependencies
805-
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
813+
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull)
806814

807815
// check if dependencies can be created across repositories
808816
ctx.Data["AllowCrossRepositoryDependencies"] = setting.Service.AllowCrossRepositoryDependencies

routers/repo/issue_dependency.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ import (
1414

1515
// AddDependency adds new dependencies
1616
func AddDependency(ctx *context.Context) {
17-
// Check if the Repo is allowed to have dependencies
18-
if !ctx.Repo.CanCreateIssueDependencies(ctx.User) {
19-
ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
20-
return
21-
}
22-
23-
depID := ctx.QueryInt64("newDependency")
24-
2517
issueIndex := ctx.ParamsInt64("index")
2618
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
2719
if err != nil {
2820
ctx.ServerError("GetIssueByIndex", err)
2921
return
3022
}
3123

24+
// Check if the Repo is allowed to have dependencies
25+
if !ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull) {
26+
ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
27+
return
28+
}
29+
30+
depID := ctx.QueryInt64("newDependency")
31+
3232
if err = issue.LoadRepo(); err != nil {
3333
ctx.ServerError("LoadRepo", err)
3434
return
@@ -73,21 +73,21 @@ func AddDependency(ctx *context.Context) {
7373

7474
// RemoveDependency removes the dependency
7575
func RemoveDependency(ctx *context.Context) {
76-
// Check if the Repo is allowed to have dependencies
77-
if !ctx.Repo.CanCreateIssueDependencies(ctx.User) {
78-
ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
79-
return
80-
}
81-
82-
depID := ctx.QueryInt64("removeDependencyID")
83-
8476
issueIndex := ctx.ParamsInt64("index")
8577
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
8678
if err != nil {
8779
ctx.ServerError("GetIssueByIndex", err)
8880
return
8981
}
9082

83+
// Check if the Repo is allowed to have dependencies
84+
if !ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull) {
85+
ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
86+
return
87+
}
88+
89+
depID := ctx.QueryInt64("removeDependencyID")
90+
9191
if err = issue.LoadRepo(); err != nil {
9292
ctx.ServerError("LoadRepo", err)
9393
return

templates/repo/issue/view_content/sidebar.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@
428428
<input type="hidden" id="repolink" value="{{$.RepoRelPath}}">
429429
<input type="hidden" id="repoId" value="{{.Repository.ID}}">
430430
<input type="hidden" id="crossRepoSearch" value="{{.AllowCrossRepositoryDependencies}}">
431+
<input type="hidden" id="type" value="{{.IssueType}}">
431432
<!-- I know, there is probably a better way to do this -->
432433
<input type="hidden" id="issueIndex" value="{{.Issue.Index}}"/>
433434

templates/swagger/v1_json.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@
13971397
"description": "repository to prioritize in the results",
13981398
"name": "priority_repo_id",
13991399
"in": "query"
1400+
},
1401+
{
1402+
"type": "string",
1403+
"description": "filter by type (issues / pulls) if set",
1404+
"name": "type",
1405+
"in": "query"
14001406
}
14011407
],
14021408
"responses": {

web_src/js/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,9 +3455,10 @@ function initIssueList() {
34553455
const repolink = $('#repolink').val();
34563456
const repoId = $('#repoId').val();
34573457
const crossRepoSearch = $('#crossRepoSearch').val();
3458-
let issueSearchUrl = `${suburl}/api/v1/repos/${repolink}/issues?q={query}`;
3458+
const tp = $('#type').val();
3459+
let issueSearchUrl = `${suburl}/api/v1/repos/${repolink}/issues?q={query}&type=${tp}`;
34593460
if (crossRepoSearch === 'true') {
3460-
issueSearchUrl = `${suburl}/api/v1/repos/issues/search?q={query}&priority_repo_id=${repoId}`;
3461+
issueSearchUrl = `${suburl}/api/v1/repos/issues/search?q={query}&priority_repo_id=${repoId}&type=${tp}`;
34613462
}
34623463
$('#new-dependency-drop-list')
34633464
.dropdown({

0 commit comments

Comments
 (0)