@@ -13,102 +13,59 @@ const SCOPES = ["repo", "gist"];
1313 * Handles authentication to GitHub, using the VS Code [authentication API](https://code.visualstudio.com/api/references/vscode-api#authentication).
1414 */
1515export class Credentials {
16+ /**
17+ * A specific octokit to return, otherwise a new authenticated octokit will be created when needed.
18+ */
1619 private octokit : Octokit . Octokit | undefined ;
1720
1821 // Explicitly make the constructor private, so that we can't accidentally call the constructor from outside the class
1922 // without also initializing the class.
20- // eslint-disable-next-line @typescript-eslint/no-empty-function
21- private constructor ( ) { }
23+ private constructor ( octokit ?: Octokit . Octokit ) {
24+ this . octokit = octokit ;
25+ }
2226
2327 /**
24- * Initializes an instance of credentials with an octokit instance.
28+ * Initializes a Credentials instance. This will generate octokit instances
29+ * authenticated as the user. If there is not already an authenticated GitHub
30+ * session available then the user will be prompted to log in.
2531 *
26- * Do not call this method until you know you actually need an instance of credentials.
27- * since calling this method will require the user to log in.
28- *
29- * @param context The extension context.
3032 * @returns An instance of credentials.
3133 */
32- static async initialize (
33- context : vscode . ExtensionContext ,
34- ) : Promise < Credentials > {
35- const c = new Credentials ( ) ;
36- c . registerListeners ( context ) ;
37- c . octokit = await c . createOctokit ( false ) ;
38- return c ;
34+ static async initialize ( ) : Promise < Credentials > {
35+ return new Credentials ( ) ;
3936 }
4037
4138 /**
4239 * Initializes an instance of credentials with an octokit instance using
43- * a token from the user's GitHub account . This method is meant to be
44- * used non-interactive environments such as tests.
40+ * a specific known token . This method is meant to be used in
41+ * non-interactive environments such as tests.
4542 *
4643 * @param overrideToken The GitHub token to use for authentication.
4744 * @returns An instance of credentials.
4845 */
4946 static async initializeWithToken ( overrideToken : string ) {
50- const c = new Credentials ( ) ;
51- c . octokit = await c . createOctokit ( false , overrideToken ) ;
52- return c ;
53- }
54-
55- private async createOctokit (
56- createIfNone : boolean ,
57- overrideToken ?: string ,
58- ) : Promise < Octokit . Octokit | undefined > {
59- if ( overrideToken ) {
60- return new Octokit . Octokit ( { auth : overrideToken , retry } ) ;
61- }
62-
63- const session = await vscode . authentication . getSession (
64- GITHUB_AUTH_PROVIDER_ID ,
65- SCOPES ,
66- { createIfNone } ,
67- ) ;
68-
69- if ( session ) {
70- return new Octokit . Octokit ( {
71- auth : session . accessToken ,
72- retry,
73- } ) ;
74- } else {
75- return undefined ;
76- }
77- }
78-
79- registerListeners ( context : vscode . ExtensionContext ) : void {
80- // Sessions are changed when a user logs in or logs out.
81- context . subscriptions . push (
82- vscode . authentication . onDidChangeSessions ( async ( e ) => {
83- if ( e . provider . id === GITHUB_AUTH_PROVIDER_ID ) {
84- this . octokit = await this . createOctokit ( false ) ;
85- }
86- } ) ,
87- ) ;
47+ return new Credentials ( new Octokit . Octokit ( { auth : overrideToken , retry } ) ) ;
8848 }
8949
9050 /**
9151 * Creates or returns an instance of Octokit.
9252 *
93- * @param requireAuthentication Whether the Octokit instance needs to be authenticated as user.
9453 * @returns An instance of Octokit.
9554 */
96- async getOctokit ( requireAuthentication = true ) : Promise < Octokit . Octokit > {
55+ async getOctokit ( ) : Promise < Octokit . Octokit > {
9756 if ( this . octokit ) {
9857 return this . octokit ;
9958 }
10059
101- this . octokit = await this . createOctokit ( requireAuthentication ) ;
102-
103- if ( ! this . octokit ) {
104- if ( requireAuthentication ) {
105- throw new Error ( "Did not initialize Octokit." ) ;
106- }
60+ const session = await vscode . authentication . getSession (
61+ GITHUB_AUTH_PROVIDER_ID ,
62+ SCOPES ,
63+ { createIfNone : true } ,
64+ ) ;
10765
108- // We don't want to set this in this.octokit because that would prevent
109- // authenticating when requireCredentials is true.
110- return new Octokit . Octokit ( { retry } ) ;
111- }
112- return this . octokit ;
66+ return new Octokit . Octokit ( {
67+ auth : session . accessToken ,
68+ retry,
69+ } ) ;
11370 }
11471}
0 commit comments