summary refs log tree commit diff
path: root/.config/nvim/lua/mod/gh.lua
diff options
context:
space:
mode:
Diffstat (limited to '.config/nvim/lua/mod/gh.lua')
-rw-r--r--.config/nvim/lua/mod/gh.lua86
1 files changed, 86 insertions, 0 deletions
diff --git a/.config/nvim/lua/mod/gh.lua b/.config/nvim/lua/mod/gh.lua
new file mode 100644
index 0000000..1f8a267
--- /dev/null
+++ b/.config/nvim/lua/mod/gh.lua
@@ -0,0 +1,86 @@
+local M = {}
+
+-- gh api \
+--   --method POST \
+--   -H "Accept: application/vnd.github+json" \
+--   -H "X-GitHub-Api-Version: 2022-11-28" \
+--   /repos/OWNER/REPO/pulls/PULL_NUMBER/comments \
+
+function M.pr_comment(repo, pull, commit_id, comment)
+  vim.print(vim.inspect(comment))
+  vim.print(vim.inspect({
+    'gh',
+    'api',
+    '--method',
+    'POST',
+    string.format('/repos/%s/pulls/%d/comments', repo, pull),
+    '-f',
+    'body=' .. comment.body,
+    '-f',
+    'commit_id=' .. commit_id,
+    '-f',
+    'path=' .. comment.path,
+    '-f',
+    'side=RIGHT',
+    '-f',
+    'line=' .. comment.line,
+  }))
+end
+
+function M.pr_view()
+  local p = vim
+    .system({
+      'gh',
+      'pr',
+      'view',
+      '--json',
+      'number,headRepository,headRefOid',
+      '--jq',
+      '{head:.headRefOid,repo:.headRepository.nameWithOwner,pull:.number}',
+    })
+    :wait()
+  return vim.json.decode(p.stdout)
+end
+
+function M.submit_review()
+  local gitroot = vim.fs.root(0, { '.git' })
+  if not gitroot then
+    vim.notify('Not a git repository.', vim.log.levels.WARN)
+    return
+  end
+
+  local review = { comments = {} }
+
+  local marks = require('haunt.api').get_bookmarks()
+  for _, mark in ipairs(marks) do
+    -- nil for bookmarks outside the current repo
+    local path = vim.fs.relpath(gitroot, mark.file)
+    if path then
+      review.comments[#review.comments] = {
+        path = path,
+        line = mark.line,
+        body = mark.note,
+      }
+    end
+  end
+
+  review.pr = M.pr_view()
+
+  vim.print(vim.inspect(review))
+
+  vim.ui.select({
+    'OK',
+    'Cancel',
+  }, {
+    prompt = 'Submit review?',
+  }, function(choice)
+    vim.print(choice)
+    if choice == 'OK' then
+      for _, comment in ipairs(review.comments) do
+        M.pr_comment(pr.repo, pr.pull, pr.head, comment)
+      end
+    end
+  end)
+end
+
+return M