fake-api-server.ci.surveillance.component.git¶
This module provides the GitOperation class, enabling various git operations such as branch management, file staging, committing, pushing, and remote repository setup.
The module also contains methods to handle specific CI/CD-related functionality, including automated branch switching and version control integration to sync and manage fake API server configurations.
GitOperation()
¶
Manages Git operations specifically designed for handling code changes and versioning in a configuration-based system. This class provides utility methods for initializing Git repositories, managing branches, handling file additions, committing changes, and pushing to remote repositories, making it suitable for CI/CD environments with automated workflows.
ATTRIBUTE | DESCRIPTION |
---|---|
_git_repo |
Represents the Git repository instance used for various Git operations.
TYPE:
|
_all_staged_files |
A set of file paths that have been staged and added to the Git index.
TYPE:
|
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_current_git_branch
property
¶
Get the name of the current active Git branch.
This method attempts to retrieve the name of the current active branch from the Git repository associated with the object. If the repository is in a detached HEAD state and running in a CI environment, it falls back to an environment variable to determine the branch name.
RETURNS | DESCRIPTION |
---|---|
str
|
Name of the current active Git branch, or an environment variable value if in CI with detached HEAD state. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If an exception is raised and the issue cannot be handled (other than the detached HEAD state in CI environments). |
default_remote_name
property
¶
Provides the default remote name used for remote operations.
RETURNS | DESCRIPTION |
---|---|
str
|
The default remote name string. |
fake_api_server_monitor_git_branch
property
¶
Determines the Git branch name to use for the fake API server monitor configuration updates based on the environment. In CI test mode, it creates a branch name specific to the GitHub Action's event and job ID. In other cases, it uses a default branch name.
RETURNS | DESCRIPTION |
---|---|
str
|
The Git branch name for fake API server monitor configuration updates. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If |
is_ci_test_mode
property
¶
Checks whether the application is running in CI test mode.
This property reads an environment variable CI_TEST_MODE
and determines
if the application is operating in a continuous integration test mode. The
value is parsed as a string, capitalized, evaluated as a Python literal,
and defaults to False
if the variable is not set.
RETURNS | DESCRIPTION |
---|---|
bool
|
Boolean indicating if the application is in CI test mode. |
is_in_ci_env
property
¶
Determine if the code is running in a Continuous Integration (CI) environment.
This method evaluates environment variables to determine if the script is running in a CI environment such as GitHub Actions.
RETURNS | DESCRIPTION |
---|---|
bool
|
A boolean indicating whether the script is running in a CI environment. |
repository
property
writable
¶
Provides access to the repository instance that is internally stored. Ensures that the repository instance is not accessed until it has been properly initialized.
RETURNS | DESCRIPTION |
---|---|
Repo
|
The repository instance. |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the repository instance (_git_repo) is not set. |
_add_files(all_files, target_files)
¶
Adds multiple files to the repository's index if they match the target criteria and logs the file addition operations. The method ensures that staged files are tracked and processes specific target files by adding them to the index.
PARAMETER | DESCRIPTION |
---|---|
all_files
|
A set containing the paths of all files which are staged in the repository. This is used to check if a file is already staged before adding.
TYPE:
|
target_files
|
A set containing names or paths of target files to be added to the repository's index. Each file in this set is checked and processed accordingly (either as a single file or recursively for YAML files).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_commit_changes(surveillance_config)
¶
Commits changes to the repository using the provided surveillance configuration and resets all staged files upon completion. This method ensures the change is recorded with the appropriate commit metadata.
PARAMETER | DESCRIPTION |
---|---|
surveillance_config
|
The surveillance configuration containing git information required for creating the commit.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Commit
|
A reference to the created commit in the repository. |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_get_all_fake_api_server_configs(surveillance_config)
¶
Retrieve all fake API server configurations based on the provided surveillance configuration.
This function processes a given surveillance configuration to locate fake API server configuration files matching a specific pattern. It identifies and returns a set of file paths pointing to those fake API server configuration files.
PARAMETER | DESCRIPTION |
---|---|
surveillance_config
|
The configuration object containing information about the surveillance setup and associated fake API server data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Set[Path]
|
A set of file paths to the identified fake API server configuration files. Only files matching the criteria will be included in the returned set. |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_init_git(surveillance_config)
¶
Initializes a Git repository based on the provided surveillance configuration.
This function verifies the existence of the required configuration file for the PyFake-API-Server. It ensures that the provided configuration is valid and then returns a reference to the repository initialized at the provided location.
PARAMETER | DESCRIPTION |
---|---|
surveillance_config
|
The surveillance configuration object containing details necessary to manage the fake API server and its subcommands.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Repo
|
A |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the configuration file specified in the subcommand arguments does not exist. |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_init_git_remote(surveillance_config, remote_name)
¶
Initializes a Git remote for the provided repository configuration and ensures the appropriate URL matches the expected format. Creates a new remote if it does not exist, or updates the existing remote URL if it is not as expected.
PARAMETER | DESCRIPTION |
---|---|
surveillance_config
|
The configuration object containing Git repository information required to set or update the remote.
TYPE:
|
remote_name
|
The name of the Git remote to create or update.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Remote
|
The created or updated Git remote instance. |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_push_to_remote(git_remote)
¶
Pushes the current branch to the specified remote server.
This method pushes the local branch to the remote Git repository specified.
The refspec
is constructed dynamically based on the branch name stored in
self.fake_api_server_monitor_git_branch
. The operation is configured with
the force option enabled. Any errors encountered during the push operation
are automatically raised.
PARAMETER | DESCRIPTION |
---|---|
git_remote
|
The remote Git repository where the local branch will be pushed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_reset_all_staged_files()
¶
Resets all staged files by clearing the internal list that holds file references.
This method ensures that all previously staged files for operation are removed and the internal state is reset.
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
_switch_git_branch(git_ref)
¶
Switches the current git branch to the specified branch reference. If the branch exists, it switches to it. Otherwise, it creates a new branch with the given reference and switches to it.
If the current branch is already the specified branch, no action is performed.
PARAMETER | DESCRIPTION |
---|---|
git_ref
|
The name of the branch to switch to. If the branch does not exist, a new branch will be created with this name.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in fake_api_server_plugin/ci/surveillance/component/git.py
version_change(surveillance_config)
¶
Update and synchronize the code version from a Git repository based on the provided surveillance configuration. This function initializes a Git repository, sets up the remote configuration, switches to the target branch, identifies files to add, stages them, and optionally commits and pushes changes.
PARAMETER | DESCRIPTION |
---|---|
surveillance_config
|
The configuration object of type SurveillanceConfig that specifies the settings and parameters required to manage the code repository.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Boolean indicating if any changes were committed and pushed. Returns True if changes were committed and pushed successfully, else False. |