|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | +# Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +defmodule FeedbackATron.HTTPIntake.Router do |
| 4 | + @moduledoc """ |
| 5 | + Optional HTTP intake for the autonomous bug-reporting pipeline. |
| 6 | +
|
| 7 | + This is the wire the boj gateway → `bug-filing-mcp` cartridge drives to reach the |
| 8 | + real engine (see `docs/AUTONOMOUS-BUG-PIPELINE.adoc`, contract C2 / D0 = new wrapping |
| 9 | + cartridge). It is a thin, localhost-only JSON adapter over the *same* `Submitter.submit/2` |
| 10 | + path used by the MCP `submit_feedback` tool — no separate submission logic, so audit |
| 11 | + logging, dedup, rate-limiting and dry-run all behave identically. |
| 12 | +
|
| 13 | + Off by default; enabled via `FEEDBACK_A_TRON_HTTP` (see `FeedbackATron.Application`). |
| 14 | +
|
| 15 | + ## Routes |
| 16 | +
|
| 17 | + - `GET /health` → `{"status":"ok"}` |
| 18 | + - `POST /api/v1/submit_feedback` → body `{title, body, repo, platforms?, labels?, dry_run?, skip_dedupe?}` |
| 19 | +
|
| 20 | + The request/response shapes intentionally match the MCP tool's `input_schema` so a |
| 21 | + cartridge can forward the same arguments unchanged. |
| 22 | + """ |
| 23 | + |
| 24 | + use Plug.Router |
| 25 | + require Logger |
| 26 | + |
| 27 | + alias FeedbackATron.Submitter |
| 28 | + |
| 29 | + plug(:match) |
| 30 | + plug(Plug.Parsers, parsers: [:json], pass: ["application/json"], json_decoder: Jason) |
| 31 | + plug(:dispatch) |
| 32 | + |
| 33 | + get "/health" do |
| 34 | + send_json(conn, 200, %{status: "ok", service: "feedback-a-tron", intake: "http"}) |
| 35 | + end |
| 36 | + |
| 37 | + post "/api/v1/submit_feedback" do |
| 38 | + try do |
| 39 | + case validate(conn.body_params) do |
| 40 | + {:ok, issue, opts} -> |
| 41 | + case Submitter.submit(issue, opts) do |
| 42 | + {:ok, submission_id, results} -> |
| 43 | + send_json(conn, 200, format_results(submission_id, results)) |
| 44 | + |
| 45 | + {:error, reason} -> |
| 46 | + Logger.error("HTTP submit_feedback failed: #{inspect(reason)}") |
| 47 | + send_json(conn, 502, %{error: "submission_failed", detail: inspect(reason)}) |
| 48 | + end |
| 49 | + |
| 50 | + {:error, missing} -> |
| 51 | + send_json(conn, 400, %{error: "missing_required_fields", fields: missing}) |
| 52 | + end |
| 53 | + rescue |
| 54 | + exception -> |
| 55 | + Logger.error("HTTP submit_feedback exception: #{Exception.message(exception)}") |
| 56 | + send_json(conn, 500, %{error: "internal_error", detail: Exception.message(exception)}) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + match _ do |
| 61 | + send_json(conn, 404, %{error: "not_found"}) |
| 62 | + end |
| 63 | + |
| 64 | + # --- helpers --------------------------------------------------------------- |
| 65 | + |
| 66 | + # Mirrors FeedbackATron.MCP.Tools.SubmitFeedback: required [title, body, repo]. |
| 67 | + defp validate(params) when is_map(params) do |
| 68 | + missing = |
| 69 | + ["title", "body", "repo"] |
| 70 | + |> Enum.filter(fn k -> blank?(Map.get(params, k)) end) |
| 71 | + |
| 72 | + if missing == [] do |
| 73 | + issue = %{ |
| 74 | + title: params["title"], |
| 75 | + body: params["body"], |
| 76 | + repo: params["repo"] |
| 77 | + } |
| 78 | + |
| 79 | + opts = [ |
| 80 | + platforms: parse_platforms(params["platforms"]), |
| 81 | + labels: params["labels"] || [], |
| 82 | + dry_run: params["dry_run"] || false, |
| 83 | + dedupe: not (params["skip_dedupe"] || false) |
| 84 | + ] |
| 85 | + |
| 86 | + {:ok, issue, opts} |
| 87 | + else |
| 88 | + {:error, missing} |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + defp validate(_), do: {:error, ["title", "body", "repo"]} |
| 93 | + |
| 94 | + defp blank?(nil), do: true |
| 95 | + defp blank?(v) when is_binary(v), do: String.trim(v) == "" |
| 96 | + defp blank?(_), do: false |
| 97 | + |
| 98 | + defp parse_platforms(nil), do: [:github] |
| 99 | + |
| 100 | + defp parse_platforms(platforms) when is_list(platforms) do |
| 101 | + platforms |
| 102 | + |> Enum.map(&platform_atom/1) |
| 103 | + |> Enum.filter(& &1) |
| 104 | + |> case do |
| 105 | + [] -> [:github] |
| 106 | + list -> list |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + defp parse_platforms(_), do: [:github] |
| 111 | + |
| 112 | + defp platform_atom("github"), do: :github |
| 113 | + defp platform_atom("gitlab"), do: :gitlab |
| 114 | + defp platform_atom("bitbucket"), do: :bitbucket |
| 115 | + defp platform_atom("codeberg"), do: :codeberg |
| 116 | + defp platform_atom("bugzilla"), do: :bugzilla |
| 117 | + defp platform_atom("email"), do: :email |
| 118 | + defp platform_atom(_), do: nil |
| 119 | + |
| 120 | + defp format_results(submission_id, results) do |
| 121 | + formatted = |
| 122 | + Enum.map(results, fn |
| 123 | + {:ok, %{platform: platform, url: url}} -> |
| 124 | + %{platform: platform, status: "success", url: url} |
| 125 | + |
| 126 | + {:ok, %{platform: platform, status: :dry_run, would_submit: issue}} -> |
| 127 | + %{platform: platform, status: "dry_run", title: issue.title} |
| 128 | + |
| 129 | + {:error, %{platform: platform, error: error}} -> |
| 130 | + %{platform: platform, status: "error", error: inspect(error)} |
| 131 | + |
| 132 | + {:error, {:duplicate_found, similar}} -> |
| 133 | + %{status: "skipped", reason: "duplicate", similar_issues: similar} |
| 134 | + |
| 135 | + {:error, {:similar_found, matches}} -> |
| 136 | + %{status: "skipped", reason: "similar_found", similar_issues: matches} |
| 137 | + |
| 138 | + {:error, other} -> |
| 139 | + %{status: "error", error: inspect(other)} |
| 140 | + end) |
| 141 | + |
| 142 | + %{submission_id: submission_id, results: formatted, summary: summarize(formatted)} |
| 143 | + end |
| 144 | + |
| 145 | + defp summarize(results) do |
| 146 | + count = fn status -> Enum.count(results, &(Map.get(&1, :status) == status)) end |
| 147 | + |
| 148 | + "Submitted: #{count.("success")}, Errors: #{count.("error")}, " <> |
| 149 | + "Skipped: #{count.("skipped")}, Dry run: #{count.("dry_run")}" |
| 150 | + end |
| 151 | + |
| 152 | + defp send_json(conn, status, payload) do |
| 153 | + conn |
| 154 | + |> put_resp_content_type("application/json") |
| 155 | + |> send_resp(status, Jason.encode!(payload)) |
| 156 | + end |
| 157 | +end |
0 commit comments