Skip to content

Commit

Permalink
make it possible to add issues to project from projects
Browse files Browse the repository at this point in the history
  • Loading branch information
patcito committed Nov 10, 2020
1 parent 68702e8 commit 2fd630c
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 109 deletions.
8 changes: 7 additions & 1 deletion models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ type IssuesOptions struct {
ExcludedLabelNames []string
SortType string
IssueIDs []int64
NotInProjectID int64
// prioritize issues from this repo
PriorityRepoID int64
}
Expand Down Expand Up @@ -1182,7 +1183,6 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
sess.Join("INNER", "project_issue", "issue.id = project_issue.issue_id").
And("project_issue.project_id=?", opts.ProjectID).OrderBy("priority")
}

if opts.ProjectBoardID != 0 {
if opts.ProjectBoardID > 0 {
sess.In("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_board_id": opts.ProjectBoardID}).OrderBy("priority"))
Expand All @@ -1191,6 +1191,12 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
}
}

if opts.NotInProjectID != 0 {
if opts.NotInProjectID > 0 {
sess.NotIn("issue.id", builder.Select("issue_id").From("project_issue").Where(builder.Eq{"project_id": opts.NotInProjectID}).OrderBy("priority"))
}
}

switch opts.IsPull {
case util.OptionalBoolTrue:
sess.And("issue.is_pull=?", true)
Expand Down
35 changes: 30 additions & 5 deletions models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Project struct {
IsClosed bool `xorm:"INDEX"`
BoardType ProjectBoardType
Type ProjectType
Repo *Repository `xorm:"-"`

RenderedContent string `xorm:"-"`

Expand Down Expand Up @@ -320,13 +321,37 @@ func UpdateBoards(boards []ProjectBoard) error {
}

// Update given issue priority and column
func UpdateBoardIssues(issues []ProjectIssue) error {
for _, issue := range issues {
if _, err := x.ID(issue.ID).Cols("priority", "project_board_id").Update(&issue); err != nil {
log.Info("failed updating cards priorities %s", err)
return err
func UpdateBoardIssues(issues []ProjectIssue) (error, []ProjectIssue) {
var updatedIssues []ProjectIssue
for _, pissue := range issues {
if pissue.ID != 0 {
if _, err := x.ID(pissue.ID).Cols("priority", "project_board_id").Update(&pissue); err != nil {
log.Info("failed updating cards priorities %s", err)
return err, updatedIssues
} else {
updatedIssues = append(updatedIssues, pissue)
}
} else {
if _, err := x.Insert(&pissue); err != nil {
log.Info("failed inserting cards priorities %s", err)
return err, updatedIssues
} else {
updatedIssues = append(updatedIssues, pissue)
}
}
}
return nil, updatedIssues
}

func (p *Project) LoadRepository() error {
var repo = Repository{}
if p.Type == ProjectTypeRepository {
_, err := x.ID(p.RepoID).Get(&repo)
p.Repo = &repo
if err != nil {
log.Info("failed getting repo %w", err)
}
return err
}
return nil
}
24 changes: 17 additions & 7 deletions models/project_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package models
import (
"fmt"

"code.gitea.io/gitea/modules/log"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -143,10 +144,9 @@ func ChangeProjectAssign(issue *Issue, doer *User, newProjectID int64) error {
}

func addUpdateIssueProject(e *xorm.Session, issue *Issue, doer *User, newProjectID int64) error {

oldProjectID := issue.projectID(e)

if _, err := e.Where("project_issue.issue_id=?", issue.ID).Delete(&ProjectIssue{}); err != nil {
log.Info("failed deleting project issue %w", err)
return err
}

Expand All @@ -167,11 +167,21 @@ func addUpdateIssueProject(e *xorm.Session, issue *Issue, doer *User, newProject
}
}

_, err := e.Insert(&ProjectIssue{
IssueID: issue.ID,
ProjectID: newProjectID,
})
return err
var projectIssues []ProjectIssue
if newProjectID != 0 {
err := e.Where("issue_id = ? and project_id = ?", issue.ID, newProjectID).Find(&projectIssues)
if err != nil {
return err
}
if len(projectIssues) == 0 {
_, err := e.Insert(&ProjectIssue{
IssueID: issue.ID,
ProjectID: newProjectID,
})
return err
}
}
return nil
}

// ____ _ _ ____ _
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ new_fork = New Repository Fork
new_org = New Organization
new_project = New Project
new_project_board = New Project board
add_project_issue = Add Issue
manage_org = Manage Organizations
admin_panel = Site Administration
account_settings = Account Settings
Expand Down
21 changes: 14 additions & 7 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func SearchIssues(ctx *context.APIContext) {
// description: repository to prioritize in the results
// type: integer
// format: int64
// - name: not_in_project_id
// in: query
// description: project to exclude from search when searching issues to add to a project
// type: integer
// format: int64
// - name: type
// in: query
// description: filter by type (issues / pulls) if set
Expand Down Expand Up @@ -158,6 +163,7 @@ func SearchIssues(ctx *context.APIContext) {
SortType: "priorityrepo",
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
IsPull: isPull,
NotInProjectID: ctx.QueryInt64("not_in_project_id"),
}

if issues, err = models.Issues(issuesOpt); err != nil {
Expand Down Expand Up @@ -314,13 +320,14 @@ func ListIssues(ctx *context.APIContext) {
// This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
issuesOpt := &models.IssuesOptions{
ListOptions: listOptions,
RepoIDs: []int64{ctx.Repo.Repository.ID},
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
MilestoneIDs: mileIDs,
IsPull: isPull,
ListOptions: listOptions,
RepoIDs: []int64{ctx.Repo.Repository.ID},
IsClosed: isClosed,
IssueIDs: issueIDs,
LabelIDs: labelIDs,
MilestoneIDs: mileIDs,
IsPull: isPull,
NotInProjectID: ctx.QueryInt64("not_in_project_id"),
}

if issues, err = models.Issues(issuesOpt); err != nil {
Expand Down
13 changes: 10 additions & 3 deletions routers/repo/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -265,6 +266,7 @@ func ViewProject(ctx *context.Context) {
}
return
}

if project.RepoID != ctx.Repo.Repository.ID {
ctx.NotFound("", nil)
return
Expand Down Expand Up @@ -298,7 +300,10 @@ func ViewProject(ctx *context.Context) {
ctx.Data["Boards"] = allBoards
ctx.Data["PageIsProjects"] = true
ctx.Data["RequiresDraggable"] = true

project.LoadRepository()
if project.Repo != nil && project.Repo.ID != 0 {
ctx.Data["Repo"] = project.Repo
}
ctx.HTML(200, tplProjectsView)
}

Expand Down Expand Up @@ -624,8 +629,10 @@ func UpdateBoardIssuePriority(ctx *context.Context, form auth.UpdateIssuePriorit
return
}

issues := form
models.UpdateBoardIssues(form.Issues)
err, issues := models.UpdateBoardIssues(form.Issues)
if err != nil {
log.Info("failed updating issues %v", err)
}

ctx.JSON(200, issues)
}
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content/sidebar.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="four wide column">
<div class="ui segment metas">
{{if eq .Sidebar true}}
<h3>#{{.Issue.ID}} {{.Issue.Title}}</h3>
<h3>#{{.Issue.Index}} {{.Issue.Title}}</h3>
{{end}}
{{template "repo/issue/branch_selector_field" .}}

Expand Down
17 changes: 12 additions & 5 deletions templates/repo/projects/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
</div>
<div class="column right aligned">
{{if and .CanWriteProjects (not .Repository.IsArchived) .PageIsProjects}}
<a class="ui green button show-modal item" data-modal="#new-board-item">{{.i18n.Tr "new_project_board"}}</a>
<button class="ui green button show-modal item"
data-modal="#new-board-item">
<i class="add icon"></i>
{{.i18n.Tr "new_project_board"}}</button>
<button class="ui green button show-modal item"
id="new-project-issue-item">
<i class="add icon"></i>
{{.i18n.Tr "add_project_issue"}}</button>
{{end}}
<div class="ui small modal" id="new-board-item">
<div class="header">
Expand Down Expand Up @@ -68,9 +75,9 @@
<div class="ui divider"></div>
</div>
<div class="ui container fluid padded" id="project-board">

<div class="board" id="board-container" data-url="{{.RepoLink}}/projects/{{$.Project.ID}}">
<div class="ui segment ignore-elements hide" id="current-card-details"></div>
<div id="current-card-details-input"></div>
<div class="board" id="board-container" data-repourl="{{.RepoLink}}" data-projectid="{{.Project.ID}}" data-url="{{.RepoLink}}/projects/{{$.Project.ID}}">
<div class="ui segment ignore-elements" id="current-card-details"></div>
{{ range $board := .Boards }}

<div class="ui segment board-column {{if eq .ID 0}}ignore-elements{{end}}"
Expand Down Expand Up @@ -156,7 +163,7 @@
{{else}}{{svg "octicon-issue-opened"}}
{{end}}
</span>
<a class="project-board-title" href="{{$.RepoLink}}/{{if .Issue.IsPull}}pulls{{else}}issues{{end}}/{{.IssueID}}" data-url="{{$.RepoLink}}/{{if .Issue.IsPull}}pulls{{else}}issues{{end}}/{{.IssueID}}/sidebar/true">#{{.IssueID}} {{.Issue.Title}}</a>
<a class="project-board-title" href="{{$.RepoLink}}/{{if .Issue.IsPull}}pulls{{else}}issues{{end}}/{{.Issue.Index}}" data-url="{{$.RepoLink}}/{{if .Issue.IsPull}}pulls{{else}}issues{{end}}/{{.Issue.Index}}/sidebar/true">#{{.Issue.Index}} {{.Issue.Title}}</a>
</div>
<div class="meta">
{{ if .Issue.MilestoneID }}
Expand Down
Loading

0 comments on commit 2fd630c

Please sign in to comment.