This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add GraphQL mutation for configuring code host rate limits #56150
Open
varsanojidan
wants to merge
4
commits into
main
Choose a base branch
from
iv/rate-limit-gql-mutation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| package graphqlbackend | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/sourcegraph/log/logtest" | ||
| "github.com/sourcegraph/sourcegraph/internal/auth" | ||
| "github.com/sourcegraph/sourcegraph/internal/database" | ||
| "github.com/sourcegraph/sourcegraph/internal/database/dbmocks" | ||
| "github.com/sourcegraph/sourcegraph/internal/types" | ||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_NotASiteAdmin(t *testing.T) { | ||
| logger := logtest.Scoped(t) | ||
| db := dbmocks.NewMockDB() | ||
| r := &schemaResolver{logger: logger, db: db} | ||
|
|
||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: false}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| _, err := r.SetCodeHostRateLimits(context.Background(), SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{}, | ||
| }) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, auth.ErrMustBeSiteAdmin, err) | ||
| } | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_InvalidConfigs(t *testing.T) { | ||
| logger := logtest.Scoped(t) | ||
| db := dbmocks.NewMockDB() | ||
| r := &schemaResolver{logger: logger, db: db} | ||
| ctx := context.Background() | ||
| wantErr := errors.New("rate limit settings must be positive integers") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args SetCodeHostRateLimitsArgs | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "Negative APIQuota", | ||
| args: SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{ | ||
| CodeHostID: "Q29kZUhvc3Q6MQ==", | ||
| APIQuota: -1, | ||
| }, | ||
| }, | ||
| wantErr: wantErr, | ||
| }, | ||
| { | ||
| name: "Negative APIReplenishmentIntervalSeconds", | ||
| args: SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{ | ||
| CodeHostID: "Q29kZUhvc3Q6MQ==", | ||
| APIReplenishmentIntervalSeconds: -1, | ||
| }, | ||
| }, | ||
| wantErr: wantErr, | ||
| }, | ||
| { | ||
| name: "Negative GitQuota", | ||
| args: SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{ | ||
| CodeHostID: "Q29kZUhvc3Q6MQ==", | ||
| GitQuota: -1, | ||
| }, | ||
| }, | ||
| wantErr: wantErr, | ||
| }, | ||
| { | ||
| name: "Negative GitReplenishmentIntervalSeconds", | ||
| args: SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{ | ||
| CodeHostID: "Q29kZUhvc3Q6MQ==", | ||
| GitReplenishmentIntervalSeconds: -1, | ||
| }, | ||
| }, | ||
| wantErr: wantErr, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: true}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| _, err := r.SetCodeHostRateLimits(ctx, test.args) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, errCodeHostRateLimitsMustBePositiveIntegers, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_InvalidCodeHostID(t *testing.T) { | ||
| logger := logtest.Scoped(t) | ||
| db := dbmocks.NewMockDB() | ||
| r := &schemaResolver{logger: logger, db: db} | ||
| wantErr := errors.New("test error") | ||
|
|
||
| db.WithTransactFunc.SetDefaultHook(func(ctx context.Context, f func(database.DB) error) error { | ||
| return f(db) | ||
| }) | ||
|
|
||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: true}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| codeHostStore := dbmocks.NewMockCodeHostStore() | ||
| codeHostStore.GetByIDFunc.SetDefaultHook(func(ctx context.Context, id int32) (*types.CodeHost, error) { | ||
| return nil, wantErr | ||
| }) | ||
| db.CodeHostsFunc.SetDefaultReturn(codeHostStore) | ||
|
|
||
| _, err := r.SetCodeHostRateLimits(context.Background(), SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{CodeHostID: ""}, | ||
| }) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, "invalid code host id: invalid graphql.ID", err.Error()) | ||
| } | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_GetCodeHostByIDError(t *testing.T) { | ||
| logger := logtest.Scoped(t) | ||
| db := dbmocks.NewMockDB() | ||
| r := &schemaResolver{logger: logger, db: db} | ||
| wantErr := errors.New("test error") | ||
|
|
||
| db.WithTransactFunc.SetDefaultHook(func(ctx context.Context, f func(database.DB) error) error { | ||
| return f(db) | ||
| }) | ||
|
|
||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: true}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| codeHostStore := dbmocks.NewMockCodeHostStore() | ||
| codeHostStore.GetByIDFunc.SetDefaultHook(func(ctx context.Context, id int32) (*types.CodeHost, error) { | ||
| return nil, wantErr | ||
| }) | ||
| db.CodeHostsFunc.SetDefaultReturn(codeHostStore) | ||
|
|
||
| _, err := r.SetCodeHostRateLimits(context.Background(), SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{CodeHostID: "Q29kZUhvc3Q6MQ=="}, | ||
| }) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, wantErr.Error(), err.Error()) | ||
| } | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_UpdateCodeHostError(t *testing.T) { | ||
| logger := logtest.Scoped(t) | ||
| db := dbmocks.NewMockDB() | ||
| r := &schemaResolver{logger: logger, db: db} | ||
| wantErr := errors.New("test error") | ||
|
|
||
| db.WithTransactFunc.SetDefaultHook(func(ctx context.Context, f func(database.DB) error) error { | ||
| return f(db) | ||
| }) | ||
|
|
||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: true}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| codeHostStore := dbmocks.NewMockCodeHostStore() | ||
| codeHostStore.GetByIDFunc.SetDefaultHook(func(ctx context.Context, id int32) (*types.CodeHost, error) { | ||
| require.Equal(t, int32(1), id) | ||
| return &types.CodeHost{ID: 1}, nil | ||
| }) | ||
| codeHostStore.UpdateFunc.SetDefaultHook(func(ctx context.Context, host *types.CodeHost) error { | ||
| return wantErr | ||
| }) | ||
| db.CodeHostsFunc.SetDefaultReturn(codeHostStore) | ||
|
|
||
| _, err := r.SetCodeHostRateLimits(context.Background(), SetCodeHostRateLimitsArgs{ | ||
| Input: SetCodeHostRateLimitsInput{CodeHostID: "Q29kZUhvc3Q6MQ=="}, | ||
| }) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, wantErr.Error(), err.Error()) | ||
| } | ||
|
|
||
| func TestSchemaResolver_SetCodeHostRateLimits_Success(t *testing.T) { | ||
| db := dbmocks.NewMockDB() | ||
| ctx := context.Background() | ||
| setCodeHostRateLimitsInput := SetCodeHostRateLimitsInput{ | ||
| CodeHostID: "Q29kZUhvc3Q6MQ==", | ||
| APIQuota: 1, | ||
| APIReplenishmentIntervalSeconds: 2, | ||
| GitQuota: 3, | ||
| GitReplenishmentIntervalSeconds: 4, | ||
| } | ||
|
|
||
| db.WithTransactFunc.SetDefaultHook(func(ctx context.Context, f func(database.DB) error) error { | ||
| return f(db) | ||
| }) | ||
|
|
||
| usersStore := dbmocks.NewMockUserStore() | ||
| usersStore.GetByCurrentAuthUserFunc.SetDefaultReturn(&types.User{SiteAdmin: true}, nil) | ||
| db.UsersFunc.SetDefaultReturn(usersStore) | ||
|
|
||
| codeHostStore := dbmocks.NewMockCodeHostStore() | ||
| codeHostStore.GetByIDFunc.SetDefaultHook(func(ctx context.Context, id int32) (*types.CodeHost, error) { | ||
| require.Equal(t, int32(1), id) | ||
| return &types.CodeHost{ID: 1}, nil | ||
| }) | ||
| codeHostStore.UpdateFunc.SetDefaultHook(func(ctx context.Context, host *types.CodeHost) error { | ||
| require.Equal(t, setCodeHostRateLimitsInput.APIQuota, *(host.APIRateLimitQuota)) | ||
| require.Equal(t, setCodeHostRateLimitsInput.APIReplenishmentIntervalSeconds, *(host.APIRateLimitIntervalSeconds)) | ||
| require.Equal(t, setCodeHostRateLimitsInput.GitQuota, *(host.GitRateLimitQuota)) | ||
| require.Equal(t, setCodeHostRateLimitsInput.GitReplenishmentIntervalSeconds, *(host.GitRateLimitIntervalSeconds)) | ||
| return nil | ||
| }) | ||
| db.CodeHostsFunc.SetDefaultReturn(codeHostStore) | ||
|
|
||
| variables := map[string]any{ | ||
| "input": map[string]any{ | ||
| "codeHostID": string(setCodeHostRateLimitsInput.CodeHostID), | ||
| "apiQuota": setCodeHostRateLimitsInput.APIQuota, | ||
| "apiReplenishmentIntervalSeconds": setCodeHostRateLimitsInput.APIReplenishmentIntervalSeconds, | ||
| "gitQuota": setCodeHostRateLimitsInput.GitQuota, | ||
| "gitReplenishmentIntervalSeconds": setCodeHostRateLimitsInput.GitReplenishmentIntervalSeconds, | ||
| }, | ||
| } | ||
| RunTest(t, &Test{ | ||
| Context: ctx, | ||
| Schema: mustParseGraphQLSchema(t, db), | ||
| Variables: variables, | ||
| Query: `mutation setCodeHostRateLimits($input:SetCodeHostRateLimitsInput!) { | ||
| setCodeHostRateLimits(input:$input) { | ||
| alwaysNil | ||
| } | ||
| }`, | ||
| ExpectedResult: `{ | ||
| "setCodeHostRateLimits": { | ||
| "alwaysNil": null | ||
| } | ||
| }`, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10604,6 +10604,43 @@ type PerforceChangelist { | |
| commit: GitCommit! | ||
| } | ||
|
|
||
| extend type Mutation { | ||
| """ | ||
| Updates a code host's rate limit configurations. All rate limit values must be positive integers. | ||
| """ | ||
| setCodeHostRateLimits(input: SetCodeHostRateLimitsInput!): EmptyResponse | ||
| } | ||
|
|
||
| """ | ||
| SetCodeHostRateLimitsInput represents the input for configuring rate limits for a code host. | ||
| """ | ||
| input SetCodeHostRateLimitsInput { | ||
| """ | ||
| ID of the code host for which rate limits are being set. | ||
| """ | ||
| codeHostID: ID! | ||
|
|
||
| """ | ||
| The maximum number of API requests allowed per time window defined by apiReplenishmentIntervalSeconds. | ||
| """ | ||
| apiQuota: Int! | ||
|
|
||
| """ | ||
| The time interval at which the apiQuota's worth of API requests are replenished. | ||
| """ | ||
| apiReplenishmentIntervalSeconds: Int! | ||
|
|
||
| """ | ||
| The maximum number of Git requests allowed per time window defined by gitReplenishmentIntervalSeconds. | ||
| """ | ||
| gitQuota: Int! | ||
|
|
||
| """ | ||
| The time interval at which the gitQuota's worth of Git requests are replenished. | ||
| """ | ||
| gitReplenishmentIntervalSeconds: Int! | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made all of the rate limit configs required for the update because this will be used by a UI that would update all of the rate limits for a code host at the same time, and translates to one Lmk if you think otherwise 😄. |
||
| } | ||
|
|
||
| extend type Query { | ||
| """ | ||
| List of all configured code hosts on this instance. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.