@@ -5,8 +5,10 @@ package project
55
66import (
77 "context"
8+ "fmt"
89
910 "code.gitea.io/gitea/models/db"
11+ "code.gitea.io/gitea/modules/log"
1012 "code.gitea.io/gitea/modules/timeutil"
1113 "code.gitea.io/gitea/modules/util"
1214)
@@ -25,12 +27,88 @@ const (
2527 WorkflowEventAutoCloseIssue WorkflowEvent = "auto_close_issue"
2628)
2729
30+ var workflowEvents = []WorkflowEvent {
31+ WorkflowEventItemAddedToProject ,
32+ WorkflowEventItemReopened ,
33+ WorkflowEventItemClosed ,
34+ WorkflowEventCodeChangesRequested ,
35+ WorkflowEventCodeReviewApproved ,
36+ WorkflowEventPullRequestMerged ,
37+ WorkflowEventAutoArchiveItems ,
38+ WorkflowEventAutoAddToProject ,
39+ WorkflowEventAutoCloseIssue ,
40+ }
41+
42+ func GetWorkflowEvents () []WorkflowEvent {
43+ return workflowEvents
44+ }
45+
46+ func (we WorkflowEvent ) ToString () string {
47+ switch we {
48+ case WorkflowEventItemAddedToProject :
49+ return "Item added to project"
50+ case WorkflowEventItemReopened :
51+ return "Item reopened"
52+ case WorkflowEventItemClosed :
53+ return "Item closed"
54+ case WorkflowEventCodeChangesRequested :
55+ return "Code changes requested"
56+ case WorkflowEventCodeReviewApproved :
57+ return "Code review approved"
58+ case WorkflowEventPullRequestMerged :
59+ return "Pull request merged"
60+ case WorkflowEventAutoArchiveItems :
61+ return "Auto archive items"
62+ case WorkflowEventAutoAddToProject :
63+ return "Auto add to project"
64+ case WorkflowEventAutoCloseIssue :
65+ return "Auto close issue"
66+ default :
67+ return string (we )
68+ }
69+ }
70+
71+ func (we WorkflowEvent ) UUID () string {
72+ switch we {
73+ case WorkflowEventItemAddedToProject :
74+ return "item_added_to_project"
75+ case WorkflowEventItemReopened :
76+ return "item_reopened"
77+ case WorkflowEventItemClosed :
78+ return "item_closed"
79+ case WorkflowEventCodeChangesRequested :
80+ return "code_changes_requested"
81+ case WorkflowEventCodeReviewApproved :
82+ return "code_review_approved"
83+ case WorkflowEventPullRequestMerged :
84+ return "pull_request_merged"
85+ case WorkflowEventAutoArchiveItems :
86+ return "auto_archive_items"
87+ case WorkflowEventAutoAddToProject :
88+ return "auto_add_to_project"
89+ case WorkflowEventAutoCloseIssue :
90+ return "auto_close_issue"
91+ default :
92+ return string (we )
93+ }
94+ }
95+
96+ type WorkflowFilterType string
97+
98+ const (
99+ WorkflowFilterTypeScope WorkflowFilterType = "scope" // issue, pull_request, etc.
100+ )
101+
102+ type WorkflowFilter struct {
103+ Type WorkflowFilterType
104+ Value string // e.g., "issue", "pull_request", etc.
105+ }
106+
28107type WorkflowActionType string
29108
30109const (
31- WorkflowActionTypeScope WorkflowActionType = "scope" // issue, pull_request, etc.
110+ WorkflowActionTypeColumn WorkflowActionType = "column" // add the item to the project's column
32111 WorkflowActionTypeLabel WorkflowActionType = "label" // choose one or more labels
33- WorkflowActionTypeColumn WorkflowActionType = "column" // choose one column
34112 WorkflowActionTypeClose WorkflowActionType = "close" // close the issue
35113)
36114
@@ -42,50 +120,44 @@ type WorkflowAction struct {
42120type ProjectWorkflow struct {
43121 ID int64
44122 ProjectID int64 `xorm:"unique(s)"`
123+ Project * Project `xorm:"-"`
45124 WorkflowEvent WorkflowEvent `xorm:"unique(s)"`
125+ WorkflowFilters []WorkflowFilter `xorm:"TEXT json"`
46126 WorkflowActions []WorkflowAction `xorm:"TEXT json"`
47127 CreatedUnix timeutil.TimeStamp `xorm:"created"`
48128 UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
49129}
50130
51- func (p * ProjectWorkflow ) Link () string {
52- return ""
53- }
54-
55- func newDefaultWorkflows () []* ProjectWorkflow {
56- return []* ProjectWorkflow {
57- {
58- WorkflowEvent : WorkflowEventItemAddedToProject ,
59- WorkflowActions : []WorkflowAction {{ActionType : WorkflowActionTypeScope , ActionValue : "issue" }},
60- },
61- {
62- ProjectID : 0 ,
63- WorkflowEvent : WorkflowEventItemReopened ,
64- WorkflowActions : []WorkflowAction {{ActionType : WorkflowActionTypeScope , ActionValue : "issue" }},
65- },
131+ func (p * ProjectWorkflow ) LoadProject (ctx context.Context ) error {
132+ if p .Project != nil || p .ProjectID <= 0 {
133+ return nil
134+ }
135+ project , err := GetProjectByID (ctx , p .ProjectID )
136+ if err != nil {
137+ return err
66138 }
139+ p .Project = project
140+ return nil
67141}
68142
69- func GetWorkflowDefaultValue (workflowIDStr string ) * ProjectWorkflow {
70- workflows := newDefaultWorkflows ()
71- for _ , workflow := range workflows {
72- if workflow .WorkflowEvent == WorkflowEvent (workflowIDStr ) {
73- return workflow
74- }
143+ func (p * ProjectWorkflow ) Link (ctx context.Context ) string {
144+ if err := p .LoadProject (ctx ); err != nil {
145+ log .Error ("ProjectWorkflow Link: %v" , err )
146+ return ""
75147 }
76- return & ProjectWorkflow {}
148+ return p . Project . Link ( ctx ) + fmt . Sprintf ( "/workflows/%d" , p . ID )
77149}
78150
79151func init () {
80152 db .RegisterModel (new (ProjectWorkflow ))
81153}
82154
83- func FindWorkflowEvents (ctx context.Context , projectID int64 ) (map [WorkflowEvent ]ProjectWorkflow , error ) {
84- events := make (map [WorkflowEvent ]ProjectWorkflow )
155+ func FindWorkflowEvents (ctx context.Context , projectID int64 ) (map [WorkflowEvent ]* ProjectWorkflow , error ) {
156+ events := make (map [WorkflowEvent ]* ProjectWorkflow )
85157 if err := db .GetEngine (ctx ).Where ("project_id=?" , projectID ).Find (& events ); err != nil {
86158 return nil , err
87159 }
88- res := make (map [WorkflowEvent ]ProjectWorkflow , len (events ))
160+ res := make (map [WorkflowEvent ]* ProjectWorkflow , len (events ))
89161 for _ , event := range events {
90162 res [event .WorkflowEvent ] = event
91163 }
@@ -102,19 +174,3 @@ func GetWorkflowByID(ctx context.Context, id int64) (*ProjectWorkflow, error) {
102174 }
103175 return p , nil
104176}
105-
106- func GetWorkflows (ctx context.Context , projectID int64 ) ([]* ProjectWorkflow , error ) {
107- events := make ([]* ProjectWorkflow , 0 , 10 )
108- if err := db .GetEngine (ctx ).Where ("project_id=?" , projectID ).Find (& events ); err != nil {
109- return nil , err
110- }
111- workflows := newDefaultWorkflows ()
112- for i , defaultWorkflow := range workflows {
113- for _ , workflow := range events {
114- if workflow .WorkflowEvent == defaultWorkflow .WorkflowEvent {
115- workflows [i ] = workflow
116- }
117- }
118- }
119- return workflows , nil
120- }
0 commit comments