-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.lua
More file actions
64 lines (56 loc) · 1.92 KB
/
Copy pathgithub_api.lua
File metadata and controls
64 lines (56 loc) · 1.92 KB
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
lunajson = require 'lunajson'
os_capture = require 'os_capture'
tprint = require 'tprint'
local function char_to_hex(c)
return string.format("%%%02X", string.byte(c))
end
local function urlencode(url)
-- from https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
if url == nil then
return
end
url = url:gsub("\n", "\r\n")
url = url:gsub("([^%w ])", char_to_hex)
url = url:gsub(" ", "+")
return url
end
function search_repositories(count)
-- maximum count of 100, requires `gh` to be available in the cmdline
count = count or 30 -- I could support higher than 100, but then I'd need to implement pagination
local search_urlquery = "per_page="..count ..
"&s=stars" ..
"&q="..urlencode("language:lua created:<2016 pushed:>=2024 archived:false fork:false")
local command = 'gh api' ..
' -H "Accept: application/vnd.github+json"' ..
' -H "X-GitHub-Api-Version: 2026-03-10"' ..
' "/search/repositories?' .. search_urlquery .. '"'
-- print(command)
local output = os_capture(command)
local parsed = lunajson.decode(output)
return parsed
end
function remove_repository(path)
return os_capture('rmdir /S /Q "' .. path .. '"')
end
function clone_repository(name, dir)
-- where name is formatted as "user/repo"
-- assumes existence of `cloned-repos` directory
-- REMOVES currently named repository
local repo = name:gsub(".*/", "")
local git_clone = "git clone -c core.protectNTFS=false https://github.com/" .. name .. ".git"
-- protectNTFS=false allows us to clone repos with colliding paths due to casing (which is fine on Linux, but not Windows)
local rm, c
if dir then
rm = remove_repository(dir .. "/" .. repo)
c = os_capture("cd " .. dir .. " & " .. git_clone)
else
rm = remove_repository(repo)
c = os_capture(git_clone)
end
return {rm, c}
end
return {
search_repositories = search_repositories,
remove_repository = remove_repository,
clone_repository = clone_repository,
}