Skip to content

Commit 04f48a9

Browse files
committed
Re #189. Create a statusbar item for authentication.
1 parent 7bbae73 commit 04f48a9

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/github/githubRepository.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface PullRequestData {
2323
export class GitHubRepository implements IGitHubRepository {
2424
private _octokit: Octokit;
2525
private _initialized: boolean;
26+
private _authenticationStatus: vscode.StatusBarItem;
2627
public get octokit(): Octokit {
2728
if (this._octokit === undefined) {
2829
if (!this._initialized) {
@@ -34,7 +35,32 @@ export class GitHubRepository implements IGitHubRepository {
3435
return this._octokit;
3536
}
3637

38+
public set octokit(newOctokit: Octokit) {
39+
if (newOctokit !== this._octokit) {
40+
this._octokit = newOctokit;
41+
42+
// update status bar item
43+
this.updateAuthenticationStatus();
44+
}
45+
}
46+
3747
constructor(public readonly remote: Remote, private readonly _credentialStore: CredentialStore) {
48+
this._authenticationStatus = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
49+
const normalizedUri = this.remote.gitProtocol.normalizeUri();
50+
this._authenticationStatus.text = `${remote.owner}/${remote.repositoryName} ⟷ Sign in to ${normalizedUri.authority}`;
51+
this._authenticationStatus.command = 'pr.signin';
52+
this._authenticationStatus.show();
53+
}
54+
55+
updateAuthenticationStatus() {
56+
setTimeout(async () => {
57+
if (this._octokit) {
58+
const currentUser = await this._octokit.users.get({});
59+
const userName = currentUser.data.login;
60+
this._authenticationStatus.text = `${this.remote.owner}/${this.remote.repositoryName}${userName}`;
61+
this._authenticationStatus.command = null;
62+
}
63+
}, 0);
3864
}
3965

4066
async ensure(): Promise<GitHubRepository> {
@@ -47,10 +73,10 @@ export class GitHubRepository implements IGitHubRepository {
4773
SIGNIN_COMMAND);
4874

4975
if (result === SIGNIN_COMMAND) {
50-
this._octokit = await this._credentialStore.login(this.remote);
76+
this.octokit = await this._credentialStore.login(this.remote);
5177
}
5278
} else {
53-
this._octokit = await this._credentialStore.getOctokit(this.remote);
79+
this.octokit = await this._credentialStore.getOctokit(this.remote);
5480
}
5581

5682
return this;
@@ -59,11 +85,11 @@ export class GitHubRepository implements IGitHubRepository {
5985
async authenticate(): Promise<boolean> {
6086
this._initialized = true;
6187
if (!await this._credentialStore.hasOctokit(this.remote)) {
62-
this._octokit = await this._credentialStore.login(this.remote);
88+
this.octokit = await this._credentialStore.login(this.remote);
6389
} else {
64-
this._octokit = this._credentialStore.getOctokit(this.remote);
90+
this.octokit = this._credentialStore.getOctokit(this.remote);
6591
}
66-
return this._octokit !== undefined;
92+
return this.octokit !== undefined;
6793
}
6894

6995
async getDefaultBranch(): Promise<string> {

0 commit comments

Comments
 (0)