Repository navigation
feat: add data access for per-subproject Octolens integrations #4977
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
+1,054
−9
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f368943
feat: add data access for per-subproject octolens integrations
themarolt fba0ea4
fix: keep auto-managed octolens integrations out of global counts
themarolt f19a34d
refactor: drop redundant comment on auto-managed platform list
themarolt 6b1bf48
fix: harden octolens integration lifecycle queries
themarolt 64fda62
Merge remote-tracking branch 'origin/main' into feat/issue-748-octole…
themarolt 4426fad
Merge remote-tracking branch 'origin/main' into feat/issue-748-octole…
themarolt 24ce5ee
fix: limit the status count change to the octolens anchor
themarolt 74f4bbf
fix: refuse manual create, update and delete of auto-managed integrat…
themarolt 6441beb
fix: keep null-platform integrations in global integration counts
themarolt c9a7d9d
Merge remote-tracking branch 'origin/main' into feat/issue-748-octole…
themarolt 7914bdb
fix: address copilot review on octolens integration access
themarolt 68dfd08
Merge remote-tracking branch 'origin/main' into feat/issue-748-octole…
themarolt 692c8d9
fix: check nested ids in the auto-managed integration guard
themarolt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { Error400 } from '@crowd/common' | ||
|
|
||
| const service = vi.hoisted(() => ({ | ||
| create: vi.fn(), | ||
| update: vi.fn(), | ||
| destroyAll: vi.fn(), | ||
| })) | ||
| const findIntegrationPlatformsByIds = vi.hoisted(() => vi.fn()) | ||
|
|
||
| vi.mock('../../services/integrationService', () => ({ | ||
| default: class { | ||
| create = service.create | ||
| update = service.update | ||
| destroyAll = service.destroyAll | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('../../services/user/permissionChecker', () => ({ | ||
| default: class { | ||
| validateHas = vi.fn() | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@/database/repositories/sequelizeRepository', () => ({ | ||
| default: { getQueryExecutor: vi.fn(() => ({})) }, | ||
| })) | ||
|
|
||
| vi.mock('@crowd/data-access-layer/src/integrations', () => ({ findIntegrationPlatformsByIds })) | ||
|
|
||
| import integrationCreate from './integrationCreate' | ||
| import integrationDestroy from './integrationDestroy' | ||
| import integrationUpdate from './integrationUpdate' | ||
|
|
||
| const GITHUB_ID = '6f1c2a5e-8f4b-4d3a-9b2c-1a2b3c4d5e6f' | ||
| const OCTOLENS_ID = '0c9d8e7f-6a5b-4c3d-8e2f-9a8b7c6d5e4f' | ||
| const platformsById: Record<string, string> = { [GITHUB_ID]: 'github', [OCTOLENS_ID]: 'octolens' } | ||
|
|
||
| const noHyphens = (id: string) => id.replace(/-/g, '') | ||
| const braced = (id: string) => `{${id.toUpperCase()}}` | ||
|
|
||
| async function call( | ||
| handler: (req: unknown, res: unknown) => Promise<void>, | ||
| req: Record<string, unknown>, | ||
| ) { | ||
| const success = vi.fn() | ||
| await handler({ language: 'en', responseHandler: { success }, ...req }, {}) | ||
| return success | ||
| } | ||
|
|
||
| describe('auto-managed integration guard', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| findIntegrationPlatformsByIds.mockImplementation(async (_qx, ids: string[]) => | ||
| ids.map((id) => platformsById[id]).filter(Boolean), | ||
| ) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['another platform', { platform: 'github' }], | ||
| ['no platform', {}], | ||
| ['no body', undefined], | ||
| ])('creates as before for %s', async (_name, body) => { | ||
| const success = await call(integrationCreate, { body }) | ||
|
|
||
| expect(service.create).toHaveBeenCalledWith(body) | ||
| expect(success).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('refuses to create an octolens integration', async () => { | ||
| await expect(call(integrationCreate, { body: { platform: 'octolens' } })).rejects.toThrow( | ||
| /managed automatically/, | ||
| ) | ||
| expect(service.create).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['another platform', GITHUB_ID], | ||
| ['another platform without hyphens', noHyphens(GITHUB_ID)], | ||
| ['another platform in braces', braced(GITHUB_ID)], | ||
| ['an unknown id', '11111111-2222-4333-8444-555555555555'], | ||
| ['a malformed id', 'not-a-uuid'], | ||
| ])('updates as before for %s', async (_name, id) => { | ||
| await call(integrationUpdate, { params: { id }, body: { status: 'done' } }) | ||
|
|
||
| expect(service.update).toHaveBeenCalledWith(id, { status: 'done' }) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['canonical', OCTOLENS_ID], | ||
| ['without hyphens', noHyphens(OCTOLENS_ID)], | ||
| ['in braces', braced(OCTOLENS_ID)], | ||
| ])('refuses to update an octolens integration given a %s id', async (_name, id) => { | ||
| await expect( | ||
| call(integrationUpdate, { params: { id }, body: { status: 'done' } }), | ||
| ).rejects.toBeInstanceOf(Error400) | ||
| expect(service.update).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('refuses to change an integration platform to octolens', async () => { | ||
| await expect( | ||
| call(integrationUpdate, { params: { id: GITHUB_ID }, body: { platform: 'octolens' } }), | ||
| ).rejects.toBeInstanceOf(Error400) | ||
| expect(service.update).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['other platforms', [GITHUB_ID]], | ||
| ['other platforms without hyphens or in braces', [noHyphens(GITHUB_ID), braced(GITHUB_ID)]], | ||
| ['malformed ids', ['not-a-uuid']], | ||
| ['no ids', undefined], | ||
| ['other platforms in nested arrays', [[GITHUB_ID]]], | ||
| ])('destroys as before for %s', async (_name, ids) => { | ||
| await call(integrationDestroy, { query: { ids } }) | ||
|
|
||
| expect(service.destroyAll).toHaveBeenCalledWith(ids) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['one of several ids', [GITHUB_ID, OCTOLENS_ID]], | ||
| ['a single id', OCTOLENS_ID], | ||
| ['an id without hyphens', [noHyphens(OCTOLENS_ID)]], | ||
| ['an id in braces', [braced(OCTOLENS_ID)]], | ||
| ['an id in a nested array', [[OCTOLENS_ID]]], | ||
| ['an id deeply nested in arrays', [GITHUB_ID, [[OCTOLENS_ID]]]], | ||
| ['an id nested in an object', { a: OCTOLENS_ID }], | ||
| ['an id nested in an array of objects', [{ x: [OCTOLENS_ID] }]], | ||
| ])('refuses to destroy when %s is an octolens integration', async (_name, ids) => { | ||
| await expect(call(integrationDestroy, { query: { ids } })).rejects.toBeInstanceOf(Error400) | ||
| expect(service.destroyAll).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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,59 @@ | ||
| import SequelizeRepository from '@/database/repositories/sequelizeRepository' | ||
| import { Error400 } from '@crowd/common' | ||
| import { findIntegrationPlatformsByIds } from '@crowd/data-access-layer/src/integrations' | ||
| import { AUTO_MANAGED_INTEGRATION_PLATFORMS } from '@crowd/types' | ||
|
|
||
| export function isAutoManagedPlatform(platform: unknown): boolean { | ||
| return AUTO_MANAGED_INTEGRATION_PLATFORMS.some((p) => p === platform) | ||
| } | ||
|
|
||
| export function assertNoAutoManagedPlatform(language: string, platforms: unknown[]): void { | ||
| if (platforms.some(isAutoManagedPlatform)) { | ||
| throw new Error400(language, 'errors.integrations.autoManaged') | ||
| } | ||
| } | ||
|
|
||
| // Postgres also accepts uuids without hyphens or wrapped in braces, so normalize before the lookup. | ||
| export function toCanonicalUuid(id: unknown): string | null { | ||
| if (typeof id !== 'string') { | ||
| return null | ||
| } | ||
| const hex = id | ||
| .trim() | ||
| .replace(/^\{(.*)\}$/, '$1') | ||
| .replace(/-/g, '') | ||
| .toLowerCase() | ||
| if (!/^[0-9a-f]{32}$/.test(hex)) { | ||
| return null | ||
| } | ||
| return [ | ||
| hex.slice(0, 8), | ||
| hex.slice(8, 12), | ||
| hex.slice(12, 16), | ||
| hex.slice(16, 20), | ||
| hex.slice(20), | ||
| ].join('-') | ||
| } | ||
|
|
||
| // Query parsing can nest ids in arrays or objects (e.g. ids[0][0]=…), so collect every leaf. | ||
| export function collectIdLeaves(ids: unknown): unknown[] { | ||
| if (Array.isArray(ids)) { | ||
| return ids.flatMap(collectIdLeaves) | ||
| } | ||
| if (ids !== null && typeof ids === 'object') { | ||
| return Object.values(ids).flatMap(collectIdLeaves) | ||
| } | ||
| return [ids] | ||
| } | ||
|
|
||
| // Malformed or unknown ids are skipped, so requests for other platforms fail exactly as before. | ||
| export async function assertNoAutoManagedIntegration(req, ids: unknown): Promise<void> { | ||
| const uuids = collectIdLeaves(ids) | ||
| .map(toCanonicalUuid) | ||
| .filter((id): id is string => id !== null) | ||
| const platforms = await findIntegrationPlatformsByIds( | ||
| SequelizeRepository.getQueryExecutor(req), | ||
| uuids, | ||
| ) | ||
| assertNoAutoManagedPlatform(req.language, platforms) | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.