From bd97736b9c7a16023bc9abf17be6157284f655b1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 29 Mar 2022 22:16:31 +0800 Subject: 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> --- routers/web/repo/issue.go | 19 +++--- routers/web/repo/projects.go | 146 +++++++++++++++++++++++++++---------------- routers/web/user/profile.go | 5 +- 3 files changed, 105 insertions(+), 65 deletions(-) (limited to 'routers') diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index ee4738c970..5af91c8e5f 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" + 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" @@ -336,9 +337,9 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti } if ctx.Repo.CanWriteIssuesOrPulls(ctx.Params(":type") == "pulls") { - projects, _, err := models.GetProjects(models.ProjectSearchOptions{ + projects, _, err := project_model.GetProjects(project_model.SearchOptions{ RepoID: repo.ID, - Type: models.ProjectTypeRepository, + Type: project_model.TypeRepository, IsClosed: util.OptionalBoolOf(isShowClosed), }) if err != nil { @@ -446,22 +447,22 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *repo_model.R func retrieveProjects(ctx *context.Context, repo *repo_model.Repository) { var err error - ctx.Data["OpenProjects"], _, err = models.GetProjects(models.ProjectSearchOptions{ + ctx.Data["OpenProjects"], _, err = project_model.GetProjects(project_model.SearchOptions{ RepoID: repo.ID, Page: -1, IsClosed: util.OptionalBoolFalse, - Type: models.ProjectTypeRepository, + Type: project_model.TypeRepository, }) if err != nil { ctx.ServerError("GetProjects", err) return } - ctx.Data["ClosedProjects"], _, err = models.GetProjects(models.ProjectSearchOptions{ + ctx.Data["ClosedProjects"], _, err = project_model.GetProjects(project_model.SearchOptions{ RepoID: repo.ID, Page: -1, IsClosed: util.OptionalBoolTrue, - Type: models.ProjectTypeRepository, + Type: project_model.TypeRepository, }) if err != nil { ctx.ServerError("GetProjects", err) @@ -814,7 +815,7 @@ func NewIssue(ctx *context.Context) { projectID := ctx.FormInt64("project") if projectID > 0 { - project, err := models.GetProjectByID(projectID) + project, err := project_model.GetProjectByID(projectID) if err != nil { log.Error("GetProjectByID: %d: %v", projectID, err) } else if project.RepoID != ctx.Repo.Repository.ID { @@ -926,7 +927,7 @@ func ValidateRepoMetas(ctx *context.Context, form forms.CreateIssueForm, isPull } if form.ProjectID > 0 { - p, err := models.GetProjectByID(form.ProjectID) + p, err := project_model.GetProjectByID(form.ProjectID) if err != nil { ctx.ServerError("GetProjectByID", err) return nil, nil, 0, 0 @@ -1413,7 +1414,7 @@ func ViewIssue(ctx *context.Context) { return } - ghostProject := &models.Project{ + ghostProject := &project_model.Project{ ID: -1, Title: ctx.Tr("repo.issues.deleted_project"), } 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 + "/") +} diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 99198e8866..f628840375 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "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/context" @@ -216,10 +217,10 @@ func Profile(ctx *context.Context) { total = int(count) case "projects": - ctx.Data["OpenProjects"], _, err = models.GetProjects(models.ProjectSearchOptions{ + ctx.Data["OpenProjects"], _, err = project_model.GetProjects(project_model.SearchOptions{ Page: -1, IsClosed: util.OptionalBoolFalse, - Type: models.ProjectTypeIndividual, + Type: project_model.TypeIndividual, }) if err != nil { ctx.ServerError("GetProjects", err) -- cgit v1.2.3