diff --git a/extensions/git/src/api/git.d.ts b/extensions/git/src/api/git.d.ts index 6a20b9017f0..997e170bbf0 100644 --- a/extensions/git/src/api/git.d.ts +++ b/extensions/git/src/api/git.d.ts @@ -274,9 +274,14 @@ export interface PushErrorHandler { handlePushError(repository: Repository, remote: Remote, refspec: string, error: Error & { gitErrorCode: GitErrorCodes }): Promise; } +export interface BranchProtection { + readonly remote: string; + readonly branches: string[]; +} + export interface BranchProtectionProvider { onDidChangeBranchProtection: Event; - provideBranchProtection(): Map; + provideBranchProtection(): BranchProtection[]; } export type APIState = 'uninitialized' | 'initialized'; diff --git a/extensions/git/src/branchProtection.ts b/extensions/git/src/branchProtection.ts index 0aece314f45..7ca1705c125 100644 --- a/extensions/git/src/branchProtection.ts +++ b/extensions/git/src/branchProtection.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { Disposable, Event, EventEmitter, Uri, workspace } from 'vscode'; -import { BranchProtectionProvider } from './api/git'; +import { BranchProtection, BranchProtectionProvider } from './api/git'; import { dispose, filterEvent } from './util'; export interface IBranchProtectionProviderRegistry { @@ -19,7 +19,8 @@ export class GitBranchProtectionProvider implements BranchProtectionProvider { private readonly _onDidChangeBranchProtection = new EventEmitter(); onDidChangeBranchProtection = this._onDidChangeBranchProtection.event; - private branchProtection = new Map<'', string[]>(); + private branchProtection!: BranchProtection; + private disposables: Disposable[] = []; constructor(private readonly repositoryRoot: Uri) { @@ -28,8 +29,8 @@ export class GitBranchProtectionProvider implements BranchProtectionProvider { this.updateBranchProtection(); } - provideBranchProtection(): Map { - return this.branchProtection; + provideBranchProtection(): BranchProtection[] { + return [this.branchProtection]; } private updateBranchProtection(): void { @@ -37,10 +38,11 @@ export class GitBranchProtectionProvider implements BranchProtectionProvider { const branchProtectionConfig = scopedConfig.get('branchProtection') ?? []; const branchProtectionValues = Array.isArray(branchProtectionConfig) ? branchProtectionConfig : [branchProtectionConfig]; - this.branchProtection.set('', branchProtectionValues + const branches = branchProtectionValues .map(bp => typeof bp === 'string' ? bp.trim() : '') - .filter(bp => bp !== '')); + .filter(bp => bp !== ''); + this.branchProtection = { remote: '', branches }; this._onDidChangeBranchProtection.fire(this.repositoryRoot); } diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 75e7c909788..65919283a83 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -2367,7 +2367,7 @@ export class Repository implements Disposable { this.branchProtection.clear(); for (const provider of this.branchProtectionProviderRegistry.getBranchProtectionProviders(root)) { - for (const [remote, branches] of provider.provideBranchProtection().entries()) { + for (const { remote, branches } of provider.provideBranchProtection()) { this.branchProtection.set(remote, branches.length !== 0 ? picomatch(branches) : undefined); } } diff --git a/extensions/github/src/branchProtection.ts b/extensions/github/src/branchProtection.ts index 05dee5465a9..e49a3eda3d4 100644 --- a/extensions/github/src/branchProtection.ts +++ b/extensions/github/src/branchProtection.ts @@ -5,7 +5,7 @@ import { EventEmitter, Uri, workspace } from 'vscode'; import { getOctokit } from './auth'; -import { API, BranchProtectionProvider, Repository } from './typings/git'; +import { API, BranchProtection, BranchProtectionProvider, Repository } from './typings/git'; import { DisposableStore, getRepositoryFromUrl } from './util'; export class GithubBranchProtectionProviderManager { @@ -62,15 +62,15 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider private readonly _onDidChangeBranchProtection = new EventEmitter(); onDidChangeBranchProtection = this._onDidChangeBranchProtection.event; - private branchProtection = new Map(); + private branchProtection!: BranchProtection[]; constructor(private readonly repository: Repository) { repository.status() .then(() => this.initializeBranchProtection()); } - provideBranchProtection(): Map { - return this.branchProtection; + provideBranchProtection(): BranchProtection[] { + return this.branchProtection ?? []; } private async initializeBranchProtection(): Promise { @@ -109,7 +109,7 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider return; } - this.branchProtection.set(remote.name, [HEAD.name]); + this.branchProtection = [{ remote: remote.name, branches: [HEAD.name] }]; this._onDidChangeBranchProtection.fire(this.repository.rootUri); } catch { // todo@lszomoru - add logging @@ -118,7 +118,7 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider private async updateBranchProtection(): Promise { try { - let branchProtectionUpdated = false; + const branchProtection: BranchProtection[] = []; for (const remote of this.repository.state.remotes) { const repository = getRepositoryFromUrl(remote.pushUrl ?? remote.fetchUrl ?? ''); @@ -143,15 +143,11 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider page++; } - if (protectedBranches.length > 0) { - this.branchProtection.set(remote.name, protectedBranches); - branchProtectionUpdated = true; - } + branchProtection.push({ remote: remote.name, branches: protectedBranches }); } - if (branchProtectionUpdated) { - this._onDidChangeBranchProtection.fire(this.repository.rootUri); - } + this.branchProtection = branchProtection; + this._onDidChangeBranchProtection.fire(this.repository.rootUri); } catch { // todo@lszomoru - add logging } diff --git a/extensions/github/src/typings/git.d.ts b/extensions/github/src/typings/git.d.ts index 5a84e952abb..fa270a382ee 100644 --- a/extensions/github/src/typings/git.d.ts +++ b/extensions/github/src/typings/git.d.ts @@ -268,9 +268,14 @@ export interface PushErrorHandler { handlePushError(repository: Repository, remote: Remote, refspec: string, error: Error & { gitErrorCode: GitErrorCodes }): Promise; } +export interface BranchProtection { + readonly remote: string; + readonly branches: string[]; +} + export interface BranchProtectionProvider { onDidChangeBranchProtection: Event; - provideBranchProtection(): Map; + provideBranchProtection(): BranchProtection[]; } export type APIState = 'uninitialized' | 'initialized';