Skip to content

Commit

Permalink
Move project files into models/project sub package (#17704)
Browse files Browse the repository at this point in the history
* Move project files into models/project sub package

* Fix test

* Fix test

* Fix test

* Fix build

* Fix test

* Fix template bug

* Fix bug

* Fix lint

* Fix test

* Fix import

* Improve codes

Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
lunny and 6543 authored Mar 29, 2022
1 parent ea6efba commit bd97736
Show file tree
Hide file tree
Showing 18 changed files with 810 additions and 713 deletions.
38 changes: 0 additions & 38 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,44 +1063,6 @@ func (err ErrLabelNotExist) Error() string {
return fmt.Sprintf("label does not exist [label_id: %d]", err.LabelID)
}

// __________ __ __
// \______ \_______ ____ |__| ____ _____/ |_ ______
// | ___/\_ __ \/ _ \ | |/ __ \_/ ___\ __\/ ___/
// | | | | \( <_> ) | \ ___/\ \___| | \___ \
// |____| |__| \____/\__| |\___ >\___ >__| /____ >
// \______| \/ \/ \/

// ErrProjectNotExist represents a "ProjectNotExist" kind of error.
type ErrProjectNotExist struct {
ID int64
RepoID int64
}

// IsErrProjectNotExist checks if an error is a ErrProjectNotExist
func IsErrProjectNotExist(err error) bool {
_, ok := err.(ErrProjectNotExist)
return ok
}

func (err ErrProjectNotExist) Error() string {
return fmt.Sprintf("projects does not exist [id: %d]", err.ID)
}

// ErrProjectBoardNotExist represents a "ProjectBoardNotExist" kind of error.
type ErrProjectBoardNotExist struct {
BoardID int64
}

// IsErrProjectBoardNotExist checks if an error is a ErrProjectBoardNotExist
func IsErrProjectBoardNotExist(err error) bool {
_, ok := err.(ErrProjectBoardNotExist)
return ok
}

func (err ErrProjectBoardNotExist) Error() string {
return fmt.Sprintf("project board does not exist [id: %d]", err.BoardID)
}

// _____ .__.__ __
// / \ |__| | ____ _______/ |_ ____ ____ ____
// / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
Expand Down
21 changes: 11 additions & 10 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
project_model "code.gitea.io/gitea/models/project"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -45,14 +46,14 @@ type Issue struct {
PosterID int64 `xorm:"INDEX"`
Poster *user_model.User `xorm:"-"`
OriginalAuthor string
OriginalAuthorID int64 `xorm:"index"`
Title string `xorm:"name"`
Content string `xorm:"LONGTEXT"`
RenderedContent string `xorm:"-"`
Labels []*Label `xorm:"-"`
MilestoneID int64 `xorm:"INDEX"`
Milestone *Milestone `xorm:"-"`
Project *Project `xorm:"-"`
OriginalAuthorID int64 `xorm:"index"`
Title string `xorm:"name"`
Content string `xorm:"LONGTEXT"`
RenderedContent string `xorm:"-"`
Labels []*Label `xorm:"-"`
MilestoneID int64 `xorm:"INDEX"`
Milestone *Milestone `xorm:"-"`
Project *project_model.Project `xorm:"-"`
Priority int
AssigneeID int64 `xorm:"-"`
Assignee *user_model.User `xorm:"-"`
Expand Down Expand Up @@ -2135,7 +2136,7 @@ func deleteIssue(ctx context.Context, issue *Issue) error {
&IssueWatch{},
&Stopwatch{},
&TrackedTime{},
&ProjectIssue{},
&project_model.ProjectIssue{},
&repo_model.Attachment{},
&PullRequest{},
); err != nil {
Expand Down Expand Up @@ -2469,7 +2470,7 @@ func deleteIssuesByRepoID(sess db.Engine, repoID int64) (attachmentPaths []strin
}

if _, err = sess.In("issue_id", deleteCond).
Delete(&ProjectIssue{}); err != nil {
Delete(&project_model.ProjectIssue{}); err != nil {
return
}

Expand Down
9 changes: 5 additions & 4 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
project_model "code.gitea.io/gitea/models/project"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
Expand Down Expand Up @@ -204,8 +205,8 @@ type Comment struct {
RemovedLabels []*Label `xorm:"-"`
OldProjectID int64
ProjectID int64
OldProject *Project `xorm:"-"`
Project *Project `xorm:"-"`
OldProject *project_model.Project `xorm:"-"`
Project *project_model.Project `xorm:"-"`
OldMilestoneID int64
MilestoneID int64
OldMilestone *Milestone `xorm:"-"`
Expand Down Expand Up @@ -469,7 +470,7 @@ func (c *Comment) LoadLabel() error {
// LoadProject if comment.Type is CommentTypeProject, then load project.
func (c *Comment) LoadProject() error {
if c.OldProjectID > 0 {
var oldProject Project
var oldProject project_model.Project
has, err := db.GetEngine(db.DefaultContext).ID(c.OldProjectID).Get(&oldProject)
if err != nil {
return err
Expand All @@ -479,7 +480,7 @@ func (c *Comment) LoadProject() error {
}

if c.ProjectID > 0 {
var project Project
var project project_model.Project
has, err := db.GetEngine(db.DefaultContext).ID(c.ProjectID).Get(&project)
if err != nil {
return err
Expand Down
181 changes: 181 additions & 0 deletions models/issue_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"context"
"fmt"

"code.gitea.io/gitea/models/db"
project_model "code.gitea.io/gitea/models/project"
user_model "code.gitea.io/gitea/models/user"
)

// LoadProject load the project the issue was assigned to
func (i *Issue) LoadProject() (err error) {
return i.loadProject(db.GetEngine(db.DefaultContext))
}

func (i *Issue) loadProject(e db.Engine) (err error) {
if i.Project == nil {
var p project_model.Project
if _, err = e.Table("project").
Join("INNER", "project_issue", "project.id=project_issue.project_id").
Where("project_issue.issue_id = ?", i.ID).
Get(&p); err != nil {
return err
}
i.Project = &p
}
return
}

// ProjectID return project id if issue was assigned to one
func (i *Issue) ProjectID() int64 {
return i.projectID(db.GetEngine(db.DefaultContext))
}

func (i *Issue) projectID(e db.Engine) int64 {
var ip project_model.ProjectIssue
has, err := e.Where("issue_id=?", i.ID).Get(&ip)
if err != nil || !has {
return 0
}
return ip.ProjectID
}

// ProjectBoardID return project board id if issue was assigned to one
func (i *Issue) ProjectBoardID() int64 {
return i.projectBoardID(db.GetEngine(db.DefaultContext))
}

func (i *Issue) projectBoardID(e db.Engine) int64 {
var ip project_model.ProjectIssue
has, err := e.Where("issue_id=?", i.ID).Get(&ip)
if err != nil || !has {
return 0
}
return ip.ProjectBoardID
}

// LoadIssuesFromBoard load issues assigned to this board
func LoadIssuesFromBoard(b *project_model.Board) (IssueList, error) {
issueList := make([]*Issue, 0, 10)

if b.ID != 0 {
issues, err := Issues(&IssuesOptions{
ProjectBoardID: b.ID,
ProjectID: b.ProjectID,
})
if err != nil {
return nil, err
}
issueList = issues
}

if b.Default {
issues, err := Issues(&IssuesOptions{
ProjectBoardID: -1, // Issues without ProjectBoardID
ProjectID: b.ProjectID,
})
if err != nil {
return nil, err
}
issueList = append(issueList, issues...)
}

if err := IssueList(issueList).LoadComments(); err != nil {
return nil, err
}

return issueList, nil
}

// LoadIssuesFromBoardList load issues assigned to the boards
func LoadIssuesFromBoardList(bs project_model.BoardList) (map[int64]IssueList, error) {
issuesMap := make(map[int64]IssueList, len(bs))
for i := range bs {
il, err := LoadIssuesFromBoard(bs[i])
if err != nil {
return nil, err
}
issuesMap[bs[i].ID] = il
}
return issuesMap, nil
}

// ChangeProjectAssign changes the project associated with an issue
func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error {
ctx, committer, err := db.TxContext()
if err != nil {
return err
}
defer committer.Close()

if err := addUpdateIssueProject(ctx, issue, doer, newProjectID); err != nil {
return err
}

return committer.Commit()
}

func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.User, newProjectID int64) error {
e := db.GetEngine(ctx)
oldProjectID := issue.projectID(e)

if _, err := e.Where("project_issue.issue_id=?", issue.ID).Delete(&project_model.ProjectIssue{}); err != nil {
return err
}

if err := issue.loadRepo(ctx); err != nil {
return err
}

if oldProjectID > 0 || newProjectID > 0 {
if _, err := createComment(ctx, &CreateCommentOptions{
Type: CommentTypeProject,
Doer: doer,
Repo: issue.Repo,
Issue: issue,
OldProjectID: oldProjectID,
ProjectID: newProjectID,
}); err != nil {
return err
}
}

_, err := e.Insert(&project_model.ProjectIssue{
IssueID: issue.ID,
ProjectID: newProjectID,
})
return err
}

// MoveIssueAcrossProjectBoards move a card from one board to another
func MoveIssueAcrossProjectBoards(issue *Issue, board *project_model.Board) error {
ctx, committer, err := db.TxContext()
if err != nil {
return err
}
defer committer.Close()
sess := db.GetEngine(ctx)

var pis project_model.ProjectIssue
has, err := sess.Where("issue_id=?", issue.ID).Get(&pis)
if err != nil {
return err
}

if !has {
return fmt.Errorf("issue has to be added to a project first")
}

pis.ProjectBoardID = board.ID
if _, err := sess.ID(pis.ID).Cols("project_board_id").Update(&pis); err != nil {
return err
}

return committer.Commit()
}
Loading

0 comments on commit bd97736

Please sign in to comment.