relay-flow 0.1.0-alpha.0 → 0.2.1-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +160 -143
- package/cmd/relay-flow/commands_test.go +968 -0
- package/cmd/relay-flow/main.go +877 -175
- package/cmd/relay-flow/scenario_test.go +1310 -0
- package/cmd/relay-flow/serve.go +610 -0
- package/examples/default-story-workflow.yaml +88 -0
- package/go.mod +69 -2
- package/go.sum +185 -0
- package/internal/config/config.go +88 -0
- package/internal/config/machine.go +99 -48
- package/internal/config/machine_test.go +248 -0
- package/internal/config/merge_test.go +118 -0
- package/internal/config/writeatomic.go +36 -0
- package/internal/config/writeatomic_test.go +98 -0
- package/internal/execution/goworkflows/activities.go +490 -0
- package/internal/execution/goworkflows/engine.go +520 -0
- package/internal/execution/goworkflows/engine_test.go +660 -0
- package/internal/execution/goworkflows/fakes_test.go +509 -0
- package/internal/execution/goworkflows/interpreter.go +613 -0
- package/internal/execution/goworkflows/logging_test.go +154 -0
- package/internal/execution/goworkflows/mailbox_test.go +423 -0
- package/internal/execution/goworkflows/node_runtime_integration_test.go +133 -0
- package/internal/execution/goworkflows/node_runtime_test.go +510 -0
- package/internal/execution/goworkflows/projection.go +504 -0
- package/internal/execution/goworkflows/recovery_test.go +1092 -0
- package/internal/execution/goworkflows/retry_log_test.go +59 -0
- package/internal/execution/goworkflows/retry_projection_test.go +98 -0
- package/internal/harness/contract_test.go +174 -0
- package/internal/harness/factory.go +63 -0
- package/internal/harness/harness.go +41 -0
- package/internal/harness/opencode/opencode.go +166 -0
- package/internal/harness/opencode/opencode_test.go +50 -0
- package/internal/harness/plugin_selection_test.go +126 -0
- package/internal/identity/identity.go +37 -0
- package/internal/logging/logging.go +56 -0
- package/internal/logging/logging_test.go +116 -0
- package/internal/paths/paths.go +69 -0
- package/internal/recover/recover.go +115 -0
- package/internal/repo/poller.go +186 -0
- package/internal/repo/poller_test.go +327 -0
- package/internal/repo/repo.go +132 -0
- package/internal/repo/service.go +216 -0
- package/internal/repo/service_test.go +298 -0
- package/internal/retry/retry.go +118 -0
- package/internal/router/router.go +84 -0
- package/internal/router/router_test.go +231 -0
- package/internal/run/manager.go +121 -0
- package/internal/run/run.go +140 -0
- package/internal/run/run_identity_test.go +52 -0
- package/internal/run/run_manager_test.go +286 -0
- package/internal/runner/contract_test.go +259 -0
- package/internal/runner/factory.go +65 -0
- package/internal/runner/orca/orca.go +341 -172
- package/internal/runner/orca/orca_test.go +256 -143
- package/internal/runner/orca/orcacli/orcacli.go +220 -0
- package/internal/runner/orca/orcacli/orcacli_test.go +157 -0
- package/internal/runner/orca/orcacli/testdata/repo-list.json +18 -0
- package/internal/runner/orca/orcacli/testdata/strict-orca.sh +32 -0
- package/internal/runner/orca/orcacli/testdata/terminal-close.json +12 -0
- package/internal/runner/orca/orcacli/testdata/terminal-create.json +18 -0
- package/internal/runner/orca/orcacli/testdata/terminal-list.json +51 -0
- package/internal/runner/orca/orcacli/testdata/terminal-send.json +1 -0
- package/internal/runner/orca/orcacli/testdata/terminal-show.json +1 -0
- package/internal/runner/orca/orcacli/testdata/worktree-create.json +22 -0
- package/internal/runner/orca/orcacli/testdata/worktree-list.json +31 -0
- package/internal/runner/orca/orcacli/testdata/worktree-remove.json +6 -0
- package/internal/runner/runner.go +54 -64
- package/internal/server/api_test.go +300 -0
- package/internal/server/client.go +192 -74
- package/internal/server/fixture_test.go +248 -0
- package/internal/server/server.go +425 -248
- package/internal/server/shutdown_test.go +116 -0
- package/internal/task/auth_test.go +48 -0
- package/internal/task/contract_test.go +225 -0
- package/internal/task/factory.go +119 -0
- package/internal/task/jira/auth.go +183 -0
- package/internal/task/jira/auth_test.go +107 -0
- package/internal/task/jira/effects_test.go +39 -0
- package/internal/task/jira/filters_test.go +254 -0
- package/internal/task/jira/helpers_test.go +70 -0
- package/internal/task/jira/jira.go +538 -0
- package/internal/task/jira/normalize.go +119 -0
- package/internal/task/jira/rest/adf.go +128 -0
- package/internal/task/jira/rest/client.go +573 -0
- package/internal/task/jira/rest/client_test.go +381 -0
- package/internal/task/jira/testdata/jira_search_issues.json +120 -0
- package/internal/task/jira/transition_defaults_test.go +158 -0
- package/internal/task/jira/validation_test.go +94 -0
- package/internal/task/task.go +84 -0
- package/internal/workflow/report.go +85 -0
- package/internal/workflow/report_test.go +259 -0
- package/internal/workflow/service.go +142 -0
- package/internal/workflow/store.go +136 -0
- package/internal/workflow/store_test.go +282 -0
- package/internal/workflow/workflow.go +345 -0
- package/internal/workflow/workflow_test.go +412 -0
- package/package.json +7 -2
- package/internal/acli/acli.go +0 -229
- package/internal/config/demo_test.go +0 -17
- package/internal/config/schema.go +0 -193
- package/internal/config/schema_test.go +0 -162
- package/internal/daemon/daemon.go +0 -218
- package/internal/daemon/daemon_test.go +0 -204
- package/internal/discovery/discovery.go +0 -122
- package/internal/discovery/discovery_test.go +0 -62
- package/internal/opencode/opencode.go +0 -26
- package/internal/orcacli/orcacli.go +0 -264
- package/internal/runner/orca/README.md +0 -64
- package/internal/runner/runner_test.go +0 -64
- package/internal/server/server_test.go +0 -195
- package/internal/tasks/jira/README.md +0 -69
- package/internal/tasks/jira/component_test.go +0 -16
- package/internal/tasks/jira/decode.go +0 -24
- package/internal/tasks/jira/jira.go +0 -231
- package/internal/tasks/jira/jira_test.go +0 -259
- package/internal/tasks/jira/jql_test.go +0 -16
- package/internal/tasks/tasks.go +0 -90
- package/internal/tasks/tasks_test.go +0 -91
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
// Package jira is the Jira task-system adapter. It owns one typed Config
|
|
2
|
+
// spanning root, repo, workflow, and node scopes; core never imports it.
|
|
3
|
+
package jira
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"context"
|
|
7
|
+
"fmt"
|
|
8
|
+
"log/slog"
|
|
9
|
+
"sort"
|
|
10
|
+
"strings"
|
|
11
|
+
"sync"
|
|
12
|
+
|
|
13
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
14
|
+
"github.com/rajpopat27/relay-flow/internal/retry"
|
|
15
|
+
"github.com/rajpopat27/relay-flow/internal/task"
|
|
16
|
+
jirarest "github.com/rajpopat27/relay-flow/internal/task/jira/rest"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// Config is the adapter-owned typed config for every scope.
|
|
20
|
+
type Config struct {
|
|
21
|
+
Assignee string `yaml:"assignee,omitempty"`
|
|
22
|
+
Project string `yaml:"project,omitempty"`
|
|
23
|
+
Component string `yaml:"component,omitempty"`
|
|
24
|
+
Filters Filters `yaml:"filters,omitempty"`
|
|
25
|
+
Transition TransitionTo `yaml:"transitionTo,omitempty"`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Filters are the structured, locally evaluable workflow ticket matchers.
|
|
29
|
+
type Filters struct {
|
|
30
|
+
ParentStatuses []string `yaml:"parentStatuses,omitempty"`
|
|
31
|
+
IssueTypes []string `yaml:"issueTypes,omitempty"`
|
|
32
|
+
Labels []string `yaml:"labels,omitempty"`
|
|
33
|
+
Assignees []string `yaml:"assignees,omitempty"`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// TransitionTo carries parent/mailbox status transitions for a node.
|
|
37
|
+
type TransitionTo struct {
|
|
38
|
+
ParentStatus string `yaml:"parentStatus,omitempty"`
|
|
39
|
+
TaskStatus string `yaml:"taskStatus,omitempty"`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Deterministic transition defaults for omitted values.
|
|
43
|
+
const (
|
|
44
|
+
defaultStartParentStatus = "In Progress"
|
|
45
|
+
defaultWorkTaskStatus = "In Progress"
|
|
46
|
+
defaultEndParentStatus = "Done"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
var (
|
|
50
|
+
clientsMu sync.Mutex
|
|
51
|
+
clients = map[string]struct {
|
|
52
|
+
token string
|
|
53
|
+
client *jirarest.HTTPClient
|
|
54
|
+
}{}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
// claimLabel is the permanent workflow claim label.
|
|
58
|
+
func claimLabel(workflow string) string { return "wf:" + workflow }
|
|
59
|
+
|
|
60
|
+
func init() {
|
|
61
|
+
task.Register("jira", task.Factory{
|
|
62
|
+
RequiredRepoKeys: func() []string { return []string{"project", "component"} },
|
|
63
|
+
TaskScopeKey: func(rootConfig, repoConfig config.RawValues) (string, error) {
|
|
64
|
+
var root, repoCfg Config
|
|
65
|
+
if err := config.DecodeStrict(rootConfig, &root); err != nil {
|
|
66
|
+
return "", fmt.Errorf("root task config: %w", err)
|
|
67
|
+
}
|
|
68
|
+
if err := config.DecodeStrict(repoConfig, &repoCfg); err != nil {
|
|
69
|
+
return "", fmt.Errorf("repo task config: %w", err)
|
|
70
|
+
}
|
|
71
|
+
proj := repoCfg.Project
|
|
72
|
+
comp := repoCfg.Component
|
|
73
|
+
if proj == "" || comp == "" {
|
|
74
|
+
return "", fmt.Errorf("jira task scope requires repo project and component")
|
|
75
|
+
}
|
|
76
|
+
creds, err := loadCredentialsDefault()
|
|
77
|
+
if err != nil {
|
|
78
|
+
return "", fmt.Errorf("jira credentials: %w", err)
|
|
79
|
+
}
|
|
80
|
+
return strings.Join([]string{creds.Site, proj, comp}, "/"), nil
|
|
81
|
+
},
|
|
82
|
+
Auth: auth,
|
|
83
|
+
New: func(ctx context.Context, spec task.RepoSpec) (task.System, error) {
|
|
84
|
+
merged := config.Merge(spec.RootConfig, spec.RepoConfig)
|
|
85
|
+
var cfg Config
|
|
86
|
+
if err := config.DecodeStrict(merged, &cfg); err != nil {
|
|
87
|
+
return nil, fmt.Errorf("jira repo %q config: %w", spec.Name, err)
|
|
88
|
+
}
|
|
89
|
+
creds, err := loadCredentialsDefault()
|
|
90
|
+
if err != nil {
|
|
91
|
+
return nil, fmt.Errorf("jira credentials: %w", err)
|
|
92
|
+
}
|
|
93
|
+
client, err := sharedClient(creds.Site, creds.Email, creds.Token)
|
|
94
|
+
if err != nil {
|
|
95
|
+
return nil, err
|
|
96
|
+
}
|
|
97
|
+
return newSystem(ctx, client, spec)
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func sharedClient(site, email, token string) (*jirarest.HTTPClient, error) {
|
|
103
|
+
key := strings.TrimRight(site, "/") + "\x00" + email
|
|
104
|
+
clientsMu.Lock()
|
|
105
|
+
defer clientsMu.Unlock()
|
|
106
|
+
if cached := clients[key]; cached.client != nil && cached.token == token {
|
|
107
|
+
return cached.client, nil
|
|
108
|
+
}
|
|
109
|
+
client, err := jirarest.New(site, email, token)
|
|
110
|
+
if err != nil {
|
|
111
|
+
return nil, err
|
|
112
|
+
}
|
|
113
|
+
clients[key] = struct {
|
|
114
|
+
token string
|
|
115
|
+
client *jirarest.HTTPClient
|
|
116
|
+
}{token: token, client: client}
|
|
117
|
+
return client, nil
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// system is the repo-bound Jira task.System. It is safe for concurrent use;
|
|
121
|
+
// the REST client owns connection reuse, caches, and request limiting.
|
|
122
|
+
type system struct {
|
|
123
|
+
cli jirarest.Client
|
|
124
|
+
repoName string
|
|
125
|
+
base config.RawValues
|
|
126
|
+
effective Config // root+repo merged
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
func newSystem(ctx context.Context, cli jirarest.Client, spec task.RepoSpec) (*system, error) {
|
|
130
|
+
if spec.Name == "" {
|
|
131
|
+
return nil, fmt.Errorf("jira: repo name is required")
|
|
132
|
+
}
|
|
133
|
+
merged := config.Merge(spec.RootConfig, spec.RepoConfig)
|
|
134
|
+
var cfg Config
|
|
135
|
+
if err := config.DecodeStrict(merged, &cfg); err != nil {
|
|
136
|
+
return nil, fmt.Errorf("jira repo %q config: %w", spec.Name, err)
|
|
137
|
+
}
|
|
138
|
+
if cfg.Assignee != "" {
|
|
139
|
+
if err := cli.ValidateAssignee(ctx, cfg.Project, cfg.Assignee); err != nil {
|
|
140
|
+
return nil, fmt.Errorf("jira repo %q assignee %q: %w", spec.Name, cfg.Assignee, err)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
s := &system{cli: cli, repoName: spec.Name, base: merged, effective: cfg}
|
|
144
|
+
if err := s.validateTransition(ctx, "repo config", cfg.Project, cfg.Transition); err != nil {
|
|
145
|
+
return nil, fmt.Errorf("jira repo %q: %w", spec.Name, err)
|
|
146
|
+
}
|
|
147
|
+
for _, status := range []string{defaultStartParentStatus, defaultEndParentStatus, "To Do"} {
|
|
148
|
+
if err := cli.ValidateStatus(ctx, cfg.Project, status); err != nil {
|
|
149
|
+
return nil, fmt.Errorf("jira repo %q default status %q: %w", spec.Name, status, err)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// project/component are required repo keys enforced at registration
|
|
153
|
+
// (RequiredRepoKeys); construction also probes external Jira names.
|
|
154
|
+
return s, nil
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// newSystemForCLI constructs a system around an explicit Jira client seam.
|
|
158
|
+
func newSystemForCLI(cli jirarest.Client) (task.System, error) {
|
|
159
|
+
return newSystem(context.Background(), cli, task.RepoSpec{
|
|
160
|
+
Name: "payments",
|
|
161
|
+
RepoConfig: config.RawValues{"project": "PAY", "component": "api"},
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// decodeConfig strictly decodes one operation's effective raw values.
|
|
166
|
+
func decodeConfig(raw config.RawValues) (Config, error) {
|
|
167
|
+
var cfg Config
|
|
168
|
+
if err := config.DecodeStrict(raw, &cfg); err != nil {
|
|
169
|
+
return Config{}, err
|
|
170
|
+
}
|
|
171
|
+
return cfg, nil
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// --- Poll / filters ---
|
|
175
|
+
|
|
176
|
+
// buildJQL scopes the parent poll by project/component and active statuses.
|
|
177
|
+
func (s *system) buildJQL() string {
|
|
178
|
+
parts := []string{
|
|
179
|
+
fmt.Sprintf("project = %s", s.effective.Project),
|
|
180
|
+
fmt.Sprintf("component = %q", s.effective.Component),
|
|
181
|
+
"issuetype != Subtask",
|
|
182
|
+
"statusCategory != Done",
|
|
183
|
+
}
|
|
184
|
+
return strings.Join(parts, " AND ")
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Poll returns active parent tickets only; mailbox subtasks are never
|
|
188
|
+
// returned as candidates.
|
|
189
|
+
func (s *system) Poll(ctx context.Context) ([]task.Ticket, error) {
|
|
190
|
+
if s.effective.Project == "" || s.effective.Component == "" {
|
|
191
|
+
return nil, fmt.Errorf("jira repo %q: project and component are required to poll", s.repoName)
|
|
192
|
+
}
|
|
193
|
+
jql := s.buildJQL()
|
|
194
|
+
slog.Debug("jira poll", "repo", s.repoName, "jql", jql)
|
|
195
|
+
raw, err := s.cli.Search(ctx, jql)
|
|
196
|
+
if err != nil {
|
|
197
|
+
return nil, err
|
|
198
|
+
}
|
|
199
|
+
tickets, err := normalizeSearchResponse(raw)
|
|
200
|
+
if err != nil {
|
|
201
|
+
return nil, err
|
|
202
|
+
}
|
|
203
|
+
for _, t := range tickets {
|
|
204
|
+
slog.Debug("jira ticket",
|
|
205
|
+
"repo", s.repoName, "ticket", t.Key, "id", t.ID,
|
|
206
|
+
"title", t.Title, "claims", strings.Join(t.WorkflowClaims, ","),
|
|
207
|
+
"fields", fmt.Sprint(t.Fields))
|
|
208
|
+
}
|
|
209
|
+
return tickets, nil
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// CompileFilter compiles workflow taskConfig.filters into an in-memory
|
|
213
|
+
// matcher over normalized ticket fields. Unknown filter fields are rejected.
|
|
214
|
+
func (s *system) CompileFilter(workflowTaskConfig config.RawValues) (func(task.Ticket) bool, error) {
|
|
215
|
+
cfg, err := decodeConfig(workflowTaskConfig)
|
|
216
|
+
if err != nil {
|
|
217
|
+
return nil, err
|
|
218
|
+
}
|
|
219
|
+
f := cfg.Filters
|
|
220
|
+
return func(t task.Ticket) bool {
|
|
221
|
+
if len(f.ParentStatuses) > 0 && !contains(f.ParentStatuses, strField(t.Fields, "status")) {
|
|
222
|
+
return false
|
|
223
|
+
}
|
|
224
|
+
if len(f.IssueTypes) > 0 && !contains(f.IssueTypes, strField(t.Fields, "issueType")) {
|
|
225
|
+
return false
|
|
226
|
+
}
|
|
227
|
+
if len(f.Labels) > 0 {
|
|
228
|
+
ticketLabels := strSliceField(t.Fields, "labels")
|
|
229
|
+
for _, want := range f.Labels {
|
|
230
|
+
if !contains(ticketLabels, want) {
|
|
231
|
+
return false
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if len(f.Assignees) > 0 && !contains(f.Assignees, strField(t.Fields, "assignee")) {
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
return true
|
|
239
|
+
}, nil
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// --- Claim ---
|
|
243
|
+
|
|
244
|
+
// Claim adds wf:<workflow> using the claims already inspected by routing.
|
|
245
|
+
// Jira's label-add operation is idempotent.
|
|
246
|
+
func (s *system) Claim(ctx context.Context, ticket task.TicketRef, workflow string) error {
|
|
247
|
+
return s.cli.EnsureLabel(ctx, ticket.Key, claimLabel(workflow))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// --- Config validation ---
|
|
251
|
+
|
|
252
|
+
// ValidateConfig strictly validates the workflow and every node task config
|
|
253
|
+
// against the adapter-owned schema for this repo. It never mutates the
|
|
254
|
+
// caller's maps.
|
|
255
|
+
func (s *system) ValidateConfig(ctx context.Context, workflowTaskConfig config.RawValues, nodeTaskConfigs map[string]config.RawValues) error {
|
|
256
|
+
workflowCfg, err := decodeConfig(config.Merge(s.base, workflowTaskConfig))
|
|
257
|
+
if err != nil {
|
|
258
|
+
return fmt.Errorf("workflow taskConfig: %w", err)
|
|
259
|
+
}
|
|
260
|
+
if err := s.validateAssignee(ctx, "workflow", workflowCfg.Project, workflowCfg.Assignee); err != nil {
|
|
261
|
+
return err
|
|
262
|
+
}
|
|
263
|
+
if err := s.validateTransition(ctx, "workflow", workflowCfg.Project, workflowCfg.Transition); err != nil {
|
|
264
|
+
return err
|
|
265
|
+
}
|
|
266
|
+
nodes := make([]string, 0, len(nodeTaskConfigs))
|
|
267
|
+
for n := range nodeTaskConfigs {
|
|
268
|
+
nodes = append(nodes, n)
|
|
269
|
+
}
|
|
270
|
+
sort.Strings(nodes)
|
|
271
|
+
for _, n := range nodes {
|
|
272
|
+
cfg, err := decodeConfig(config.Merge(s.base, workflowTaskConfig, nodeTaskConfigs[n]))
|
|
273
|
+
if err != nil {
|
|
274
|
+
return fmt.Errorf("node %q taskConfig: %w", n, err)
|
|
275
|
+
}
|
|
276
|
+
if err := s.validateAssignee(ctx, fmt.Sprintf("node %q", n), cfg.Project, cfg.Assignee); err != nil {
|
|
277
|
+
return err
|
|
278
|
+
}
|
|
279
|
+
if err := s.validateTransition(ctx, fmt.Sprintf("node %q", n), cfg.Project, cfg.Transition); err != nil {
|
|
280
|
+
return err
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return nil
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
func (s *system) validateAssignee(ctx context.Context, scope, project, assignee string) error {
|
|
287
|
+
if assignee == "" {
|
|
288
|
+
return nil
|
|
289
|
+
}
|
|
290
|
+
if err := s.cli.ValidateAssignee(ctx, project, assignee); err != nil {
|
|
291
|
+
return fmt.Errorf("%s assignee %q: %w", scope, assignee, err)
|
|
292
|
+
}
|
|
293
|
+
return nil
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
func (s *system) validateTransition(ctx context.Context, scope, project string, transition TransitionTo) error {
|
|
297
|
+
for _, candidate := range []struct {
|
|
298
|
+
field string
|
|
299
|
+
status string
|
|
300
|
+
}{
|
|
301
|
+
{field: "parentStatus", status: transition.ParentStatus},
|
|
302
|
+
{field: "taskStatus", status: transition.TaskStatus},
|
|
303
|
+
} {
|
|
304
|
+
field, status := candidate.field, candidate.status
|
|
305
|
+
if status == "" {
|
|
306
|
+
continue
|
|
307
|
+
}
|
|
308
|
+
if err := s.cli.ValidateStatus(ctx, project, status); err != nil {
|
|
309
|
+
return fmt.Errorf("%s %s %q: %w", scope, field, status, err)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return nil
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// LifecycleDefaults exposure: the deterministic Jira transition defaults per
|
|
316
|
+
// lifecycle point (spec: Jira transition defaults are deterministic). Run
|
|
317
|
+
// orchestration merges these raw values under the effective node config
|
|
318
|
+
// before ApplyTaskConfig; omitted values inherit the default, explicit
|
|
319
|
+
// values win (map merge with defaults as the lower layer).
|
|
320
|
+
|
|
321
|
+
// StartDefaults defaults the parent to In Progress.
|
|
322
|
+
func (s *system) StartDefaults() config.RawValues {
|
|
323
|
+
return config.RawValues{"transitionTo": map[string]any{"parentStatus": defaultStartParentStatus}}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// WorkDefaults defaults the mailbox task status to In Progress; the parent
|
|
327
|
+
// is left unchanged when parentStatus is omitted.
|
|
328
|
+
func (s *system) WorkDefaults() config.RawValues {
|
|
329
|
+
return config.RawValues{"transitionTo": map[string]any{"taskStatus": defaultWorkTaskStatus}}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// EndDefaults defaults the parent to Done.
|
|
333
|
+
func (s *system) EndDefaults() config.RawValues {
|
|
334
|
+
return config.RawValues{"transitionTo": map[string]any{"parentStatus": defaultEndParentStatus}}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// endConfig applies the deterministic end default: omitted parentStatus
|
|
338
|
+
// becomes Done.
|
|
339
|
+
func endConfig(cfg config.RawValues) config.RawValues {
|
|
340
|
+
return withTransitionDefault(cfg, "parentStatus", defaultEndParentStatus)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
func withTransitionDefault(cfg config.RawValues, key, value string) config.RawValues {
|
|
344
|
+
out := config.RawValues{}
|
|
345
|
+
for k, v := range cfg {
|
|
346
|
+
out[k] = v
|
|
347
|
+
}
|
|
348
|
+
merged := map[string]any{}
|
|
349
|
+
if tr, ok := out["transitionTo"].(map[string]any); ok {
|
|
350
|
+
for k, v := range tr {
|
|
351
|
+
merged[k] = v
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if _, ok := merged[key]; !ok {
|
|
355
|
+
merged[key] = value
|
|
356
|
+
}
|
|
357
|
+
out["transitionTo"] = merged
|
|
358
|
+
return out
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// --- Mailboxes ---
|
|
362
|
+
|
|
363
|
+
// EnsureMailboxes finds existing child mailboxes by parent and title
|
|
364
|
+
// (<ticket>:<node>), creates only missing ones with the workflow label, and
|
|
365
|
+
// returns the complete node-to-mailbox map.
|
|
366
|
+
func (s *system) EnsureMailboxes(ctx context.Context, parent task.TicketRef, workflow string, specs []task.MailboxSpec) (map[string]task.Mailbox, error) {
|
|
367
|
+
raw, err := s.cli.View(ctx, parent.Key)
|
|
368
|
+
if err != nil {
|
|
369
|
+
return nil, err
|
|
370
|
+
}
|
|
371
|
+
existing, err := subtasksOf(raw)
|
|
372
|
+
if err != nil {
|
|
373
|
+
return nil, err
|
|
374
|
+
}
|
|
375
|
+
out := map[string]task.Mailbox{}
|
|
376
|
+
missing := make([]task.MailboxSpec, 0)
|
|
377
|
+
for _, spec := range specs {
|
|
378
|
+
if mb, ok := existing[spec.Title]; ok {
|
|
379
|
+
if err := s.cli.UpdateMailbox(ctx, mb.Key, spec.Description, claimLabel(workflow)); err != nil {
|
|
380
|
+
return nil, fmt.Errorf("reconcile mailbox %q: %w", mb.Key, err)
|
|
381
|
+
}
|
|
382
|
+
mb.Node = spec.Node
|
|
383
|
+
out[spec.Node] = mb
|
|
384
|
+
continue
|
|
385
|
+
}
|
|
386
|
+
missing = append(missing, spec)
|
|
387
|
+
}
|
|
388
|
+
if len(missing) == 0 {
|
|
389
|
+
return out, nil
|
|
390
|
+
}
|
|
391
|
+
createSpecs := make([]jirarest.SubtaskSpec, 0, len(missing))
|
|
392
|
+
for _, spec := range missing {
|
|
393
|
+
createSpecs = append(createSpecs, jirarest.SubtaskSpec{Title: spec.Title, Description: spec.Description})
|
|
394
|
+
}
|
|
395
|
+
created, err := s.cli.CreateSubtasks(ctx, parent.Key, s.effective.Project, claimLabel(workflow), createSpecs)
|
|
396
|
+
if err != nil {
|
|
397
|
+
return nil, fmt.Errorf("create mailboxes: %w", err)
|
|
398
|
+
}
|
|
399
|
+
if len(created) != len(missing) {
|
|
400
|
+
return nil, fmt.Errorf("create mailboxes: created %d of %d", len(created), len(missing))
|
|
401
|
+
}
|
|
402
|
+
for i, mailbox := range created {
|
|
403
|
+
spec := missing[i]
|
|
404
|
+
if mailbox.ID == "" || mailbox.Key == "" {
|
|
405
|
+
return nil, fmt.Errorf("create mailbox %q: Jira returned no id/key", spec.Title)
|
|
406
|
+
}
|
|
407
|
+
out[spec.Node] = task.Mailbox{ID: mailbox.ID, Key: mailbox.Key, Node: spec.Node}
|
|
408
|
+
}
|
|
409
|
+
return out, nil
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// --- Transitions ---
|
|
413
|
+
|
|
414
|
+
// ApplyTaskConfig applies the adapter-owned taskConfig to the parent and
|
|
415
|
+
// optional mailbox. Deterministic defaults: an omitted work-node taskStatus
|
|
416
|
+
// defaults the mailbox to In Progress and leaves the parent unchanged; an
|
|
417
|
+
// omitted parent-only parentStatus defaults to In Progress. Run
|
|
418
|
+
// orchestration merges EndDefaults into the end node's config before this
|
|
419
|
+
// call, so end processing transitions the parent to Done when omitted.
|
|
420
|
+
func (s *system) ApplyTaskConfig(ctx context.Context, target task.Target, taskConfig config.RawValues) error {
|
|
421
|
+
cfg, err := decodeConfig(taskConfig)
|
|
422
|
+
if err != nil {
|
|
423
|
+
return err
|
|
424
|
+
}
|
|
425
|
+
tr := cfg.Transition
|
|
426
|
+
if target.Mailbox != nil {
|
|
427
|
+
status := tr.TaskStatus
|
|
428
|
+
if status == "" {
|
|
429
|
+
status = defaultWorkTaskStatus
|
|
430
|
+
}
|
|
431
|
+
if err := s.transition(ctx, target.Mailbox.Key, status, cfg.Assignee); err != nil {
|
|
432
|
+
return err
|
|
433
|
+
}
|
|
434
|
+
if tr.ParentStatus != "" {
|
|
435
|
+
return s.transition(ctx, target.Parent.Key, tr.ParentStatus, "")
|
|
436
|
+
}
|
|
437
|
+
return nil
|
|
438
|
+
}
|
|
439
|
+
// Parent-only target (start/end lifecycle processing).
|
|
440
|
+
status := tr.ParentStatus
|
|
441
|
+
if status == "" {
|
|
442
|
+
status = defaultStartParentStatus
|
|
443
|
+
}
|
|
444
|
+
return s.transition(ctx, target.Parent.Key, status, "")
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// transition applies a Jira status transition, mapping a human-incompatible
|
|
448
|
+
// current state to a conflict.
|
|
449
|
+
func (s *system) transition(ctx context.Context, key, status, assignee string) error {
|
|
450
|
+
err := s.cli.Transition(ctx, key, status, assignee)
|
|
451
|
+
if err != nil && isConflict(err) {
|
|
452
|
+
return retry.ConflictError(err)
|
|
453
|
+
}
|
|
454
|
+
return err
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
func isConflict(err error) bool {
|
|
458
|
+
msg := strings.ToLower(err.Error())
|
|
459
|
+
return strings.Contains(msg, "transition") && (strings.Contains(msg, "not available") ||
|
|
460
|
+
strings.Contains(msg, "cannot") || strings.Contains(msg, "invalid"))
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// CompleteMailbox marks the mailbox Done using task-system semantics.
|
|
464
|
+
func (s *system) CompleteMailbox(ctx context.Context, mailbox task.Mailbox) error {
|
|
465
|
+
return s.transition(ctx, mailbox.Key, "Done", "")
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// --- Comments ---
|
|
469
|
+
|
|
470
|
+
// HasComment reports whether a comment carrying the stable relay-flow
|
|
471
|
+
// marker exists on the target.
|
|
472
|
+
func (s *system) HasComment(ctx context.Context, target task.Target, marker string) (bool, error) {
|
|
473
|
+
key := target.Parent.Key
|
|
474
|
+
if target.Mailbox != nil {
|
|
475
|
+
key = target.Mailbox.Key
|
|
476
|
+
}
|
|
477
|
+
bodies, err := s.cli.ListComments(ctx, key)
|
|
478
|
+
if err != nil {
|
|
479
|
+
return false, err
|
|
480
|
+
}
|
|
481
|
+
for _, b := range bodies {
|
|
482
|
+
if strings.Contains(b, marker) {
|
|
483
|
+
return true, nil
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return false, nil
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Comment writes a human-readable comment carrying the stable marker. It is
|
|
490
|
+
// idempotent: when a comment with the marker already exists on the target it
|
|
491
|
+
// returns success without posting, so retried summary/feedback/cancellation
|
|
492
|
+
// comments are not duplicated.
|
|
493
|
+
func (s *system) Comment(ctx context.Context, target task.Target, body, marker string) error {
|
|
494
|
+
key := target.Parent.Key
|
|
495
|
+
if target.Mailbox != nil {
|
|
496
|
+
key = target.Mailbox.Key
|
|
497
|
+
}
|
|
498
|
+
exists, err := s.HasComment(ctx, target, marker)
|
|
499
|
+
if err != nil {
|
|
500
|
+
return err
|
|
501
|
+
}
|
|
502
|
+
if exists {
|
|
503
|
+
return nil
|
|
504
|
+
}
|
|
505
|
+
return s.cli.AddComment(ctx, key, body+"\n\n<!-- "+marker+" -->")
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// --- Recovery ---
|
|
509
|
+
|
|
510
|
+
// ResetForRecovery resets mailbox subtasks to To Do while preserving
|
|
511
|
+
// comments, labels, and history. No parent rollback runs.
|
|
512
|
+
func (s *system) ResetForRecovery(ctx context.Context, _ task.TicketRef, mailboxes []task.Mailbox, _ config.RawValues) error {
|
|
513
|
+
for _, mb := range mailboxes {
|
|
514
|
+
if err := s.transition(ctx, mb.Key, "To Do", ""); err != nil {
|
|
515
|
+
return fmt.Errorf("reset mailbox %s: %w", mb.Key, err)
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return nil
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
func contains(xs []string, want string) bool {
|
|
522
|
+
for _, x := range xs {
|
|
523
|
+
if x == want {
|
|
524
|
+
return true
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return false
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
func strField(fields map[string]any, key string) string {
|
|
531
|
+
s, _ := fields[key].(string)
|
|
532
|
+
return s
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
func strSliceField(fields map[string]any, key string) []string {
|
|
536
|
+
out, _ := fields[key].([]string)
|
|
537
|
+
return out
|
|
538
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package jira
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
"strings"
|
|
7
|
+
|
|
8
|
+
"github.com/rajpopat27/relay-flow/internal/task"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// rawIssue is the subset of Jira REST issue fields normalized by the adapter.
|
|
12
|
+
type rawIssue struct {
|
|
13
|
+
ID string `json:"id"`
|
|
14
|
+
Key string `json:"key"`
|
|
15
|
+
Fields struct {
|
|
16
|
+
Summary string `json:"summary"`
|
|
17
|
+
Status struct {
|
|
18
|
+
Name string `json:"name"`
|
|
19
|
+
} `json:"status"`
|
|
20
|
+
IssueType struct {
|
|
21
|
+
Name string `json:"name"`
|
|
22
|
+
} `json:"issuetype"`
|
|
23
|
+
Labels []string `json:"labels"`
|
|
24
|
+
Assignee *struct {
|
|
25
|
+
DisplayName string `json:"displayName"`
|
|
26
|
+
EmailAddress string `json:"emailAddress"`
|
|
27
|
+
} `json:"assignee"`
|
|
28
|
+
Subtasks []struct {
|
|
29
|
+
ID string `json:"id"`
|
|
30
|
+
Key string `json:"key"`
|
|
31
|
+
Fields struct {
|
|
32
|
+
Summary string `json:"summary"`
|
|
33
|
+
} `json:"fields"`
|
|
34
|
+
} `json:"subtasks"`
|
|
35
|
+
IssueLinks []struct {
|
|
36
|
+
Type struct {
|
|
37
|
+
Name string `json:"name"`
|
|
38
|
+
} `json:"type"`
|
|
39
|
+
InwardIssue *struct {
|
|
40
|
+
Key string `json:"key"`
|
|
41
|
+
Fields struct {
|
|
42
|
+
Status struct {
|
|
43
|
+
StatusCategory struct {
|
|
44
|
+
Key string `json:"key"`
|
|
45
|
+
} `json:"statusCategory"`
|
|
46
|
+
} `json:"status"`
|
|
47
|
+
} `json:"fields"`
|
|
48
|
+
} `json:"inwardIssue"`
|
|
49
|
+
} `json:"issuelinks"`
|
|
50
|
+
} `json:"fields"`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// normalizeSearchResponse converts REST issue objects into normalized parent
|
|
54
|
+
// tickets: status, issueType, labels,
|
|
55
|
+
// and assignee become plain Fields entries. Assignee is normalized to the
|
|
56
|
+
// user's email address — the stable, machine-comparable identity workflow
|
|
57
|
+
// filters match against (displayName is human-readable, not an identifier).
|
|
58
|
+
// Subtasks are never returned as parents.
|
|
59
|
+
func normalizeSearchResponse(raw []byte) ([]task.Ticket, error) {
|
|
60
|
+
var issues []rawIssue
|
|
61
|
+
if err := json.Unmarshal(raw, &issues); err != nil {
|
|
62
|
+
return nil, fmt.Errorf("jira search: parse json: %w", err)
|
|
63
|
+
}
|
|
64
|
+
out := make([]task.Ticket, 0, len(issues))
|
|
65
|
+
for _, issue := range issues {
|
|
66
|
+
if hasOpenBlocker(issue) {
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
fields := map[string]any{
|
|
70
|
+
"status": issue.Fields.Status.Name,
|
|
71
|
+
"issueType": issue.Fields.IssueType.Name,
|
|
72
|
+
"labels": append([]string{}, issue.Fields.Labels...),
|
|
73
|
+
}
|
|
74
|
+
if issue.Fields.Assignee != nil {
|
|
75
|
+
fields["assignee"] = issue.Fields.Assignee.EmailAddress
|
|
76
|
+
}
|
|
77
|
+
out = append(out, task.Ticket{
|
|
78
|
+
ID: issue.ID,
|
|
79
|
+
Key: issue.Key,
|
|
80
|
+
Title: issue.Fields.Summary,
|
|
81
|
+
WorkflowClaims: claimLabels(issue.Fields.Labels),
|
|
82
|
+
Fields: fields,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
return out, nil
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func hasOpenBlocker(issue rawIssue) bool {
|
|
89
|
+
for _, link := range issue.Fields.IssueLinks {
|
|
90
|
+
if link.Type.Name == "Blocks" && link.InwardIssue != nil &&
|
|
91
|
+
!strings.EqualFold(link.InwardIssue.Fields.Status.StatusCategory.Key, "done") {
|
|
92
|
+
return true
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func claimLabels(labels []string) []string {
|
|
99
|
+
var out []string
|
|
100
|
+
for _, l := range labels {
|
|
101
|
+
if len(l) > 3 && l[:3] == "wf:" {
|
|
102
|
+
out = append(out, l)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return out
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// subtasksOf maps existing subtask titles (<ticket>:<node>) to mailboxes.
|
|
109
|
+
func subtasksOf(raw []byte) (map[string]task.Mailbox, error) {
|
|
110
|
+
var issue rawIssue
|
|
111
|
+
if err := json.Unmarshal(raw, &issue); err != nil {
|
|
112
|
+
return nil, fmt.Errorf("jira view: parse json: %w", err)
|
|
113
|
+
}
|
|
114
|
+
out := map[string]task.Mailbox{}
|
|
115
|
+
for _, st := range issue.Fields.Subtasks {
|
|
116
|
+
out[st.Fields.Summary] = task.Mailbox{ID: st.ID, Key: st.Key}
|
|
117
|
+
}
|
|
118
|
+
return out, nil
|
|
119
|
+
}
|