207207 (write-region (prin1-to-string obj) nil file nil :silent )
208208 obj))
209209
210- (defun gptel--gh-login ()
211- " Manage github login."
210+ (defun gptel-gh-login ()
211+ " Login to GitHub Copilot API.
212+
213+ This will prompt you to authorize in a browser and store the token."
214+ (interactive )
212215 (pcase-let (((map :device_code :user_code :verification_uri )
213216 (gptel--url-retrieve
214- " https://github.com/login/device/code"
215- :method 'post
216- :headers gptel--gh-auth-common-headers
217- :data `( :client_id , gptel--gh-client-id
218- :scope " read:user" ))))
217+ " https://github.com/login/device/code"
218+ :method 'post
219+ :headers gptel--gh-auth-common-headers
220+ :data `( :client_id , gptel--gh-client-id
221+ :scope " read:user" ))))
219222 (gui-set-selection 'CLIPBOARD user_code)
220223 (read-from-minibuffer
221224 (format " Your one-time code %s is copied. \
@@ -227,15 +230,20 @@ If your browser does not open automatically, browse to %s."
227230 (thread-last
228231 (plist-get
229232 (gptel--url-retrieve
230- " https://github.com/login/oauth/access_token"
231- :method 'post
232- :headers gptel--gh-auth-common-headers
233- :data `( :client_id , gptel--gh-client-id
234- :device_code , device_code
235- :grant_type " urn:ietf:params:oauth:grant-type:device_code" ))
233+ " https://github.com/login/oauth/access_token"
234+ :method 'post
235+ :headers gptel--gh-auth-common-headers
236+ :data `( :client_id , gptel--gh-client-id
237+ :device_code , device_code
238+ :grant_type " urn:ietf:params:oauth:grant-type:device_code" ))
236239 :access_token )
237240 (gptel--gh-save gptel-gh-github-token-file)
238- (setf (gptel--gh-github-token gptel-backend)))))
241+ (setf (gptel--gh-github-token gptel-backend))))
242+ (if (and (gptel--gh-github-token gptel-backend)
243+ (not (string-empty-p
244+ (gptel--gh-github-token gptel-backend))))
245+ (message " Successfully logged in to GitHub Copilot " )
246+ (user-error " Error: You might not have access to GitHub Copilot Chat!" )))
239247
240248(defun gptel--gh-renew-token ()
241249 " Renew session token."
@@ -249,7 +257,7 @@ If your browser does not open automatically, browse to %s."
249257 (if (not (plist-get token :token ))
250258 (progn
251259 (setf (gptel--gh-github-token gptel-backend) nil )
252- (user-error " Error: You might not have access to Github Copilot Chat!" ))
260+ (user-error " Error: You might not have access to GitHub Copilot Chat!" ))
253261 (thread-last
254262 (gptel--gh-save gptel-gh-token-file token)
255263 (setf (gptel--gh-token gptel-backend))))))
@@ -263,7 +271,7 @@ Then we need a session token."
263271 (let ((token (gptel--gh-restore gptel-gh-github-token-file)))
264272 (if token
265273 (setf (gptel--gh-github-token gptel-backend) token)
266- (gptel-- gh-login))))
274+ (gptel-gh-login))))
267275
268276 (when (null (gptel--gh-token gptel-backend))
269277 ; ; try to load token from `gptel-gh-token-file'
0 commit comments