-
Notifications
You must be signed in to change notification settings - Fork 175
Move ServiceNow choice values to shared and add state fields helper #2316
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
Show all changes
13 commits
Select commit
Hold shift + click to select a range
81f0094
Include inherited ServiceNow table fields in field lookup
ravikiranvm 6c82000
Fix copilot comments
ravikiranvm ee618de
Revert the sysparm limit
ravikiranvm 7056ce9
Merge branch 'main' into ops-4414
MarceloRGonc 80655f9
Add tests and improve UX
ravikiranvm 8129571
Merge branch 'main' into ops-4414
ravikiranvm c859cbd
Fix sonar
ravikiranvm df77e4a
Improve note
ravikiranvm 99cdb2b
Log the fallback
ravikiranvm a710708
Move ServiceNow choice values to shared and add state fields helper
ravikiranvm 1f8b415
Merge branch 'main' into ops-4414
MarceloRGonc 1affe3b
Merge branch 'ops-4414' into ops-4424
MarceloRGonc ed978a4
Merge branch 'main' into ops-4424
MarceloRGonc 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
7 changes: 5 additions & 2 deletions
7
packages/blocks/servicenow/src/lib/create-field-value-property.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
11 changes: 8 additions & 3 deletions
11
packages/blocks/servicenow/test/lib/create-field-value-property.test.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
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
2 changes: 1 addition & 1 deletion
2
...s/servicenow/src/lib/get-choice-values.ts → ...s/src/lib/servicenow/get-choice-values.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
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,53 @@ | ||
| import { logger } from '@openops/server-shared'; | ||
| import { ServiceNowAuth } from './auth'; | ||
| import { getServiceNowTableFields } from './get-table-fields'; | ||
|
|
||
| export interface ServiceNowStateField { | ||
| element: string; | ||
| column_label: string; | ||
| internal_type: string; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the columns on the given table (including parent-table columns) | ||
| * that are usable as a status field — i.e. those whose values come from a | ||
| * fixed choice list, so we can show them in a dropdown and reliably map | ||
| * polled values back to OpenOps statuses. | ||
| * | ||
| * Picks columns whose `internal_type` is `choice`, or whose sys_dictionary | ||
| * `choice` flag is `'1'` (dropdown) or `'3'` (dropdown without --None--). | ||
| * `'2'` (suggestion) is excluded because it allows free-text values. | ||
| */ | ||
| export async function getServiceNowStateFields( | ||
|
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. |
||
| auth: ServiceNowAuth, | ||
| tableName: string, | ||
| ): Promise<ServiceNowStateField[]> { | ||
| try { | ||
| const fields = await getServiceNowTableFields(auth, tableName); | ||
|
|
||
| return fields | ||
| .filter((f) => { | ||
| if (!f.element?.trim()) { | ||
| return false; | ||
| } | ||
| const internalType = | ||
| typeof f.internal_type === 'string' | ||
| ? f.internal_type | ||
| : f.internal_type?.value ?? ''; | ||
| return ( | ||
| internalType === 'choice' || f.choice === '1' || f.choice === '3' | ||
| ); | ||
| }) | ||
| .map((f) => ({ | ||
| element: f.element, | ||
| column_label: f.column_label || f.element, | ||
| internal_type: | ||
| typeof f.internal_type === 'string' | ||
| ? f.internal_type | ||
| : f.internal_type?.value ?? '', | ||
| })); | ||
| } catch (error) { | ||
| logger.warn('Error fetching ServiceNow state fields', { error }); | ||
| return []; | ||
| } | ||
| } | ||
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
154 changes: 154 additions & 0 deletions
154
packages/openops/test/servicenow-get-state-fields.test.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,154 @@ | ||
| import { ServiceNowAuth } from '../src/lib/servicenow/auth'; | ||
| import { getServiceNowStateFields } from '../src/lib/servicenow/get-state-fields'; | ||
| import { | ||
| getServiceNowTableFields, | ||
| ServiceNowTableField, | ||
| } from '../src/lib/servicenow/get-table-fields'; | ||
|
|
||
| jest.mock('../src/lib/servicenow/get-table-fields'); | ||
|
|
||
| const mockGetTableFields = getServiceNowTableFields as jest.Mock; | ||
|
|
||
| describe('getServiceNowStateFields', () => { | ||
| const mockAuth: ServiceNowAuth = { | ||
| username: 'testuser', | ||
| password: 'testpass', | ||
| instanceName: 'dev12345', | ||
| }; | ||
|
|
||
| const field = ( | ||
| overrides: Partial<ServiceNowTableField>, | ||
| ): ServiceNowTableField => ({ | ||
| element: 'state', | ||
| column_label: 'State', | ||
| internal_type: { value: 'integer' }, | ||
| choice: '1', | ||
| ...overrides, | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('should include columns whose internal_type is choice', async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ | ||
| element: 'category', | ||
| column_label: 'Category', | ||
| internal_type: { value: 'choice' }, | ||
| choice: '0', | ||
| }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([ | ||
| { | ||
| element: 'category', | ||
| column_label: 'Category', | ||
| internal_type: 'choice', | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("should include columns with choice flag '1' (dropdown)", async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: 'state', choice: '1' }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([ | ||
| { element: 'state', column_label: 'State', internal_type: 'integer' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("should include columns with choice flag '3' (dropdown without --None--)", async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: 'priority', column_label: 'Priority', choice: '3' }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([ | ||
| { | ||
| element: 'priority', | ||
| column_label: 'Priority', | ||
| internal_type: 'integer', | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should exclude plain integer columns without a choice flag', async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: 'sys_mod_count', column_label: 'Updates', choice: '0' }), | ||
| field({ | ||
| element: 'reopen_count', | ||
| column_label: 'Reopen count', | ||
| choice: undefined, | ||
| }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| test("should exclude columns with choice flag '2' (suggestion)", async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: 'category', column_label: 'Category', choice: '2' }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| test('should exclude columns with a blank element', async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: '', choice: '1' }), | ||
| field({ element: ' ', choice: '1' }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| test('should fall back to element when column_label is missing', async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| field({ element: 'state', column_label: '', choice: '1' }), | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([ | ||
| { element: 'state', column_label: 'state', internal_type: 'integer' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should handle internal_type provided as a plain string', async () => { | ||
| mockGetTableFields.mockResolvedValue([ | ||
| { | ||
| element: 'state', | ||
| column_label: 'State', | ||
| internal_type: 'choice' as unknown as { value: string }, | ||
| choice: '0', | ||
| }, | ||
| ]); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([ | ||
| { element: 'state', column_label: 'State', internal_type: 'choice' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should return an empty array when the underlying fetch throws', async () => { | ||
| mockGetTableFields.mockRejectedValue(new Error('boom')); | ||
|
|
||
| const result = await getServiceNowStateFields(mockAuth, 'incident'); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
| }); |
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.