Skip to content

Commit

Permalink
allow project to be selected when creating an issue
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Oct 1, 2019
1 parent 5c334d7 commit c55d44e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ type CreateIssueForm struct {
AssigneeIDs string `form:"assignee_ids"`
Ref string `form:"ref"`
MilestoneID int64
ProjectID int64
AssigneeID int64
Content string
Files []string
Expand Down
1 change: 1 addition & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ function initCommentForm() {
}
switch (input_id) {
case '#milestone_id':
case '#projects_id':
$list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
htmlEncode($(this).text()) + '</a>');
break;
Expand Down
48 changes: 38 additions & 10 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.
return nil
}

retrieveProjects(ctx, repo)
if ctx.Written() {
return nil
}

brs, err := ctx.Repo.GitRepo.GetBranches()
if err != nil {
ctx.ServerError("GetBranches", err)
Expand Down Expand Up @@ -455,6 +460,17 @@ func NewIssue(ctx *context.Context) {
}
}

projectID := ctx.QueryInt64("project")
if projectID > 0 {
project, err := models.GetProjectByRepoID(ctx.Repo.Repository.ID, projectID)
if err != nil {
log.Error("GetProjectByRepoID: %d: %v", projectID, err)
} else {
ctx.Data["project_id"] = projectID
ctx.Data["Project"] = project
}
}

setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
renderAttachmentSettings(ctx)

Expand All @@ -467,15 +483,15 @@ func NewIssue(ctx *context.Context) {
}

// ValidateRepoMetas check and returns repository's meta informations
func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull bool) ([]int64, []int64, int64) {
func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull bool) ([]int64, []int64, int64, int64) {
var (
repo = ctx.Repo.Repository
err error
)

labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
if ctx.Written() {
return nil, nil, 0
return nil, nil, 0, 0
}

var labelIDs []int64
Expand All @@ -484,7 +500,7 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull b
if len(form.LabelIDs) > 0 {
labelIDs, err = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
if err != nil {
return nil, nil, 0
return nil, nil, 0, 0
}
labelIDMark := base.Int64sToMap(labelIDs)

Expand All @@ -506,35 +522,46 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull b
ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
if err != nil {
ctx.ServerError("GetMilestoneByID", err)
return nil, nil, 0
return nil, nil, 0, 0
}
ctx.Data["milestone_id"] = milestoneID
}

projectID := form.ProjectID
if projectID > 0 {
ctx.Data["Project"], err = models.GetProjectByRepoID(ctx.Repo.Repository.ID, projectID)
if err != nil {
ctx.ServerError("GetMilestoneByID", err)
return nil, nil, 0, 0
}

ctx.Data["project_id"] = projectID
}

// Check assignees
var assigneeIDs []int64
if len(form.AssigneeIDs) > 0 {
assigneeIDs, err = base.StringsToInt64s(strings.Split(form.AssigneeIDs, ","))
if err != nil {
return nil, nil, 0
return nil, nil, 0, 0
}

// Check if the passed assignees actually exists and has write access to the repo
for _, aID := range assigneeIDs {
user, err := models.GetUserByID(aID)
if err != nil {
ctx.ServerError("GetUserByID", err)
return nil, nil, 0
return nil, nil, 0, 0
}

perm, err := models.GetUserRepoPermission(repo, user)
if err != nil {
ctx.ServerError("GetUserRepoPermission", err)
return nil, nil, 0
return nil, nil, 0, 0
}
if !perm.CanWriteIssuesOrPulls(isPull) {
ctx.ServerError("CanWriteIssuesOrPulls", fmt.Errorf("No permission for %s", user.Name))
return nil, nil, 0
return nil, nil, 0, 0
}
}
}
Expand All @@ -544,7 +571,7 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm, isPull b
assigneeIDs = append(assigneeIDs, form.AssigneeID)
}

return labelIDs, assigneeIDs, milestoneID
return labelIDs, assigneeIDs, milestoneID, projectID
}

// NewIssuePost response for creating new issue
Expand All @@ -562,7 +589,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
attachments []string
)

labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, false)
labelIDs, assigneeIDs, milestoneID, projectID := ValidateRepoMetas(ctx, form, false)
if ctx.Written() {
return
}
Expand All @@ -587,6 +614,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
PosterID: ctx.User.ID,
Poster: ctx.User,
MilestoneID: milestoneID,
ProjectID: projectID,
Content: form.Content,
Ref: form.Ref,
}
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
return
}

labelIDs, assigneeIDs, milestoneID := ValidateRepoMetas(ctx, form, true)
labelIDs, assigneeIDs, milestoneID, _ := ValidateRepoMetas(ctx, form, true)
if ctx.Written() {
return
}
Expand Down
41 changes: 41 additions & 0 deletions templates/repo/issue/new_form.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,47 @@
</div>
</div>

<div class="ui divider"></div>

<input id="projects_id" name="project_id" type="hidden" value="{{.project_id}}">
<div class="ui {{if not (or .OpenProjects .ClosedProjects)}}disabled{{end}} floating jump select-project dropdown">
<span class="text">
<strong>{{.i18n.Tr "repo.issues.new.projects"}}</strong>
<span class="octicon octicon-gear"></span>
</span>
<div class="menu">
<div class="no-select item">{{.i18n.Tr "repo.issues.new.clear_projects"}}</div>
{{if .OpenMilestones}}
<div class="divider"></div>
<div class="header">
<i class="octicon octicon-milestone"></i>
{{.i18n.Tr "repo.issues.new.open_projects"}}
</div>
{{range .OpenProjects}}
<div class="item" data-id="{{.ID}}" data-href="{{$.RepoLink}}/issues?project={{.ID}}"> {{.Title}}</div>
{{end}}
{{end}}
{{if .ClosedProjects}}
<div class="divider"></div>
<div class="header">
<i class="octicon octicon-milestone"></i>
{{.i18n.Tr "repo.issues.new.closed_projects"}}
</div>
{{range .ClosedProjects}}
<a class="item" data-id="{{.ID}}" data-href="{{$.RepoLink}}/issues?project={{.ID}}"> {{.Title}}</a>
{{end}}
{{end}}
</div>
</div>
<div class="ui select-project list">
<span class="no-select item {{if .Project}}hide{{end}}">{{.i18n.Tr "repo.issues.new.no_projects"}}</span>
<div class="selected">
{{if .Project}}
<a class="item" href="{{.RepoLink}}/issues?project={{.Project.ID}}"> {{.Project.Title}}</a>
{{end}}
</div>
</div>

<div class="ui divider"></div>

<input id="assignee_ids" name="assignee_ids" type="hidden" value="{{.assignee_ids}}">
Expand Down

0 comments on commit c55d44e

Please sign in to comment.