Skip to content

Commit b86fab1

Browse files
authored
Merge pull request #559 from codex-team/feat/rate-limits-settings
feat(hawk-api): add rate limits settings
2 parents 3fa6755 + f6413ce commit b86fab1

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hawk.api",
3-
"version": "1.2.9",
3+
"version": "1.2.10",
44
"main": "index.ts",
55
"license": "BUSL-1.1",
66
"scripts": {

src/resolvers/project.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,79 @@ module.exports = {
200200
}
201201
},
202202

203+
/**
204+
* Update project rate limits settings
205+
*
206+
* @param {ResolverObj} _obj
207+
* @param {string} id - project id
208+
* @param {Object | null} rateLimitSettings - rate limit settings (null to remove)
209+
* @param {UserInContext} user - current authorized user {@see ../index.js}
210+
* @param {ContextFactories} factories - factories for working with models
211+
*
212+
* @returns {Project}
213+
*/
214+
async updateProjectRateLimits(_obj, { id, rateLimitSettings }, { user, factories }) {
215+
const project = await factories.projectsFactory.findById(id);
216+
217+
if (!project) {
218+
throw new ApolloError('There is no project with that id');
219+
}
220+
221+
if (project.workspaceId.toString() === '6213b6a01e6281087467cc7a') {
222+
throw new ApolloError('Unable to update demo project');
223+
}
224+
225+
// Validate rate limit settings if provided
226+
if (rateLimitSettings) {
227+
const { N, T } = rateLimitSettings;
228+
229+
// Validate that N and T exist
230+
if (!N || !T) {
231+
throw new UserInputError(
232+
'Rate limit settings must contain both N (threshold) and T (period) fields.'
233+
);
234+
}
235+
236+
// Validate N (threshold) - must be positive integer > 0
237+
if (typeof N !== 'number' || !Number.isInteger(N) || N <= 0) {
238+
throw new UserInputError(
239+
'Invalid rate limit threshold. Must be a positive integer greater than 0.'
240+
);
241+
}
242+
243+
// Validate T (period) - must be positive integer >= 60 (1 minute)
244+
if (typeof T !== 'number' || !Number.isInteger(T) || T < 60) {
245+
throw new UserInputError(
246+
'Invalid rate limit period. Must be a positive integer greater than or equal to 60 seconds.'
247+
);
248+
}
249+
250+
// Validate reasonable maximums (prevent extremely large values)
251+
const MAX_THRESHOLD = 1000000000; // 1 billion
252+
const MAX_PERIOD = 60 * 60 * 24 * 31; // 1 month in seconds
253+
254+
if (N > MAX_THRESHOLD) {
255+
throw new UserInputError(
256+
`Rate limit threshold cannot exceed ${MAX_THRESHOLD.toLocaleString()}.`
257+
);
258+
}
259+
260+
if (T > MAX_PERIOD) {
261+
throw new UserInputError(
262+
`Rate limit period cannot exceed ${MAX_PERIOD.toLocaleString()} seconds (1 month).`
263+
);
264+
}
265+
}
266+
267+
try {
268+
return project.updateProject({
269+
rateLimitSettings: rateLimitSettings || null,
270+
});
271+
} catch (err) {
272+
throw new ApolloError('Failed to update project rate limit settings', { originalError: err });
273+
}
274+
},
275+
203276
/**
204277
* Generates new project integration token by id
205278
*

src/typeDefs/project.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { gql } from 'apollo-server-express';
22

33
export default gql`
4+
"""
5+
Rate limits configuration input
6+
"""
7+
input RateLimitSettingsInput {
8+
"""
9+
Rate limit threshold (N events)
10+
"""
11+
N: Int!
12+
13+
"""
14+
Rate limit period in seconds (T seconds)
15+
"""
16+
T: Int!
17+
}
18+
19+
"""
20+
Rate limits configuration
21+
"""
22+
type RateLimitSettings {
23+
"""
24+
Rate limit threshold (N events)
25+
"""
26+
N: Int!
27+
28+
"""
29+
Rate limit period in seconds (T seconds)
30+
"""
31+
T: Int!
32+
}
433
534
"""
635
Possible events order
@@ -284,6 +313,11 @@ type Project {
284313
"""
285314
eventGroupingPatterns: [ProjectEventGroupingPattern]
286315
316+
"""
317+
Rate limits configuration
318+
"""
319+
rateLimitSettings: RateLimitSettings
320+
287321
"""
288322
List of releases with unique events count, commits count and files count
289323
"""
@@ -340,6 +374,26 @@ extend type Mutation {
340374
Project image
341375
"""
342376
image: Upload @uploadImage
377+
378+
"""
379+
Rate limits configuration
380+
"""
381+
rateLimitSettings: RateLimitSettingsInput
382+
): Project! @requireAdmin
383+
384+
"""
385+
Update project rate limits settings
386+
"""
387+
updateProjectRateLimits(
388+
"""
389+
What project to update
390+
"""
391+
id: ID!
392+
393+
"""
394+
Rate limits configuration. Pass null to remove rate limits.
395+
"""
396+
rateLimitSettings: RateLimitSettingsInput
343397
): Project! @requireAdmin
344398
345399
"""

0 commit comments

Comments
 (0)