Skip to content

Commit 1c59c16

Browse files
authored
GitHub - avoid double prompting when github.branchProtection setting is enabled (microsoft#181137)
1 parent 71bb936 commit 1c59c16

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

extensions/github/src/branchProtection.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,16 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
101101
}
102102

103103
private async initializeBranchProtection(): Promise<void> {
104-
// Branch protection (HEAD)
105-
await this.updateHEADBranchProtection();
104+
try {
105+
// Branch protection (HEAD)
106+
await this.updateHEADBranchProtection();
106107

107-
// Branch protection (remotes)
108-
await this.updateRepositoryBranchProtection();
108+
// Branch protection (remotes)
109+
await this.updateRepositoryBranchProtection();
110+
} catch (err) {
111+
// noop
112+
this.logger.warn(`Failed to initialize branch protection: ${this.formatErrorMessage(err)}`);
113+
}
109114
}
110115

111116
private async hasPushPermission(repository: { owner: string; repo: string }): Promise<boolean> {
@@ -115,8 +120,8 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
115120

116121
return response.data.permissions?.push === true;
117122
} catch (err) {
118-
this.logger.warn(`Failed to get repository permissions for repository (${repository.owner}/${repository.repo}): ${err.message} (${err.status})`);
119-
return false;
123+
this.logger.warn(`Failed to get repository permissions for repository (${repository.owner}/${repository.repo}): ${this.formatErrorMessage(err)}`);
124+
throw err;
120125
}
121126
}
122127

@@ -132,8 +137,8 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
132137
});
133138
return response.data as RepositoryRule[];
134139
} catch (err) {
135-
this.logger.warn(`Failed to get branch rules for repository (${repository.owner}/${repository.repo}), branch (${branch}): ${err.message} (${err.status})`);
136-
return [];
140+
this.logger.warn(`Failed to get branch rules for repository (${repository.owner}/${repository.repo}), branch (${branch}): ${this.formatErrorMessage(err)}`);
141+
throw err;
137142
}
138143
}
139144

@@ -170,8 +175,8 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
170175
return rulesets;
171176
}
172177
catch (err) {
173-
this.logger.warn(`Failed to get repository rulesets for repository (${repository.owner}/${repository.repo}): ${err.message} (${err.status})`);
174-
return [];
178+
this.logger.warn(`Failed to get repository rulesets for repository (${repository.owner}/${repository.repo}): ${this.formatErrorMessage(err)}`);
179+
throw err;
175180
}
176181
}
177182

@@ -208,8 +213,8 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
208213
this.branchProtection = [{ remote: remote.name, rules: [{ include: [HEAD.name] }] }];
209214
this._onDidChangeBranchProtection.fire(this.repository.rootUri);
210215
} catch (err) {
211-
// noop
212-
this.logger.warn(`Failed to update HEAD branch protection: ${err.message} (${err.status})`);
216+
this.logger.warn(`Failed to update HEAD branch protection: ${this.formatErrorMessage(err)}`);
217+
throw err;
213218
}
214219
}
215220

@@ -264,9 +269,12 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
264269
// Save branch protection to global state
265270
await this.globalState.update(this.globalStateKey, branchProtection);
266271
} catch (err) {
267-
// noop
268-
this.logger.warn(`Failed to update repository branch protection: ${err.message} (${err.status})`);
272+
this.logger.warn(`Failed to update repository branch protection: ${this.formatErrorMessage(err)}`);
273+
throw err;
269274
}
270275
}
271276

277+
private formatErrorMessage(err: any): string {
278+
return `${err.message ?? ''}${err.status ? ` (${err.status})` : ''}`;
279+
}
272280
}

0 commit comments

Comments
 (0)