Skip to content

Conversation

@vgarcia13
Copy link
Contributor

Related Link:

https://allenai.atlassian.net/browse/GUNDI-4878


This pull request introduces a new Django management command script to automate the creation and updating of field mappings between integrations in Gundi v2.

The script ensures that the correct field mapping configuration is set up between a source and destination integration using a provided provider key.

Key additions and features:

New Management Command for Field Mappings:

  • Added a management command (field_mappings_creation_script.py) that creates or updates field mapping configurations between integrations, using command-line arguments for source connection ID, destination ID, and provider key.
  • Implements transaction handling and error reporting to ensure data consistency and clear feedback if integrations are not found.
  • Automatically updates or creates a RouteConfiguration object with the correct field mappings and associates it with the source integration's default route.
  • Provides detailed output to the command line for successful operations and errors, improving usability for operators.

@vgarcia13 vgarcia13 changed the title Field mapping creation script v1.0 GUNDI-4878: Field mapping creation script v1.0 Nov 10, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a Django management command to automate the creation and updating of field mapping configurations between source and destination integrations in Gundi v2.

Key Changes:

  • Added field_mappings_creation_script.py management command with CLI arguments for connection ID, destination ID, and provider key
  • Implements RouteConfiguration creation/update logic with field mappings
  • Includes transaction handling and error reporting for integration lookups

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +48 to +63
with transaction.atomic():
try:
source_integration = Integration.objects.get(
id=connection_id,
)
except Integration.DoesNotExist:
self.stdout.write(f" -- ERROR: Connection ID {connection_id} does not exist in Gundi v2 -- ")
return

try:
destination_integration = Integration.objects.get(
id=destination_id,
)
except Integration.DoesNotExist:
self.stdout.write(f" -- ERROR: Destination ID {destination_id} does not exist in Gundi v2 -- ")
return
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

The transaction will be rolled back on error (due to the transaction.atomic() context), but the script returns early without rolling back when integrations are not found (lines 54-55, 62-63). This creates inconsistent behavior where some errors cause rollback and others don't. Additionally, returning inside a transaction block that will be exited normally means the transaction commits successfully even though an error was reported. Consider raising an exception or using a different error handling strategy to ensure consistent transaction handling.

Copilot uses AI. Check for mistakes.
Comment on lines +54 to +62
self.stdout.write(f" -- ERROR: Connection ID {connection_id} does not exist in Gundi v2 -- ")
return

try:
destination_integration = Integration.objects.get(
id=destination_id,
)
except Integration.DoesNotExist:
self.stdout.write(f" -- ERROR: Destination ID {destination_id} does not exist in Gundi v2 -- ")
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

Error messages on lines 54 and 62 are written to self.stdout instead of self.stderr. Error messages should be written to stderr for proper error handling and to allow users to separate normal output from error output. Change these to self.stderr.write().

Copilot uses AI. Check for mistakes.

class Command(BaseCommand):

help = "Gundi v2 field mapping creation script"
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The help text for this command is generic and doesn't clearly explain what the script does or when it should be used. Consider adding a more descriptive help text that explains the purpose of creating field mappings between integrations and what the expected outcome is. For example: "Creates or updates field mapping configuration between a source integration and destination integration using a provider key."

Suggested change
help = "Gundi v2 field mapping creation script"
help = (
"Creates or updates field mapping configuration between a source integration and a "
"destination integration using a provider key. "
"This command should be used to establish or modify how fields are mapped from the source "
"integration to the destination integration in Gundi v2. "
"Expected outcome: the specified field mapping is created or updated for the given integrations."
)

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +71
field_mappings = {
str(source_integration.id): {
"obv": {}
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] The nested structure uses a hardcoded key "obv" without explanation of what it represents or why this specific key is used. Add a comment explaining the meaning and purpose of this key in the field mapping structure, or consider using a named constant to make the code more self-documenting.

Copilot uses AI. Check for mistakes.
Comment on lines +83 to +88
route_config, created = RouteConfiguration.objects.get_or_create(
name=f"{source_integration.default_route.name} (Integration ID: {source_integration.id}) - Default Config",
defaults={
"data": field_mappings_result
}
)
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

Using get_or_create with only the name field as the unique identifier can be problematic if multiple integrations could have the same route name pattern. This could lead to unintended sharing of RouteConfiguration objects between different integrations. Consider adding a unique constraint or using the integration ID in a more unique way (e.g., as part of a unique_together constraint with name, or using update_or_create with a more specific lookup).

Copilot uses AI. Check for mistakes.
Comment on lines +30 to +33
'--provider-key',
type=str,
required=True,
help='Provider key to be used in field mapping creation [REQUIRED]'
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Inconsistent quote style: The first two arguments use double quotes for the argument name (lines 18, 24), but the third argument uses single quotes (line 30). For consistency, use double quotes for all argument names, or follow the project's style guide if it specifies a preference.

Suggested change
'--provider-key',
type=str,
required=True,
help='Provider key to be used in field mapping creation [REQUIRED]'
"--provider-key",
type=str,
required=True,
help="Provider key to be used in field mapping creation [REQUIRED]"

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants