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
5 changed files with 30 additions and 22 deletions

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';