|
| 1 | +local NuiTree = require "nui.tree" |
| 2 | +local NuiLine = require "nui.line" |
| 3 | +local Split = require "nui.split" |
| 4 | +local util = require "codeql.util" |
| 5 | +local vim = vim |
| 6 | + |
| 7 | +local M = {} |
| 8 | + |
| 9 | +-- type RunStatus struct { |
| 10 | +-- Id int `json:"id"` |
| 11 | +-- Query string `json:"query"` |
| 12 | +-- Status string `json:"status"` |
| 13 | +-- FailureReason string `json:"failure_reason"` |
| 14 | +-- } |
| 15 | +-- |
| 16 | +-- type RepoWithFindings struct { |
| 17 | +-- Nwo string `json:"nwo"` |
| 18 | +-- Count int `json:"count"` |
| 19 | +-- RunId int `json:"run_id"` |
| 20 | +-- } |
| 21 | +-- type Results struct { |
| 22 | +-- Runs []RunStatus `json:"runs"` |
| 23 | +-- ResositoriesWithFindings []RepoWithFindings `json:"repositories_with_findings"` |
| 24 | +-- TotalFindingsCount int `json:"total_findings_count"` |
| 25 | +-- TotalSuccessfulScans int `json:"total_successful_scans"` |
| 26 | +-- TotalFailedScans int `json:"total_failed_scans"` |
| 27 | +-- TotalRepositoriesWithFindings int `json:"total_repositories_with_findings"` |
| 28 | +-- TotalSkippedRepositories int `json:"total_skipped_repositories"` |
| 29 | +-- TotalSkippedAccessMismatchRepositories int `json:"total_skipped_access_mismatch_repositories"` |
| 30 | +-- TotalSkippedNotFoundRepositories int `json:"total_skipped_not_found_repositories"` |
| 31 | +-- TotalSkippedNoDatabaseRepositories int `json:"total_skipped_no_database_repositories"` |
| 32 | +-- TotalSkippedOverLimitRepositories int `json:"total_skipped_over_limit_repositories"` |
| 33 | +-- } |
| 34 | + |
| 35 | +M.draw = function(name) |
| 36 | + local json = vim.fn.system("gh mrva status --json --name " .. name) |
| 37 | + local status, err = util.json_decode(json) |
| 38 | + if err then |
| 39 | + vim.notify(err, 2) |
| 40 | + return |
| 41 | + end |
| 42 | + |
| 43 | + local nodes = {} |
| 44 | + |
| 45 | + -- add title node |
| 46 | + table.insert(nodes, NuiTree.Node({ type = "label", label = "MRVA Results - " .. name })) |
| 47 | + |
| 48 | + -- total finding count |
| 49 | + table.insert(nodes, NuiTree.Node({ type = "label", label = "Findings: " .. status.total_findings_count })) |
| 50 | + |
| 51 | + for _, item in ipairs(status.repositories_with_findings) do |
| 52 | + table.insert(nodes, NuiTree.Node({ |
| 53 | + type = "result", |
| 54 | + nwo = item.nwo, |
| 55 | + name = name, |
| 56 | + results_count = item.count, |
| 57 | + })) |
| 58 | + end |
| 59 | + |
| 60 | + local split = Split { |
| 61 | + relative = "win", |
| 62 | + position = "left", |
| 63 | + size = 50, |
| 64 | + win_options = { |
| 65 | + number = false, |
| 66 | + relativenumber = false, |
| 67 | + wrap = false, |
| 68 | + winhighlight = "Normal:NormalAlt", |
| 69 | + }, |
| 70 | + buf_options = { |
| 71 | + bufhidden = "delete", |
| 72 | + buftype = "nowrite", |
| 73 | + modifiable = false, |
| 74 | + swapfile = false, |
| 75 | + filetype = "codeql_mrva", |
| 76 | + }, |
| 77 | + } |
| 78 | + split:mount() |
| 79 | + |
| 80 | + local tree = NuiTree { |
| 81 | + winid = split.winid, |
| 82 | + nodes = nodes, |
| 83 | + prepare_node = function(node) |
| 84 | + local line = NuiLine() |
| 85 | + if node.type == "label" then |
| 86 | + line:append(node.label, "Comment") |
| 87 | + end |
| 88 | + if node.type == "result" then |
| 89 | + line:append(" ") |
| 90 | + line:append("", "SpecialKey") |
| 91 | + line:append(" ") |
| 92 | + line:append(node.nwo, "String") |
| 93 | + line:append(" ") |
| 94 | + line:append("(" .. tostring(node.results_count) .. ")", "Comment") |
| 95 | + end |
| 96 | + return line |
| 97 | + end |
| 98 | + } |
| 99 | + |
| 100 | + local map_options = { noremap = true, nowait = true } |
| 101 | + |
| 102 | + split:map("n", "q", function() |
| 103 | + split:unmount() |
| 104 | + end, { noremap = true }) |
| 105 | + |
| 106 | + split:map("n", "<CR>", function() |
| 107 | + local node = tree:get_node() |
| 108 | + if node.type == "result" then |
| 109 | + local tmpdir = vim.fn.fnamemodify(vim.fn.tempname(), ":p:h") |
| 110 | + vim.fn.system("gh mrva download --name " .. node.name .. " --nwo " .. node.nwo .. " --output-dir " .. tmpdir) |
| 111 | + vim.api.nvim_command("LoadSarif " .. tmpdir .. "/" .. node.nwo:gsub("/", "_") .. ".sarif") |
| 112 | + return |
| 113 | + end |
| 114 | + end, map_options) |
| 115 | + |
| 116 | + tree:render() |
| 117 | + vim.api.nvim_buf_set_option(tree.bufnr, "filetype", "codeql_mrva") |
| 118 | +end |
| 119 | + |
| 120 | +return M |
0 commit comments