Skip to content

Refactor: Extract common git repository preparation logic into reusable function #229

@SergK

Description

@SergK

Is your feature request related to a problem? Please describe.

The codebase has duplicated git repository preparation logic across multiple chain handlers (put_deploy_configs, put_gitlab_ci_config). Each handler independently retrieves GitServer resources, extracts SSH credentials, clones repositories, and checks out branches, leading to code duplication and increased maintenance burden.

Describe the solution you'd like

Extract the common git repository preparation workflow into a reusable function:

  • Create a GitRepositoryContext struct to hold necessary git operation context (GitServer, Secret, SSH keys, URLs, working directory)
  • Implement a PrepareGitRepository function that handles:
    • GitServer resource and Secret retrieval
    • SSH credential extraction
    • Repository cloning (if needed)
    • Default branch checkout
  • Refactor existing chain handlers to use this common function
  • Add comprehensive unit tests for the new functionality

Describe alternatives you've considered

  • Keep the current duplicated code - rejected due to maintainability concerns
  • Use a builder pattern - overly complex for this straightforward use case
  • Create a separate service struct - adds unnecessary abstraction

Additional context

This refactoring improves code maintainability by centralizing git operations setup logic and makes it easier to add new chain handlers that require similar git operations. The change affects put_deploy_configs.go and put_gitlab_ci_config.go, reducing ~50 lines of duplicated code per handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions