44package  projects
55
66import  (
7- 	stdCtx "context" 
87	"io" 
98	"net/http" 
109	"strconv" 
@@ -17,92 +16,14 @@ import (
1716	"code.gitea.io/gitea/modules/log" 
1817	"code.gitea.io/gitea/modules/templates" 
1918	"code.gitea.io/gitea/services/context" 
19+ 	project_service "code.gitea.io/gitea/services/projects" 
2020)
2121
2222var  (
2323	tmplRepoWorkflows  =  templates .TplName ("repo/projects/workflows" )
2424	tmplOrgWorkflows   =  templates .TplName ("org/projects/workflows" )
2525)
2626
27- // getFilterSummary returns a human-readable summary of the filters 
28- func  getFilterSummary (ctx  stdCtx.Context , filters  []project_model.WorkflowFilter ) string  {
29- 	if  len (filters ) ==  0  {
30- 		return  "" 
31- 	}
32- 
33- 	var  summary  strings.Builder 
34- 	labelIDs  :=  make ([]int64 , 0 )
35- 	for  _ , filter  :=  range  filters  {
36- 		switch  filter .Type  {
37- 		case  project_model .WorkflowFilterTypeIssueType :
38- 			switch  filter .Value  {
39- 			case  "issue" :
40- 				if  summary .Len () >  0  {
41- 					summary .WriteString (" " )
42- 				}
43- 				summary .WriteString ("(Issues only)" )
44- 			case  "pull_request" :
45- 				if  summary .Len () >  0  {
46- 					summary .WriteString (" " )
47- 				}
48- 				summary .WriteString ("(Pull requests only)" )
49- 			}
50- 		case  project_model .WorkflowFilterTypeSourceColumn :
51- 			columnID , _  :=  strconv .ParseInt (filter .Value , 10 , 64 )
52- 			if  columnID  <=  0  {
53- 				continue 
54- 			}
55- 			col , err  :=  project_model .GetColumn (ctx , columnID )
56- 			if  err  !=  nil  {
57- 				log .Error ("GetColumn: %v" , err )
58- 				continue 
59- 			}
60- 			if  summary .Len () >  0  {
61- 				summary .WriteString (" " )
62- 			}
63- 			summary .WriteString ("(Source: "  +  col .Title  +  ")" )
64- 		case  project_model .WorkflowFilterTypeTargetColumn :
65- 			columnID , _  :=  strconv .ParseInt (filter .Value , 10 , 64 )
66- 			if  columnID  <=  0  {
67- 				continue 
68- 			}
69- 			col , err  :=  project_model .GetColumn (ctx , columnID )
70- 			if  err  !=  nil  {
71- 				log .Error ("GetColumn: %v" , err )
72- 				continue 
73- 			}
74- 			if  summary .Len () >  0  {
75- 				summary .WriteString (" " )
76- 			}
77- 			summary .WriteString ("(Target: "  +  col .Title  +  ")" )
78- 		case  project_model .WorkflowFilterTypeLabels :
79- 			labelID , _  :=  strconv .ParseInt (filter .Value , 10 , 64 )
80- 			if  labelID  >  0  {
81- 				labelIDs  =  append (labelIDs , labelID )
82- 			}
83- 		}
84- 	}
85- 	if  len (labelIDs ) >  0  {
86- 		labels , err  :=  issues_model .GetLabelsByIDs (ctx , labelIDs )
87- 		if  err  !=  nil  {
88- 			log .Error ("GetLabelsByIDs: %v" , err )
89- 		} else  {
90- 			if  summary .Len () >  0  {
91- 				summary .WriteString (" " )
92- 			}
93- 			summary .WriteString ("(Labels: " )
94- 			for  i , label  :=  range  labels  {
95- 				summary .WriteString (label .Name )
96- 				if  i  <  len (labels )- 1  {
97- 					summary .WriteString (", " )
98- 				}
99- 			}
100- 			summary .WriteString (")" )
101- 		}
102- 	}
103- 	return  summary .String ()
104- }
105- 
10627// convertFormToFilters converts form filters to WorkflowFilter objects 
10728func  convertFormToFilters (formFilters  map [string ]any ) []project_model.WorkflowFilter  {
10829	filters  :=  make ([]project_model.WorkflowFilter , 0 )
@@ -111,7 +32,7 @@ func convertFormToFilters(formFilters map[string]any) []project_model.WorkflowFi
11132		switch  key  {
11233		case  "labels" :
11334			// Handle labels array 
114- 			if  labelInterfaces , ok  :=  value .([]interface {} ); ok  &&  len (labelInterfaces ) >  0  {
35+ 			if  labelInterfaces , ok  :=  value .([]any ); ok  &&  len (labelInterfaces ) >  0  {
11536				for  _ , labelInterface  :=  range  labelInterfaces  {
11637					if  label , ok  :=  labelInterface .(string ); ok  &&  label  !=  ""  {
11738						filters  =  append (filters , project_model.WorkflowFilter {
@@ -152,7 +73,7 @@ func convertFormToActions(formActions map[string]any) []project_model.WorkflowAc
15273				}
15374			}
15475		case  "add_labels" :
155- 			// Handle both []string and []interface{}  from JSON unmarshaling 
76+ 			// Handle both []string and []any  from JSON unmarshaling 
15677			if  labels , ok  :=  value .([]string ); ok  &&  len (labels ) >  0  {
15778				for  _ , label  :=  range  labels  {
15879					if  label  !=  ""  {
@@ -162,7 +83,7 @@ func convertFormToActions(formActions map[string]any) []project_model.WorkflowAc
16283						})
16384					}
16485				}
165- 			} else  if  labelInterfaces , ok  :=  value .([]interface {} ); ok  &&  len (labelInterfaces ) >  0  {
86+ 			} else  if  labelInterfaces , ok  :=  value .([]any ); ok  &&  len (labelInterfaces ) >  0  {
16687				for  _ , labelInterface  :=  range  labelInterfaces  {
16788					if  label , ok  :=  labelInterface .(string ); ok  &&  label  !=  ""  {
16889						actions  =  append (actions , project_model.WorkflowAction {
@@ -173,7 +94,7 @@ func convertFormToActions(formActions map[string]any) []project_model.WorkflowAc
17394				}
17495			}
17596		case  "remove_labels" :
176- 			// Handle both []string and []interface{}  from JSON unmarshaling 
97+ 			// Handle both []string and []any  from JSON unmarshaling 
17798			if  labels , ok  :=  value .([]string ); ok  &&  len (labels ) >  0  {
17899				for  _ , label  :=  range  labels  {
179100					if  label  !=  ""  {
@@ -183,7 +104,7 @@ func convertFormToActions(formActions map[string]any) []project_model.WorkflowAc
183104						})
184105					}
185106				}
186- 			} else  if  labelInterfaces , ok  :=  value .([]interface {} ); ok  &&  len (labelInterfaces ) >  0  {
107+ 			} else  if  labelInterfaces , ok  :=  value .([]any ); ok  &&  len (labelInterfaces ) >  0  {
187108				for  _ , labelInterface  :=  range  labelInterfaces  {
188109					if  label , ok  :=  labelInterface .(string ); ok  &&  label  !=  ""  {
189110						actions  =  append (actions , project_model.WorkflowAction {
@@ -243,7 +164,7 @@ func WorkflowsEvents(ctx *context.Context) {
243164		Capabilities   project_model.WorkflowEventCapabilities  `json:"capabilities"` 
244165		Filters        []project_model.WorkflowFilter           `json:"filters"` 
245166		Actions        []project_model.WorkflowAction           `json:"actions"` 
246- 		FilterSummary   string                                   `json:"filter_summary "`  // Human readable filter description 
167+ 		Summary         string                                   `json:"summary "`  // Human readable filter description 
247168		Enabled        bool                                     `json:"enabled"` 
248169		IsConfigured   bool                                     `json:"isConfigured"`  // Whether this workflow is configured/saved 
249170	}
@@ -263,7 +184,7 @@ func WorkflowsEvents(ctx *context.Context) {
263184		if  len (existingWorkflows ) >  0  {
264185			// Add all existing workflows for this event 
265186			for  _ , wf  :=  range  existingWorkflows  {
266- 				filterSummary  :=  getFilterSummary (ctx , wf . WorkflowFilters )
187+ 				workflowSummary  :=  project_service . GetWorkflowSummary (ctx , wf )
267188				outputWorkflows  =  append (outputWorkflows , & WorkflowConfig {
268189					ID :            wf .ID ,
269190					EventID :       strconv .FormatInt (wf .ID , 10 ),
@@ -272,7 +193,7 @@ func WorkflowsEvents(ctx *context.Context) {
272193					Capabilities :  capabilities [event ],
273194					Filters :       wf .WorkflowFilters ,
274195					Actions :       wf .WorkflowActions ,
275- 					FilterSummary :  filterSummary ,
196+ 					Summary :        workflowSummary ,
276197					Enabled :       wf .Enabled ,
277198					IsConfigured :  true ,
278199				})
@@ -285,7 +206,7 @@ func WorkflowsEvents(ctx *context.Context) {
285206				DisplayName :   string (ctx .Tr (event .LangKey ())),
286207				WorkflowEvent : string (event ),
287208				Capabilities :  capabilities [event ],
288- 				FilterSummary :  "" ,
209+ 				Summary :        "" ,
289210				Enabled :       true , // Default to enabled for new workflows 
290211				IsConfigured :  false ,
291212			})
@@ -537,17 +458,17 @@ func WorkflowsPost(ctx *context.Context) {
537458		}
538459
539460		// Return the newly created workflow with filter summary 
540- 		filterSummary  :=  getFilterSummary (ctx , wf . WorkflowFilters )
461+ 		workflowSummary  :=  project_service . GetWorkflowSummary (ctx , wf )
541462		ctx .JSON (http .StatusOK , map [string ]any {
542463			"success" : true ,
543464			"workflow" : map [string ]any {
544- 				"id" :              wf .ID ,
545- 				"event_id" :        strconv .FormatInt (wf .ID , 10 ),
546- 				"display_name" :    string (ctx .Tr (wf .WorkflowEvent .LangKey ())),
547- 				"filters" :         wf .WorkflowFilters ,
548- 				"actions" :         wf .WorkflowActions ,
549- 				"filter_summary " : filterSummary ,
550- 				"enabled" :         wf .Enabled ,
465+ 				"id" :           wf .ID ,
466+ 				"event_id" :     strconv .FormatInt (wf .ID , 10 ),
467+ 				"display_name" : string (ctx .Tr (wf .WorkflowEvent .LangKey ())),
468+ 				"filters" :      wf .WorkflowFilters ,
469+ 				"actions" :      wf .WorkflowActions ,
470+ 				"summary " :       workflowSummary ,
471+ 				"enabled" :      wf .Enabled ,
551472			},
552473		})
553474	} else  {
@@ -570,17 +491,17 @@ func WorkflowsPost(ctx *context.Context) {
570491		}
571492
572493		// Return the updated workflow with filter summary 
573- 		filterSummary  :=  getFilterSummary (ctx , wf . WorkflowFilters )
494+ 		workflowSummary  :=  project_service . GetWorkflowSummary (ctx , wf )
574495		ctx .JSON (http .StatusOK , map [string ]any {
575496			"success" : true ,
576497			"workflow" : map [string ]any {
577- 				"id" :              wf .ID ,
578- 				"event_id" :        strconv .FormatInt (wf .ID , 10 ),
579- 				"display_name" :    string (ctx .Tr (wf .WorkflowEvent .LangKey ()))  +   filterSummary ,
580- 				"filters" :         wf .WorkflowFilters ,
581- 				"actions" :         wf .WorkflowActions ,
582- 				"filter_summary " : filterSummary ,
583- 				"enabled" :         wf .Enabled ,
498+ 				"id" :           wf .ID ,
499+ 				"event_id" :     strconv .FormatInt (wf .ID , 10 ),
500+ 				"display_name" : string (ctx .Tr (wf .WorkflowEvent .LangKey ())),
501+ 				"filters" :      wf .WorkflowFilters ,
502+ 				"actions" :      wf .WorkflowActions ,
503+ 				"summary " :       workflowSummary ,
504+ 				"enabled" :      wf .Enabled ,
584505			},
585506		})
586507	}
0 commit comments