Skip to main content

# GitHub Automation Toolkit Overview

Project description

GitHub Automation Toolkit Overview

This package provides a Python class gh for automating common GitHub and Git tasks using the GitHub API and the Git command-line interface. This is designed to shorten and simplify github commands.

why this is different and needed:

I created it to simplify using github: many things i wish I could do in github with one command actually require several commands. For example:

  • uploading an entire folder (which beforehand wasn't initiated into github) to a repo is one command in this package: uploadFolderFileAsCommitToRepo(...). In normal git this would have been surprisingly complicated: first you would need to initiate the folder, but to initiate the folder you would need to pull from the original github repo, but that would then wipe all the contents of that folder or require a merging procedure.
  • in this package you can push with one command: uploadFolderFileAsCommitToRepo(...). Normally using github pushing requires 3 steps: 1) adding, committing, and pushing.
  • this package allows one to forcibly pull from a repo to a folder regardless of whether the folder was initiated or not using this command: force_pull_repo_to_folder(...).
  • Ultimately, there is a reason why github is so complex: it is intended to allow multiple users to work on the same project - but when you are just a single user this complexity is burdensome.

Capabilities:

  • Repository Management: Create and delete GitHub repositories.
  • Branch Management: Create, checkout, and delete branches both locally and on GitHub.
  • Commit & Upload: Upload a local folder to a GitHub repository as a commit, with support for automatic initialization and remote setup.
  • Version Comparison: Compare differences between two Git commits based on timestamps using Git's internal log and diff mechanisms.

Docs

init( self, access_token ):

Initialize GitHub access

Authenticates the user using a personal access token.

Parameters:

  • access_token (str): Your GitHub personal access token.

Example:

g = gh("your_github_access_token")

createRepository( self, repo_name = "new-repository" , repo_description = "This is a description for the new repository", is_private=True ):

Create a new GitHub repository.

Parameters:

  • repo_name (str): Name of the repository.
  • repo_description (str): Description of the repository.
  • is_private (bool): Whether the repository is private.

Returns:

  • None

Example:

g = gh("your_github_access_token")
g.createRepository(repo_name="test-repo", repo_description="Testing", is_private=False)

create_branch_in_repo(self, repo_path, branch_name):

Create or checkout a branch in a local Git repository

Parameters:

  • repo_path (str): Local path to the repository.
  • branch_name (str): Name of the branch to create or checkout.

Example:

g.create_branch_in_repo("/path/to/repo", "feature-branch")

force_pull_repo_to_folder(self, repo_name, target_folder, force_overwrite=True):

Clone or pull a GitHub repo into a local folder.

Parameters:

  • repo_name (str): Name of the GitHub repository to pull.
  • target_folder (str): Path to the folder to receive the repo contents.
  • force_overwrite (bool): If True, forcibly overwrites the contents of the local folder with the remote repo.

Behavior:

  • If the folder is not a git repo, it will be replaced with the contents of the remote repo.
  • If the folder is a git repo but has untracked or conflicting files, they will be overwritten if force_overwrite=True.

Example:

g.force_pull_repo_to_folder("my-repo", "/path/to/folder", force_overwrite=True)

uploadFolderFileAsCommitToRepo( self, repo_name="your_repo_name", folder_path="path/to/your/folder", commit_message="Add folder content", branch="main", force=True):

Upload an entire folder as a commit to a GitHub repo

Parameters:

  • repo_name (str): Name of the GitHub repository.
  • folder_path (str): Path to the local folder.
  • commit_message (str): Commit message.
  • branch (str): Branch name.
  • force (bool): Force push if conflicts occur.

Example:

g.uploadFolderFileAsCommitToRepo(
repo_name="test-repo",
folder_path="./local_folder",
commit_message="Initial upload",
branch="main",
force=True
)

delete_github_repository(self, repo_name):

Delete a GitHub repository

Parameters:

  • repo_name (str): Name of the repository to delete.

Example:

g.delete_github_repository("test-repo")

delete_github_branch( self, repo_name, branch_name ):

Delete a branch from a GitHub repository

Parameters:

  • repo_name (str): Repository name.
  • branch_name (str): Name of the branch to delete.

Example:

g.delete_github_branch("test-repo", "feature-branch")

displayDifferences( self, repo_path, time1, time2, time_choice):

Display differences between two commits based on timestamps

Finds two Git commits near given times and shows their git diff.

Parameters:

  • repo_path (str): Path to the local repository.
  • time1 (datetime): Timestamp near the first commit.
  • time2 (datetime): Timestamp near the second commit.
  • time_choice (str): Strategy for selecting commits - 'closest', 'before', 'after'.

Example:

from datetime import datetime

t1 = datetime(2023, 5, 1, 12, 0)
t2 = datetime(2023, 5, 2, 12, 0)

g.displayDifferences("/path/to/repo", t1, t2, "closest")

explaining time_choice (str): Strategy for selecting which commit to use based on the provided times. Options:

  • 'closest': Picks the commit whose timestamp is closest to the given time (either before or after).
  • 'before': Picks the most recent commit before the given time. If no earlier commit exists, selects the earliest available commit.
  • 'after': Picks the first commit after the given time. If no later commit exists, selects the latest available commit.

How It Works:

  1. For both time1 and time2, it runs a Git command to list all commits along with their timestamps.
  2. It chooses a commit near each provided time according to the selected time_choice option.
  3. It then runs git diff between the two selected commits and prints the differences to the console.

upload_file(self, repo_name, file_path, target_path=None, commit_message="Add file"):

Upload a single file to a GitHub repository using the GitHub API

This method uploads a single file to a GitHub repository. If the file already exists, it is updated; otherwise, a new file is created. The commit includes a user-defined commit message.

Parameters:

  • repo_name (str): Full name of the repository (e.g. "username/repo").
  • file_path (str): Local path to the file you want to upload.
  • target_path (str, optional): Target path inside the repo (e.g. "folder/file.txt"). Defaults to the file’s basename.
  • commit_message (str, optional): The commit message to use. Defaults to "Add file".

Example:

gh.upload_file(
repo_name="octocat/Hello-World",
file_path="local/path/to/file.txt",
target_path="docs/file.txt",
commit_message="Upload documentation file"
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

githubinteract-0.2.19.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

githubInteract-0.2.19-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file githubinteract-0.2.19.tar.gz.

File metadata

  • Download URL: githubinteract-0.2.19.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for githubinteract-0.2.19.tar.gz
Algorithm Hash digest
SHA256 9751e1762ed116be18d4849fc0fb12a46149656e5d5303bc9804b9f1898a9bb4
MD5 c88e2bdf04896516a9af847ada2e8b56
BLAKE2b-256 6465a750f2197e94570dc1def0a61e6e91e3cc680fcfb16ab8646612cb1bf496

See more details on using hashes here.

File details

Details for the file githubInteract-0.2.19-py3-none-any.whl.

File metadata

File hashes

Hashes for githubInteract-0.2.19-py3-none-any.whl
Algorithm Hash digest
SHA256 2e4bf9163b65d783505564e6b4de9c1bb31d69840f51f0bd339d5c7c8f8b9d68
MD5 db5c91513db8976cf492ec650e3f8bd7
BLAKE2b-256 54ecdeab22b21b07c9e034a3125e7f735a69bb5f8a9d1d6ccbcf3d1740d3bf73

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page