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,116 @@
|
|
|
1
|
+
package server_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"net/http"
|
|
7
|
+
"testing"
|
|
8
|
+
"time"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// 3.37: graceful shutdown per specs/workflow-repo-management "Graceful
|
|
12
|
+
// shutdown is bounded". Uses the handler fixture on a real Unix socket so a
|
|
13
|
+
// restart on the same dir resumes the same state.
|
|
14
|
+
|
|
15
|
+
func TestShutdownStopsAcceptingWithinBound(t *testing.T) {
|
|
16
|
+
dir := t.TempDir()
|
|
17
|
+
c, shutdown := startHandlerOnSocket(t, dir, &fakeServices{})
|
|
18
|
+
|
|
19
|
+
if _, err := c.Get("http://unix/workflows"); err != nil {
|
|
20
|
+
t.Fatalf("pre-shutdown request failed: %v", err)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
start := time.Now()
|
|
24
|
+
shutdown()
|
|
25
|
+
if elapsed := time.Since(start); elapsed > 35*time.Second {
|
|
26
|
+
t.Fatalf("shutdown took %v, exceeding the bounded 30s wait", elapsed)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if _, err := c.Get("http://unix/workflows"); err == nil {
|
|
30
|
+
t.Fatal("server still accepting requests after shutdown")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
func TestShutdownWaitsForRunningCall(t *testing.T) {
|
|
35
|
+
dir := t.TempDir()
|
|
36
|
+
release := make(chan struct{})
|
|
37
|
+
c, shutdown := startHandlerOnSocket(t, dir, &fakeServices{slowReport: release})
|
|
38
|
+
|
|
39
|
+
done := make(chan error, 1)
|
|
40
|
+
go func() {
|
|
41
|
+
resp, err := c.Post("http://unix/reports", "application/json", slowReportBody(t))
|
|
42
|
+
if err == nil {
|
|
43
|
+
resp.Body.Close()
|
|
44
|
+
}
|
|
45
|
+
done <- err
|
|
46
|
+
}()
|
|
47
|
+
time.Sleep(100 * time.Millisecond) // let the slow request start
|
|
48
|
+
|
|
49
|
+
stopped := make(chan struct{})
|
|
50
|
+
go func() { shutdown(); close(stopped) }()
|
|
51
|
+
|
|
52
|
+
// While the report is in flight, shutdown must be waiting.
|
|
53
|
+
select {
|
|
54
|
+
case <-stopped:
|
|
55
|
+
t.Fatal("shutdown returned while a call was still running")
|
|
56
|
+
case <-time.After(300 * time.Millisecond):
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Let the running call return; shutdown then completes within the bound.
|
|
60
|
+
close(release)
|
|
61
|
+
start := time.Now()
|
|
62
|
+
select {
|
|
63
|
+
case <-stopped:
|
|
64
|
+
case <-time.After(35 * time.Second):
|
|
65
|
+
t.Fatal("shutdown did not complete after the running call returned")
|
|
66
|
+
}
|
|
67
|
+
if err := <-done; err != nil {
|
|
68
|
+
t.Fatalf("running call interrupted by shutdown: %v", err)
|
|
69
|
+
}
|
|
70
|
+
if elapsed := time.Since(start); elapsed > 35*time.Second {
|
|
71
|
+
t.Fatalf("shutdown exceeded the bounded wait: %v", elapsed)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
func TestRestartOnSameStateResumes(t *testing.T) {
|
|
76
|
+
// Same backing services across shutdown+restart resume the same state.
|
|
77
|
+
// The fake services instance is shared so its store survives the restart.
|
|
78
|
+
dir := t.TempDir()
|
|
79
|
+
shared := &fakeServices{}
|
|
80
|
+
c1, shutdown1 := startHandlerOnSocket(t, dir, shared)
|
|
81
|
+
|
|
82
|
+
// Submit a workflow so there is state. POST /workflows body IS raw YAML.
|
|
83
|
+
yamlBody := "name: basicFlow\nrepos: [payments]\nnodes:\n start: {onSuccess: [{target: end}]}\n end: {}\n"
|
|
84
|
+
resp, err := c1.Post("http://unix/workflows", "application/yaml", bytes.NewReader([]byte(yamlBody)))
|
|
85
|
+
if err != nil {
|
|
86
|
+
t.Fatal(err)
|
|
87
|
+
}
|
|
88
|
+
resp.Body.Close()
|
|
89
|
+
shutdown1()
|
|
90
|
+
|
|
91
|
+
// Restart on the same dir with the SAME services; the workflow persists.
|
|
92
|
+
// Durable unfinished-run resume across restart is the engine's
|
|
93
|
+
// responsibility and is covered by the goworkflows same-db restart tests
|
|
94
|
+
// (TestVisitIDStableAcrossNormalRestart / crash tests); the server layer
|
|
95
|
+
// resumes serving the same backing state.
|
|
96
|
+
c2, shutdown2 := startHandlerOnSocket(t, dir, shared)
|
|
97
|
+
defer shutdown2()
|
|
98
|
+
code, env := do(t, c2, http.MethodGet, "http://unix/workflows", nil)
|
|
99
|
+
if code != http.StatusOK || !env.OK {
|
|
100
|
+
t.Fatalf("restart list: code=%d env=%+v", code, env)
|
|
101
|
+
}
|
|
102
|
+
var wfs []map[string]any
|
|
103
|
+
if err := json.Unmarshal(env.Data, &wfs); err != nil || len(wfs) != 1 {
|
|
104
|
+
t.Fatalf("state not resumed after restart: data=%s err=%v", env.Data, err)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
func slowReportBody(t *testing.T) *bytes.Reader {
|
|
109
|
+
t.Helper()
|
|
110
|
+
return bytes.NewReader([]byte(`{"runId":"r","node":"coding","reportId":"s:m","report":{"status":"success","nextStep":"end","summary":{"completed":"x","commits":"abc123","notCompleted":"None","issuesDiscovered":"None","verification":"x","notes":"None"},"feedback":{"reasonForNextStep":"None","requiredActions":"None","relevantContext":"None","expectedResult":"None"}}}`))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// The server.sock ownership/mode is set by the serve startup path (5.5), which
|
|
114
|
+
// binds and chmods the socket; server.New returns only an http.Handler and does
|
|
115
|
+
// not create sockets. The 0600 assertion lives in cmd/relay-flow/commands_test.go
|
|
116
|
+
// (TestServerSocketIsOwnerOnly) which exercises the serve fixture. See 3.29.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package task_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"io"
|
|
6
|
+
"os"
|
|
7
|
+
"path/filepath"
|
|
8
|
+
"strings"
|
|
9
|
+
"sync"
|
|
10
|
+
"testing"
|
|
11
|
+
|
|
12
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
13
|
+
"github.com/rajpopat27/relay-flow/internal/task"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
const alternateAuthPlugin = "alternate-auth-format-test"
|
|
17
|
+
|
|
18
|
+
var registerAlternateAuth sync.Once
|
|
19
|
+
|
|
20
|
+
func TestAlternativeTaskPluginOwnsCredentialsFormat(t *testing.T) {
|
|
21
|
+
registerAlternateAuth.Do(func() {
|
|
22
|
+
task.Register(alternateAuthPlugin, task.Factory{
|
|
23
|
+
RequiredRepoKeys: func() []string { return nil },
|
|
24
|
+
TaskScopeKey: func(_, _ config.RawValues) (string, error) { return "alternate", nil },
|
|
25
|
+
Auth: func(_ context.Context, _ []string, stdin io.Reader) error {
|
|
26
|
+
key, err := io.ReadAll(stdin)
|
|
27
|
+
if err != nil {
|
|
28
|
+
return err
|
|
29
|
+
}
|
|
30
|
+
return os.WriteFile(filepath.Join(os.Getenv("RELAY_FLOW_HOME"), "credentials.yaml"),
|
|
31
|
+
[]byte("alternateApiKey: "+strings.TrimSpace(string(key))+"\n"), 0o600)
|
|
32
|
+
},
|
|
33
|
+
New: func(context.Context, task.RepoSpec) (task.System, error) { return nil, nil },
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
root := t.TempDir()
|
|
37
|
+
t.Setenv("RELAY_FLOW_HOME", root)
|
|
38
|
+
if err := task.Auth(context.Background(), alternateAuthPlugin, nil, strings.NewReader("different-secret")); err != nil {
|
|
39
|
+
t.Fatal(err)
|
|
40
|
+
}
|
|
41
|
+
raw, err := os.ReadFile(filepath.Join(root, "credentials.yaml"))
|
|
42
|
+
if err != nil {
|
|
43
|
+
t.Fatal(err)
|
|
44
|
+
}
|
|
45
|
+
if string(raw) != "alternateApiKey: different-secret\n" {
|
|
46
|
+
t.Fatalf("alternate credentials = %q", raw)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
package task_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"testing"
|
|
6
|
+
|
|
7
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
8
|
+
"github.com/rajpopat27/relay-flow/internal/task"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// 3.6: task-system contract tests against a fake adapter, per
|
|
12
|
+
// specs/integration-contracts. The fake implements task.System and the
|
|
13
|
+
// tests assert the contract shape and primitive separation.
|
|
14
|
+
|
|
15
|
+
// fakeSystem is a minimal in-memory task.System used by contract tests. It
|
|
16
|
+
// holds a mixed backing store (active parents, mailbox subtasks, completed
|
|
17
|
+
// parents) and Poll returns only active parents, per the contract.
|
|
18
|
+
type fakeSystem struct {
|
|
19
|
+
all []storedTicket // mixed backing store
|
|
20
|
+
mailboxes map[string]task.Mailbox
|
|
21
|
+
completed []string
|
|
22
|
+
comments []commentCall
|
|
23
|
+
applied []applyCall
|
|
24
|
+
resets []string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var _ task.System = (*fakeSystem)(nil)
|
|
28
|
+
|
|
29
|
+
type storedTicket struct {
|
|
30
|
+
ticket task.Ticket
|
|
31
|
+
isMailbox bool
|
|
32
|
+
active bool
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type commentCall struct {
|
|
36
|
+
Target task.Target
|
|
37
|
+
Body string
|
|
38
|
+
Marker string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type applyCall struct {
|
|
42
|
+
Target task.Target
|
|
43
|
+
TaskConfig config.RawValues
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
func newFakeSystem() *fakeSystem {
|
|
47
|
+
return &fakeSystem{mailboxes: map[string]task.Mailbox{}}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func (f *fakeSystem) Poll(context.Context) ([]task.Ticket, error) {
|
|
51
|
+
var out []task.Ticket
|
|
52
|
+
for _, st := range f.all {
|
|
53
|
+
if st.active && !st.isMailbox {
|
|
54
|
+
out = append(out, st.ticket)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out, nil
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func (f *fakeSystem) CompileFilter(config.RawValues) (func(task.Ticket) bool, error) {
|
|
61
|
+
return func(task.Ticket) bool { return true }, nil
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
func (f *fakeSystem) Claim(context.Context, task.TicketRef, string) error { return nil }
|
|
65
|
+
|
|
66
|
+
func (f *fakeSystem) ValidateConfig(context.Context, config.RawValues, map[string]config.RawValues) error {
|
|
67
|
+
return nil
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func (f *fakeSystem) EnsureMailboxes(_ context.Context, _ task.TicketRef, _ string, specs []task.MailboxSpec) (map[string]task.Mailbox, error) {
|
|
71
|
+
for _, s := range specs {
|
|
72
|
+
if _, ok := f.mailboxes[s.Node]; !ok {
|
|
73
|
+
f.mailboxes[s.Node] = task.Mailbox{ID: "mb-" + s.Node, Key: "PAY-" + s.Node, Node: s.Node}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
out := map[string]task.Mailbox{}
|
|
77
|
+
for _, s := range specs {
|
|
78
|
+
out[s.Node] = f.mailboxes[s.Node]
|
|
79
|
+
}
|
|
80
|
+
return out, nil
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func (f *fakeSystem) ApplyTaskConfig(_ context.Context, target task.Target, cfg config.RawValues) error {
|
|
84
|
+
f.applied = append(f.applied, applyCall{Target: target, TaskConfig: cfg})
|
|
85
|
+
return nil
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func (f *fakeSystem) CompleteMailbox(_ context.Context, mb task.Mailbox) error {
|
|
89
|
+
f.completed = append(f.completed, mb.Key)
|
|
90
|
+
return nil
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func (f *fakeSystem) HasComment(_ context.Context, _ task.Target, marker string) (bool, error) {
|
|
94
|
+
for _, c := range f.comments {
|
|
95
|
+
if c.Marker == marker {
|
|
96
|
+
return true, nil
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return false, nil
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func (f *fakeSystem) Comment(_ context.Context, target task.Target, body, marker string) error {
|
|
103
|
+
f.comments = append(f.comments, commentCall{Target: target, Body: body, Marker: marker})
|
|
104
|
+
return nil
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
func (f *fakeSystem) ResetForRecovery(_ context.Context, parent task.TicketRef, _ []task.Mailbox, _ config.RawValues) error {
|
|
108
|
+
f.resets = append(f.resets, parent.Key)
|
|
109
|
+
return nil
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
func TestPollReturnsActiveParentsOnly(t *testing.T) {
|
|
113
|
+
f := newFakeSystem()
|
|
114
|
+
// Mixed backing store: an active parent, a mailbox subtask that also
|
|
115
|
+
// carries the wf: label, and a completed (inactive) parent.
|
|
116
|
+
f.all = []storedTicket{
|
|
117
|
+
{ticket: task.Ticket{ID: "1", Key: "PAY-101", Title: "parent", WorkflowClaims: []string{"wf:basicFlow"}}, active: true},
|
|
118
|
+
{ticket: task.Ticket{ID: "2", Key: "PAY-102", Title: "PAY-101:coding", WorkflowClaims: []string{"wf:basicFlow"}}, isMailbox: true, active: true},
|
|
119
|
+
{ticket: task.Ticket{ID: "3", Key: "PAY-103", Title: "done parent"}, active: false},
|
|
120
|
+
}
|
|
121
|
+
f.mailboxes["coding"] = task.Mailbox{ID: "2", Key: "PAY-102", Node: "coding"}
|
|
122
|
+
|
|
123
|
+
got, err := f.Poll(context.Background())
|
|
124
|
+
if err != nil {
|
|
125
|
+
t.Fatalf("Poll failed: %v", err)
|
|
126
|
+
}
|
|
127
|
+
if len(got) != 1 || got[0].Key != "PAY-101" {
|
|
128
|
+
t.Fatalf("Poll = %+v, want only the active parent PAY-101", got)
|
|
129
|
+
}
|
|
130
|
+
for _, ticket := range got {
|
|
131
|
+
if ticket.Key == "PAY-102" {
|
|
132
|
+
t.Fatal("Poll returned a mailbox subtask as a run candidate")
|
|
133
|
+
}
|
|
134
|
+
if ticket.Key == "PAY-103" {
|
|
135
|
+
t.Fatal("Poll returned an inactive/completed parent")
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
func TestEnsureMailboxesFindsExistingCreatesOnlyMissing(t *testing.T) {
|
|
141
|
+
f := newFakeSystem()
|
|
142
|
+
parent := task.TicketRef{ID: "1", Key: "PAY-101", Title: "parent"}
|
|
143
|
+
specs := []task.MailboxSpec{
|
|
144
|
+
{Node: "exploration", Title: "PAY-101:exploration", Description: "explore"},
|
|
145
|
+
{Node: "coding", Title: "PAY-101:coding", Description: "code"},
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
first, err := f.EnsureMailboxes(context.Background(), parent, "basicFlow", specs)
|
|
149
|
+
if err != nil {
|
|
150
|
+
t.Fatalf("EnsureMailboxes failed: %v", err)
|
|
151
|
+
}
|
|
152
|
+
if len(first) != 2 {
|
|
153
|
+
t.Fatalf("EnsureMailboxes returned %d mailboxes, want 2", len(first))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Second call: finds existing, creates none.
|
|
157
|
+
second, err := f.EnsureMailboxes(context.Background(), parent, "basicFlow", specs)
|
|
158
|
+
if err != nil {
|
|
159
|
+
t.Fatalf("EnsureMailboxes (repeat) failed: %v", err)
|
|
160
|
+
}
|
|
161
|
+
for node, mb := range first {
|
|
162
|
+
if second[node] != mb {
|
|
163
|
+
t.Fatalf("node %s: mailbox %v changed to %v on repeat; must be reused", node, mb, second[node])
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Add a node: only the missing one is created.
|
|
168
|
+
specs = append(specs, task.MailboxSpec{Node: "review", Title: "PAY-101:review", Description: "review"})
|
|
169
|
+
third, err := f.EnsureMailboxes(context.Background(), parent, "basicFlow", specs)
|
|
170
|
+
if err != nil {
|
|
171
|
+
t.Fatalf("EnsureMailboxes (extended) failed: %v", err)
|
|
172
|
+
}
|
|
173
|
+
if len(third) != 3 {
|
|
174
|
+
t.Fatalf("EnsureMailboxes returned %d, want complete map of 3", len(third))
|
|
175
|
+
}
|
|
176
|
+
if third["exploration"] != first["exploration"] || third["coding"] != first["coding"] {
|
|
177
|
+
t.Fatal("existing mailboxes were recreated instead of found")
|
|
178
|
+
}
|
|
179
|
+
if third["review"].Node != "review" {
|
|
180
|
+
t.Fatalf("missing mailbox not created: %+v", third["review"])
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
func TestCompleteMailboxIsNarrow(t *testing.T) {
|
|
185
|
+
f := newFakeSystem()
|
|
186
|
+
mb := task.Mailbox{ID: "2", Key: "PAY-102", Node: "coding"}
|
|
187
|
+
if err := f.CompleteMailbox(context.Background(), mb); err != nil {
|
|
188
|
+
t.Fatalf("CompleteMailbox failed: %v", err)
|
|
189
|
+
}
|
|
190
|
+
if len(f.completed) != 1 || f.completed[0] != "PAY-102" {
|
|
191
|
+
t.Fatalf("completed = %v", f.completed)
|
|
192
|
+
}
|
|
193
|
+
// CompleteMailbox performs no comment/routing/runner work.
|
|
194
|
+
if len(f.comments) != 0 {
|
|
195
|
+
t.Fatalf("CompleteMailbox wrote comments: %v", f.comments)
|
|
196
|
+
}
|
|
197
|
+
if len(f.applied) != 0 {
|
|
198
|
+
t.Fatalf("CompleteMailbox applied task config: %v", f.applied)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
func TestSeparatePrimitives(t *testing.T) {
|
|
203
|
+
// Compile-time: the System interface exposes each primitive separately.
|
|
204
|
+
var sys task.System = newFakeSystem()
|
|
205
|
+
ctx := context.Background()
|
|
206
|
+
parent := task.TicketRef{ID: "1", Key: "PAY-101"}
|
|
207
|
+
mb := task.Mailbox{ID: "2", Key: "PAY-102", Node: "coding"}
|
|
208
|
+
target := task.Target{Parent: parent, Mailbox: &mb}
|
|
209
|
+
|
|
210
|
+
if err := sys.ApplyTaskConfig(ctx, target, config.RawValues{}); err != nil {
|
|
211
|
+
t.Fatal(err)
|
|
212
|
+
}
|
|
213
|
+
if err := sys.CompleteMailbox(ctx, mb); err != nil {
|
|
214
|
+
t.Fatal(err)
|
|
215
|
+
}
|
|
216
|
+
if _, err := sys.HasComment(ctx, target, "m"); err != nil {
|
|
217
|
+
t.Fatal(err)
|
|
218
|
+
}
|
|
219
|
+
if err := sys.Comment(ctx, target, "body", "m"); err != nil {
|
|
220
|
+
t.Fatal(err)
|
|
221
|
+
}
|
|
222
|
+
if err := sys.ResetForRecovery(ctx, parent, []task.Mailbox{mb}, config.RawValues{}); err != nil {
|
|
223
|
+
t.Fatal(err)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package task
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"fmt"
|
|
6
|
+
"io"
|
|
7
|
+
"sort"
|
|
8
|
+
"strings"
|
|
9
|
+
"sync"
|
|
10
|
+
|
|
11
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
// RepoSpec carries the registration values used to construct a repo-bound
|
|
15
|
+
// task System.
|
|
16
|
+
type RepoSpec struct {
|
|
17
|
+
Name string
|
|
18
|
+
Path string
|
|
19
|
+
RootConfig config.RawValues
|
|
20
|
+
RepoConfig config.RawValues
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Factory constructs a task System. RequiredRepoKeys returns the explicit
|
|
24
|
+
// repo YAML keys needed at registration. TaskScopeKey derives an opaque
|
|
25
|
+
// canonical physical task scope (such as Jira site/project/component) used
|
|
26
|
+
// to reject duplicate scope registration.
|
|
27
|
+
type Factory struct {
|
|
28
|
+
RequiredRepoKeys func() []string
|
|
29
|
+
TaskScopeKey func(rootConfig, repoConfig config.RawValues) (string, error)
|
|
30
|
+
Auth func(context.Context, []string, io.Reader) error
|
|
31
|
+
New func(context.Context, RepoSpec) (System, error)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var (
|
|
35
|
+
registryMu sync.RWMutex
|
|
36
|
+
registry = map[string]Factory{}
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
// Register adds a task factory by name. Duplicate registration panics.
|
|
40
|
+
func Register(name string, factory Factory) {
|
|
41
|
+
registryMu.Lock()
|
|
42
|
+
defer registryMu.Unlock()
|
|
43
|
+
if _, exists := registry[name]; exists {
|
|
44
|
+
panic(fmt.Sprintf("task: duplicate registration of %q", name))
|
|
45
|
+
}
|
|
46
|
+
registry[name] = factory
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
func lookup(name string) (Factory, error) {
|
|
50
|
+
registryMu.RLock()
|
|
51
|
+
defer registryMu.RUnlock()
|
|
52
|
+
f, ok := registry[name]
|
|
53
|
+
if !ok {
|
|
54
|
+
return Factory{}, fmt.Errorf("task: unknown plugin %q (registered: %s)", name, strings.Join(Names(), ", "))
|
|
55
|
+
}
|
|
56
|
+
return f, nil
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// New constructs the repo-bound task System for the named plugin.
|
|
60
|
+
func New(ctx context.Context, name string, spec RepoSpec) (System, error) {
|
|
61
|
+
f, err := lookup(name)
|
|
62
|
+
if err != nil {
|
|
63
|
+
return nil, err
|
|
64
|
+
}
|
|
65
|
+
return f.New(ctx, spec)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Auth dispatches system-wide authentication to the selected task plugin.
|
|
69
|
+
// The plugin owns its flags, prompts, validation, credential format, and
|
|
70
|
+
// credential storage.
|
|
71
|
+
func Auth(ctx context.Context, name string, args []string, stdin io.Reader) error {
|
|
72
|
+
f, err := lookup(name)
|
|
73
|
+
if err != nil {
|
|
74
|
+
return err
|
|
75
|
+
}
|
|
76
|
+
if f.Auth == nil {
|
|
77
|
+
return fmt.Errorf("task: plugin %q does not support authentication", name)
|
|
78
|
+
}
|
|
79
|
+
return f.Auth(ctx, args, stdin)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// RequiredRepoKeys returns the repo YAML keys the named plugin requires at
|
|
83
|
+
// registration.
|
|
84
|
+
func RequiredRepoKeys(name string) ([]string, error) {
|
|
85
|
+
f, err := lookup(name)
|
|
86
|
+
if err != nil {
|
|
87
|
+
return nil, err
|
|
88
|
+
}
|
|
89
|
+
return f.RequiredRepoKeys(), nil
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// TaskScopeKey derives the canonical task scope for the named plugin.
|
|
93
|
+
func TaskScopeKey(name string, rootConfig, repoConfig config.RawValues) (string, error) {
|
|
94
|
+
f, err := lookup(name)
|
|
95
|
+
if err != nil {
|
|
96
|
+
return "", err
|
|
97
|
+
}
|
|
98
|
+
return f.TaskScopeKey(rootConfig, repoConfig)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ValidateName returns an error listing registered names when name is not
|
|
102
|
+
// a registered plugin. Used by `relay-flow init` to reject unknown plugin
|
|
103
|
+
// selections without constructing a System.
|
|
104
|
+
func ValidateName(name string) error {
|
|
105
|
+
_, err := lookup(name)
|
|
106
|
+
return err
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Names returns the registered plugin names sorted.
|
|
110
|
+
func Names() []string {
|
|
111
|
+
registryMu.RLock()
|
|
112
|
+
defer registryMu.RUnlock()
|
|
113
|
+
out := make([]string, 0, len(registry))
|
|
114
|
+
for name := range registry {
|
|
115
|
+
out = append(out, name)
|
|
116
|
+
}
|
|
117
|
+
sort.Strings(out)
|
|
118
|
+
return out
|
|
119
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
package jira
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bufio"
|
|
5
|
+
"context"
|
|
6
|
+
"errors"
|
|
7
|
+
"flag"
|
|
8
|
+
"fmt"
|
|
9
|
+
"io"
|
|
10
|
+
"os"
|
|
11
|
+
"path/filepath"
|
|
12
|
+
"strings"
|
|
13
|
+
|
|
14
|
+
"github.com/charmbracelet/huh"
|
|
15
|
+
"github.com/mattn/go-isatty"
|
|
16
|
+
"github.com/rajpopat27/relay-flow/internal/config"
|
|
17
|
+
jirarest "github.com/rajpopat27/relay-flow/internal/task/jira/rest"
|
|
18
|
+
"gopkg.in/yaml.v3"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
type credentials struct {
|
|
22
|
+
Site string `yaml:"site"`
|
|
23
|
+
Email string `yaml:"email"`
|
|
24
|
+
Token string `yaml:"token"`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func auth(ctx context.Context, args []string, stdin io.Reader) error {
|
|
28
|
+
fs := flag.NewFlagSet("task auth", flag.ContinueOnError)
|
|
29
|
+
fs.SetOutput(io.Discard)
|
|
30
|
+
site := fs.String("site", "", "Jira site URL")
|
|
31
|
+
email := fs.String("email", "", "Jira account email")
|
|
32
|
+
token := fs.String("token", "", "Jira API token")
|
|
33
|
+
if err := fs.Parse(args); err != nil {
|
|
34
|
+
return err
|
|
35
|
+
}
|
|
36
|
+
if fs.NArg() != 0 {
|
|
37
|
+
return errors.New("unexpected positional arguments")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
values := credentials{Site: strings.TrimSpace(*site), Email: strings.TrimSpace(*email), Token: *token}
|
|
41
|
+
flagged := values.Site != "" || values.Email != "" || values.Token != ""
|
|
42
|
+
if flagged && (values.Site == "" || values.Email == "" || values.Token == "") {
|
|
43
|
+
return errors.New("--site, --email, and --token must be given together")
|
|
44
|
+
}
|
|
45
|
+
if !flagged {
|
|
46
|
+
if isTTY(stdin) {
|
|
47
|
+
var err error
|
|
48
|
+
values, err = promptCredentials(values)
|
|
49
|
+
if err != nil {
|
|
50
|
+
return err
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
var ok bool
|
|
54
|
+
values, ok = readCredentialLines(stdin)
|
|
55
|
+
if !ok {
|
|
56
|
+
return errors.New("expected Jira site, email, and API token on stdin (or pass --site, --email, and --token)")
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
client, err := jirarest.New(values.Site, values.Email, values.Token)
|
|
62
|
+
if err != nil {
|
|
63
|
+
return err
|
|
64
|
+
}
|
|
65
|
+
if err := client.ValidateCredentials(ctx); err != nil {
|
|
66
|
+
return err
|
|
67
|
+
}
|
|
68
|
+
values.Site = strings.TrimRight(clientSite(values.Site), "/")
|
|
69
|
+
path, err := credentialsPath()
|
|
70
|
+
if err != nil {
|
|
71
|
+
return err
|
|
72
|
+
}
|
|
73
|
+
return saveCredentials(path, values)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func isTTY(stdin io.Reader) bool {
|
|
77
|
+
f, ok := stdin.(*os.File)
|
|
78
|
+
return ok && isatty.IsTerminal(f.Fd())
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func promptCredentials(values credentials) (credentials, error) {
|
|
82
|
+
required := func(name string) func(string) error {
|
|
83
|
+
return func(value string) error {
|
|
84
|
+
if strings.TrimSpace(value) == "" {
|
|
85
|
+
return fmt.Errorf("%s is required", name)
|
|
86
|
+
}
|
|
87
|
+
return nil
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
form := huh.NewForm(huh.NewGroup(
|
|
91
|
+
huh.NewInput().Title("Jira site").Description("Your Atlassian site URL.").Placeholder("https://company.atlassian.net").Validate(required("Jira site")).Value(&values.Site),
|
|
92
|
+
huh.NewInput().Title("Jira email").Description("Email for the Jira API token.").Placeholder("you@company.com").Validate(required("Jira email")).Value(&values.Email),
|
|
93
|
+
huh.NewInput().Title("Jira API token").Description("Stored locally and never displayed.").EchoMode(huh.EchoModePassword).Validate(required("Jira API token")).Value(&values.Token),
|
|
94
|
+
).Title("Configure Jira"))
|
|
95
|
+
if err := form.Run(); err != nil {
|
|
96
|
+
return credentials{}, err
|
|
97
|
+
}
|
|
98
|
+
values.Site = strings.TrimSpace(values.Site)
|
|
99
|
+
values.Email = strings.TrimSpace(values.Email)
|
|
100
|
+
return values, nil
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
func readCredentialLines(stdin io.Reader) (credentials, bool) {
|
|
104
|
+
values := make([]string, 0, 3)
|
|
105
|
+
scanner := bufio.NewScanner(stdin)
|
|
106
|
+
for len(values) < 3 && scanner.Scan() {
|
|
107
|
+
line := strings.TrimSpace(scanner.Text())
|
|
108
|
+
if line != "" {
|
|
109
|
+
values = append(values, line)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if len(values) != 3 {
|
|
113
|
+
return credentials{}, false
|
|
114
|
+
}
|
|
115
|
+
return credentials{Site: values[0], Email: values[1], Token: values[2]}, true
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func clientSite(site string) string {
|
|
119
|
+
if !strings.Contains(site, "://") {
|
|
120
|
+
return "https://" + site
|
|
121
|
+
}
|
|
122
|
+
return site
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
func credentialsPath() (string, error) {
|
|
126
|
+
if root := os.Getenv("RELAY_FLOW_HOME"); root != "" {
|
|
127
|
+
return filepath.Join(root, "credentials.yaml"), nil
|
|
128
|
+
}
|
|
129
|
+
home, err := os.UserHomeDir()
|
|
130
|
+
if err != nil {
|
|
131
|
+
return "", fmt.Errorf("resolve user home: %w", err)
|
|
132
|
+
}
|
|
133
|
+
return filepath.Join(home, ".relay-flow", "credentials.yaml"), nil
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
func loadCredentialsDefault() (credentials, error) {
|
|
137
|
+
path, err := credentialsPath()
|
|
138
|
+
if err != nil {
|
|
139
|
+
return credentials{}, err
|
|
140
|
+
}
|
|
141
|
+
return loadCredentials(path)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
func loadCredentials(path string) (credentials, error) {
|
|
145
|
+
info, err := os.Stat(path)
|
|
146
|
+
if err != nil {
|
|
147
|
+
return credentials{}, fmt.Errorf("stat credentials %s: %w", path, err)
|
|
148
|
+
}
|
|
149
|
+
if info.Mode().Perm() != 0o600 {
|
|
150
|
+
return credentials{}, fmt.Errorf("credentials %s must have mode 0600", path)
|
|
151
|
+
}
|
|
152
|
+
raw, err := os.ReadFile(path)
|
|
153
|
+
if err != nil {
|
|
154
|
+
return credentials{}, fmt.Errorf("read credentials %s: %w", path, err)
|
|
155
|
+
}
|
|
156
|
+
var out credentials
|
|
157
|
+
decoder := yaml.NewDecoder(strings.NewReader(string(raw)))
|
|
158
|
+
decoder.KnownFields(true)
|
|
159
|
+
if err := decoder.Decode(&out); err != nil {
|
|
160
|
+
return credentials{}, fmt.Errorf("parse credentials %s: %w", path, err)
|
|
161
|
+
}
|
|
162
|
+
if out.Site == "" || out.Email == "" || out.Token == "" {
|
|
163
|
+
return credentials{}, errors.New("Jira site, email, and API token are required")
|
|
164
|
+
}
|
|
165
|
+
return out, nil
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
func saveCredentials(path string, value credentials) error {
|
|
169
|
+
if value.Site == "" || value.Email == "" || value.Token == "" {
|
|
170
|
+
return errors.New("Jira site, email, and API token are required")
|
|
171
|
+
}
|
|
172
|
+
raw, err := yaml.Marshal(value)
|
|
173
|
+
if err != nil {
|
|
174
|
+
return fmt.Errorf("marshal credentials: %w", err)
|
|
175
|
+
}
|
|
176
|
+
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
177
|
+
return fmt.Errorf("create credentials directory: %w", err)
|
|
178
|
+
}
|
|
179
|
+
if err := config.WriteAtomic(path, raw, 0o600); err != nil {
|
|
180
|
+
return fmt.Errorf("write credentials: %w", err)
|
|
181
|
+
}
|
|
182
|
+
return nil
|
|
183
|
+
}
|