2020-08-17 08:37:38 +05:30
|
|
|
// 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.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2020-08-17 08:37:38 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-03-29 19:46:31 +05:30
|
|
|
project_model "code.gitea.io/gitea/models/project"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2020-08-17 08:37:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-12-17 06:45:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-04-20 03:55:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2020-08-17 08:37:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2020-08-17 08:37:38 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplProjects base.TplName = "repo/projects/list"
|
|
|
|
tplProjectsNew base.TplName = "repo/projects/new"
|
|
|
|
tplProjectsView base.TplName = "repo/projects/view"
|
|
|
|
tplGenericProjectsNew base.TplName = "user/project"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MustEnableProjects check if projects are enabled in settings
|
|
|
|
func MustEnableProjects(ctx *context.Context) {
|
2021-11-10 01:27:58 +05:30
|
|
|
if unit.TypeProjects.UnitGlobalDisabled() {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("EnableKanbanBoard", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Repo.Repository != nil {
|
2021-11-10 01:27:58 +05:30
|
|
|
if !ctx.Repo.CanRead(unit.TypeProjects) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("MustEnableProjects", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Projects renders the home page of projects
|
|
|
|
func Projects(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.project_board")
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
sortType := ctx.FormTrim("sort")
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
isShowClosed := strings.ToLower(ctx.FormTrim("state")) == "closed"
|
2020-08-17 08:37:38 +05:30
|
|
|
repo := ctx.Repo.Repository
|
2021-07-29 07:12:15 +05:30
|
|
|
page := ctx.FormInt("page")
|
2020-08-17 08:37:38 +05:30
|
|
|
if page <= 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["OpenCount"] = repo.NumOpenProjects
|
|
|
|
ctx.Data["ClosedCount"] = repo.NumClosedProjects
|
|
|
|
|
|
|
|
var total int
|
|
|
|
if !isShowClosed {
|
|
|
|
total = repo.NumOpenProjects
|
|
|
|
} else {
|
|
|
|
total = repo.NumClosedProjects
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
projects, count, err := project_model.GetProjects(ctx, project_model.SearchOptions{
|
2020-08-17 08:37:38 +05:30
|
|
|
RepoID: repo.ID,
|
|
|
|
Page: page,
|
|
|
|
IsClosed: util.OptionalBoolOf(isShowClosed),
|
|
|
|
SortType: sortType,
|
2022-03-29 19:46:31 +05:30
|
|
|
Type: project_model.TypeRepository,
|
2020-08-17 08:37:38 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetProjects", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range projects {
|
2021-04-20 03:55:08 +05:30
|
|
|
projects[i].RenderedContent, err = markdown.RenderString(&markup.RenderContext{
|
|
|
|
URLPrefix: ctx.Repo.RepoLink,
|
|
|
|
Metas: ctx.Repo.Repository.ComposeMetas(),
|
2021-06-21 04:09:12 +05:30
|
|
|
GitRepo: ctx.Repo.GitRepo,
|
2021-08-29 01:45:56 +05:30
|
|
|
Ctx: ctx,
|
2021-04-20 03:55:08 +05:30
|
|
|
}, projects[i].Description)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("RenderString", err)
|
|
|
|
return
|
|
|
|
}
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Projects"] = projects
|
|
|
|
|
|
|
|
if isShowClosed {
|
|
|
|
ctx.Data["State"] = "closed"
|
|
|
|
} else {
|
|
|
|
ctx.Data["State"] = "open"
|
|
|
|
}
|
|
|
|
|
|
|
|
numPages := 0
|
|
|
|
if count > 0 {
|
|
|
|
numPages = int((int(count) - 1) / setting.UI.IssuePagingNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)
|
|
|
|
pager.AddParam(ctx, "state", "State")
|
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.Data["IsShowClosed"] = isShowClosed
|
|
|
|
ctx.Data["IsProjectsPage"] = true
|
|
|
|
ctx.Data["SortType"] = sortType
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// NewProject render creating a project page
|
|
|
|
func NewProject(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.projects.new")
|
2022-03-29 19:46:31 +05:30
|
|
|
ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig()
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjectsNew)
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
2020-08-22 12:28:59 +05:30
|
|
|
// NewProjectPost creates a new project
|
2021-01-26 21:06:53 +05:30
|
|
|
func NewProjectPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.CreateProjectForm)
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.projects.new")
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2022-03-29 19:46:31 +05:30
|
|
|
ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig()
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjectsNew)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.NewProject(&project_model.Project{
|
2020-08-17 08:37:38 +05:30
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
Title: form.Title,
|
|
|
|
Description: form.Content,
|
2022-03-22 12:33:22 +05:30
|
|
|
CreatorID: ctx.Doer.ID,
|
2020-08-17 08:37:38 +05:30
|
|
|
BoardType: form.BoardType,
|
2022-03-29 19:46:31 +05:30
|
|
|
Type: project_model.TypeRepository,
|
2020-08-17 08:37:38 +05:30
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("NewProject", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.projects.create_success", form.Title))
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/projects")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeProjectStatus updates the status of a project between "open" and "close"
|
|
|
|
func ChangeProjectStatus(ctx *context.Context) {
|
|
|
|
toClose := false
|
|
|
|
switch ctx.Params(":action") {
|
|
|
|
case "open":
|
|
|
|
toClose = false
|
|
|
|
case "close":
|
|
|
|
toClose = true
|
|
|
|
default:
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/projects")
|
|
|
|
}
|
|
|
|
id := ctx.ParamsInt64(":id")
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx.Repo.Repository.ID, id, toClose); err != nil {
|
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", err)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("ChangeProjectStatusByIDAndRepoID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-11-16 23:48:25 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/projects?state=" + url.QueryEscape(ctx.Params(":action")))
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteProject delete a project
|
|
|
|
func DeleteProject(ctx *context.Context) {
|
2022-05-20 19:38:52 +05:30
|
|
|
p, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound("", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.DeleteProjectByID(p.ID); err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.Flash.Error("DeleteProjectByID: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success"))
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"redirect": ctx.Repo.RepoLink + "/projects",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditProject allows a project to be edited
|
|
|
|
func EditProject(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.projects.edit")
|
|
|
|
ctx.Data["PageIsEditProjects"] = true
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
p, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound("", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["title"] = p.Title
|
|
|
|
ctx.Data["content"] = p.Description
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjectsNew)
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// EditProjectPost response for editing a project
|
2021-01-26 21:06:53 +05:30
|
|
|
func EditProjectPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.CreateProjectForm)
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.projects.edit")
|
|
|
|
ctx.Data["PageIsEditProjects"] = true
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjectsNew)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
p, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound("", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Title = form.Title
|
|
|
|
p.Description = form.Content
|
2022-05-20 19:38:52 +05:30
|
|
|
if err = project_model.UpdateProject(ctx, p); err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.ServerError("UpdateProjects", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.projects.edit_success", p.Title))
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/projects")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ViewProject renders the project board for a project
|
|
|
|
func ViewProject(ctx *context.Context) {
|
2022-05-20 19:38:52 +05:30
|
|
|
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if project.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound("", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
boards, err := project_model.GetBoards(ctx, project.ID)
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetProjectBoards", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:59:32 +05:30
|
|
|
if boards[0].ID == 0 {
|
|
|
|
boards[0].Title = ctx.Tr("repo.projects.type.uncategorized")
|
|
|
|
}
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
issuesMap, err := models.LoadIssuesFromBoardList(boards)
|
2021-01-21 01:23:48 +05:30
|
|
|
if err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.ServerError("LoadIssuesOfBoards", err)
|
|
|
|
return
|
|
|
|
}
|
2021-01-21 01:23:48 +05:30
|
|
|
|
|
|
|
linkedPrsMap := make(map[int64][]*models.Issue)
|
2022-03-29 19:46:31 +05:30
|
|
|
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)
|
|
|
|
}
|
2021-01-21 01:23:48 +05:30
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if len(referencedIds) > 0 {
|
|
|
|
if linkedPrs, err := models.Issues(&models.IssuesOptions{
|
|
|
|
IssueIDs: referencedIds,
|
|
|
|
IsPull: util.OptionalBoolTrue,
|
|
|
|
}); err == nil {
|
|
|
|
linkedPrsMap[issue.ID] = linkedPrs
|
|
|
|
}
|
2021-01-21 01:23:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["LinkedPRs"] = linkedPrsMap
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
project.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
|
|
|
|
URLPrefix: ctx.Repo.RepoLink,
|
|
|
|
Metas: ctx.Repo.Repository.ComposeMetas(),
|
2021-06-21 04:09:12 +05:30
|
|
|
GitRepo: ctx.Repo.GitRepo,
|
2021-08-29 01:45:56 +05:30
|
|
|
Ctx: ctx,
|
2021-04-20 03:55:08 +05:30
|
|
|
}, project.Description)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("RenderString", err)
|
|
|
|
return
|
|
|
|
}
|
2020-11-10 09:16:19 +05:30
|
|
|
|
2021-11-22 16:27:05 +05:30
|
|
|
ctx.Data["IsProjectsPage"] = true
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.Data["Project"] = project
|
2022-03-29 19:46:31 +05:30
|
|
|
ctx.Data["IssuesMap"] = issuesMap
|
2021-01-16 01:59:32 +05:30
|
|
|
ctx.Data["Boards"] = boards
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplProjectsView)
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueProject change an issue's project
|
|
|
|
func UpdateIssueProject(ctx *context.Context) {
|
|
|
|
issues := getActionIssues(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
projectID := ctx.FormInt64("id")
|
2020-08-17 08:37:38 +05:30
|
|
|
for _, issue := range issues {
|
|
|
|
oldProjectID := issue.ProjectID()
|
|
|
|
if oldProjectID == projectID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if err := models.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.ServerError("ChangeProjectAssign", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteProjectBoard allows for the deletion of a project board
|
|
|
|
func DeleteProjectBoard(ctx *context.Context) {
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer == nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only signed in users are allowed to perform this action.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:28:28 +05:30
|
|
|
if !ctx.Repo.IsOwner() && !ctx.Repo.IsAdmin() && !ctx.Repo.CanAccess(perm.AccessModeWrite, unit.TypeProjects) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only authorized users are allowed to perform this action.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
pb, err := project_model.GetBoard(ctx, ctx.ParamsInt64(":boardID"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2021-01-15 01:57:22 +05:30
|
|
|
ctx.ServerError("GetProjectBoard", err)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
if pb.ProjectID != ctx.ParamsInt64(":id") {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": fmt.Sprintf("ProjectBoard[%d] is not in Project[%d] as expected", pb.ID, project.ID),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.RepoID != ctx.Repo.Repository.ID {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": fmt.Sprintf("ProjectBoard[%d] is not in Repository[%d] as expected", pb.ID, ctx.Repo.Repository.ID),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.DeleteBoardByID(ctx.ParamsInt64(":boardID")); err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.ServerError("DeleteProjectBoardByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBoardToProjectPost allows a new board to be added to a project.
|
2021-01-26 21:06:53 +05:30
|
|
|
func AddBoardToProjectPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.EditProjectBoardForm)
|
2021-11-28 17:28:28 +05:30
|
|
|
if !ctx.Repo.IsOwner() && !ctx.Repo.IsAdmin() && !ctx.Repo.CanAccess(perm.AccessModeWrite, unit.TypeProjects) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only authorized users are allowed to perform this action.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.NewBoard(&project_model.Board{
|
2020-08-17 08:37:38 +05:30
|
|
|
ProjectID: project.ID,
|
|
|
|
Title: form.Title,
|
2021-09-30 02:23:12 +05:30
|
|
|
Color: form.Color,
|
2022-03-22 12:33:22 +05:30
|
|
|
CreatorID: ctx.Doer.ID,
|
2020-08-17 08:37:38 +05:30
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("NewProjectBoard", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
func checkProjectBoardChangePermissions(ctx *context.Context) (*project_model.Project, *project_model.Board) {
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer == nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only signed in users are allowed to perform this action.",
|
|
|
|
})
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
2021-11-28 17:28:28 +05:30
|
|
|
if !ctx.Repo.IsOwner() && !ctx.Repo.IsAdmin() && !ctx.Repo.CanAccess(perm.AccessModeWrite, unit.TypeProjects) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only authorized users are allowed to perform this action.",
|
|
|
|
})
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.NotFound("", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
board, err := project_model.GetBoard(ctx, ctx.ParamsInt64(":boardID"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2021-01-15 01:57:22 +05:30
|
|
|
ctx.ServerError("GetProjectBoard", err)
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
if board.ProjectID != ctx.ParamsInt64(":id") {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": fmt.Sprintf("ProjectBoard[%d] is not in Project[%d] as expected", board.ID, project.ID),
|
|
|
|
})
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if project.RepoID != ctx.Repo.Repository.ID {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": fmt.Sprintf("ProjectBoard[%d] is not in Repository[%d] as expected", board.ID, ctx.Repo.Repository.ID),
|
|
|
|
})
|
2021-01-16 01:59:32 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return project, board
|
|
|
|
}
|
|
|
|
|
2021-02-11 22:02:27 +05:30
|
|
|
// EditProjectBoard allows a project board's to be updated
|
|
|
|
func EditProjectBoard(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.EditProjectBoardForm)
|
2021-01-16 01:59:32 +05:30
|
|
|
_, board := checkProjectBoardChangePermissions(ctx)
|
|
|
|
if ctx.Written() {
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Title != "" {
|
|
|
|
board.Title = form.Title
|
|
|
|
}
|
|
|
|
|
2021-09-30 02:23:12 +05:30
|
|
|
board.Color = form.Color
|
|
|
|
|
2021-02-11 22:02:27 +05:30
|
|
|
if form.Sorting != 0 {
|
|
|
|
board.Sorting = form.Sorting
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
if err := project_model.UpdateBoard(ctx, board); err != nil {
|
2020-08-17 08:37:38 +05:30
|
|
|
ctx.ServerError("UpdateProjectBoard", err)
|
2021-01-16 01:59:32 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2021-01-16 01:59:32 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaultProjectBoard set default board for uncategorized issues/pulls
|
|
|
|
func SetDefaultProjectBoard(ctx *context.Context) {
|
|
|
|
project, board := checkProjectBoardChangePermissions(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err := project_model.SetDefaultBoard(project.ID, board.ID); err != nil {
|
2021-01-16 01:59:32 +05:30
|
|
|
ctx.ServerError("SetDefaultBoard", err)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:27:18 +05:30
|
|
|
// MoveIssues moves or keeps issues in a column and sorts them inside that column
|
|
|
|
func MoveIssues(ctx *context.Context) {
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer == nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only signed in users are allowed to perform this action.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:28:28 +05:30
|
|
|
if !ctx.Repo.IsOwner() && !ctx.Repo.IsAdmin() && !ctx.Repo.CanAccess(perm.AccessModeWrite, unit.TypeProjects) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusForbidden, map[string]string{
|
2020-08-17 08:37:38 +05:30
|
|
|
"message": "Only authorized users are allowed to perform this action.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectNotExist(err) {
|
2021-12-08 12:27:18 +05:30
|
|
|
ctx.NotFound("ProjectNotExist", nil)
|
2020-08-17 08:37:38 +05:30
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectByID", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-12-08 12:27:18 +05:30
|
|
|
if project.RepoID != ctx.Repo.Repository.ID {
|
|
|
|
ctx.NotFound("InvalidRepoID", nil)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
var board *project_model.Board
|
2020-08-17 08:37:38 +05:30
|
|
|
|
|
|
|
if ctx.ParamsInt64(":boardID") == 0 {
|
2022-03-29 19:46:31 +05:30
|
|
|
board = &project_model.Board{
|
2020-08-17 08:37:38 +05:30
|
|
|
ID: 0,
|
2021-12-08 12:27:18 +05:30
|
|
|
ProjectID: project.ID,
|
2020-08-17 08:37:38 +05:30
|
|
|
Title: ctx.Tr("repo.projects.type.uncategorized"),
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-20 19:38:52 +05:30
|
|
|
board, err = project_model.GetBoard(ctx, ctx.ParamsInt64(":boardID"))
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
2022-03-29 19:46:31 +05:30
|
|
|
if project_model.IsErrProjectBoardNotExist(err) {
|
2021-12-08 12:27:18 +05:30
|
|
|
ctx.NotFound("ProjectBoardNotExist", nil)
|
2020-08-17 08:37:38 +05:30
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetProjectBoard", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-12-08 12:27:18 +05:30
|
|
|
if board.ProjectID != project.ID {
|
|
|
|
ctx.NotFound("BoardNotInProject", nil)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:27:18 +05:30
|
|
|
type movedIssuesForm struct {
|
|
|
|
Issues []struct {
|
|
|
|
IssueID int64 `json:"issueID"`
|
|
|
|
Sorting int64 `json:"sorting"`
|
|
|
|
} `json:"issues"`
|
|
|
|
}
|
|
|
|
|
|
|
|
form := &movedIssuesForm{}
|
|
|
|
if err = json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
|
|
|
|
ctx.ServerError("DecodeMovedIssuesForm", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
issueIDs := make([]int64, 0, len(form.Issues))
|
|
|
|
sortedIssueIDs := make(map[int64]int64)
|
|
|
|
for _, issue := range form.Issues {
|
|
|
|
issueIDs = append(issueIDs, issue.IssueID)
|
|
|
|
sortedIssueIDs[issue.Sorting] = issue.IssueID
|
|
|
|
}
|
2022-05-20 19:38:52 +05:30
|
|
|
movedIssues, err := models.GetIssuesByIDs(ctx, issueIDs)
|
2020-08-17 08:37:38 +05:30
|
|
|
if err != nil {
|
|
|
|
if models.IsErrIssueNotExist(err) {
|
2021-12-08 12:27:18 +05:30
|
|
|
ctx.NotFound("IssueNotExisting", nil)
|
2020-08-17 08:37:38 +05:30
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetIssueByID", err)
|
|
|
|
}
|
2021-12-08 12:27:18 +05:30
|
|
|
return
|
|
|
|
}
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2021-12-08 12:27:18 +05:30
|
|
|
if len(movedIssues) != len(form.Issues) {
|
|
|
|
ctx.ServerError("IssuesNotFound", err)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:46:31 +05:30
|
|
|
if err = project_model.MoveIssuesOnProjectBoard(board, sortedIssueIDs); err != nil {
|
2021-12-08 12:27:18 +05:30
|
|
|
ctx.ServerError("MoveIssuesOnProjectBoard", err)
|
2020-08-17 08:37:38 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2020-08-17 08:37:38 +05:30
|
|
|
"ok": true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateProject renders the generic project creation page
|
|
|
|
func CreateProject(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.projects.new")
|
2022-03-29 19:46:31 +05:30
|
|
|
ctx.Data["ProjectTypes"] = project_model.GetProjectsConfig()
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
2020-08-17 08:37:38 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplGenericProjectsNew)
|
2020-08-17 08:37:38 +05:30
|
|
|
}
|
2022-03-29 19:46:31 +05:30
|
|
|
|
|
|
|
// 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 + "/")
|
|
|
|
}
|