Skip to content

Commit e166375

Browse files
authored
Merge pull request #2 from editor-code-assistant/fix-gh-rl
Make authenticated GitHub API request
2 parents 70978b3 + c441c0f commit e166375

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ mise use -g [email protected]
3232
eca --version
3333
```
3434

35+
## GitHub API Rate Limiting
36+
37+
This plugin fetches ECA releases from GitHub. To avoid rate limiting issues, you can optionally set a GitHub token:
38+
39+
```bash
40+
# Set one of these environment variables:
41+
export GITHUB_TOKEN="your_token_here"
42+
# or
43+
export GH_TOKEN="your_token_here"
44+
# or
45+
export GITHUB_API_TOKEN="your_token_here"
46+
```
47+
48+
You can create a personal access token at https://github.com/settings/tokens (no special permissions needed for public repositories).
49+
3550
## Usage
3651

3752
After installation, you can use ECA with your preferred editor:

hooks/available.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
-- hooks/available.lua
22
function PLUGIN:Available(ctx)
3-
local http = require("http")
3+
local helper = require("helper")
44
local json = require("json")
5-
local resp, err = http.get({url = "https://api.github.com/repos/editor-code-assistant/eca/releases"})
6-
if err or resp.status_code ~= 200 then
7-
error("Failed to fetch releases: " .. (err or resp.status_code))
5+
6+
local resp, err = helper.github_request("https://api.github.com/repos/editor-code-assistant/eca/releases")
7+
if err then
8+
error("Failed to fetch releases: " .. err)
89
end
10+
911
local releases = json.decode(resp.body)
1012
local result = {}
1113
for i, r in ipairs(releases) do

hooks/pre_install.lua

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
-- hooks/pre_install.lua
22
function PLUGIN:PreInstall(ctx)
33
local version = ctx.version
4-
local http = require("http")
4+
local helper = require("helper")
55

6-
-- Normalize architecture
7-
local arch = RUNTIME.archType:lower()
8-
local arch_token
9-
if arch == "amd64" or arch == "x86_64" then
10-
arch_token = "amd64"
11-
elseif arch == "arm64" or arch == "aarch64" then
12-
arch_token = "aarch64"
13-
else
14-
error("Unsupported architecture: " .. RUNTIME.archType)
15-
end
16-
17-
-- Normalize OS
18-
local os_l = RUNTIME.osType:lower()
19-
local os_token
20-
if os_l:find("windows") then
21-
os_token = "windows"
22-
elseif os_l:find("darwin") then
23-
os_token = "macos"
24-
elseif os_l:find("linux") then
25-
os_token = "linux"
26-
else
27-
error("Unsupported OS: " .. RUNTIME.osType)
28-
end
6+
-- Get normalized OS and architecture
7+
local os_token = helper.get_os()
8+
local arch_token = helper.get_arch()
299

3010
-- Construct filename (dynamic builds)
3111
local filename = string.format("eca-native-%s-%s.zip", os_token, arch_token)
@@ -34,8 +14,8 @@ function PLUGIN:PreInstall(ctx)
3414
-- Fetch checksum
3515
local sha256
3616
do
37-
local resp, err = http.get({url = url .. ".sha256"})
38-
if not err and resp.status_code == 200 then
17+
local resp, err = helper.github_request(url .. ".sha256")
18+
if not err and resp then
3919
sha256 = resp.body:match("^(%w+)")
4020
end
4121
end

lib/helper.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,45 @@ end
2525
function M.get_platform()
2626
return M.get_os() .. "-" .. M.get_arch()
2727
end
28+
29+
-- Make authenticated GitHub API request
30+
function M.github_request(url)
31+
local http = require("http")
32+
local headers = {}
33+
34+
-- Try to get GitHub token from environment
35+
local token = os.getenv("GITHUB_TOKEN") or os.getenv("GITHUB_API_TOKEN") or os.getenv("GH_TOKEN")
36+
if token then
37+
headers["Authorization"] = "Bearer " .. token
38+
end
39+
40+
-- Set User-Agent (required by GitHub API)
41+
headers["User-Agent"] = "mise-vfox-eca-plugin"
42+
43+
local resp, err = http.get({
44+
url = url,
45+
headers = headers
46+
})
47+
48+
if err then
49+
return nil, "HTTP request failed: " .. err
50+
end
51+
52+
if resp.status_code == 403 then
53+
local err_msg = "GitHub API rate limit exceeded. "
54+
if not token then
55+
err_msg = err_msg .. "Please set GITHUB_TOKEN environment variable to authenticate and increase rate limit."
56+
else
57+
err_msg = err_msg .. "Authenticated request failed with 403."
58+
end
59+
return nil, err_msg
60+
end
61+
62+
if resp.status_code ~= 200 then
63+
return nil, "GitHub API returned status code: " .. resp.status_code
64+
end
65+
66+
return resp, nil
67+
end
68+
2869
return M

0 commit comments

Comments
 (0)