Skip to content

Git Agent

osa_tool.core.git.git_agent.GitAgent

Bases: ABC

Abstract base class for Git platform agents.

This class provides functionality to clone repositories, create and checkout branches, commit and push changes, and create pull requests.

Attributes:

Name Type Description
agent_signature str

A signature string appended to a pull request descriptions.

author

An author name that appended to a pull request description.

repo_url

The URL of the Git repository.

clone_dir

The directory where the repository will be cloned.

branch_name

The name of the branch to be created.

repo

The GitPython Repo object representing the repository.

token

The Git token for authentication.

fork_url

The URL of the created fork of a Git repository.

metadata

Git repository metadata.

base_branch

The name of the repository's branch.

pr_report_body

A formatted message for a pull request.

__init__(repo_url, repo_branch_name=None, branch_name='osa_tool', author=None)

Initializes the agent with repository info.

Parameters:

Name Type Description Default
repo_url str

The URL of the GitHub repository.

required
repo_branch_name str

The name of the repository's branch to be checked out.

None
branch_name str

The name of the branch to be created. Defaults to "osa_tool".

'osa_tool'
author str

The name of the author of the pull request.

None

clone_repository()

Clones or initializes the Git repository in the local filesystem.

This is the main entry point for obtaining a local copy of the repository. It implements a multi-step strategy: 1. If the repository is already initialized, returns early. 2. If the directory exists locally, initializes from existing files. 3. If cloning is needed, checks for existing 'osa_tool' branch first. 4. Falls back to cloning the default branch if 'osa_tool' doesn't exist.

Raises:

Type Description
InvalidGitRepositoryError

If the local directory exists but is not a valid Git repository.

Exception

If all cloning attempts fail.

Note

This method handles both authenticated and unauthenticated cloning attempts for the default branch. It prefers the fork URL when available.

commit_and_push_changes(branch=None, commit_message='osa_tool recommendations', force=False)

Commits and pushes changes to the forked repository.

Parameters:

Name Type Description Default
branch str

The name of the branch to push changes to. Defaults to branch_name.

None
commit_message str

The commit message. Defaults to "osa_tool recommendations".

'osa_tool recommendations'
force bool

Option to force push the commit. Defaults to False

False

create_and_checkout_branch(branch=None)

Creates and checks out a new branch.

If the branch already exists, it simply checks out the branch.

Parameters:

Name Type Description Default
branch str

The name of the branch to create or check out. Defaults to branch_name.

None

create_fork() abstractmethod

Create a fork of the repository.

create_pull_request(title=None, body=None) abstractmethod

Create a pull request / merge request on the platform.

Parameters:

Name Type Description Default
title str

The title of the PR. If None, the commit message will be used.

None
body str

The body/description of the PR. If None, the commit message with agent signature will be used.

None

get_attachment_branch_files(branch='osa_tool_attachments')

Gets list of report files from attachment branch.

Parameters:

Name Type Description Default
branch str

The name of the attachment branch.

'osa_tool_attachments'

Returns:

Type Description
List[str]

List of report filenames found in the branch.

star_repository() abstractmethod

Star the repository on the platform.

update_about_section(about_content)

Tries to update the 'About' section of the base and fork repository with the provided content.

Parameters:

Name Type Description Default
about_content dict

Dictionary containing the metadata to update about section.

required

Raises:

Type Description
ValueError

If the Git token is not set or inappropriate platform used.

upload_report(report_filename, report_filepath, report_branch='osa_tool_attachments', commit_message='docs: upload pdf report')

Uploads the generated PDF report to a separate branch.

Parameters:

Name Type Description Default
report_filename str

Name of the report file.

required
report_filepath str

Path to the report file.

required
report_branch str

Name of the branch for storing reports. Defaults to "osa_tool_attachments".

'osa_tool_attachments'
commit_message str

Commit message for the report upload. Defaults to "upload pdf report".

'docs: upload pdf report'

validate_topics(topics) abstractmethod

Validates topics against platform-specific APIs.

Parameters:

Name Type Description Default
topics List[str]

List of potential topics to validate

required

Returns:

Type Description
List[str]

List[str]: List of validated topics that exist on platform