2020-08-17 06:07:38 +03:00
// Copyright 2020 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.
2022-03-29 17:16:31 +03:00
package project
2020-08-17 06:07:38 +03:00
import (
2022-03-29 17:16:31 +03:00
"context"
2020-08-17 06:07:38 +03:00
"errors"
"fmt"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2020-08-17 06:07:38 +03:00
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
)
type (
// ProjectsConfig is used to identify the type of board that is being created
ProjectsConfig struct {
2022-03-29 17:16:31 +03:00
BoardType BoardType
2020-08-17 06:07:38 +03:00
Translation string
}
2022-03-29 17:16:31 +03:00
// Type is used to identify the type of project in question and ownership
Type uint8
2020-08-17 06:07:38 +03:00
)
const (
2022-03-29 17:16:31 +03:00
// TypeIndividual is a type of project board that is owned by an individual
TypeIndividual Type = iota + 1
2020-08-17 06:07:38 +03:00
2022-03-29 17:16:31 +03:00
// TypeRepository is a project that is tied to a repository
TypeRepository
2020-08-17 06:07:38 +03:00
2022-03-29 17:16:31 +03:00
// TypeOrganization is a project that is tied to an organisation
TypeOrganization
2020-08-17 06:07:38 +03:00
)
2022-03-29 17:16:31 +03:00
// 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 )
}
2022-10-18 08:50:37 +03:00
func ( err ErrProjectNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2022-03-29 17:16:31 +03:00
// 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 )
}
2022-10-18 08:50:37 +03:00
func ( err ErrProjectBoardNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2020-08-17 06:07:38 +03:00
// Project represents a project board
type Project struct {
ID int64 ` xorm:"pk autoincr" `
Title string ` xorm:"INDEX NOT NULL" `
Description string ` xorm:"TEXT" `
RepoID int64 ` xorm:"INDEX" `
CreatorID int64 ` xorm:"NOT NULL" `
IsClosed bool ` xorm:"INDEX" `
2022-03-29 17:16:31 +03:00
BoardType BoardType
Type Type
2020-08-17 06:07:38 +03:00
RenderedContent string ` xorm:"-" `
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
ClosedDateUnix timeutil . TimeStamp
}
2021-09-19 14:49:59 +03:00
func init ( ) {
db . RegisterModel ( new ( Project ) )
}
2020-08-17 06:07:38 +03:00
// GetProjectsConfig retrieves the types of configurations projects could have
func GetProjectsConfig ( ) [ ] ProjectsConfig {
return [ ] ProjectsConfig {
2022-03-29 17:16:31 +03:00
{ BoardTypeNone , "repo.projects.type.none" } ,
{ BoardTypeBasicKanban , "repo.projects.type.basic_kanban" } ,
{ BoardTypeBugTriage , "repo.projects.type.bug_triage" } ,
2020-08-17 06:07:38 +03:00
}
}
2022-03-29 17:16:31 +03:00
// IsTypeValid checks if a project type is valid
func IsTypeValid ( p Type ) bool {
2020-08-17 06:07:38 +03:00
switch p {
2022-03-29 17:16:31 +03:00
case TypeRepository :
2020-08-17 06:07:38 +03:00
return true
default :
return false
}
}
2022-03-29 17:16:31 +03:00
// SearchOptions are options for GetProjects
type SearchOptions struct {
2020-08-17 06:07:38 +03:00
RepoID int64
Page int
IsClosed util . OptionalBool
SortType string
2022-03-29 17:16:31 +03:00
Type Type
2020-08-17 06:07:38 +03:00
}
// GetProjects returns a list of all projects that have been created in the repository
2022-05-20 17:08:52 +03:00
func GetProjects ( ctx context . Context , opts SearchOptions ) ( [ ] * Project , int64 , error ) {
2022-03-29 17:16:31 +03:00
e := db . GetEngine ( ctx )
2020-08-17 06:07:38 +03:00
projects := make ( [ ] * Project , 0 , setting . UI . IssuePagingNum )
var cond builder . Cond = builder . Eq { "repo_id" : opts . RepoID }
switch opts . IsClosed {
case util . OptionalBoolTrue :
cond = cond . And ( builder . Eq { "is_closed" : true } )
case util . OptionalBoolFalse :
cond = cond . And ( builder . Eq { "is_closed" : false } )
}
if opts . Type > 0 {
cond = cond . And ( builder . Eq { "type" : opts . Type } )
}
count , err := e . Where ( cond ) . Count ( new ( Project ) )
if err != nil {
return nil , 0 , fmt . Errorf ( "Count: %v" , err )
}
e = e . Where ( cond )
if opts . Page > 0 {
e = e . Limit ( setting . UI . IssuePagingNum , ( opts . Page - 1 ) * setting . UI . IssuePagingNum )
}
switch opts . SortType {
case "oldest" :
e . Desc ( "created_unix" )
case "recentupdate" :
e . Desc ( "updated_unix" )
case "leastupdate" :
e . Asc ( "updated_unix" )
default :
e . Asc ( "created_unix" )
}
return projects , count , e . Find ( & projects )
}
// NewProject creates a new Project
func NewProject ( p * Project ) error {
2022-03-29 17:16:31 +03:00
if ! IsBoardTypeValid ( p . BoardType ) {
p . BoardType = BoardTypeNone
2020-08-17 06:07:38 +03:00
}
2022-03-29 17:16:31 +03:00
if ! IsTypeValid ( p . Type ) {
2020-08-17 06:07:38 +03:00
return errors . New ( "project type is not valid" )
}
2021-11-21 18:41:00 +03:00
ctx , committer , err := db . TxContext ( )
if err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2020-08-17 06:07:38 +03:00
2021-11-21 18:41:00 +03:00
if err := db . Insert ( ctx , p ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
if _ , err := db . Exec ( ctx , "UPDATE `repository` SET num_projects = num_projects + 1 WHERE id = ?" , p . RepoID ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2022-03-29 17:16:31 +03:00
if err := createBoardsForProjectsType ( ctx , p ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2020-08-17 06:07:38 +03:00
}
// GetProjectByID returns the projects in a repository
2022-05-20 17:08:52 +03:00
func GetProjectByID ( ctx context . Context , id int64 ) ( * Project , error ) {
2020-08-17 06:07:38 +03:00
p := new ( Project )
2022-05-20 17:08:52 +03:00
has , err := db . GetEngine ( ctx ) . ID ( id ) . Get ( p )
2020-08-17 06:07:38 +03:00
if err != nil {
return nil , err
} else if ! has {
return nil , ErrProjectNotExist { ID : id }
}
return p , nil
}
// UpdateProject updates project properties
2022-05-20 17:08:52 +03:00
func UpdateProject ( ctx context . Context , p * Project ) error {
_ , err := db . GetEngine ( ctx ) . ID ( p . ID ) . Cols (
2020-08-17 06:07:38 +03:00
"title" ,
"description" ,
) . Update ( p )
return err
}
2022-05-20 17:08:52 +03:00
func updateRepositoryProjectCount ( ctx context . Context , repoID int64 ) error {
if _ , err := db . GetEngine ( ctx ) . Exec ( builder . Update (
2020-08-17 06:07:38 +03:00
builder . Eq {
"`num_projects`" : builder . Select ( "count(*)" ) . From ( "`project`" ) .
Where ( builder . Eq { "`project`.`repo_id`" : repoID } .
2022-03-29 17:16:31 +03:00
And ( builder . Eq { "`project`.`type`" : TypeRepository } ) ) ,
2020-08-17 06:07:38 +03:00
} ) . From ( "`repository`" ) . Where ( builder . Eq { "id" : repoID } ) ) ; err != nil {
return err
}
2022-05-20 17:08:52 +03:00
if _ , err := db . GetEngine ( ctx ) . Exec ( builder . Update (
2020-08-17 06:07:38 +03:00
builder . Eq {
"`num_closed_projects`" : builder . Select ( "count(*)" ) . From ( "`project`" ) .
Where ( builder . Eq { "`project`.`repo_id`" : repoID } .
2022-03-29 17:16:31 +03:00
And ( builder . Eq { "`project`.`type`" : TypeRepository } ) .
2020-08-17 06:07:38 +03:00
And ( builder . Eq { "`project`.`is_closed`" : true } ) ) ,
} ) . From ( "`repository`" ) . Where ( builder . Eq { "id" : repoID } ) ) ; err != nil {
return err
}
return nil
}
// ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed
func ChangeProjectStatusByRepoIDAndID ( repoID , projectID int64 , isClosed bool ) error {
2021-11-21 18:41:00 +03:00
ctx , committer , err := db . TxContext ( )
if err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2020-08-17 06:07:38 +03:00
p := new ( Project )
2022-03-29 17:16:31 +03:00
has , err := db . GetEngine ( ctx ) . ID ( projectID ) . Where ( "repo_id = ?" , repoID ) . Get ( p )
2020-08-17 06:07:38 +03:00
if err != nil {
return err
} else if ! has {
return ErrProjectNotExist { ID : projectID , RepoID : repoID }
}
2022-03-29 17:16:31 +03:00
if err := changeProjectStatus ( ctx , p , isClosed ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2020-08-17 06:07:38 +03:00
}
// ChangeProjectStatus toggle a project between opened and closed
func ChangeProjectStatus ( p * Project , isClosed bool ) error {
2021-11-21 18:41:00 +03:00
ctx , committer , err := db . TxContext ( )
if err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2020-08-17 06:07:38 +03:00
2022-03-29 17:16:31 +03:00
if err := changeProjectStatus ( ctx , p , isClosed ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2020-08-17 06:07:38 +03:00
}
2022-03-29 17:16:31 +03:00
func changeProjectStatus ( ctx context . Context , p * Project , isClosed bool ) error {
2020-08-17 06:07:38 +03:00
p . IsClosed = isClosed
p . ClosedDateUnix = timeutil . TimeStampNow ( )
2022-05-20 17:08:52 +03:00
count , err := db . GetEngine ( ctx ) . ID ( p . ID ) . Where ( "repo_id = ? AND is_closed = ?" , p . RepoID , ! isClosed ) . Cols ( "is_closed" , "closed_date_unix" ) . Update ( p )
2020-08-17 06:07:38 +03:00
if err != nil {
return err
}
if count < 1 {
return nil
}
2022-05-20 17:08:52 +03:00
return updateRepositoryProjectCount ( ctx , p . RepoID )
2020-08-17 06:07:38 +03:00
}
// DeleteProjectByID deletes a project from a repository.
func DeleteProjectByID ( id int64 ) error {
2021-11-21 18:41:00 +03:00
ctx , committer , err := db . TxContext ( )
if err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2020-08-17 06:07:38 +03:00
2022-03-29 17:16:31 +03:00
if err := DeleteProjectByIDCtx ( ctx , id ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2020-08-17 06:07:38 +03:00
}
2022-03-29 17:16:31 +03:00
// DeleteProjectByIDCtx deletes a project from a repository.
func DeleteProjectByIDCtx ( ctx context . Context , id int64 ) error {
2022-05-20 17:08:52 +03:00
p , err := GetProjectByID ( ctx , id )
2020-08-17 06:07:38 +03:00
if err != nil {
if IsErrProjectNotExist ( err ) {
return nil
}
return err
}
2022-05-20 17:08:52 +03:00
if err := deleteProjectIssuesByProjectID ( ctx , id ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2022-05-20 17:08:52 +03:00
if err := deleteBoardByProjectID ( ctx , id ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2022-05-20 17:08:52 +03:00
if _ , err = db . GetEngine ( ctx ) . ID ( p . ID ) . Delete ( new ( Project ) ) ; err != nil {
2020-08-17 06:07:38 +03:00
return err
}
2022-05-20 17:08:52 +03:00
return updateRepositoryProjectCount ( ctx , p . RepoID )
2020-08-17 06:07:38 +03:00
}
2022-07-14 10:22:09 +03:00
func DeleteProjectByRepoIDCtx ( ctx context . Context , repoID int64 ) error {
switch {
case setting . Database . UseSQLite3 :
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE FROM project_issue WHERE project_issue.id IN (SELECT project_issue.id FROM project_issue INNER JOIN project WHERE project.id = project_issue.project_id AND project.repo_id = ?)" , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE FROM project_board WHERE project_board.id IN (SELECT project_board.id FROM project_board INNER JOIN project WHERE project.id = project_board.project_id AND project.repo_id = ?)" , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Table ( "project" ) . Where ( "repo_id = ? " , repoID ) . Delete ( & Project { } ) ; err != nil {
return err
}
case setting . Database . UsePostgreSQL :
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE FROM project_issue USING project WHERE project.id = project_issue.project_id AND project.repo_id = ? " , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE FROM project_board USING project WHERE project.id = project_board.project_id AND project.repo_id = ? " , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Table ( "project" ) . Where ( "repo_id = ? " , repoID ) . Delete ( & Project { } ) ; err != nil {
return err
}
default :
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE project_issue FROM project_issue INNER JOIN project ON project.id = project_issue.project_id WHERE project.repo_id = ? " , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Exec ( "DELETE project_board FROM project_board INNER JOIN project ON project.id = project_board.project_id WHERE project.repo_id = ? " , repoID ) ; err != nil {
return err
}
if _ , err := db . GetEngine ( ctx ) . Table ( "project" ) . Where ( "repo_id = ? " , repoID ) . Delete ( & Project { } ) ; err != nil {
return err
}
}
return updateRepositoryProjectCount ( ctx , repoID )
}