Skip to content

Commit 6b653f8

Browse files
committed
🐛 *fix* Fix API key retrieval and validation logic
1 parent 35fb352 commit 6b653f8

File tree

8 files changed

+155
-36
lines changed

8 files changed

+155
-36
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
trim_trailing_whitespace = true
5+
6+
[*.lua]
7+
indent_style = space
8+
indent_size = 4

lua/commit-ai/backends/common.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local Log = require("commit-ai.log")
2+
local M = {}
3+
4+
---@param job Job
5+
function M.register_job(job)
6+
table.insert(M.current_jobs, job)
7+
Log.info("Registered completion job")
8+
end
9+
10+
return M

lua/commit-ai/backends/gemini.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
local Utils = require("commit-ai.utils")
2+
local Job = require('plenary.job')
3+
local Log = require("commit-ai.log")
4+
local Common = require("commit-ai.backends.common")
5+
6+
local M = {}
7+
8+
function M.is_available()
9+
local config = require("commit-ai.config")
10+
return Utils.get_api_key(config.provider_options.gemini.api_key) and true or false
11+
end
12+
13+
if not M.is_available() then
14+
Log.error("Gemini API_KEY is not set")
15+
end
16+
17+
function M.query_ai(prompt, cb)
18+
local config = require("commit-ai.config")
19+
local options = vim.deepcopy(config.provider_options.gemini)
20+
21+
local request_data = {
22+
contents = {
23+
{
24+
parts = {
25+
{
26+
text = prompt
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
local tmp_file = os.tmpname()
34+
local f = io.open(tmp_file, "w")
35+
if f ~= nil then
36+
f:write(vim.json.encode(request_data))
37+
f:close()
38+
end
39+
40+
local args = {
41+
'-X', 'POST',
42+
string.format(
43+
'https://generativelanguage.googleapis.com/v1beta/models/%s:%skey=%s',
44+
options.model,
45+
options.stream and 'streamGenerateContent?alt=sse&' or 'generateContent?',
46+
Utils.get_api_key(options.api_key)
47+
),
48+
'-H', 'Content-Type: application/json',
49+
'-d', '@' .. tmp_file
50+
}
51+
52+
53+
local new_job = Job:new {
54+
command = "curl",
55+
args = args,
56+
on_exit = vim.schedule_wrap(function(job, exit_code)
57+
os.remove(tmp_file)
58+
59+
if exit_code ~= 0 then
60+
Log.error("API request failed with exit code " .. exit_code)
61+
if cb then cb(nil) end
62+
return
63+
end
64+
65+
local result = table.concat(job:result(), "\n")
66+
67+
local success, parsed = pcall(vim.json.decode, result)
68+
69+
if not success then
70+
Log.error("Failed to parse JSON API response: " .. parsed)
71+
if cb then cb(nil) end
72+
return
73+
end
74+
75+
if cb then cb(parsed) end
76+
end)
77+
}
78+
79+
Common.register_job(new_job)
80+
new_job:start()
81+
82+
return new_job
83+
end
84+
85+
return M

lua/commit-ai/commit.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local M = {}
22

33

4+
45
return M

lua/commit-ai/commons.lua

Whitespace-only changes.

lua/commit-ai/config.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
local M = {}
2-
3-
-- default config
4-
M.config = {
1+
local M = {
2+
-- default config
53
conventions = {
64
docs = { icon = "📖", prefix = "docs", type = "Documentation changes" },
75
fix = { icon = "🐛", prefix = "fix", type = "Bug fix" },
@@ -10,17 +8,19 @@ M.config = {
108
chore = { icon = "🧹", prefix = "chore", type = "Chore" },
119
refactor = { icon = "⚠️", prefix = "refactor", type = "Breaking change" }
1210
},
13-
provider_options = {
14-
openai = {
15-
api_key = 'YOUR_API_KEY',
16-
},
17-
gemini = {
18-
api_key = 'YOUR_API_KEY',
19-
},
20-
claude = {
21-
api_key = 'YOUR_API_KEY',
22-
},
23-
}
2411
}
2512

13+
M.provider_options = {
14+
openai = {
15+
api_key = 'YOUR_API_KEY',
16+
},
17+
gemini = {
18+
api_key = 'YOUR_API_KEY',
19+
},
20+
claude = {
21+
api_key = 'YOUR_API_KEY',
22+
},
23+
}
24+
25+
2626
return M

lua/commit-ai/log.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ local log = {}
33
local p = "[commit-ai.nvim] "
44

55
function log.info(txt)
6-
vim.notify(p .. txt)
6+
vim.notify(p .. txt)
77
end
88

99
function log.error(txt)
10-
vim.notify(p .. txt)
10+
vim.notify(p .. txt)
1111
end
1212

1313
return log

lua/commit-ai/utils.lua

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@ local log = require("commit-ai.log")
22
local M = {}
33

44
function M.get_git_diff()
5-
local staged_diff = vim.fn.system('git diff --cached --no-color')
6-
if staged_diff == "" then
7-
staged_diff = vim.fn.system('git diff --no-color')
8-
end
9-
staged_diff = staged_diff:gsub("^%s*(.-)%s*$", "%1") -- Remove trailing newlines
10-
11-
-- Check if we're in a git repository
12-
if vim.fn.system('git rev-parse --is-inside-work-tree 2>/dev/null'):find("true") == nil then
13-
log.error("Not in a git repository")
14-
return nil
15-
end
16-
17-
-- Check if there are any changes
18-
if staged_diff == "" then
19-
log.warn("No changes detected")
20-
return nil
21-
end
22-
23-
return staged_diff
5+
local staged_diff = vim.fn.system('git diff --cached --no-color')
6+
if staged_diff == "" then
7+
staged_diff = vim.fn.system('git diff --no-color')
8+
end
9+
staged_diff = staged_diff:gsub("^%s*(.-)%s*$", "%1") -- Remove trailing newlines
10+
11+
-- Check if we're in a git repository
12+
if vim.fn.system('git rev-parse --is-inside-work-tree 2>/dev/null'):find("true") == nil then
13+
log.error("Not in a git repository")
14+
return nil
15+
end
16+
17+
-- Check if there are any changes
18+
if staged_diff == "" then
19+
log.warn("No changes detected")
20+
return nil
21+
end
22+
23+
return staged_diff
24+
end
25+
26+
function M.get_api_key(env_var)
27+
local api_key
28+
if type(env_var) == 'function' then
29+
api_key = env_var()
30+
elseif type(env_var) == 'string' then
31+
api_key = vim.env[env_var]
32+
end
33+
34+
if type(api_key) ~= 'string' or api_key == '' then
35+
return nil
36+
end
37+
38+
return api_key
2439
end
2540

2641
return M

0 commit comments

Comments
 (0)