Git/GitHub - Branch protection refactoring (#179848)

Branch protection refactoring
This commit is contained in:
Ladislau Szomoru 2023-04-13 11:14:33 +02:00 committed by GitHub
parent 213f4fa39d
commit 45a44d1786
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 22 deletions

View file

@ -274,9 +274,14 @@ export interface PushErrorHandler {
handlePushError(repository: Repository, remote: Remote, refspec: string, error: Error & { gitErrorCode: GitErrorCodes }): Promise<boolean>;
}
export interface BranchProtection {
readonly remote: string;
readonly branches: string[];
}
export interface BranchProtectionProvider {
onDidChangeBranchProtection: Event<Uri>;
provideBranchProtection(): Map<string, string[]>;
provideBranchProtection(): BranchProtection[];
}
export type APIState = 'uninitialized' | 'initialized';

View file

@ -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<Uri>();
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<string, string[]> {
return this.branchProtection;
provideBranchProtection(): BranchProtection[] {
return [this.branchProtection];
}
private updateBranchProtection(): void {
@ -37,10 +38,11 @@ export class GitBranchProtectionProvider implements BranchProtectionProvider {
const branchProtectionConfig = scopedConfig.get<unknown>('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);
}

View file

@ -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);
}
}

View file

@ -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<Uri>();
onDidChangeBranchProtection = this._onDidChangeBranchProtection.event;
private branchProtection = new Map<string, string[]>();
private branchProtection!: BranchProtection[];
constructor(private readonly repository: Repository) {
repository.status()
.then(() => this.initializeBranchProtection());
}
provideBranchProtection(): Map<string, string[]> {
return this.branchProtection;
provideBranchProtection(): BranchProtection[] {
return this.branchProtection ?? [];
}
private async initializeBranchProtection(): Promise<void> {
@ -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<void> {
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
}

View file

@ -268,9 +268,14 @@ export interface PushErrorHandler {
handlePushError(repository: Repository, remote: Remote, refspec: string, error: Error & { gitErrorCode: GitErrorCodes }): Promise<boolean>;
}
export interface BranchProtection {
readonly remote: string;
readonly branches: string[];
}
export interface BranchProtectionProvider {
onDidChangeBranchProtection: Event<Uri>;
provideBranchProtection(): Map<string, string[]>;
provideBranchProtection(): BranchProtection[];
}
export type APIState = 'uninitialized' | 'initialized';