Skip to content
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

SALTO-7399: advanced fields references #7209

Merged
merged 7 commits into from
Mar 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/jira-adapter/e2e_test/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const nullProgressReporter: ProgressReporter = {
reportProgress: () => {},
}

const deleteElementsAtTheEnd = true // use for debugging
const deleteElementsAtTheEnd = true // when debugging you can change to false to keep the created elements and see the deployed elements in the service

each([
['Cloud', false],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const advancedFieldsReferenceFunc =
if (value == null) {
return WALK_NEXT_STEP.SKIP
}
if (Object.prototype.hasOwnProperty.call(value, ADVANCED_FIELDS)) {
if (value[ADVANCED_FIELDS] !== undefined) {
func(value, ADVANCED_FIELDS)
return WALK_NEXT_STEP.SKIP
}
Expand Down
11 changes: 6 additions & 5 deletions packages/jira-adapter/src/filters/field_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* CERTAIN THIRD PARTY SOFTWARE MAY BE CONTAINED IN PORTIONS OF THE SOFTWARE. See NOTICE FILE AT https://github.com/salto-io/salto/blob/main/NOTICES
*/
import { Element, InstanceElement, isInstanceElement } from '@salto-io/adapter-api'
import { InstanceElement, isInstanceElement } from '@salto-io/adapter-api'
import { references as referenceUtils } from '@salto-io/adapter-components'
import _ from 'lodash'
import { referencesRules, JiraFieldReferenceResolver, contextStrategyLookup } from '../reference_mapping'
Expand All @@ -22,14 +22,15 @@ import { addFieldsTemplateReferences } from './fields/reference_to_fields'

const NO_REFERENCES_TYPES = (): string[] => [PROJECT_COMPONENT_TYPE, FIELD_CONFIGURATION_TYPE_NAME]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need it as a function?
I agree that it should be a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as I answered in the previous comment


const addWalkOnReferences = (elements: Element[], config: JiraConfig): void => {
const addWalkOnReferences = (instances: InstanceElement[], config: JiraConfig): void => {
if (!config.fetch.walkOnReferences) {
return
}
const instances = elements.filter(isInstanceElement)
const fieldInstances = instances.filter(instance => instance.elemID.typeName === FIELD_TYPE_NAME)
const fieldInstancesById = new Map(
fieldInstances.map(instance => [instance.value.id, instance] as [string, InstanceElement]),
fieldInstances
.filter(instance => typeof instance.value.id === 'string')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
I guess you prefer is that way, but IMO _.isString is cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to go with a natural solution over a package one

.map(instance => [instance.value.id, instance] as [string, InstanceElement]),
)
walkOnAutomations(
instances.filter(instance => instance.elemID.typeName === AUTOMATION_TYPE),
Expand All @@ -42,7 +43,7 @@ const addWalkOnReferences = (elements: Element[], config: JiraConfig): void => {
const filter: FilterCreator = ({ config }) => ({
name: 'fieldReferencesFilter',
onFetch: async elements => {
addWalkOnReferences(elements, config)
addWalkOnReferences(elements.filter(isInstanceElement), config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
you can save the instances in a variable before, because you filter by isInstanceElement again in line 53


const fixedDefs = referencesRules.map(def =>
config.fetch.enableMissingReferences ? def : _.omit(def, 'missingRefStrategy'),
Expand Down
16 changes: 10 additions & 6 deletions packages/jira-adapter/src/filters/fields/reference_to_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import { FIELD_TYPE_NAME } from './constants'

const CUSTOM_FIELD_PATTERN = /(customfield_\d+)/

const referenceCustomFields = (
text: string,
fieldInstancesById: Map<string, InstanceElement>,
enableMissingReferences: boolean,
): TemplateExpression | string =>
const referenceCustomFields = ({
text,
fieldInstancesById,
enableMissingReferences,
}: {
text: string
fieldInstancesById: Map<string, InstanceElement>
enableMissingReferences: boolean
}): TemplateExpression | string =>
extractTemplate(text, [CUSTOM_FIELD_PATTERN], expression => {
if (!expression.match(CUSTOM_FIELD_PATTERN)) {
return expression
Expand All @@ -37,6 +41,6 @@ export const addFieldsTemplateReferences =
(fieldInstancesById: Map<string, InstanceElement>, enableMissingReferences: boolean): referenceFunc =>
(value: Value, fieldName: string): void => {
if (typeof value[fieldName] === 'string') {
value[fieldName] = referenceCustomFields(value[fieldName], fieldInstancesById, enableMissingReferences)
value[fieldName] = referenceCustomFields({ text: value[fieldName], fieldInstancesById, enableMissingReferences })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ describe('addFieldsTemplateReferences', () => {
return [id, new InstanceElement(`field_${index + 1}`, createEmptyType('Field'), { id })]
}),
)

// beforeEach(() => {
// context = new InstanceElement(
// 'context',
// new ObjectType({ elemID: new ElemID(JIRA, 'CustomFieldContext') }),
// {
// id: 2,
// projectIds: ['3', '4'],
// },
// undefined,
// {
// [CORE_ANNOTATIONS.PARENT]: [new ReferenceExpression(field.elemID, field)],
// },
// )
// })

it('should make a reference to a custom field', async () => {
const value: Value = {
script: 'abc customfield_2 def',
Expand Down