@@ -3,23 +3,56 @@ import * as vscode from "vscode";
33const AUTH_PROVIDER_ID = "github" ;
44const DEFAULT_SCOPES = [ "repo" , "workflow" ] ;
55
6+ let signInPrompted = false ;
7+
8+ const SESSION_ERROR = "Could not get token from the GitHub authentication provider. \nPlease sign-in and allow access." ;
9+
610/**
7- * Retrieves a session from the GitHub authentication provider
8- * @param forceMessage Force a new session with a prompt to the user
11+ * Creates a session from the GitHub authentication provider
12+ * @param forceMessage Prompt to the user when forcing a new session
913 * @returns A {@link vscode.AuthenticationSession}
1014 */
11- export async function getSession ( forceMessage ?: string ) : Promise < vscode . AuthenticationSession > {
12- // forceNewSession and createIfNone are mutually exclusive
13- const options : vscode . AuthenticationGetSessionOptions = forceMessage
14- ? { forceNewSession : { detail : forceMessage } }
15- : { createIfNone : true } ;
16- const existingSession = await vscode . authentication . getSession ( AUTH_PROVIDER_ID , getScopes ( ) , options ) ;
15+ export async function newSession ( forceMessage : string ) : Promise < vscode . AuthenticationSession > {
16+ const session = await getSessionInternal ( forceMessage ) ;
17+ if ( session ) {
18+ return session ;
19+ }
20+ throw new Error ( SESSION_ERROR ) ;
21+ }
1722
18- if ( ! existingSession ) {
19- throw new Error ( "Could not get token from the GitHub authentication provider. \nPlease sign-in and allow access." ) ;
23+ /**
24+ * Retrieves a session from the GitHub authentication provider or prompts the user to sign in
25+ * @returns A {@link vscode.AuthenticationSession} or undefined
26+ */
27+ export async function getSession ( ) : Promise < vscode . AuthenticationSession | undefined > {
28+ const session = await getSessionInternal ( false ) ;
29+ if ( session || signInPrompted ) {
30+ return session ;
2031 }
2132
22- return existingSession ;
33+ signInPrompted = true ;
34+ const signInAction = "Sign in to GitHub" ;
35+ const result = await vscode . window . showInformationMessage (
36+ "Sign in to GitHub to access your repositories and GitHub Actions workflows." ,
37+ signInAction
38+ ) ;
39+ if ( result === signInAction ) {
40+ return await getSessionInternal ( true ) ;
41+ }
42+ throw new Error ( SESSION_ERROR ) ;
43+ }
44+
45+ async function getSessionInternal ( forceNewMessage : string ) : Promise < vscode . AuthenticationSession | undefined > ;
46+ async function getSessionInternal ( createIfNone : boolean ) : Promise < vscode . AuthenticationSession | undefined > ;
47+ async function getSessionInternal (
48+ createOrForceMessage : string | boolean = false
49+ ) : Promise < vscode . AuthenticationSession | undefined > {
50+ // forceNewSession and createIfNone are mutually exclusive
51+ const options : vscode . AuthenticationGetSessionOptions =
52+ typeof createOrForceMessage === "string"
53+ ? { forceNewSession : { detail : createOrForceMessage } }
54+ : { createIfNone : createOrForceMessage } ;
55+ return await vscode . authentication . getSession ( AUTH_PROVIDER_ID , getScopes ( ) , options ) ;
2356}
2457
2558function getScopes ( ) : string [ ] {
0 commit comments