Skip to content

Commit 923e30b

Browse files
committed
fix: GitHub token process improvements and incorrect documentation
1 parent f49d6e3 commit 923e30b

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

README.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ As lua is far more efficient and makes things easier to integrate with modern pl
1515

1616
- Curl
1717
- NeoVim 0.10.0 or higher
18+
- NodeJS v20 or higher if using the default nodejs LSP version
1819

1920
## Install
2021

@@ -29,21 +30,27 @@ use { "zbirenbaum/copilot.lua" }
2930

3031
You can authenticate using one of the following methods:
3132

32-
#### Permanent sign-in (Recommended)
33+
<details>
34+
<summary>Permanent sign-in (Recommended)</summary>
3335

3436
Once copilot is running, run `:Copilot auth` to start the authentication process.
3537

36-
#### Token
38+
</details>
3739

38-
Get a token from the github cli using:
40+
<details>
41+
<summary>Token (not officially supported)</summary>
3942

40-
```sh
41-
gh auth token
42-
```
43+
Tokens given by `gh auth token` do not support Copilot, you therefore need to first generate a token through the LSP by:
44+
45+
- Authenticating using the `Permanent sign-in` method
46+
- Grab the token by running `:Copilot auth info`
47+
- You can then safely delete the `github-copilot` folder created in your NeoVim base data directory.
4348

4449
Set either the environment variable `GITHUB_COPILOT_TOKEN` or `GH_COPILOT_TOKEN` to that token.
4550
Note that if you have the variable set, even empty, the LSP will attempt to use it to log in.
4651

52+
</details>
53+
4754
#### Authentication with Alternate GitHub Instances
4855

4956
If your access to Copilot is not provided by the public GitHub instance, you can set your
@@ -385,7 +392,7 @@ example:
385392
require("copilot").setup {
386393
server = {
387394
type = "nodejs",
388-
custom_server_filepath = "/home/user/copilot-lsp/language-server.js",,
395+
custom_server_filepath = "/home/user/copilot-lsp/language-server.js",
389396
},
390397
}
391398
```

lua/copilot/auth/init.lua

+26-9
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function M.signout()
132132
end)
133133
end
134134

135-
local function find_config_path()
135+
function M.find_config_path()
136136
local config = vim.fn.expand("$XDG_CONFIG_HOME")
137137
if config and vim.fn.isdirectory(config) > 0 then
138138
return config
@@ -151,16 +151,33 @@ local function find_config_path()
151151
end
152152
end
153153

154-
local function oauth_user(token)
155-
return vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
154+
M.get_creds = function()
155+
local filename = M.find_config_path() .. "/github-copilot/apps.json"
156+
157+
if vim.fn.filereadable(filename) == 0 then
158+
logger.error("Copilot auth file could not be read from:" .. filename)
159+
return
160+
end
161+
162+
local filedata = vim.api.nvim_eval("readfile('" .. filename .. "')")
163+
164+
if not filedata or #filedata == 0 then
165+
logger.error("Copilot's apps.json file not found or empty, make sure to sign in first")
166+
return
167+
end
168+
169+
local appsdata = vim.json.decode(filedata[1])
170+
return appsdata
156171
end
157172

158-
M.get_cred = function()
159-
local userdata =
160-
vim.json.decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1])
161-
local token = userdata["github.com"].oauth_token
162-
local user = oauth_user(token)
163-
return { user = user, token = token }
173+
function M.info()
174+
local info = M.get_creds()
175+
if not info then
176+
logger.error("no GitHub Copilot token found, make sure to sign in first")
177+
return
178+
end
179+
180+
logger.notify("GitHub Copilot token information: ", info)
164181
end
165182

166183
return M

plugin/copilot.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local completion_store = {
22
[""] = { "auth", "attach", "detach", "disable", "enable", "panel", "status", "suggestion", "toggle", "version" },
3-
auth = { "signin", "signout" },
3+
auth = { "signin", "signout", "info" },
44
panel = { "accept", "jump_next", "jump_prev", "open", "refresh" },
55
suggestion = { "accept", "accept_line", "accept_word", "dismiss", "next", "prev", "toggle_auto_trigger" },
66
workspace = { "add" },

0 commit comments

Comments
 (0)