Skip to content

Commit 709cf22

Browse files
committed
Limit Code Scanning API to 25 features per request
1 parent 3eaefb4 commit 709cf22

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/feature-flags.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,8 @@ test("Include no more than 25 features in each API request", async (t) => {
150150
// we ask for. Under the hood, the features library will request all features
151151
// from the API.
152152
const feature = Object.values(Feature)[0];
153-
// TODO: change to `t.notThrowsAsync` once we implement request chunking.
154-
await t.throwsAsync(
155-
async () => features.getValue(feature, includeCodeQlIfRequired(feature)),
156-
{
157-
message:
158-
"Encountered an error while trying to determine feature enablement: " +
159-
"Error: Can request a maximum of 25 features.",
160-
},
153+
await t.notThrowsAsync(async () =>
154+
features.getValue(feature, includeCodeQlIfRequired(feature)),
161155
);
162156
});
163157
});

src/feature-flags.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -622,18 +622,29 @@ class GitHubFeatureFlags {
622622
try {
623623
const featuresToRequest = Object.entries(featureConfig)
624624
.filter(([, config]) => !config.legacyApi)
625-
.map(([f]) => f)
626-
.join(",");
627-
628-
const response = await getApiClient().request(
629-
"GET /repos/:owner/:repo/code-scanning/codeql-action/features",
630-
{
631-
owner: this.repositoryNwo.owner,
632-
repo: this.repositoryNwo.repo,
633-
features: featuresToRequest,
634-
},
635-
);
636-
const remoteFlags = response.data as GitHubFeatureFlagsApiResponse;
625+
.map(([f]) => f);
626+
627+
const FEATURES_PER_REQUEST = 25;
628+
const featureChunks: string[][] = [];
629+
while (featuresToRequest.length > 0) {
630+
featureChunks.push(featuresToRequest.splice(0, FEATURES_PER_REQUEST));
631+
}
632+
633+
let remoteFlags: GitHubFeatureFlagsApiResponse = {};
634+
635+
for (const chunk of featureChunks) {
636+
const response = await getApiClient().request(
637+
"GET /repos/:owner/:repo/code-scanning/codeql-action/features",
638+
{
639+
owner: this.repositoryNwo.owner,
640+
repo: this.repositoryNwo.repo,
641+
features: chunk.join(","),
642+
},
643+
);
644+
const chunkFlags = response.data as GitHubFeatureFlagsApiResponse;
645+
remoteFlags = { ...remoteFlags, ...chunkFlags };
646+
}
647+
637648
this.logger.debug(
638649
"Loaded the following default values for the feature flags from the Code Scanning API:",
639650
);

0 commit comments

Comments
 (0)