summary refs log tree commit diff
path: root/.config/nvim/lua/mod/gh.lua
blob: 1f8a267d768ca8ae50cbca476aa0f547d76c09aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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