-
Notifications
You must be signed in to change notification settings - Fork 7
fix(server-nestjs): make SonarQube user creation idempotent on create race #2639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
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
64 changes: 64 additions & 0 deletions
64
apps/server-nestjs/src/modules/sonarqube/sonarqube.utils.spec.ts
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,64 @@ | ||
| import { HttpStatus } from '@nestjs/common' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { SonarqubeError } from './sonarqube-http-client.service' | ||
| import { ensure, isSonarqubeAlreadyExists, sonarProjectPropertiesFile } from './sonarqube.utils' | ||
|
|
||
| describe('sonarProjectPropertiesFile', () => { | ||
| it('targets the project key with a quality-gate wait', () => { | ||
| expect(sonarProjectPropertiesFile('my-key')).toEqual([ | ||
| 'sonar.projectKey=my-key', | ||
| 'sonar.qualitygate.wait=true', | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isSonarqubeAlreadyExists', () => { | ||
| it('matches a 409 or an already/exists message', () => { | ||
| expect(isSonarqubeAlreadyExists(new SonarqubeError('ClientError', 'conflict', { status: HttpStatus.CONFLICT }))).toBe(true) | ||
| expect(isSonarqubeAlreadyExists(new SonarqubeError('ClientError', `User 'bob' already exists`, { status: 400 }))).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects other errors', () => { | ||
| expect(isSonarqubeAlreadyExists(new SonarqubeError('ClientError', 'forbidden', { status: 403 }))).toBe(false) | ||
| expect(isSonarqubeAlreadyExists(new Error('already exists'))).toBe(false) | ||
| expect(isSonarqubeAlreadyExists(null)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('ensure', () => { | ||
| it('returns the created value when create succeeds', async () => { | ||
| const reload = vi.fn() | ||
|
|
||
| await expect(ensure({ create: async () => 'created', reload })).resolves.toBe('created') | ||
|
|
||
| expect(reload).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('reloads once on a collision and never retries create', async () => { | ||
| const error = new SonarqubeError('ClientError', 'already exists', { status: 409 }) | ||
| const create = vi.fn(async () => { throw error }) | ||
| const onCollision = vi.fn() | ||
| const reload = vi.fn(async () => 'existing') | ||
|
|
||
| await expect(ensure({ create, reload, onCollision })).resolves.toBe('existing') | ||
|
|
||
| expect(create).toHaveBeenCalledOnce() | ||
| expect(onCollision).toHaveBeenCalledWith(error) | ||
| expect(reload).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('rethrows the original error when a collision finds nothing on reload', async () => { | ||
| const error = new SonarqubeError('ClientError', 'already exists', { status: 409 }) | ||
|
|
||
| await expect(ensure({ create: async () => { throw error }, reload: async () => undefined })).rejects.toBe(error) | ||
| }) | ||
|
|
||
| it('rethrows non-collision errors without reloading', async () => { | ||
| const error = new SonarqubeError('ClientError', 'forbidden', { status: 403 }) | ||
| const reload = vi.fn() | ||
|
|
||
| await expect(ensure({ create: async () => { throw error }, reload })).rejects.toBe(error) | ||
|
|
||
| expect(reload).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
38 changes: 38 additions & 0 deletions
38
apps/server-nestjs/src/modules/sonarqube/sonarqube.utils.ts
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 |
|---|---|---|
| @@ -1,6 +1,44 @@ | ||
| import { HttpStatus } from '@nestjs/common' | ||
| import { SonarqubeError } from './sonarqube-http-client.service' | ||
|
|
||
| export function sonarProjectPropertiesFile(projectKey: string) { | ||
| return [ | ||
| `sonar.projectKey=${projectKey}`, | ||
| 'sonar.qualitygate.wait=true', | ||
| ] | ||
| } | ||
|
|
||
| // Whether a SonarQube error signals an entity already existing (race | ||
| // collision): a 409, or a 4xx whose message mentions "already"/"exists" | ||
| // (SonarQube reports some collisions as a generic Bad Request). | ||
| export function isSonarqubeAlreadyExists(error: unknown): error is SonarqubeError { | ||
| if (!(error instanceof SonarqubeError)) return false | ||
| if (error.status === HttpStatus.CONFLICT) return true | ||
| return error.status !== undefined && error.status >= 400 && error.status < 500 && /already|exists/i.test(error.message) | ||
| } | ||
|
|
||
| // Runs an idempotent write: tries `create`, and on a SonarQube race collision | ||
| // reloads via `reload` and returns the existing entity instead of failing. | ||
| // `onCollision` is invoked once when a collision is detected. If the reload | ||
| // finds nothing, the original error is rethrown so genuine failures are not | ||
| // swallowed. | ||
| export async function ensure<T>({ | ||
| create, | ||
| reload, | ||
| onCollision, | ||
| }: { | ||
| create: () => Promise<T> | ||
| reload: () => Promise<T | undefined> | ||
| onCollision?: (error: unknown) => void | ||
| }): Promise<T> { | ||
| try { | ||
| return await create() | ||
| } catch (error) { | ||
| if (isSonarqubeAlreadyExists(error)) { | ||
| onCollision?.(error) | ||
| const existing = await reload() | ||
| if (existing) return existing | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
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.