Skip to content

Commit 8add1df

Browse files
lunnytechknowlogick
authored andcommitted
Fix issues/pulls dependencies problems (#9842) (#9864)
* Fix issues/pulls dependencies problems * fix swagger and api param name * fix js
1 parent aa6ed1b commit 8add1df

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
@@ -95,8 +95,8 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b
9595
}
9696

9797
// CanCreateIssueDependencies returns whether or not a user can create dependencies.
98-
func (r *Repository) CanCreateIssueDependencies(user *models.User) bool {
99-
return r.Permission.CanWrite(models.UnitTypeIssues) && r.Repository.IsDependenciesEnabled()
98+
func (r *Repository) CanCreateIssueDependencies(user *models.User, isPull bool) bool {
99+
return r.Repository.IsDependenciesEnabled() && r.Permission.CanWriteIssuesOrPulls(isPull)
100100
}
101101

102102
// 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"
@@ -129,6 +133,16 @@ func SearchIssues(ctx *context.APIContext) {
129133
}
130134
}
131135

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

routers/repo/compare.go

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

416416
if !nothingToCompare {
417417
// Setup information for new form.
418-
RetrieveRepoMetas(ctx, ctx.Repo.Repository)
418+
RetrieveRepoMetas(ctx, ctx.Repo.Repository, true)
419419
if ctx.Written() {
420420
return
421421
}

routers/repo/issue.go

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

350350
// RetrieveRepoMetas find all the meta information of a repository
351-
func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
351+
func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull bool) []*models.Label {
352352
if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
353353
return nil
354354
}
@@ -373,7 +373,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.
373373
ctx.Data["Branches"] = brs
374374

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

378378
return labels
379379
}
@@ -443,7 +443,7 @@ func NewIssue(ctx *context.Context) {
443443
setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
444444
renderAttachmentSettings(ctx)
445445

446-
RetrieveRepoMetas(ctx, ctx.Repo.Repository)
446+
RetrieveRepoMetas(ctx, ctx.Repo.Repository, false)
447447
if ctx.Written() {
448448
return
449449
}
@@ -458,7 +458,7 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull b
458458
err error
459459
)
460460

461-
labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
461+
labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository, isPull)
462462
if ctx.Written() {
463463
return nil, nil, 0
464464
}
@@ -670,6 +670,14 @@ func ViewIssue(ctx *context.Context) {
670670
ctx.Data["PageIsIssueList"] = true
671671
}
672672

673+
if issue.IsPull && !ctx.Repo.CanRead(models.UnitTypeIssues) {
674+
ctx.Data["IssueType"] = "pulls"
675+
} else if !issue.IsPull && !ctx.Repo.CanRead(models.UnitTypePullRequests) {
676+
ctx.Data["IssueType"] = "issues"
677+
} else {
678+
ctx.Data["IssueType"] = "all"
679+
}
680+
673681
ctx.Data["RequireHighlightJS"] = true
674682
ctx.Data["RequireDropzone"] = true
675683
ctx.Data["RequireTribute"] = true
@@ -807,7 +815,7 @@ func ViewIssue(ctx *context.Context) {
807815
}
808816

809817
// Check if the user can use the dependencies
810-
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User)
818+
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull)
811819

812820
// check if dependencies can be created across repositories
813821
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
@@ -1173,6 +1173,12 @@
11731173
"description": "repository to prioritize in the results",
11741174
"name": "priority_repo_id",
11751175
"in": "query"
1176+
},
1177+
{
1178+
"type": "string",
1179+
"description": "filter by type (issues / pulls) if set",
1180+
"name": "type",
1181+
"in": "query"
11761182
}
11771183
],
11781184
"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)