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

feat(server): support integration in import cmd #1372

Merged
merged 5 commits into from
Jan 27, 2025
Merged

Conversation

yk-eukarya
Copy link
Contributor

@yk-eukarya yk-eukarya commented Jan 27, 2025

Summary by CodeRabbit

  • New Features

    • Added support for specifying an integration ID during import operations
    • Enhanced import functionality to work with both user and integration identifiers
  • Bug Fixes

    • Improved operator validation to ensure valid import credentials
    • Added more robust error handling for import tasks
  • Refactor

    • Updated task payload and validation logic to support more flexible import scenarios

Copy link
Contributor

coderabbitai bot commented Jan 27, 2025

Walkthrough

The pull request introduces support for integration-based item imports in the Re:Earth CMS system. The changes modify the command-line import functionality to allow specifying an integration ID alongside the existing user ID. This enhancement provides more flexibility in how import operations are initiated, enabling imports to be performed using either user or integration credentials. The modifications span multiple files, updating the import process's logic to handle integration-based operators and validate payloads accordingly.

Changes

File Change Summary
server/cmd/reearth-cms/main.go - Added new generateIntegrationOperator function
- Modified generateUserOperator to accept string user ID
- Updated main function to support integration ID argument
server/internal/infrastructure/gcp/taskrunner.go - Added -integrationId argument to import items command
server/internal/usecase/interactor/item.go - Enhanced error handling for operator validation
- Modified task payload construction to support integration ID
server/pkg/task/task.go - Added IntegrationId field to ImportPayload
- Updated Validate() method to allow either user or integration ID

Sequence Diagram

sequenceDiagram
    participant CLI as Command Line Interface
    participant MainFunc as Main Function
    participant OperatorGen as Operator Generator
    participant ImportInteractor as Import Interactor
    participant TaskRunner as Task Runner

    CLI->>MainFunc: Provide userId or integrationId
    alt User-based Import
        MainFunc->>OperatorGen: generateUserOperator()
    else Integration-based Import
        MainFunc->>OperatorGen: generateIntegrationOperator()
    end
    OperatorGen-->>MainFunc: Return Operator
    MainFunc->>ImportInteractor: Initiate Import
    ImportInteractor->>TaskRunner: Create Import Task
    TaskRunner->>TaskRunner: Execute Import
Loading

Possibly related PRs

Poem

🐰 A Rabbit's Import Tale 🥕
With integrations now in sight,
Our import process shines so bright!
User or integration, take your pick,
Flexibility is our magic trick!
Re:Earth CMS, hopping with glee! 🌈

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Jan 27, 2025

Deploy Preview for reearth-cms canceled.

Name Link
🔨 Latest commit 843da78
🔍 Latest deploy log https://app.netlify.com/sites/reearth-cms/deploys/67976719a1e81f0008ed2477

# Conflicts:
#	server/cmd/reearth-cms/main.go
#	server/internal/infrastructure/gcp/taskrunner.go
#	server/internal/usecase/interactor/item.go
#	server/pkg/task/task.go
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 48c4346 and e4a90f4.

📒 Files selected for processing (4)
  • server/cmd/reearth-cms/main.go (5 hunks)
  • server/internal/infrastructure/gcp/taskrunner.go (1 hunks)
  • server/internal/usecase/interactor/item.go (1 hunks)
  • server/pkg/task/task.go (2 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
server/cmd/reearth-cms/main.go

[error] 350-350: Type mismatch error: cannot use &iId (value of type *idx.ID[accountdomain.Integration]) as *idx.ID[id.Integration] value in struct literal

🔇 Additional comments (9)
server/cmd/reearth-cms/main.go (3)

43-43: Flag naming convention looks good.

The new -integrationId flag is consistently named and clearly indicates its purpose in the CLI interface.


95-101: Ensure mutual exclusivity or fallback logic is well-defined.

Currently, the code checks whether uIdStr or iIdStr is provided and picks the first non-empty one. Consider clarifying in the help text or validation logic if both flags are provided at the same time or if only one is expected.


281-285: Refined error handling for string-based user ID conversion.

Good approach switching to string for the user ID input. The error return is appropriate if parsing fails, ensuring invalid user IDs do not propagate further.

server/pkg/task/task.go (2)

80-80: Field reflects expanded use case.

The new IntegrationId field correctly extends the payload structure to support integration-based imports. No immediate issues detected.


90-90: Validation logic updated for flexible identity.

Allowing either UserId or IntegrationId removes the strict reliance on user-based imports. The logical OR condition is concise and clear.

server/internal/infrastructure/gcp/taskrunner.go (1)

236-236: Expanded CLI args.

Adding -integrationId supports the new integration-based import. Make sure the build steps handle both user and integration IDs as intended.

server/internal/usecase/interactor/item.go (3)

637-639: LGTM! Early operator validation is properly implemented.

The validation ensures that either a user or integration is present before proceeding with the import operation.


654-659: LGTM! Task payload correctly handles both user and integration scenarios.

The implementation properly sets either UserId or IntegrationId in the task payload based on the operator type, which aligns with the PR objective to support integration-based imports.


654-659: Verify task runner compatibility with IntegrationId.

Let's ensure the task runner implementation can handle the new IntegrationId field in the payload.

✅ Verification successful

Task runner properly handles IntegrationId field

The task system is correctly implemented to handle the IntegrationId field:

  • IntegrationId is properly defined in ImportPayload struct
  • Validation logic explicitly accepts either UserId or IntegrationId
  • The implementation confirms full compatibility with integration-based imports
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the task runner implementation handles IntegrationId

# Search for IntegrationId handling in task runner code
rg -A 5 'IntegrationId.*string' server/pkg/task/

Length of output: 344


Script:

#!/bin/bash
# Search for task processing logic that uses IntegrationId
rg -A 10 'func.*Process.*Task' server/pkg/task/

Length of output: 49


Script:

#!/bin/bash
# Search for task-related code with broader patterns
rg -l "task" server/pkg/task/ && \
echo "=== Task Handler Contents ===" && \
cat server/pkg/task/task.go

Length of output: 2015

Comment on lines 322 to 357
func generateIntegrationOperator(ctx context.Context, iIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
iId, err := accountdomain.IntegrationIDFrom(iIdStr)
if err != nil {
return nil, err
}
w, err := accRepo.Workspace.FindByIntegration(ctx, iId)
if err != nil {
return nil, err
}

rw := w.FilterByIntegrationRole(iId, workspace.RoleReader).IDs()
ww := w.FilterByIntegrationRole(iId, workspace.RoleWriter).IDs()
mw := w.FilterByIntegrationRole(iId, workspace.RoleMaintainer).IDs()
ow := w.FilterByIntegrationRole(iId, workspace.RoleOwner).IDs()

rp, wp, mp, op, err := operatorProjects(ctx, repo, w, rw, ww, mw, ow)
if err != nil {
return nil, err
}

return &usecase.Operator{
AcOperator: &accountusecase.Operator{
User: nil,
ReadableWorkspaces: rw,
WritableWorkspaces: ww,
MaintainableWorkspaces: mw,
OwningWorkspaces: ow,
},
Integration: &iId,
Lang: language.English.String(),
ReadableProjects: rp,
WritableProjects: wp,
MaintainableProjects: mp,
OwningProjects: op,
}, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix type mismatch for the Integration field.

The pipeline error indicates a mismatch between accountdomain.IntegrationID and id.IntegrationID. Update the code to properly convert the parsed integration ID to match the type expected by usecase.Operator. For example:

-        Integration:          &iId,
+        // Convert accountdomain.IntegrationID (iId) to the correct id.IntegrationID.
+        integrationId2, err := id.IntegrationIDFrom(iId.String())
+        if err != nil {
+            return nil, err
+        }
+        Integration:          &integrationId2,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func generateIntegrationOperator(ctx context.Context, iIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
iId, err := accountdomain.IntegrationIDFrom(iIdStr)
if err != nil {
return nil, err
}
w, err := accRepo.Workspace.FindByIntegration(ctx, iId)
if err != nil {
return nil, err
}
rw := w.FilterByIntegrationRole(iId, workspace.RoleReader).IDs()
ww := w.FilterByIntegrationRole(iId, workspace.RoleWriter).IDs()
mw := w.FilterByIntegrationRole(iId, workspace.RoleMaintainer).IDs()
ow := w.FilterByIntegrationRole(iId, workspace.RoleOwner).IDs()
rp, wp, mp, op, err := operatorProjects(ctx, repo, w, rw, ww, mw, ow)
if err != nil {
return nil, err
}
return &usecase.Operator{
AcOperator: &accountusecase.Operator{
User: nil,
ReadableWorkspaces: rw,
WritableWorkspaces: ww,
MaintainableWorkspaces: mw,
OwningWorkspaces: ow,
},
Integration: &iId,
Lang: language.English.String(),
ReadableProjects: rp,
WritableProjects: wp,
MaintainableProjects: mp,
OwningProjects: op,
}, nil
}
func generateIntegrationOperator(ctx context.Context, iIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
iId, err := accountdomain.IntegrationIDFrom(iIdStr)
if err != nil {
return nil, err
}
w, err := accRepo.Workspace.FindByIntegration(ctx, iId)
if err != nil {
return nil, err
}
rw := w.FilterByIntegrationRole(iId, workspace.RoleReader).IDs()
ww := w.FilterByIntegrationRole(iId, workspace.RoleWriter).IDs()
mw := w.FilterByIntegrationRole(iId, workspace.RoleMaintainer).IDs()
ow := w.FilterByIntegrationRole(iId, workspace.RoleOwner).IDs()
rp, wp, mp, op, err := operatorProjects(ctx, repo, w, rw, ww, mw, ow)
if err != nil {
return nil, err
}
integrationId2, err := id.IntegrationIDFrom(iId.String())
if err != nil {
return nil, err
}
return &usecase.Operator{
AcOperator: &accountusecase.Operator{
User: nil,
ReadableWorkspaces: rw,
WritableWorkspaces: ww,
MaintainableWorkspaces: mw,
OwningWorkspaces: ow,
},
Integration: &integrationId2,
Lang: language.English.String(),
ReadableProjects: rp,
WritableProjects: wp,
MaintainableProjects: mp,
OwningProjects: op,
}, nil
}
🧰 Tools
🪛 GitHub Actions: ci

[error] 350-350: Type mismatch error: cannot use &iId (value of type *idx.ID[accountdomain.Integration]) as *idx.ID[id.Integration] value in struct literal

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
server/cmd/reearth-cms/main.go (2)

96-102: Enhance error handling specificity.

While the logic for operator generation is correct, the error message could be more specific about which operation failed (user-based or integration-based).

Consider this improvement:

-		if err != nil || op == nil {
-			log.Fatalf("failed to generate operator: %v", err)
+		if err != nil || op == nil {
+			source := "user"
+			if iIdStr != nil && *iIdStr != "" {
+				source = "integration"
+			}
+			log.Fatalf("failed to generate %s-based operator: %v", source, err)

323-358: Add function documentation.

Consider adding documentation to explain the purpose and behavior of this function, especially regarding workspace permissions and integration-based access control.

Add this documentation:

+// generateIntegrationOperator creates a new operator for integration-based operations.
+// It retrieves workspace permissions for the given integration ID and sets up appropriate
+// access levels for projects within those workspaces.
 func generateIntegrationOperator(ctx context.Context, iIdStr string, repo *repo.Container, accRepo *accountrepo.Container) (*usecase.Operator, error) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4a90f4 and 843da78.

📒 Files selected for processing (1)
  • server/cmd/reearth-cms/main.go (5 hunks)
🔇 Additional comments (2)
server/cmd/reearth-cms/main.go (2)

17-17: LGTM: Import and flag definitions are correct.

The new import and flag definitions are properly implemented to support integration-based imports.

Also applies to: 44-44


351-351: Verify integration ID type conversion.

The integration ID conversion needs verification to ensure type compatibility between accountdomain.IntegrationID and integration2.ID.

Run this script to verify the type conversion:

@yk-eukarya yk-eukarya enabled auto-merge (squash) January 27, 2025 11:11
@yk-eukarya yk-eukarya disabled auto-merge January 27, 2025 11:12
@yk-eukarya yk-eukarya self-assigned this Jan 27, 2025
@yk-eukarya yk-eukarya merged commit 7088693 into main Jan 27, 2025
24 checks passed
@yk-eukarya yk-eukarya deleted the feat/import-cmd branch January 27, 2025 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant