diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-03-29 22:16:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 22:16:31 +0800 |
commit | bd97736b9c7a16023bc9abf17be6157284f655b1 (patch) | |
tree | eea5870d406bd00fa2e675901f24d9f1fbf1796f /routers/web/repo/projects.go | |
parent | ea6efba9b3fd298c842a935266060e405e2ae4a3 (diff) | |
download | gitea-bd97736b9c7a16023bc9abf17be6157284f655b1.tar.gz gitea-bd97736b9c7a16023bc9abf17be6157284f655b1.zip |
Move project files into models/project sub package (#17704)
* 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>
Diffstat (limited to 'routers/web/repo/projects.go')
-rw-r--r-- | routers/web/repo/projects.go | 146 |
1 files changed, 92 insertions, 54 deletions
diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index 9df2520a52..a6f843d848 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/perm" + project_model "code.gitea.io/gitea/models/project" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -69,12 +70,12 @@ func Projects(ctx *context.Context) { total = repo.NumClosedProjects } - projects, count, err := models.GetProjects(models.ProjectSearchOptions{ + projects, count, err := project_model.GetProjects(project_model.SearchOptions{ RepoID: repo.ID, Page: page, IsClosed: util.OptionalBoolOf(isShowClosed), SortType: sortType, - Type: models.ProjectTypeRepository, + Type: project_model.TypeRepository, }) if err != nil { ctx.ServerError("GetProjects", err) @@ -122,7 +123,7 @@ func Projects(ctx *context.Context) { // NewProject render creating a project page func NewProject(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.projects.new") - ctx.Data["ProjectTypes"] = models.GetProjectsConfig() + ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig() ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) ctx.HTML(http.StatusOK, tplProjectsNew) } @@ -134,18 +135,18 @@ func NewProjectPost(ctx *context.Context) { if ctx.HasError() { ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) - ctx.Data["ProjectTypes"] = models.GetProjectsConfig() + ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig() ctx.HTML(http.StatusOK, tplProjectsNew) return } - if err := models.NewProject(&models.Project{ + if err := project_model.NewProject(&project_model.Project{ RepoID: ctx.Repo.Repository.ID, Title: form.Title, Description: form.Content, CreatorID: ctx.Doer.ID, BoardType: form.BoardType, - Type: models.ProjectTypeRepository, + Type: project_model.TypeRepository, }); err != nil { ctx.ServerError("NewProject", err) return @@ -168,8 +169,8 @@ func ChangeProjectStatus(ctx *context.Context) { } id := ctx.ParamsInt64(":id") - if err := models.ChangeProjectStatusByRepoIDAndID(ctx.Repo.Repository.ID, id, toClose); err != nil { - if models.IsErrProjectNotExist(err) { + if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx.Repo.Repository.ID, id, toClose); err != nil { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", err) } else { ctx.ServerError("ChangeProjectStatusByIDAndRepoID", err) @@ -181,9 +182,9 @@ func ChangeProjectStatus(ctx *context.Context) { // DeleteProject delete a project func DeleteProject(ctx *context.Context) { - p, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + p, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -195,7 +196,7 @@ func DeleteProject(ctx *context.Context) { return } - if err := models.DeleteProjectByID(p.ID); err != nil { + if err := project_model.DeleteProjectByID(p.ID); err != nil { ctx.Flash.Error("DeleteProjectByID: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success")) @@ -212,9 +213,9 @@ func EditProject(ctx *context.Context) { ctx.Data["PageIsEditProjects"] = true ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) - p, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + p, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -244,9 +245,9 @@ func EditProjectPost(ctx *context.Context) { return } - p, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + p, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -260,7 +261,7 @@ func EditProjectPost(ctx *context.Context) { p.Title = form.Title p.Description = form.Content - if err = models.UpdateProject(p); err != nil { + if err = project_model.UpdateProject(p); err != nil { ctx.ServerError("UpdateProjects", err) return } @@ -271,9 +272,9 @@ func EditProjectPost(ctx *context.Context) { // ViewProject renders the project board for a project func ViewProject(ctx *context.Context) { - project, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + project, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -285,7 +286,7 @@ func ViewProject(ctx *context.Context) { return } - boards, err := models.GetProjectBoards(project.ID) + boards, err := project_model.GetBoards(project.ID) if err != nil { ctx.ServerError("GetProjectBoards", err) return @@ -295,27 +296,29 @@ func ViewProject(ctx *context.Context) { boards[0].Title = ctx.Tr("repo.projects.type.uncategorized") } - issueList, err := boards.LoadIssues() + issuesMap, err := models.LoadIssuesFromBoardList(boards) if err != nil { ctx.ServerError("LoadIssuesOfBoards", err) return } linkedPrsMap := make(map[int64][]*models.Issue) - for _, issue := range issueList { - var referencedIds []int64 - for _, comment := range issue.Comments { - if comment.RefIssueID != 0 && comment.RefIsPull { - referencedIds = append(referencedIds, comment.RefIssueID) + for _, issuesList := range issuesMap { + for _, issue := range issuesList { + var referencedIds []int64 + for _, comment := range issue.Comments { + if comment.RefIssueID != 0 && comment.RefIsPull { + referencedIds = append(referencedIds, comment.RefIssueID) + } } - } - if len(referencedIds) > 0 { - if linkedPrs, err := models.Issues(&models.IssuesOptions{ - IssueIDs: referencedIds, - IsPull: util.OptionalBoolTrue, - }); err == nil { - linkedPrsMap[issue.ID] = linkedPrs + if len(referencedIds) > 0 { + if linkedPrs, err := models.Issues(&models.IssuesOptions{ + IssueIDs: referencedIds, + IsPull: util.OptionalBoolTrue, + }); err == nil { + linkedPrsMap[issue.ID] = linkedPrs + } } } } @@ -335,6 +338,7 @@ func ViewProject(ctx *context.Context) { ctx.Data["IsProjectsPage"] = true ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) ctx.Data["Project"] = project + ctx.Data["IssuesMap"] = issuesMap ctx.Data["Boards"] = boards ctx.HTML(http.StatusOK, tplProjectsView) @@ -381,9 +385,9 @@ func DeleteProjectBoard(ctx *context.Context) { return } - project, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + project, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -391,7 +395,7 @@ func DeleteProjectBoard(ctx *context.Context) { return } - pb, err := models.GetProjectBoard(ctx.ParamsInt64(":boardID")) + pb, err := project_model.GetBoard(ctx.ParamsInt64(":boardID")) if err != nil { ctx.ServerError("GetProjectBoard", err) return @@ -410,7 +414,7 @@ func DeleteProjectBoard(ctx *context.Context) { return } - if err := models.DeleteProjectBoardByID(ctx.ParamsInt64(":boardID")); err != nil { + if err := project_model.DeleteBoardByID(ctx.ParamsInt64(":boardID")); err != nil { ctx.ServerError("DeleteProjectBoardByID", err) return } @@ -430,9 +434,9 @@ func AddBoardToProjectPost(ctx *context.Context) { return } - project, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + project, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -440,7 +444,7 @@ func AddBoardToProjectPost(ctx *context.Context) { return } - if err := models.NewProjectBoard(&models.ProjectBoard{ + if err := project_model.NewBoard(&project_model.Board{ ProjectID: project.ID, Title: form.Title, Color: form.Color, @@ -455,7 +459,7 @@ func AddBoardToProjectPost(ctx *context.Context) { }) } -func checkProjectBoardChangePermissions(ctx *context.Context) (*models.Project, *models.ProjectBoard) { +func checkProjectBoardChangePermissions(ctx *context.Context) (*project_model.Project, *project_model.Board) { if ctx.Doer == nil { ctx.JSON(http.StatusForbidden, map[string]string{ "message": "Only signed in users are allowed to perform this action.", @@ -470,9 +474,9 @@ func checkProjectBoardChangePermissions(ctx *context.Context) (*models.Project, return nil, nil } - project, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + project, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -480,7 +484,7 @@ func checkProjectBoardChangePermissions(ctx *context.Context) (*models.Project, return nil, nil } - board, err := models.GetProjectBoard(ctx.ParamsInt64(":boardID")) + board, err := project_model.GetBoard(ctx.ParamsInt64(":boardID")) if err != nil { ctx.ServerError("GetProjectBoard", err) return nil, nil @@ -519,7 +523,7 @@ func EditProjectBoard(ctx *context.Context) { board.Sorting = form.Sorting } - if err := models.UpdateProjectBoard(board); err != nil { + if err := project_model.UpdateBoard(board); err != nil { ctx.ServerError("UpdateProjectBoard", err) return } @@ -536,7 +540,7 @@ func SetDefaultProjectBoard(ctx *context.Context) { return } - if err := models.SetDefaultBoard(project.ID, board.ID); err != nil { + if err := project_model.SetDefaultBoard(project.ID, board.ID); err != nil { ctx.ServerError("SetDefaultBoard", err) return } @@ -562,9 +566,9 @@ func MoveIssues(ctx *context.Context) { return } - project, err := models.GetProjectByID(ctx.ParamsInt64(":id")) + project, err := project_model.GetProjectByID(ctx.ParamsInt64(":id")) if err != nil { - if models.IsErrProjectNotExist(err) { + if project_model.IsErrProjectNotExist(err) { ctx.NotFound("ProjectNotExist", nil) } else { ctx.ServerError("GetProjectByID", err) @@ -576,19 +580,18 @@ func MoveIssues(ctx *context.Context) { return } - var board *models.ProjectBoard + var board *project_model.Board if ctx.ParamsInt64(":boardID") == 0 { - board = &models.ProjectBoard{ + board = &project_model.Board{ ID: 0, ProjectID: project.ID, Title: ctx.Tr("repo.projects.type.uncategorized"), } } else { - // column - board, err = models.GetProjectBoard(ctx.ParamsInt64(":boardID")) + board, err = project_model.GetBoard(ctx.ParamsInt64(":boardID")) if err != nil { - if models.IsErrProjectBoardNotExist(err) { + if project_model.IsErrProjectBoardNotExist(err) { ctx.NotFound("ProjectBoardNotExist", nil) } else { ctx.ServerError("GetProjectBoard", err) @@ -634,7 +637,7 @@ func MoveIssues(ctx *context.Context) { return } - if err = models.MoveIssuesOnProjectBoard(board, sortedIssueIDs); err != nil { + if err = project_model.MoveIssuesOnProjectBoard(board, sortedIssueIDs); err != nil { ctx.ServerError("MoveIssuesOnProjectBoard", err) return } @@ -647,8 +650,43 @@ func MoveIssues(ctx *context.Context) { // CreateProject renders the generic project creation page func CreateProject(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.projects.new") - ctx.Data["ProjectTypes"] = models.GetProjectsConfig() + ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig() ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) ctx.HTML(http.StatusOK, tplGenericProjectsNew) } + +// CreateProjectPost creates an individual and/or organization project +func CreateProjectPost(ctx *context.Context, form forms.UserCreateProjectForm) { + user := checkContextUser(ctx, form.UID) + if ctx.Written() { + return + } + + ctx.Data["ContextUser"] = user + + if ctx.HasError() { + ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects) + ctx.HTML(http.StatusOK, tplGenericProjectsNew) + return + } + + projectType := project_model.TypeIndividual + if user.IsOrganization() { + projectType = project_model.TypeOrganization + } + + if err := project_model.NewProject(&project_model.Project{ + Title: form.Title, + Description: form.Content, + CreatorID: user.ID, + BoardType: form.BoardType, + Type: projectType, + }); err != nil { + ctx.ServerError("NewProject", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.projects.create_success", form.Title)) + ctx.Redirect(setting.AppSubURL + "/") +} |