Skip to content

Commit 64e0ba1

Browse files
committed
Add project workflow feature so users can define how to execute steps when project related events fired
1 parent 66f7d47 commit 64e0ba1

File tree

6 files changed

+242
-1
lines changed

6 files changed

+242
-1
lines changed

models/project/board.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ func GetBoard(ctx context.Context, boardID int64) (*Board, error) {
219219
return board, nil
220220
}
221221

222+
func GetBoardByProjectIDAndBoardName(ctx context.Context, projectID int64, boardName string) (*Board, error) {
223+
board := new(Board)
224+
has, err := db.GetEngine(ctx).Where("project_id=? AND title=?", projectID, boardName).Get(board)
225+
if err != nil {
226+
return nil, err
227+
} else if !has {
228+
return nil, ErrProjectBoardNotExist{ProjectID: projectID, Name: boardName}
229+
}
230+
231+
return board, nil
232+
}
233+
222234
// UpdateBoard updates a project board
223235
func UpdateBoard(ctx context.Context, board *Board) error {
224236
var fieldToUpdate []string

models/project/issue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func (p *Project) NumOpenIssues(ctx context.Context) int {
7575
return int(c)
7676
}
7777

78+
func MoveIssueToAnotherBoard(ctx context.Context, issueID int64, newBoard *Board) error {
79+
_, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id=? WHERE issue_id=?", newBoard.ID, issueID)
80+
return err
81+
}
82+
7883
// MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column
7984
func MoveIssuesOnProjectBoard(ctx context.Context, board *Board, sortedIssueIDs map[int64]int64) error {
8085
return db.WithTx(ctx, func(ctx context.Context) error {

models/project/project.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252
type ErrProjectNotExist struct {
5353
ID int64
5454
RepoID int64
55+
Name string
5556
}
5657

5758
// IsErrProjectNotExist checks if an error is a ErrProjectNotExist
@@ -61,6 +62,9 @@ func IsErrProjectNotExist(err error) bool {
6162
}
6263

6364
func (err ErrProjectNotExist) Error() string {
65+
if err.RepoID > 0 && len(err.Name) > 0 {
66+
return fmt.Sprintf("projects does not exist [repo_id: %d, name: %s]", err.RepoID, err.Name)
67+
}
6468
return fmt.Sprintf("projects does not exist [id: %d]", err.ID)
6569
}
6670

@@ -70,7 +74,9 @@ func (err ErrProjectNotExist) Unwrap() error {
7074

7175
// ErrProjectBoardNotExist represents a "ProjectBoardNotExist" kind of error.
7276
type ErrProjectBoardNotExist struct {
73-
BoardID int64
77+
BoardID int64
78+
ProjectID int64
79+
Name string
7480
}
7581

7682
// IsErrProjectBoardNotExist checks if an error is a ErrProjectBoardNotExist
@@ -80,6 +86,9 @@ func IsErrProjectBoardNotExist(err error) bool {
8086
}
8187

8288
func (err ErrProjectBoardNotExist) Error() string {
89+
if err.ProjectID > 0 && len(err.Name) > 0 {
90+
return fmt.Sprintf("project board does not exist [project_id: %d, name: %s]", err.ProjectID, err.Name)
91+
}
8392
return fmt.Sprintf("project board does not exist [id: %d]", err.BoardID)
8493
}
8594

@@ -293,6 +302,19 @@ func GetProjectByID(ctx context.Context, id int64) (*Project, error) {
293302
return p, nil
294303
}
295304

305+
// GetProjectByName returns the projects in a repository
306+
func GetProjectByName(ctx context.Context, repoID int64, name string) (*Project, error) {
307+
p := new(Project)
308+
has, err := db.GetEngine(ctx).Where("repo_id=? AND title=?", repoID, name).Get(p)
309+
if err != nil {
310+
return nil, err
311+
} else if !has {
312+
return nil, ErrProjectNotExist{RepoID: repoID, Name: name}
313+
}
314+
315+
return p, nil
316+
}
317+
296318
// GetProjectForRepoByID returns the projects in a repository
297319
func GetProjectForRepoByID(ctx context.Context, repoID, id int64) (*Project, error) {
298320
p := new(Project)

modules/projects/workflow.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package projects
5+
6+
// Action represents an action that can be taken in a workflow
7+
type Action struct {
8+
SetValue string
9+
}
10+
11+
const (
12+
// Project workflow event names
13+
EventItemAddedToProject = "item_added_to_project"
14+
EventItemClosed = "item_closed"
15+
)
16+
17+
type Event struct {
18+
Name string
19+
Types []string
20+
Actions []Action
21+
}
22+
23+
type Workflow struct {
24+
Name string
25+
Events []Event
26+
}
27+
28+
func ParseWorkflow(content string) (*Workflow, error) {
29+
return &Workflow{}, nil
30+
}
31+
32+
func (w *Workflow) FireAction(evtName string, f func(action Action) error) error {
33+
for _, evt := range w.Events {
34+
if evt.Name == evtName {
35+
for _, action := range evt.Actions {
36+
// Do something with action
37+
if err := f(action); err != nil {
38+
return err
39+
}
40+
}
41+
break
42+
}
43+
}
44+
return nil
45+
}

modules/projects/workflow_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package projects
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestParseWorkflow(t *testing.T) {
13+
workflowFile := `
14+
name: Test Workflow
15+
on:
16+
item_added_to_project:
17+
types: [issue, pull_request]
18+
action:
19+
- set_value: "status=Todo"
20+
21+
item_closed:
22+
types: [issue, pull_request]
23+
action:
24+
- remove_label: ""
25+
26+
item_reopened:
27+
action:
28+
29+
code_changes_requested:
30+
action:
31+
32+
code_review_approved:
33+
action:
34+
35+
pull_request_merged:
36+
action:
37+
38+
auto_add_to_project:
39+
action:
40+
`
41+
42+
wf, err := ParseWorkflow(workflowFile)
43+
assert.NoError(t, err)
44+
45+
assert.Equal(t, "Test Workflow", wf.Name)
46+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package projects
5+
6+
import (
7+
"context"
8+
"strings"
9+
10+
issues_model "code.gitea.io/gitea/models/issues"
11+
project_model "code.gitea.io/gitea/models/project"
12+
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/gitrepo"
15+
"code.gitea.io/gitea/modules/log"
16+
project_module "code.gitea.io/gitea/modules/projects"
17+
notify_service "code.gitea.io/gitea/services/notify"
18+
)
19+
20+
func init() {
21+
notify_service.RegisterNotifier(&workflowNotifier{})
22+
}
23+
24+
type workflowNotifier struct {
25+
notify_service.NullNotifier
26+
}
27+
28+
var _ notify_service.Notifier = &workflowNotifier{}
29+
30+
// NewNotifier create a new workflowNotifier notifier
31+
func NewNotifier() notify_service.Notifier {
32+
return &workflowNotifier{}
33+
}
34+
35+
func (m *workflowNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
36+
if isClosed {
37+
if err := issue.LoadRepo(ctx); err != nil {
38+
log.Error("IssueChangeStatus: LoadRepo: %v", err)
39+
return
40+
}
41+
gitRepo, err := gitrepo.OpenRepository(ctx, issue.Repo)
42+
if err != nil {
43+
log.Error("IssueChangeStatus: OpenRepository: %v", err)
44+
return
45+
}
46+
defer gitRepo.Close()
47+
48+
// Get the commit object for the ref
49+
commit, err := gitRepo.GetCommit(issue.Repo.DefaultBranch)
50+
if err != nil {
51+
log.Error("gitRepo.GetCommit: %w", err)
52+
return
53+
}
54+
55+
tree, err := commit.SubTree(".gitea/projects")
56+
if _, ok := err.(git.ErrNotExist); ok {
57+
return
58+
}
59+
if err != nil {
60+
log.Error("commit.SubTree: %w", err)
61+
return
62+
}
63+
64+
entries, err := tree.ListEntriesRecursiveFast()
65+
if err != nil {
66+
log.Error("tree.ListEntriesRecursiveFast: %w", err)
67+
return
68+
}
69+
70+
ret := make(git.Entries, 0, len(entries))
71+
for _, entry := range entries {
72+
if strings.HasSuffix(entry.Name(), ".yml") || strings.HasSuffix(entry.Name(), ".yaml") {
73+
ret = append(ret, entry)
74+
}
75+
}
76+
if len(ret) == 0 {
77+
return
78+
}
79+
80+
for _, entry := range ret {
81+
workflowContent, err := commit.GetFileContent(".gitea/projects/"+entry.Name(), 1024*1024)
82+
if err != nil {
83+
log.Error("gitRepo.GetCommit: %w", err)
84+
return
85+
}
86+
87+
wf, err := project_module.ParseWorkflow(workflowContent)
88+
if err != nil {
89+
log.Error("IssueChangeStatus: OpenRepository: %v", err)
90+
return
91+
}
92+
projectName := strings.TrimSuffix(strings.TrimSuffix(entry.Name(), ".yml"), ".yaml")
93+
project, err := project_model.GetProjectByName(ctx, issue.RepoID, projectName)
94+
if err != nil {
95+
log.Error("IssueChangeStatus: GetProjectByName: %v", err)
96+
return
97+
}
98+
if err := wf.FireAction(project_module.EventItemClosed, func(action project_module.Action) error {
99+
board, err := project_model.GetBoardByProjectIDAndBoardName(ctx, project.ID, action.SetValue)
100+
if err != nil {
101+
log.Error("IssueChangeStatus: GetBoardByProjectIDAndBoardName: %v", err)
102+
return err
103+
}
104+
return project_model.MoveIssueToAnotherBoard(ctx, issue.ID, board)
105+
}); err != nil {
106+
log.Error("IssueChangeStatus: FireAction: %v", err)
107+
return
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)