Skip to content

Commit 3719626

Browse files
committed
improvements
1 parent ae28239 commit 3719626

File tree

5 files changed

+133
-111
lines changed

5 files changed

+133
-111
lines changed

models/user/user_system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const (
7171
ProjectWorkflowsUserEmail = "[email protected]"
7272
)
7373

74-
func IsGiteaWorkflowsUserName(name string) bool {
74+
func IsProjectWorkflowsUserName(name string) bool {
7575
return strings.EqualFold(name, ProjectWorkflowsUserName)
7676
}
7777

@@ -103,7 +103,7 @@ func GetSystemUserByName(name string) *User {
103103
if IsGiteaActionsUserName(name) {
104104
return NewActionsUser()
105105
}
106-
if IsGiteaWorkflowsUserName(name) {
106+
if IsProjectWorkflowsUserName(name) {
107107
return NewProjectWorkflowsUser()
108108
}
109109
return nil

models/user/user_system_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ func TestSystemUser(t *testing.T) {
2525
assert.True(t, u.IsGiteaActions())
2626
assert.True(t, IsGiteaActionsUserName("Gitea-actionS"))
2727

28-
_, err = GetPossibleUserByID(t.Context(), -3)
28+
u, err = GetPossibleUserByID(t.Context(), -3)
29+
require.NoError(t, err)
30+
assert.Equal(t, "project-workflows", u.Name)
31+
assert.Equal(t, "project-workflows", u.LowerName)
32+
assert.True(t, u.IsProjectWorkflows())
33+
assert.True(t, IsProjectWorkflowsUserName("Project-Workflows"))
34+
35+
_, err = GetPossibleUserByID(t.Context(), -4)
2936
require.Error(t, err)
3037
}

routers/web/projects/workflows.go

Lines changed: 26 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package projects
55

66
import (
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

2222
var (
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
10728
func 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
}

services/projects/workflow.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package projects
5+
6+
import (
7+
"context"
8+
"strconv"
9+
"strings"
10+
11+
issues_model "code.gitea.io/gitea/models/issues"
12+
project_model "code.gitea.io/gitea/models/project"
13+
"code.gitea.io/gitea/modules/log"
14+
)
15+
16+
// GetWorkflowSummary returns a human-readable summary of the workflow
17+
func GetWorkflowSummary(ctx context.Context, wf *project_model.Workflow) string {
18+
filters := wf.WorkflowFilters
19+
if len(filters) == 0 {
20+
return ""
21+
}
22+
23+
var summary strings.Builder
24+
labelIDs := make([]int64, 0)
25+
for _, filter := range filters {
26+
switch filter.Type {
27+
case project_model.WorkflowFilterTypeIssueType:
28+
switch filter.Value {
29+
case "issue":
30+
if summary.Len() > 0 {
31+
summary.WriteString(" ")
32+
}
33+
summary.WriteString("(Issues only)")
34+
case "pull_request":
35+
if summary.Len() > 0 {
36+
summary.WriteString(" ")
37+
}
38+
summary.WriteString("(Pull requests only)")
39+
}
40+
case project_model.WorkflowFilterTypeSourceColumn:
41+
columnID, _ := strconv.ParseInt(filter.Value, 10, 64)
42+
if columnID <= 0 {
43+
continue
44+
}
45+
col, err := project_model.GetColumn(ctx, columnID)
46+
if err != nil {
47+
log.Error("GetColumn: %v", err)
48+
continue
49+
}
50+
if summary.Len() > 0 {
51+
summary.WriteString(" ")
52+
}
53+
summary.WriteString("(Source: " + col.Title + ")")
54+
case project_model.WorkflowFilterTypeTargetColumn:
55+
columnID, _ := strconv.ParseInt(filter.Value, 10, 64)
56+
if columnID <= 0 {
57+
continue
58+
}
59+
col, err := project_model.GetColumn(ctx, columnID)
60+
if err != nil {
61+
log.Error("GetColumn: %v", err)
62+
continue
63+
}
64+
if summary.Len() > 0 {
65+
summary.WriteString(" ")
66+
}
67+
summary.WriteString("(Target: " + col.Title + ")")
68+
case project_model.WorkflowFilterTypeLabels:
69+
labelID, _ := strconv.ParseInt(filter.Value, 10, 64)
70+
if labelID > 0 {
71+
labelIDs = append(labelIDs, labelID)
72+
}
73+
}
74+
}
75+
if len(labelIDs) > 0 {
76+
labels, err := issues_model.GetLabelsByIDs(ctx, labelIDs)
77+
if err != nil {
78+
log.Error("GetLabelsByIDs: %v", err)
79+
} else {
80+
if summary.Len() > 0 {
81+
summary.WriteString(" ")
82+
}
83+
summary.WriteString("(Labels: ")
84+
for i, label := range labels {
85+
summary.WriteString(label.Name)
86+
if i < len(labels)-1 {
87+
summary.WriteString(", ")
88+
}
89+
}
90+
summary.WriteString(")")
91+
}
92+
}
93+
return summary.String()
94+
}

web_src/js/components/projects/ProjectWorkflow.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ const createNewWorkflow = (eventType: any, capabilities: any, displayName: any)
351351
capabilities,
352352
filters: [] as any[],
353353
actions: [] as any[],
354-
filter_summary: '',
354+
summary: '',
355355
workflow_event: eventType,
356356
enabled: true, // Ensure new workflows are enabled by default
357357
};
@@ -683,8 +683,8 @@ onUnmounted(() => {
683683
<div class="workflow-title">
684684
{{ getWorkflowDisplayName(item, index) }}
685685
</div>
686-
<div v-if="item.filter_summary" class="workflow-subtitle">
687-
{{ item.filter_summary }}
686+
<div v-if="item.summary" class="workflow-subtitle">
687+
{{ item.summary }}
688688
</div>
689689
</div>
690690
</div>

0 commit comments

Comments
 (0)