-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgithub.lua
329 lines (274 loc) · 8.77 KB
/
github.lua
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
local Job = require("plenary.job")
local utils = require("cmp_git.utils")
local sort = require("cmp_git.sort")
local log = require("cmp_git.log")
local format = require("cmp_git.format")
local GitHub = {
cache = {
issues = {},
mentions = {},
pull_requests = {},
},
config = {},
}
GitHub.new = function(overrides)
local self = setmetatable({}, {
__index = GitHub,
})
self.config = vim.tbl_deep_extend("force", require("cmp_git.config").github, overrides or {})
if overrides.filter_fn then
self.config.format.filterText = overrides.filter_fn
end
table.insert(self.config.hosts, "github.com")
GitHub.config = self.config
return self
end
-- build a github api url
local github_url = function(git_host, path)
local url = ""
if git_host == "github.com" then
url = "https://api.github.com"
else
url = string.format("https://%s/api/v3/%s", git_host, path)
end
return url
end
local get_items = function(callback, gh_args, curl_url, handle_item, handle_parsed)
local gh_job = utils.build_job("gh", callback, gh_args, handle_item, handle_parsed)
curl_args = {
"curl",
"-s",
"-L",
"-H",
"'Accept: application/vnd.github.v3+json'",
curl_url,
}
if vim.fn.exists("$GITHUB_API_TOKEN") == 1 then
local token = vim.fn.getenv("GITHUB_API_TOKEN")
local authorization_header = string.format("Authorization: token %s", token)
table.insert(curl_args, "-H")
table.insert(curl_args, authorization_header)
end
local curl_job = utils.build_job("curl", callback, curl_args, handle_item, handle_parsed)
return utils.chain_fallback(gh_job, curl_job)
end
local get_pull_requests_job = function(callback, git_info, trigger_char, config)
return get_items(
callback,
{
"pr",
"list",
"--repo",
string.format("%s/%s/%s", git_info.host, git_info.owner, git_info.repo),
"--limit",
config.limit,
"--state",
config.state,
"--json",
table.concat(config.fields, ","),
},
github_url(
git_info.host,
string.format(
"repos/%s/%s/pulls?state=%s&per_page=%d&page=%d",
git_info.owner,
git_info.repo,
config.state,
config.limit,
1
)
),
function(pr)
if pr.body ~= vim.NIL then
pr.body = string.gsub(pr.body or "", "\r", "")
else
pr.body = ""
end
if not pr.updatedAt then
pr.updatedAt = pr.updated_at
end
return format.item(config, trigger_char, pr)
end
)
end
local get_issues_job = function(callback, git_info, trigger_char, config)
return get_items(
callback,
{
"issue",
"list",
"--repo",
string.format("%s/%s/%s", git_info.host, git_info.owner, git_info.repo),
"--limit",
config.limit,
"--state",
config.state,
"--json",
table.concat(config.fields, ","),
},
github_url(
git_info.host,
string.format(
"repos/%s/%s/issues?filter=%s&state=%s&per_page=%d&page=%d",
git_info.owner,
git_info.repo,
config.filter,
config.state,
config.limit,
1
)
),
function(issue)
if issue.body ~= vim.NIL then
issue.body = string.gsub(issue.body or "", "\r", "")
else
issue.body = ""
end
if not issue.updatedAt then
issue.updatedAt = issue.updated_at
end
return format.item(config, trigger_char, issue)
end
)
end
local use_gh_default_repo_if_set = function(git_info)
local gh_default_repo = vim.fn.system({ 'gh', 'repo', 'set-default', '--view' })
if vim.v.shell_error ~= 0 then
return git_info
end
local owner, repo = string.match(vim.fn.trim(gh_default_repo), "^(.+)/(.+)$")
if owner ~= nil and repo ~= nil then
git_info.owner = owner
git_info.repo = repo
end
return git_info
end
function GitHub:is_valid_host(git_info)
if
git_info.host == nil
or git_info.owner == nil
or git_info.repo == nil
or not vim.tbl_contains(GitHub.config.hosts, git_info.host)
then
return false
end
return true
end
function GitHub:_get_issues(callback, git_info, trigger_char)
local config = self.config.issues
local bufnr = vim.api.nvim_get_current_buf()
if self.cache.issues[bufnr] then
callback({ items = self.cache.issues[bufnr], isIncomplete = false })
return nil
end
local issues_job = get_issues_job(function(args)
self.cache.issues[bufnr] = args.items
callback(args)
end, git_info, trigger_char, config)
return issues_job
end
function GitHub:_get_pull_requests(callback, git_info, trigger_char)
local config = self.config.pull_requests
local bufnr = vim.api.nvim_get_current_buf()
if self.cache.pull_requests[bufnr] then
callback({ items = self.cache.pull_requests[bufnr], isIncomplete = false })
return nil
end
local pr_job = get_pull_requests_job(function(args)
self.cache.pull_requests[bufnr] = args.items
callback(args)
end, git_info, trigger_char, config)
return pr_job
end
function GitHub:get_issues(callback, git_info, trigger_char)
if not GitHub:is_valid_host(git_info) then
return false
end
git_info = use_gh_default_repo_if_set(git_info)
local job = self:_get_issues(callback, git_info, trigger_char)
if job then
job:start()
end
return true
end
function GitHub:get_pull_requests(callback, git_info, trigger_char)
if not GitHub:is_valid_host(git_info) then
return false
end
git_info = use_gh_default_repo_if_set(git_info)
local job = self:_get_pull_requests(callback, git_info, trigger_char)
if job then
job:start()
end
return true
end
function GitHub:get_issues_and_prs(callback, git_info, trigger_char)
if not GitHub:is_valid_host(git_info) then
return false
end
git_info = use_gh_default_repo_if_set(git_info)
local bufnr = vim.api.nvim_get_current_buf()
if self.cache.issues[bufnr] and self.cache.pull_requests[bufnr] then
local issues = self.cache.issues[bufnr]
local prs = self.cache.pull_requests[bufnr]
local items = {}
items = vim.list_extend(items, issues)
items = vim.list_extend(items, prs)
log.fmt_debug("Got %d issues and prs from cache", #items)
callback({ items = issues, isIncomplete = false })
else
local items = {}
local issues_job = self:_get_issues(function(args)
items = args.items
self.cache.issues[bufnr] = args.items
end, git_info, trigger_char)
local pull_requests_job = self:_get_pull_requests(function(args)
local prs = args.items
self.cache.pull_requests[bufnr] = args.items
items = vim.list_extend(items, prs)
log.fmt_debug("Got %d issues and prs from GitHub", #items)
callback({ items = items, isIncomplete = false })
end, git_info, trigger_char)
Job.chain(issues_job, pull_requests_job)
end
return true
end
function GitHub:get_mentions(callback, git_info, trigger_char)
if not GitHub:is_valid_host(git_info) then
return false
end
local config = self.config.mentions
local bufnr = vim.api.nvim_get_current_buf()
if self.cache.mentions[bufnr] then
callback({ items = self.cache.mentions[bufnr], isIncomplete = false })
return true
end
local job = get_items(
function(args)
callback(args)
self.cache.mentions[bufnr] = args.items
end,
{
"api",
string.format("repos/%s/%s/contributors", git_info.owner, git_info.repo),
"--hostname",
git_info.host,
},
github_url(
git_info.host,
string.format("%s/%s/contributors?per_page=%d&page=%d", git_info.owner, git_info.repo, config.limit, 1)
),
function(mention)
return format.item(config, trigger_char, mention)
end,
function(parsed)
if parsed["mentionableUsers"] then
return parsed["mentionableUsers"]
end
return parsed
end
)
job:start()
return true
end
return GitHub