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

Development: Remove duplicated code in GitService #8752

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

krusche
Copy link
Member

@krusche krusche commented Jun 6, 2024

Checklist

General

Description

Remove duplicated code in GitService and fix an issue with SSH cloning of git repositories in external services (e.g. Gitlab)

Review Progress

Code Review

  • Code Review 1
  • Code Review 2

Summary by CodeRabbit

  • New Features

    • Introduced SSH-related cleanup functionality to enhance system stability.
  • Improvements

    • Enhanced SSH configuration and key management for better security and maintainability.
    • Improved access control for certain methods to ensure better encapsulation and modularity.
  • Maintenance

    • Added @PreDestroy annotations for proper resource cleanup during shutdown.

@krusche krusche requested a review from a team as a code owner June 6, 2024 19:59
@krusche krusche added this to the 7.2.0 milestone Jun 6, 2024
Copy link

coderabbitai bot commented Jun 6, 2024

Walkthrough

The recent changes introduce new entities like JGitKeyCache and sshdSessionFactory to the AbstractGitService class, enhance SSH configuration handling with a new cleanup() method, and modify method signatures for better encapsulation and flexibility. Additionally, the GitService and BuildJobGitService classes now utilize the @PreDestroy annotation for cleanup tasks and have adjusted method access levels to align with their respective subclasses.

Changes

File Path Change Summary
src/main/java/de/tum/in/www1/artemis/service/connectors/AbstractGitService.java Introduced JGitKeyCache, sshdSessionFactory, added cleanup() method, and modified several method signatures.
src/main/java/de/tum/in/www1/artemis/service/connectors/GitService.java Added @PreDestroy to cleanup() method, changed getGitUri access level to protected and overridden it.
src/main/java/de/tum/in/www1/artemis/service/connectors/localci/buildagent/BuildJobGitService.java Added @PreDestroy to cleanup() method, removed configureSsh() and useSsh(), and changed getGitUriAsString to getGitUri with protected access, overriding it.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant GitService
    participant AbstractGitService
    participant JGitKeyCache
    participant SshdSessionFactory

    Client->>GitService: getGitUri(vcsRepositoryUri)
    GitService->>AbstractGitService: getGitUri(vcsRepositoryUri)
    AbstractGitService->>SshdSessionFactory: createSession()
    SshdSessionFactory->>JGitKeyCache: retrieveKey()
    JGitKeyCache-->>SshdSessionFactory: returnKey()
    SshdSessionFactory-->>AbstractGitService: returnSession()
    AbstractGitService-->>GitService: returnUri()
    GitService-->>Client: returnUri()

    Note over GitService, AbstractGitService: During cleanup
    GitService->>AbstractGitService: cleanup()
    AbstractGitService->>SshdSessionFactory: closeSessions()
    SshdSessionFactory-->>AbstractGitService: sessionsClosed()
    AbstractGitService-->>GitService: cleanupComplete()
Loading

Recent review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between a8455b3 and 2934420.

Files selected for processing (3)
  • src/main/java/de/tum/in/www1/artemis/service/connectors/AbstractGitService.java (6 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/connectors/GitService.java (4 hunks)
  • src/main/java/de/tum/in/www1/artemis/service/connectors/localci/buildagent/BuildJobGitService.java (4 hunks)
Additional context used
Path-based instructions (3)
src/main/java/de/tum/in/www1/artemis/service/connectors/localci/buildagent/BuildJobGitService.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

src/main/java/de/tum/in/www1/artemis/service/connectors/AbstractGitService.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

src/main/java/de/tum/in/www1/artemis/service/connectors/GitService.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

Additional comments not posted (10)
src/main/java/de/tum/in/www1/artemis/service/connectors/localci/buildagent/BuildJobGitService.java (2)

61-63: Proper use of @PreDestroy for cleanup operations.

This ensures that resources are properly released before the object is destroyed, aligning with best practices for resource management.


77-78: Effective simplification of URI retrieval logic.

The method now appropriately checks the usage of SSH and simplifies the process of URI retrieval, which enhances maintainability and reduces duplication.

src/main/java/de/tum/in/www1/artemis/service/connectors/AbstractGitService.java (5)

58-62: Addition of fields to enhance SSH handling.

The new fields (jgitKeyCache, sshCallback, sshdSessionFactory) are essential for setting up and managing SSH sessions, aligning with the goal of enhancing SSH handling in the service.


93-96: Enhanced logic in useSsh method.

The method now checks for the presence of both the SSH private key path and the SSH URL template, ensuring a more robust determination of SSH usage.


101-109: Comprehensive setup in configureSsh method.

The method now includes a more comprehensive setup for SSH sessions, including setting a default credentials provider and configuring the SSH session factory, which enhances security and session management.


118-124: Enhanced configurations in getSshdSessionFactoryBuilder.

The method now includes additional configurations for the SSH directory and home directory, which are crucial for the proper setup and security of SSH sessions.


272-276: Proper resource management in cleanup method.

The cleanup method effectively releases resources related to SSH handling, aligning with best practices for resource management and avoiding potential resource leaks.

src/main/java/de/tum/in/www1/artemis/service/connectors/GitService.java (3)

32-32: Addition of PreDestroy annotation to ensure cleanup before destruction.

This is a good practice for managing resources, especially in a service that interacts with external systems like Git.


160-162: Proper use of @PreDestroy to enhance resource management.

The override of the cleanup method from AbstractGitService ensures that any additional cleanup specific to GitService can be implemented here.


176-177: Visibility change of getGitUri to protected allows subclass overrides.

This change is aligned with the principle of least privilege and allows for more flexible subclass behavior.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@github-actions github-actions bot added the server Pull requests that update Java code. (Added Automatically!) label Jun 6, 2024
Copy link
Contributor

@SimonEntholzer SimonEntholzer left a comment

Choose a reason for hiding this comment

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

Fixes the bug where the garbage collection closes down the session factory 👍
Viewing repositories in the gitlab+jenkins setup works locally

@krusche krusche merged commit 3f27335 into develop Jun 7, 2024
25 of 34 checks passed
@krusche krusche deleted the bugfix/improve-gitservice branch June 7, 2024 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Pull requests that update Java code. (Added Automatically!)
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

None yet

2 participants