|
| 1 | +local M = {} |
| 2 | + |
| 3 | +local base_dir = vim.fn.stdpath("data") .. "/coderabbit" |
| 4 | + |
| 5 | +--- Override the base directory (for testing only). |
| 6 | +function M._set_base_dir(dir) |
| 7 | + base_dir = dir |
| 8 | +end |
| 9 | + |
| 10 | +--- Resolve the git root for the current repo. |
| 11 | +--- @return string|nil |
| 12 | +local function git_root() |
| 13 | + local out = vim.fn.systemlist({ "git", "rev-parse", "--show-toplevel" }) |
| 14 | + if vim.v.shell_error ~= 0 or not out[1] or out[1] == "" then |
| 15 | + return nil |
| 16 | + end |
| 17 | + return out[1] |
| 18 | +end |
| 19 | + |
| 20 | +--- Turn a git root path into a safe directory name. |
| 21 | +--- e.g. /Users/sam/projects/my-repo -> Users-sam-projects-my-repo |
| 22 | +--- C:\Users\me\repo -> Users-me-repo |
| 23 | +local function repo_key(root) |
| 24 | + local key = root |
| 25 | + -- Strip Windows drive letter (e.g. "C:") |
| 26 | + key = key:gsub("^%a:", "") |
| 27 | + -- Strip leading separators |
| 28 | + key = key:gsub("^[\\/]+", "") |
| 29 | + -- Replace all path separators with dashes |
| 30 | + key = key:gsub("[\\/]+", "-") |
| 31 | + return key |
| 32 | +end |
| 33 | + |
| 34 | +--- Return the per-repo review storage directory. |
| 35 | +--- @return string |
| 36 | +local function repo_dir() |
| 37 | + local root = git_root() |
| 38 | + if not root then |
| 39 | + return base_dir .. "/_unknown" |
| 40 | + end |
| 41 | + return base_dir .. "/" .. repo_key(root) |
| 42 | +end |
| 43 | + |
| 44 | +local function ensure_dir(dir) |
| 45 | + vim.fn.mkdir(dir, "p") |
| 46 | +end |
| 47 | + |
| 48 | +--- Format a timestamp for use as a filename. |
| 49 | +--- @param ts number epoch seconds |
| 50 | +--- @return string e.g. "2026-04-14_15-30-00" |
| 51 | +local function ts_filename(ts) |
| 52 | + return os.date("%Y-%m-%d_%H-%M-%S", ts) |
| 53 | +end |
| 54 | + |
| 55 | +--- Save a completed review to disk. |
| 56 | +--- @param findings table[] Array of { diagnostic, filepath } |
| 57 | +--- @param context table|nil Review context metadata |
| 58 | +--- @return string|nil filename The review filename (without path), or nil if nothing saved |
| 59 | +function M.save(findings, context) |
| 60 | + if #findings == 0 then |
| 61 | + return nil |
| 62 | + end |
| 63 | + local dir = repo_dir() |
| 64 | + ensure_dir(dir) |
| 65 | + local ts = os.time() |
| 66 | + local entry = { |
| 67 | + findings = findings, |
| 68 | + context = context, |
| 69 | + timestamp = ts, |
| 70 | + } |
| 71 | + local json = vim.json.encode(entry) |
| 72 | + -- Avoid collisions when multiple reviews finish in the same second |
| 73 | + local base = ts_filename(ts) |
| 74 | + local filename = base .. ".json" |
| 75 | + local path = dir .. "/" .. filename |
| 76 | + local suffix = 1 |
| 77 | + while vim.fn.filereadable(path) == 1 do |
| 78 | + filename = base .. "_" .. suffix .. ".json" |
| 79 | + path = dir .. "/" .. filename |
| 80 | + suffix = suffix + 1 |
| 81 | + end |
| 82 | + local file = io.open(path, "w") |
| 83 | + if not file then |
| 84 | + return nil |
| 85 | + end |
| 86 | + local ok = file:write(json) |
| 87 | + file:close() |
| 88 | + if not ok then |
| 89 | + return nil |
| 90 | + end |
| 91 | + return filename |
| 92 | +end |
| 93 | + |
| 94 | +--- List all saved reviews for the current repo (summary only, no findings). |
| 95 | +--- Returns entries sorted chronologically (oldest first), each with an |
| 96 | +--- ordinal `id` field for display / lookup. |
| 97 | +--- @return table[] Array of { id, timestamp, context, finding_count, filename } |
| 98 | +function M.list() |
| 99 | + local dir = repo_dir() |
| 100 | + ensure_dir(dir) |
| 101 | + local files = vim.fn.glob(dir .. "/*.json", false, true) |
| 102 | + table.sort(files) |
| 103 | + |
| 104 | + local entries = {} |
| 105 | + for i, path in ipairs(files) do |
| 106 | + local file = io.open(path, "r") |
| 107 | + if file then |
| 108 | + local content = file:read("*a") |
| 109 | + file:close() |
| 110 | + local ok, data = pcall(vim.json.decode, content) |
| 111 | + if ok and type(data) == "table" then |
| 112 | + table.insert(entries, { |
| 113 | + id = i, |
| 114 | + timestamp = data.timestamp, |
| 115 | + context = data.context, |
| 116 | + finding_count = data.findings and #data.findings or 0, |
| 117 | + filename = vim.fn.fnamemodify(path, ":t"), |
| 118 | + }) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + return entries |
| 123 | +end |
| 124 | + |
| 125 | +--- Load a review by ordinal ID (1-indexed position in chronological order). |
| 126 | +--- @param id number |
| 127 | +--- @return table|nil |
| 128 | +function M.load(id) |
| 129 | + local dir = repo_dir() |
| 130 | + local files = vim.fn.glob(dir .. "/*.json", false, true) |
| 131 | + table.sort(files) |
| 132 | + |
| 133 | + local path = files[id] |
| 134 | + if not path then |
| 135 | + return nil |
| 136 | + end |
| 137 | + local file = io.open(path, "r") |
| 138 | + if not file then |
| 139 | + return nil |
| 140 | + end |
| 141 | + local content = file:read("*a") |
| 142 | + file:close() |
| 143 | + local ok, data = pcall(vim.json.decode, content) |
| 144 | + if not ok or type(data) ~= "table" then |
| 145 | + return nil |
| 146 | + end |
| 147 | + data.id = id |
| 148 | + return data |
| 149 | +end |
| 150 | + |
| 151 | +--- Return list of existing review IDs as strings (for command completion). |
| 152 | +--- @return string[] |
| 153 | +function M.ids() |
| 154 | + local dir = repo_dir() |
| 155 | + ensure_dir(dir) |
| 156 | + local files = vim.fn.glob(dir .. "/*.json", false, true) |
| 157 | + local ids = {} |
| 158 | + for i = 1, #files do |
| 159 | + table.insert(ids, tostring(i)) |
| 160 | + end |
| 161 | + return ids |
| 162 | +end |
| 163 | + |
| 164 | +return M |
0 commit comments