|
| 1 | +package runbook |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "text/template" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type jiraConfig struct { |
| 17 | + WebhookURL string `yaml:"webhook_url"` |
| 18 | + Secret string `yaml:"secret"` // optional |
| 19 | + SecretEnv string `yaml:"secret_env"` // optional |
| 20 | + SummaryTemplate string `yaml:"summary_template"` |
| 21 | + DescriptionTemplate string `yaml:"description_template"` |
| 22 | + ProjectKey string `yaml:"project_key"` // e.g. "PREQ" |
| 23 | +} |
| 24 | + |
| 25 | +type jiraAction struct { |
| 26 | + cfg jiraConfig |
| 27 | + summaryTmpl *template.Template |
| 28 | + descTmpl *template.Template |
| 29 | + httpc *http.Client |
| 30 | +} |
| 31 | + |
| 32 | +func newJiraAction(cfg jiraConfig) (Action, error) { |
| 33 | + if cfg.WebhookURL == "" { |
| 34 | + return nil, errors.New("jira.webhook_url is required") |
| 35 | + } |
| 36 | + if cfg.SummaryTemplate == "" { |
| 37 | + return nil, errors.New("jira.summary_template is required") |
| 38 | + } |
| 39 | + if cfg.ProjectKey == "" { |
| 40 | + return nil, errors.New("jira.project_key is required when using REST API mode") |
| 41 | + } |
| 42 | + st, err := template.New("jira-summary").Funcs(funcMap()).Parse(cfg.SummaryTemplate) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + dt, err := template.New("jira-desc").Funcs(funcMap()).Parse(cfg.DescriptionTemplate) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + if cfg.Secret == "" && cfg.SecretEnv != "" { |
| 52 | + cfg.Secret = os.Getenv(cfg.SecretEnv) |
| 53 | + } |
| 54 | + // optional: hard‑fail if both were empty |
| 55 | + if cfg.Secret == "" { |
| 56 | + return nil, errors.New("jira secret missing; set either 'secret' or 'secret_env'") |
| 57 | + } |
| 58 | + |
| 59 | + return &jiraAction{ |
| 60 | + cfg: cfg, |
| 61 | + summaryTmpl: st, |
| 62 | + descTmpl: dt, |
| 63 | + httpc: &http.Client{ |
| 64 | + Timeout: 5 * time.Second, |
| 65 | + }, |
| 66 | + }, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (j *jiraAction) Execute(ctx context.Context, cre map[string]any) error { |
| 70 | + var summary, desc string |
| 71 | + if err := executeTemplate(&summary, j.summaryTmpl, cre); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if err := executeTemplate(&desc, j.descTmpl, cre); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + payload := map[string]any{ |
| 78 | + "project": map[string]any{"key": j.cfg.ProjectKey}, |
| 79 | + "summary": summary, |
| 80 | + "description": adfParagraph(desc), |
| 81 | + "issuetype": map[string]any{"name": "Bug"}, |
| 82 | + } |
| 83 | + body, _ := json.Marshal(payload) |
| 84 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, j.cfg.WebhookURL, |
| 85 | + bytes.NewReader(body)) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("jira post: %w", err) |
| 88 | + } |
| 89 | + req.Header.Set("Content-Type", "application/json") |
| 90 | + if j.cfg.Secret != "" { |
| 91 | + req.Header.Set("X-Automation-Webhook-Token", j.cfg.Secret) |
| 92 | + } |
| 93 | + resp, err := j.httpc.Do(req) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("jira post: %w", err) |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + if resp.StatusCode >= 300 { |
| 99 | + respBody, _ := io.ReadAll(resp.Body) |
| 100 | + return fmt.Errorf("jira post failed: %s – %s", resp.Status, respBody) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func adfParagraph(txt string) map[string]any { |
| 106 | + return map[string]any{ |
| 107 | + "type": "doc", |
| 108 | + "version": 1, |
| 109 | + "content": []any{ |
| 110 | + map[string]any{ |
| 111 | + "type": "paragraph", |
| 112 | + "content": []any{ |
| 113 | + map[string]any{ |
| 114 | + "type": "text", |
| 115 | + "text": txt, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | +} |
0 commit comments