Skip to content

Commit 42d2b28

Browse files
authored
Polish sign in again for projects (#5355)
1 parent cc3b638 commit 42d2b28

File tree

7 files changed

+40
-22
lines changed

7 files changed

+40
-22
lines changed

src/github/credentials.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ export class CredentialStore implements vscode.Disposable {
5454
private _isInitialized: boolean = false;
5555
private _onDidInitialize: vscode.EventEmitter<void> = new vscode.EventEmitter();
5656
public readonly onDidInitialize: vscode.Event<void> = this._onDidInitialize.event;
57-
private _scopes: string[] = SCOPES;
57+
private _scopes: string[] = SCOPES_OLD;
5858
private _scopesEnterprise: string[] = SCOPES_OLD;
5959

6060
private _onDidGetSession: vscode.EventEmitter<void> = new vscode.EventEmitter();
6161
public readonly onDidGetSession = this._onDidGetSession.event;
6262

63+
private _onDidUpgradeSession: vscode.EventEmitter<void> = new vscode.EventEmitter();
64+
public readonly onDidUpgradeSession = this._onDidUpgradeSession.event;
65+
6366
constructor(private readonly _telemetry: ITelemetry, private readonly context: vscode.ExtensionContext) {
6467
this.setScopesFromState();
6568

@@ -88,8 +91,8 @@ export class CredentialStore implements vscode.Disposable {
8891
}
8992

9093
private setScopesFromState() {
91-
this._scopes = this.context.globalState.get(LAST_USED_SCOPES_GITHUB_KEY, SCOPES);
92-
this._scopesEnterprise = this.context.globalState.get(LAST_USED_SCOPES_ENTERPRISE_KEY, SCOPES);
94+
this._scopes = this.context.globalState.get(LAST_USED_SCOPES_GITHUB_KEY, SCOPES_OLD);
95+
this._scopesEnterprise = this.context.globalState.get(LAST_USED_SCOPES_ENTERPRISE_KEY, SCOPES_OLD);
9396
}
9497

9598
private async saveScopesInState() {
@@ -111,7 +114,7 @@ export class CredentialStore implements vscode.Disposable {
111114

112115
let session: vscode.AuthenticationSession | undefined = undefined;
113116
let isNew: boolean = false;
114-
let usedScopes: string[] | undefined = SCOPES;
117+
let usedScopes: string[] | undefined = SCOPES_OLD;
115118
const oldScopes = this._scopes;
116119
const oldEnterpriseScopes = this._scopesEnterprise;
117120
const authResult: AuthResult = { canceled: false };
@@ -243,6 +246,9 @@ export class CredentialStore implements vscode.Disposable {
243246
public async getHubEnsureAdditionalScopes(authProviderId: AuthProvider): Promise<GitHub | undefined> {
244247
const hasScopesAlready = this.isAuthenticatedWithAdditionalScopes(authProviderId);
245248
await this.initialize(authProviderId, { createIfNone: !hasScopesAlready }, SCOPES_WITH_ADDITIONAL, true);
249+
if (!hasScopesAlready) {
250+
this._onDidUpgradeSession.fire();
251+
}
246252
return this.getHub(authProviderId);
247253
}
248254

@@ -355,8 +361,8 @@ export class CredentialStore implements vscode.Disposable {
355361
return { session: existingSession.session, isNew: false, scopes: existingSession.scopes };
356362
}
357363

358-
const session = await vscode.authentication.getSession(authProviderId, requireScopes ? scopes : SCOPES, getAuthSessionOptions);
359-
return { session, isNew: !!session, scopes: requireScopes ? scopes : SCOPES };
364+
const session = await vscode.authentication.getSession(authProviderId, requireScopes ? scopes : SCOPES_OLD, getAuthSessionOptions);
365+
return { session, isNew: !!session, scopes: requireScopes ? scopes : SCOPES_OLD };
360366
}
361367

362368
private async findExistingScopes(authProviderId: AuthProvider): Promise<{ session: vscode.AuthenticationSession, scopes: string[] } | undefined> {

src/github/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export interface Issue {
141141
updatedAt: string;
142142
user: IAccount;
143143
labels: ILabel[];
144-
projectItems: IProjectItem[];
144+
projectItems?: IProjectItem[];
145145
milestone?: IMilestone;
146146
repositoryOwner?: string;
147147
repositoryName?: string;

src/github/issueModel.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class IssueModel<TItem extends Issue = Issue> {
291291
},
292292
},
293293
})));
294-
this.item.projectItems = this.item.projectItems.filter(project => !projectItems.find(p => p.project.id === project.project.id));
294+
this.item.projectItems = this.item.projectItems?.filter(project => !projectItems.find(p => p.project.id === project.project.id));
295295
} catch (err) {
296296
Logger.error(err, IssueModel.ID);
297297
}
@@ -311,14 +311,17 @@ export class IssueModel<TItem extends Issue = Issue> {
311311
},
312312
},
313313
})));
314+
if (!this.item.projectItems) {
315+
this.item.projectItems = [];
316+
}
314317
this.item.projectItems.push(...projects.map((project, index) => { return { project, id: itemIds[index].data!.addProjectV2ItemById.item.id }; }));
315318
} catch (err) {
316319
Logger.error(err, IssueModel.ID);
317320
}
318321
}
319322

320-
async updateProjects(projects: IProject[]): Promise<IProjectItem[]> {
321-
const projectsToAdd: IProject[] = projects.filter(project => !this.item.projectItems.find(p => p.project.id === project.id));
323+
async updateProjects(projects: IProject[]): Promise<IProjectItem[] | undefined> {
324+
const projectsToAdd: IProject[] = projects.filter(project => !this.item.projectItems?.find(p => p.project.id === project.id));
322325
const projectsToRemove: IProjectItem[] = this.item.projectItems?.filter(project => !projects.find(p => p.id === project.project.id)) ?? [];
323326
await this.removeProjects(projectsToRemove);
324327
await this.addProjects(projectsToAdd);

src/github/pullRequestOverview.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
126126
});
127127
}),
128128
);
129+
this._disposables.push(folderRepositoryManager.credentialStore.onDidUpgradeSession(() => {
130+
this.updatePullRequest(this._item);
131+
}));
129132
}
130133

131134
registerPrListeners() {

src/github/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,9 @@ function parseActor(
579579
}
580580
}
581581

582-
export function parseProjectItems(projects: { id: string; project: { id: string; title: string; } }[] | undefined): IProjectItem[] {
582+
export function parseProjectItems(projects: { id: string; project: { id: string; title: string; } }[] | undefined): IProjectItem[] | undefined {
583583
if (!projects) {
584-
return [];
584+
return undefined;
585585
}
586586
return projects.map(project => {
587587
return {

src/github/views.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ export interface PullRequest {
8181
}
8282

8383
export interface ProjectItemsReply {
84-
projectItems: IProjectItem[];
84+
projectItems: IProjectItem[] | undefined;
8585
}

webviews/components/sidebar.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
2525
updatePR,
2626
pr,
2727
} = useContext(PullRequestContext);
28+
29+
const updateProjects = async () => {
30+
const newProjects = await changeProjects();
31+
updatePR({ ...newProjects });
32+
};
33+
2834
return (
2935
<div id="sidebar">
3036
{!isIssue ? (
@@ -130,10 +136,7 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
130136
)}
131137
</div>
132138
<div id="project" className="section">
133-
<div className="section-header" onClick={async () => {
134-
const newProjects = await changeProjects();
135-
updatePR({ ...newProjects });
136-
}}>
139+
<div className="section-header" onClick={updateProjects}>
137140
<div className="section-title">Project</div>
138141
{hasWritePermission ? (
139142
<button
@@ -143,11 +146,14 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
143146
</button>
144147
) : null}
145148
</div>
146-
{(projects && projects.length > 0) ? projects.map(project => (
147-
<Project key={project.project.title} {...project} canDelete={hasWritePermission} />
148-
)) : (
149-
<div className="section-placeholder">None Yet</div>
150-
)}
149+
{!projects ?
150+
<a onClick={updateProjects}>Sign in with more permissions to see projects</a>
151+
: (projects.length > 0)
152+
? projects.map(project => (
153+
<Project key={project.project.title} {...project} canDelete={hasWritePermission} />
154+
)) :
155+
<div className="section-placeholder">None Yet</div>
156+
}
151157
</div>
152158
<div id="milestone" className="section">
153159
<div className="section-header" onClick={async () => {

0 commit comments

Comments
 (0)