mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-11 12:36:15 +00:00
feat(api): add project APIs (#38691)
Adds REST APIs for project boards for repo, org and user scopes, using as much shared code as possible for all 3 scopes. Fixes: https://github.com/go-gitea/gitea/issues/14299 Fixes: https://github.com/go-gitea/gitea/issues/31769 Fixes: https://github.com/go-gitea/gitea/issues/35921 Replaces: https://github.com/go-gitea/gitea/pull/37518 Replaces: https://github.com/go-gitea/gitea/pull/36008 Replaces: https://github.com/go-gitea/gitea/pull/28111 Replaces: https://github.com/go-gitea/gitea/pull/31768 Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Supen.Huang <supen.huang@qq.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Ember <ember@mubergacres.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: beardev-in <abhinav.edulakanti@gmail.com>
This commit is contained in:
@@ -5,7 +5,6 @@ package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
@@ -14,10 +13,69 @@ import (
|
||||
project_model "gitea.dev/models/project"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/optional"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// ErrIssueNotInProject unwraps as ErrUnprocessableContent, not ErrNotExist: ctx.ServerError
|
||||
// diverts ErrNotExist to a 404, which would hide this from the web caller's logs.
|
||||
var ErrIssueNotInProject = util.ErrorWrap(util.ErrUnprocessableContent, "all issues have to be added to a project first")
|
||||
|
||||
// AddIssueToColumn assigns the issue to the column's project if needed, then places it in
|
||||
// the column. One transaction, so a failure cannot strand it in the default column.
|
||||
func AddIssueToColumn(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, column *project_model.Column) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
projectIDs, err := issue.ProjectIDs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !slices.Contains(projectIDs, column.ProjectID) {
|
||||
// lands in the default column, the move below puts it in the requested one
|
||||
if err := issues_model.IssueAssignOrRemoveProject(ctx, issue, doer, append(projectIDs, column.ProjectID)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return MoveIssueToColumn(ctx, doer, issue, column, optional.None[int64]())
|
||||
})
|
||||
}
|
||||
|
||||
// MoveIssueToColumn places an issue already in the project into a column, appending it
|
||||
// when sorting is absent.
|
||||
func MoveIssueToColumn(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, column *project_model.Column, sorting optional.Option[int64]) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
position := sorting.Value()
|
||||
if !sorting.Has() {
|
||||
next, err := project_model.GetColumnIssueNextSorting(ctx, column)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
position = next
|
||||
}
|
||||
return MoveIssuesOnProjectColumn(ctx, doer, column, map[int64]int64{position: issue.ID})
|
||||
})
|
||||
}
|
||||
|
||||
// RemoveIssueFromColumn detaches the issue from the column's project, reporting a
|
||||
// not-exist error when it is not in that column.
|
||||
func RemoveIssueFromColumn(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, column *project_model.Column) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
exists, err := project_model.IsIssueInColumn(ctx, issue.ID, column)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return util.NewNotExistErrorf("issue %d is not in column %d", issue.ID, column.ID)
|
||||
}
|
||||
projectIDs, err := issue.ProjectIDs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
remaining := util.SliceRemoveAll(projectIDs, column.ProjectID)
|
||||
return issues_model.IssueAssignOrRemoveProject(ctx, issue, doer, remaining)
|
||||
})
|
||||
}
|
||||
|
||||
// MoveIssuesOnProjectColumn moves or keeps issues in a column and sorts them inside that column
|
||||
func MoveIssuesOnProjectColumn(ctx context.Context, doer *user_model.User, column *project_model.Column, sortedIssueIDs map[int64]int64) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
@@ -33,7 +91,7 @@ func MoveIssuesOnProjectColumn(ctx context.Context, doer *user_model.User, colum
|
||||
return err
|
||||
}
|
||||
if int(count) != len(sortedIssueIDs) {
|
||||
return errors.New("all issues have to be added to a project first")
|
||||
return ErrIssueNotInProject
|
||||
}
|
||||
|
||||
issues, err := issues_model.GetIssuesByIDs(ctx, issueIDs)
|
||||
@@ -87,12 +145,8 @@ func MoveIssuesOnProjectColumn(ctx context.Context, doer *user_model.User, colum
|
||||
// IMPORTANT: The WHERE clause must include both issue_id AND project_id to ensure
|
||||
// that moving an issue's column in one project doesn't affect its column in other
|
||||
// projects when the issue is assigned to multiple projects.
|
||||
_, err = db.GetEngine(ctx).Table("project_issue").
|
||||
Where("issue_id = ? AND project_id = ?", issueID, column.ProjectID).
|
||||
Update(map[string]any{
|
||||
"project_board_id": column.ID,
|
||||
"sorting": sorting,
|
||||
})
|
||||
_, err = db.Exec(ctx, "UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=? AND project_id=?",
|
||||
column.ID, sorting, issueID, column.ProjectID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
@@ -169,6 +170,44 @@ func Test_Projects(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Moving an issue in one project keeps its column in other projects", func(t *testing.T) {
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
// issue 11 is in repo1 but in no fixture project, so reconciling its memberships disturbs nothing
|
||||
issue11 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 11})
|
||||
|
||||
projects := make([]*project_model.Project, 2)
|
||||
for i := range projects {
|
||||
projects[i] = &project_model.Project{
|
||||
Title: "multi-project isolation " + strconv.Itoa(i),
|
||||
RepoID: repo1.ID,
|
||||
Type: project_model.TypeRepository,
|
||||
TemplateType: project_model.TemplateTypeBasicKanban,
|
||||
}
|
||||
assert.NoError(t, project_model.NewProject(t.Context(), projects[i]))
|
||||
defer func() {
|
||||
assert.NoError(t, project_model.DeleteProjectByID(t.Context(), projects[i].ID))
|
||||
}()
|
||||
}
|
||||
|
||||
assert.NoError(t, issues_model.IssueAssignOrRemoveProject(t.Context(), issue11, user2, []int64{projects[0].ID, projects[1].ID}))
|
||||
|
||||
// the column the issue must stay in for the second project
|
||||
otherColumn, err := projects[1].MustDefaultColumn(t.Context())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// move the issue into a non-default column of the first project only
|
||||
targetColumn := &project_model.Column{Title: "target", ProjectID: projects[0].ID}
|
||||
assert.NoError(t, project_model.NewColumn(t.Context(), targetColumn))
|
||||
assert.NoError(t, MoveIssuesOnProjectColumn(t.Context(), user2, targetColumn, map[int64]int64{0: issue11.ID}))
|
||||
|
||||
unittest.AssertExistsAndLoadBean(t, &project_model.ProjectIssue{
|
||||
IssueID: issue11.ID, ProjectID: projects[0].ID, ProjectColumnID: targetColumn.ID,
|
||||
})
|
||||
unittest.AssertExistsAndLoadBean(t, &project_model.ProjectIssue{
|
||||
IssueID: issue11.ID, ProjectID: projects[1].ID, ProjectColumnID: otherColumn.ID,
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Repository projects", func(t *testing.T) {
|
||||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
project_model "gitea.dev/models/project"
|
||||
"gitea.dev/modules/optional"
|
||||
)
|
||||
|
||||
// UpdateProjectOptions represents updatable project fields. Fields with no value are left unchanged.
|
||||
type UpdateProjectOptions struct {
|
||||
Title optional.Option[string]
|
||||
Description optional.Option[string]
|
||||
CardType optional.Option[project_model.CardType]
|
||||
IsClosed optional.Option[bool]
|
||||
}
|
||||
|
||||
// UpdateProject applies the provided options to the project atomically.
|
||||
func UpdateProject(ctx context.Context, project *project_model.Project, opts UpdateProjectOptions) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
project.Title = opts.Title.ValueOrDefault(project.Title)
|
||||
project.Description = opts.Description.ValueOrDefault(project.Description)
|
||||
project.CardType = opts.CardType.ValueOrDefault(project.CardType)
|
||||
if err := project_model.UpdateProject(ctx, project); err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.IsClosed.Has() && opts.IsClosed.Value() != project.IsClosed {
|
||||
if err := project_model.ChangeProjectStatus(ctx, project, opts.IsClosed.Value()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user