Skip to content

Commit

Permalink
Polish sign in again for projects (#5355)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 authored Oct 17, 2023
1 parent cc3b638 commit 42d2b28
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
18 changes: 12 additions & 6 deletions src/github/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ export class CredentialStore implements vscode.Disposable {
private _isInitialized: boolean = false;
private _onDidInitialize: vscode.EventEmitter<void> = new vscode.EventEmitter();
public readonly onDidInitialize: vscode.Event<void> = this._onDidInitialize.event;
private _scopes: string[] = SCOPES;
private _scopes: string[] = SCOPES_OLD;
private _scopesEnterprise: string[] = SCOPES_OLD;

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

private _onDidUpgradeSession: vscode.EventEmitter<void> = new vscode.EventEmitter();
public readonly onDidUpgradeSession = this._onDidUpgradeSession.event;

constructor(private readonly _telemetry: ITelemetry, private readonly context: vscode.ExtensionContext) {
this.setScopesFromState();

Expand Down Expand Up @@ -88,8 +91,8 @@ export class CredentialStore implements vscode.Disposable {
}

private setScopesFromState() {
this._scopes = this.context.globalState.get(LAST_USED_SCOPES_GITHUB_KEY, SCOPES);
this._scopesEnterprise = this.context.globalState.get(LAST_USED_SCOPES_ENTERPRISE_KEY, SCOPES);
this._scopes = this.context.globalState.get(LAST_USED_SCOPES_GITHUB_KEY, SCOPES_OLD);
this._scopesEnterprise = this.context.globalState.get(LAST_USED_SCOPES_ENTERPRISE_KEY, SCOPES_OLD);
}

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

let session: vscode.AuthenticationSession | undefined = undefined;
let isNew: boolean = false;
let usedScopes: string[] | undefined = SCOPES;
let usedScopes: string[] | undefined = SCOPES_OLD;
const oldScopes = this._scopes;
const oldEnterpriseScopes = this._scopesEnterprise;
const authResult: AuthResult = { canceled: false };
Expand Down Expand Up @@ -243,6 +246,9 @@ export class CredentialStore implements vscode.Disposable {
public async getHubEnsureAdditionalScopes(authProviderId: AuthProvider): Promise<GitHub | undefined> {
const hasScopesAlready = this.isAuthenticatedWithAdditionalScopes(authProviderId);
await this.initialize(authProviderId, { createIfNone: !hasScopesAlready }, SCOPES_WITH_ADDITIONAL, true);
if (!hasScopesAlready) {
this._onDidUpgradeSession.fire();
}
return this.getHub(authProviderId);
}

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

const session = await vscode.authentication.getSession(authProviderId, requireScopes ? scopes : SCOPES, getAuthSessionOptions);
return { session, isNew: !!session, scopes: requireScopes ? scopes : SCOPES };
const session = await vscode.authentication.getSession(authProviderId, requireScopes ? scopes : SCOPES_OLD, getAuthSessionOptions);
return { session, isNew: !!session, scopes: requireScopes ? scopes : SCOPES_OLD };
}

private async findExistingScopes(authProviderId: AuthProvider): Promise<{ session: vscode.AuthenticationSession, scopes: string[] } | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion src/github/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export interface Issue {
updatedAt: string;
user: IAccount;
labels: ILabel[];
projectItems: IProjectItem[];
projectItems?: IProjectItem[];
milestone?: IMilestone;
repositoryOwner?: string;
repositoryName?: string;
Expand Down
9 changes: 6 additions & 3 deletions src/github/issueModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class IssueModel<TItem extends Issue = Issue> {
},
},
})));
this.item.projectItems = this.item.projectItems.filter(project => !projectItems.find(p => p.project.id === project.project.id));
this.item.projectItems = this.item.projectItems?.filter(project => !projectItems.find(p => p.project.id === project.project.id));
} catch (err) {
Logger.error(err, IssueModel.ID);
}
Expand All @@ -311,14 +311,17 @@ export class IssueModel<TItem extends Issue = Issue> {
},
},
})));
if (!this.item.projectItems) {
this.item.projectItems = [];
}
this.item.projectItems.push(...projects.map((project, index) => { return { project, id: itemIds[index].data!.addProjectV2ItemById.item.id }; }));
} catch (err) {
Logger.error(err, IssueModel.ID);
}
}

async updateProjects(projects: IProject[]): Promise<IProjectItem[]> {
const projectsToAdd: IProject[] = projects.filter(project => !this.item.projectItems.find(p => p.project.id === project.id));
async updateProjects(projects: IProject[]): Promise<IProjectItem[] | undefined> {
const projectsToAdd: IProject[] = projects.filter(project => !this.item.projectItems?.find(p => p.project.id === project.id));
const projectsToRemove: IProjectItem[] = this.item.projectItems?.filter(project => !projects.find(p => p.id === project.project.id)) ?? [];
await this.removeProjects(projectsToRemove);
await this.addProjects(projectsToAdd);
Expand Down
3 changes: 3 additions & 0 deletions src/github/pullRequestOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
});
}),
);
this._disposables.push(folderRepositoryManager.credentialStore.onDidUpgradeSession(() => {
this.updatePullRequest(this._item);
}));
}

registerPrListeners() {
Expand Down
4 changes: 2 additions & 2 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,9 @@ function parseActor(
}
}

export function parseProjectItems(projects: { id: string; project: { id: string; title: string; } }[] | undefined): IProjectItem[] {
export function parseProjectItems(projects: { id: string; project: { id: string; title: string; } }[] | undefined): IProjectItem[] | undefined {
if (!projects) {
return [];
return undefined;
}
return projects.map(project => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/github/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ export interface PullRequest {
}

export interface ProjectItemsReply {
projectItems: IProjectItem[];
projectItems: IProjectItem[] | undefined;
}
24 changes: 15 additions & 9 deletions webviews/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
updatePR,
pr,
} = useContext(PullRequestContext);

const updateProjects = async () => {
const newProjects = await changeProjects();
updatePR({ ...newProjects });
};

return (
<div id="sidebar">
{!isIssue ? (
Expand Down Expand Up @@ -130,10 +136,7 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
)}
</div>
<div id="project" className="section">
<div className="section-header" onClick={async () => {
const newProjects = await changeProjects();
updatePR({ ...newProjects });
}}>
<div className="section-header" onClick={updateProjects}>
<div className="section-title">Project</div>
{hasWritePermission ? (
<button
Expand All @@ -143,11 +146,14 @@ export default function Sidebar({ reviewers, labels, hasWritePermission, isIssue
</button>
) : null}
</div>
{(projects && projects.length > 0) ? projects.map(project => (
<Project key={project.project.title} {...project} canDelete={hasWritePermission} />
)) : (
<div className="section-placeholder">None Yet</div>
)}
{!projects ?
<a onClick={updateProjects}>Sign in with more permissions to see projects</a>
: (projects.length > 0)
? projects.map(project => (
<Project key={project.project.title} {...project} canDelete={hasWritePermission} />
)) :
<div className="section-placeholder">None Yet</div>
}
</div>
<div id="milestone" className="section">
<div className="section-header" onClick={async () => {
Expand Down

0 comments on commit 42d2b28

Please sign in to comment.