Skip to content

Commit 34c50c1

Browse files
authored
Merge pull request #3251 from github/mbg/user-error/enablement
Turn enablement errors into configuration errors
2 parents ac9aeee + 52a7bd7 commit 34c50c1

File tree

8 files changed

+144
-0
lines changed

8 files changed

+144
-0
lines changed

lib/analyze-action.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql-action.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,39 @@ test("wrapApiConfigurationError correctly wraps specific configuration errors",
169169
res,
170170
new util.ConfigurationError("Resource not accessible by integration"),
171171
);
172+
173+
// Enablement errors.
174+
const codeSecurityNotEnabledError = new util.HTTPError(
175+
"Code Security must be enabled for this repository to use code scanning",
176+
403,
177+
);
178+
res = api.wrapApiConfigurationError(codeSecurityNotEnabledError);
179+
t.deepEqual(
180+
res,
181+
new util.ConfigurationError(
182+
api.getFeatureEnablementError(codeSecurityNotEnabledError.message),
183+
),
184+
);
185+
const advancedSecurityNotEnabledError = new util.HTTPError(
186+
"Advanced Security must be enabled for this repository to use code scanning",
187+
403,
188+
);
189+
res = api.wrapApiConfigurationError(advancedSecurityNotEnabledError);
190+
t.deepEqual(
191+
res,
192+
new util.ConfigurationError(
193+
api.getFeatureEnablementError(advancedSecurityNotEnabledError.message),
194+
),
195+
);
196+
const codeScanningNotEnabledError = new util.HTTPError(
197+
"Code Scanning is not enabled for this repository. Please enable code scanning in the repository settings.",
198+
403,
199+
);
200+
res = api.wrapApiConfigurationError(codeScanningNotEnabledError);
201+
t.deepEqual(
202+
res,
203+
new util.ConfigurationError(
204+
api.getFeatureEnablementError(codeScanningNotEnabledError.message),
205+
),
206+
);
172207
});

src/api-client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ export async function getRepositoryProperties(repositoryNwo: RepositoryNwo) {
283283
});
284284
}
285285

286+
function isEnablementError(msg: string) {
287+
return [
288+
/Code Security must be enabled/,
289+
/Advanced Security must be enabled/,
290+
/Code Scanning is not enabled/,
291+
].some((pattern) => pattern.test(msg));
292+
}
293+
294+
// TODO: Move to `error-messages.ts` after refactoring import order to avoid cycle
295+
// since `error-messages.ts` currently depends on this file.
296+
export function getFeatureEnablementError(message: string): string {
297+
return `Please verify that the necessary features are enabled: ${message}`;
298+
}
299+
286300
export function wrapApiConfigurationError(e: unknown) {
287301
const httpError = asHTTPError(e);
288302
if (httpError !== undefined) {
@@ -304,6 +318,11 @@ export function wrapApiConfigurationError(e: unknown) {
304318
"Please check that your token is valid and has the required permissions: contents: read, security-events: write",
305319
);
306320
}
321+
if (httpError.status === 403 && isEnablementError(httpError.message)) {
322+
return new ConfigurationError(
323+
getFeatureEnablementError(httpError.message),
324+
);
325+
}
307326
if (httpError.status === 429) {
308327
return new ConfigurationError("API rate limit exceeded");
309328
}

0 commit comments

Comments
 (0)