|
| 1 | +package runbook |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "github.com/prequel-dev/preq/internal/pkg/ux" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "regexp" |
| 13 | + "testing" |
| 14 | + "text/template" |
| 15 | +) |
| 16 | + |
| 17 | +type stubAction struct{ called bool } |
| 18 | + |
| 19 | +func (s *stubAction) Execute(ctx context.Context, m map[string]any) error { |
| 20 | + s.called = true |
| 21 | + return nil |
| 22 | +} |
| 23 | + |
| 24 | +func TestFilteredAction(t *testing.T) { |
| 25 | + a := &stubAction{} |
| 26 | + f := &filteredAction{pattern: regexp.MustCompile("CRE-1"), inner: a} |
| 27 | + |
| 28 | + // no match |
| 29 | + if err := f.Execute(context.Background(), map[string]any{"cre": map[string]any{"id": "CRE-2"}}); err != nil { |
| 30 | + t.Fatalf("unexpected error: %v", err) |
| 31 | + } |
| 32 | + if a.called { |
| 33 | + t.Fatalf("action should not run") |
| 34 | + } |
| 35 | + |
| 36 | + // match |
| 37 | + if err := f.Execute(context.Background(), map[string]any{"cre": map[string]any{"id": "CRE-1"}}); err != nil { |
| 38 | + t.Fatalf("unexpected error: %v", err) |
| 39 | + } |
| 40 | + if !a.called { |
| 41 | + t.Fatalf("action should run") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestFuncMapAndExecuteTemplate(t *testing.T) { |
| 46 | + data := map[string]any{"cre": map[string]any{"ID": "CRE-9"}, "desc": "- message"} |
| 47 | + funcs := funcMap() |
| 48 | + tmpl, err := template.New("t").Funcs(funcs).Parse("{{field .cre \"ID\"}} {{stripdash .desc}}") |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("template parse: %v", err) |
| 51 | + } |
| 52 | + var out string |
| 53 | + if err := executeTemplate(&out, tmpl, data); err != nil { |
| 54 | + t.Fatalf("executeTemplate: %v", err) |
| 55 | + } |
| 56 | + if out != "CRE-9 message" { |
| 57 | + t.Fatalf("got %q", out) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestExtractCreId(t *testing.T) { |
| 62 | + ev := map[string]any{"cre": map[string]any{"id": "A"}} |
| 63 | + if id := extractCreId(ev); id != "A" { |
| 64 | + t.Fatalf("map: expected A got %s", id) |
| 65 | + } |
| 66 | + type cre struct{ ID string } |
| 67 | + ev["cre"] = &cre{ID: "B"} |
| 68 | + if id := extractCreId(ev); id != "B" { |
| 69 | + t.Fatalf("struct: expected B got %s", id) |
| 70 | + } |
| 71 | + delete(ev, "cre") |
| 72 | + ev["id"] = "C" |
| 73 | + if id := extractCreId(ev); id != "C" { |
| 74 | + t.Fatalf("top-level: expected C got %s", id) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestNewExecAction(t *testing.T) { |
| 79 | + _, err := newExecAction(execConfig{}) |
| 80 | + if err == nil { |
| 81 | + t.Fatalf("expected error for missing path") |
| 82 | + } |
| 83 | + script := filepath.Join(t.TempDir(), "script.sh") |
| 84 | + os.WriteFile(script, []byte("#!/bin/sh\ncat >/dev/null"), 0755) |
| 85 | + a, err := newExecAction(execConfig{Path: script}) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("newExecAction: %v", err) |
| 88 | + } |
| 89 | + if err := a.Execute(context.Background(), map[string]any{"id": "x"}); err != nil { |
| 90 | + t.Fatalf("execute: %v", err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestNewSlackAction(t *testing.T) { |
| 95 | + _, err := newSlackAction(slackConfig{}) |
| 96 | + if err == nil { |
| 97 | + t.Fatalf("expected error for missing fields") |
| 98 | + } |
| 99 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 100 | + body, _ := io.ReadAll(r.Body) |
| 101 | + if !bytes.Contains(body, []byte("CRE-5")) { |
| 102 | + t.Errorf("missing id") |
| 103 | + } |
| 104 | + w.WriteHeader(200) |
| 105 | + })) |
| 106 | + defer srv.Close() |
| 107 | + a, err := newSlackAction(slackConfig{WebhookURL: srv.URL, MessageTemplate: "{{field .cre \"ID\"}}"}) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("newSlackAction: %v", err) |
| 110 | + } |
| 111 | + err = a.Execute(context.Background(), map[string]any{"cre": map[string]any{"ID": "CRE-5"}}) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("execute slack: %v", err) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestBuildActions(t *testing.T) { |
| 118 | + script := filepath.Join(t.TempDir(), "run.sh") |
| 119 | + os.WriteFile(script, []byte("#!/bin/sh\nexit 0"), 0755) |
| 120 | + cfg := "actions:\n- type: exec\n exec:\n path: " + script + "\n" |
| 121 | + path := filepath.Join(t.TempDir(), "cfg.yaml") |
| 122 | + os.WriteFile(path, []byte(cfg), 0644) |
| 123 | + acts, err := buildActions(path) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("buildActions: %v", err) |
| 126 | + } |
| 127 | + if len(acts) != 1 { |
| 128 | + t.Fatalf("expected 1 action got %d", len(acts)) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestNewJiraActionAndAdfParagraph(t *testing.T) { |
| 133 | + _, err := newJiraAction(jiraConfig{}) |
| 134 | + if err == nil { |
| 135 | + t.Fatalf("expected error for missing fields") |
| 136 | + } |
| 137 | + os.Setenv("JIRA_TOKEN", "tok") |
| 138 | + defer os.Unsetenv("JIRA_TOKEN") |
| 139 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 140 | + body, _ := io.ReadAll(r.Body) |
| 141 | + if !bytes.Contains(body, []byte("CRE-7")) { |
| 142 | + t.Errorf("missing id") |
| 143 | + } |
| 144 | + w.WriteHeader(200) |
| 145 | + })) |
| 146 | + defer srv.Close() |
| 147 | + cfg := jiraConfig{ |
| 148 | + WebhookURL: srv.URL, |
| 149 | + SecretEnv: "JIRA_TOKEN", |
| 150 | + SummaryTemplate: "{{field .cre \"ID\"}}", |
| 151 | + DescriptionTemplate: "d", |
| 152 | + ProjectKey: "PR", |
| 153 | + } |
| 154 | + a, err := newJiraAction(cfg) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("newJiraAction: %v", err) |
| 157 | + } |
| 158 | + para := adfParagraph("x") |
| 159 | + if para["type"] != "doc" { |
| 160 | + t.Fatalf("unexpected adf") |
| 161 | + } |
| 162 | + if err := a.Execute(context.Background(), map[string]any{"cre": map[string]any{"ID": "CRE-7"}}); err != nil { |
| 163 | + t.Fatalf("execute: %v", err) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestNewLinearAction(t *testing.T) { |
| 168 | + _, err := newLinearAction(linearConfig{}) |
| 169 | + if err == nil { |
| 170 | + t.Fatalf("expected error for missing fields") |
| 171 | + } |
| 172 | + os.Setenv("LIN_TOKEN", "tok") |
| 173 | + defer os.Unsetenv("LIN_TOKEN") |
| 174 | + cfg := linearConfig{TeamID: "T", SecretEnv: "LIN_TOKEN", TitleTemplate: "{{.}}", DescriptionTemplate: "d"} |
| 175 | + a, err := newLinearAction(cfg) |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("newLinearAction: %v", err) |
| 178 | + } |
| 179 | + if a == nil { |
| 180 | + t.Fatalf("expected action") |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func TestRunbook(t *testing.T) { |
| 185 | + script := filepath.Join(t.TempDir(), "run.sh") |
| 186 | + os.WriteFile(script, []byte("#!/bin/sh\nexit 0"), 0755) |
| 187 | + cfg := "actions:\n- type: exec\n exec:\n path: " + script + "\n" |
| 188 | + path := filepath.Join(t.TempDir(), "cfg.yaml") |
| 189 | + os.WriteFile(path, []byte(cfg), 0644) |
| 190 | + report := ux.ReportDocT{{"cre": map[string]any{"ID": "CRE"}}} |
| 191 | + if err := Runbook(context.Background(), path, report); err != nil { |
| 192 | + t.Fatalf("Runbook: %v", err) |
| 193 | + } |
| 194 | +} |
0 commit comments