summaryrefslogtreecommitdiffstats
path: root/models/project
diff options
context:
space:
mode:
Diffstat (limited to 'models/project')
-rw-r--r--models/project/board.go84
-rw-r--r--models/project/board_test.go44
-rw-r--r--models/project/project_test.go12
3 files changed, 104 insertions, 36 deletions
diff --git a/models/project/board.go b/models/project/board.go
index c0e6529880..5f142a356c 100644
--- a/models/project/board.go
+++ b/models/project/board.go
@@ -123,6 +123,17 @@ func createBoardsForProjectsType(ctx context.Context, project *Project) error {
return nil
}
+ board := Board{
+ CreatedUnix: timeutil.TimeStampNow(),
+ CreatorID: project.CreatorID,
+ Title: "Backlog",
+ ProjectID: project.ID,
+ Default: true,
+ }
+ if err := db.Insert(ctx, board); err != nil {
+ return err
+ }
+
if len(items) == 0 {
return nil
}
@@ -176,6 +187,10 @@ func deleteBoardByID(ctx context.Context, boardID int64) error {
return err
}
+ if board.Default {
+ return fmt.Errorf("deleteBoardByID: cannot delete default board")
+ }
+
if err = board.removeIssues(ctx); err != nil {
return err
}
@@ -194,7 +209,6 @@ func deleteBoardByProjectID(ctx context.Context, projectID int64) error {
// GetBoard fetches the current board of a project
func GetBoard(ctx context.Context, boardID int64) (*Board, error) {
board := new(Board)
-
has, err := db.GetEngine(ctx).ID(boardID).Get(board)
if err != nil {
return nil, err
@@ -228,7 +242,6 @@ func UpdateBoard(ctx context.Context, board *Board) error {
}
// GetBoards fetches all boards related to a project
-// if no default board set, first board is a temporary "Uncategorized" board
func (p *Project) GetBoards(ctx context.Context) (BoardList, error) {
boards := make([]*Board, 0, 5)
@@ -244,53 +257,64 @@ func (p *Project) GetBoards(ctx context.Context) (BoardList, error) {
return append([]*Board{defaultB}, boards...), nil
}
-// getDefaultBoard return default board and create a dummy if none exist
+// getDefaultBoard return default board and ensure only one exists
func (p *Project) getDefaultBoard(ctx context.Context) (*Board, error) {
var board Board
- exist, err := db.GetEngine(ctx).Where("project_id=? AND `default`=?", p.ID, true).Get(&board)
+ has, err := db.GetEngine(ctx).
+ Where("project_id=? AND `default` = ?", p.ID, true).
+ Desc("id").Get(&board)
if err != nil {
return nil, err
}
- if exist {
+
+ if has {
return &board, nil
}
- // represents a board for issues not assigned to one
- return &Board{
+ // create a default board if none is found
+ board = Board{
ProjectID: p.ID,
- Title: "Uncategorized",
Default: true,
- }, nil
+ Title: "Uncategorized",
+ CreatorID: p.CreatorID,
+ }
+ if _, err := db.GetEngine(ctx).Insert(&board); err != nil {
+ return nil, err
+ }
+ return &board, nil
}
// SetDefaultBoard represents a board for issues not assigned to one
-// if boardID is 0 unset default
func SetDefaultBoard(ctx context.Context, projectID, boardID int64) error {
- _, err := db.GetEngine(ctx).Where(builder.Eq{
- "project_id": projectID,
- "`default`": true,
- }).Cols("`default`").Update(&Board{Default: false})
- if err != nil {
- return err
- }
+ return db.WithTx(ctx, func(ctx context.Context) error {
+ if _, err := GetBoard(ctx, boardID); err != nil {
+ return err
+ }
- if boardID > 0 {
- _, err = db.GetEngine(ctx).ID(boardID).Where(builder.Eq{"project_id": projectID}).
- Cols("`default`").Update(&Board{Default: true})
- }
+ if _, err := db.GetEngine(ctx).Where(builder.Eq{
+ "project_id": projectID,
+ "`default`": true,
+ }).Cols("`default`").Update(&Board{Default: false}); err != nil {
+ return err
+ }
- return err
+ _, err := db.GetEngine(ctx).ID(boardID).
+ Where(builder.Eq{"project_id": projectID}).
+ Cols("`default`").Update(&Board{Default: true})
+ return err
+ })
}
// UpdateBoardSorting update project board sorting
func UpdateBoardSorting(ctx context.Context, bs BoardList) error {
- for i := range bs {
- _, err := db.GetEngine(ctx).ID(bs[i].ID).Cols(
- "sorting",
- ).Update(bs[i])
- if err != nil {
- return err
+ return db.WithTx(ctx, func(ctx context.Context) error {
+ for i := range bs {
+ if _, err := db.GetEngine(ctx).ID(bs[i].ID).Cols(
+ "sorting",
+ ).Update(bs[i]); err != nil {
+ return err
+ }
}
- }
- return nil
+ return nil
+ })
}
diff --git a/models/project/board_test.go b/models/project/board_test.go
new file mode 100644
index 0000000000..71ba29a589
--- /dev/null
+++ b/models/project/board_test.go
@@ -0,0 +1,44 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package project
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestGetDefaultBoard(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ projectWithoutDefault, err := GetProjectByID(db.DefaultContext, 5)
+ assert.NoError(t, err)
+
+ // check if default board was added
+ board, err := projectWithoutDefault.getDefaultBoard(db.DefaultContext)
+ assert.NoError(t, err)
+ assert.Equal(t, int64(5), board.ProjectID)
+ assert.Equal(t, "Uncategorized", board.Title)
+
+ projectWithMultipleDefaults, err := GetProjectByID(db.DefaultContext, 6)
+ assert.NoError(t, err)
+
+ // check if multiple defaults were removed
+ board, err = projectWithMultipleDefaults.getDefaultBoard(db.DefaultContext)
+ assert.NoError(t, err)
+ assert.Equal(t, int64(6), board.ProjectID)
+ assert.Equal(t, int64(9), board.ID)
+
+ // set 8 as default board
+ assert.NoError(t, SetDefaultBoard(db.DefaultContext, board.ProjectID, 8))
+
+ // then 9 will become a non-default board
+ board, err = GetBoard(db.DefaultContext, 9)
+ assert.NoError(t, err)
+ assert.Equal(t, int64(6), board.ProjectID)
+ assert.False(t, board.Default)
+}
diff --git a/models/project/project_test.go b/models/project/project_test.go
index 7a37c1faf2..8fbbdedecf 100644
--- a/models/project/project_test.go
+++ b/models/project/project_test.go
@@ -92,19 +92,19 @@ func TestProjectsSort(t *testing.T) {
}{
{
sortType: "default",
- wants: []int64{1, 3, 2, 4},
+ wants: []int64{1, 3, 2, 6, 5, 4},
},
{
sortType: "oldest",
- wants: []int64{4, 2, 3, 1},
+ wants: []int64{4, 5, 6, 2, 3, 1},
},
{
sortType: "recentupdate",
- wants: []int64{1, 3, 2, 4},
+ wants: []int64{1, 3, 2, 6, 5, 4},
},
{
sortType: "leastupdate",
- wants: []int64{4, 2, 3, 1},
+ wants: []int64{4, 5, 6, 2, 3, 1},
},
}
@@ -113,8 +113,8 @@ func TestProjectsSort(t *testing.T) {
OrderBy: GetSearchOrderByBySortType(tt.sortType),
})
assert.NoError(t, err)
- assert.EqualValues(t, int64(4), count)
- if assert.Len(t, projects, 4) {
+ assert.EqualValues(t, int64(6), count)
+ if assert.Len(t, projects, 6) {
for i := range projects {
assert.EqualValues(t, tt.wants[i], projects[i].ID)
}