This Python-based bot automates the process of committing changes to a GitHub repository. It tracks changes and logs them with timestamps in a streak_tracker.txt file, pushing them to your GitHub repository. The bot uses a JSON file to store commit messages and their corresponding changes, ensuring that commits are not repeated.
- Appends timestamps to a tracking file (
streak_tracker.txt) in your repository. - Commits changes with messages stored in a JSON file.
- Pushes the commits to your GitHub repository.
- Logs each operation to a file (
github_bot.log) for troubleshooting. - Supports handling multiple commits stored in a JSON file.
Before using this bot, ensure the following:
- Python 3.7 or later
- Python packages:
pygit2,python-dotenv, andlogging - A GitHub account with a repository to track
- A personal access token for GitHub (with
repopermissions) - PythonAnywhere account (for deployment)
-
Clone this repository or download the code.
-
Install the required Python packages:
pip install pygit2 python-dotenv
-
Create a
.envfile in the root of your project directory. Add your GitHub username and personal access token:GITHUB_USERNAME=your_github_username GITHUB_TOKEN=your_github_token -
Create a
commit_data.jsonfile in the root of your project directory. This file will contain the commit messages and the corresponding changes to be logged.Example
commit_data.json:{ "commits": [ { "message": "Added a new timestamp entry to the streak tracker", "change": "Appended the current timestamp to the streak_tracker.txt file to ensure continuous tracking of commit history." }, { "message": "Refined streak tracker with improved formatting", "change": "Updated the streak_tracker.txt file with consistent formatting to enhance readability and structure." } ] } -
The
streak_tracker.txtfile will automatically be created and updated with each commit.
-
Run the Python script to commit changes:
python main.py
-
The bot will:
- Read the
commit_data.jsonfile for commit messages and changes. - Append the change (with timestamp) to
streak_tracker.txt. - Commit the change to the GitHub repository.
- Push the changes to the remote repository.
- Log the action in
github_bot.log.
- Read the
-
You can check the log file (
github_bot.log) for details about each operation.
You can deploy this bot to PythonAnywhere to run it periodically.
-
Create a PythonAnywhere Account: If you don’t have one already, sign up.
-
Upload the Code:
- After logging in, go to the Files tab.
- Upload your Python script (
main.py),.env, andcommit_data.jsonfiles to your PythonAnywhere file system.
-
Set Up a Virtual Environment:
- Open a Bash console in PythonAnywhere and create a virtual environment:
python3 -m venv github_bot_env source github_bot_env/bin/activate - Install the required packages:
pip install pygit2 python-dotenv
- Open a Bash console in PythonAnywhere and create a virtual environment:
-
Schedule the Bot to Run Automatically:
- Go to the Tasks tab on PythonAnywhere.
- Add a new scheduled task to run your script at your desired interval (e.g., every hour). For example, you can use the following command:
python /home/yourusername/path_to_your_script/main.py
-
Environment Variables:
- You can add your
.envfile using the Files tab, or you can set environment variables directly in PythonAnywhere's Bash console:export GITHUB_USERNAME="your_github_username" export GITHUB_TOKEN="your_github_token"
- You can add your
-
Logs: PythonAnywhere provides logs for scheduled tasks, so you can monitor the output of your script by checking the logs in the Tasks tab.
- Author: SudhuCodes
- Project: GitHub Streak Tracker Bot
- License: MIT License
- Error reading or writing to JSON file: Ensure that the
commit_data.jsonfile is correctly formatted and accessible. - Push fails: Make sure your GitHub token has the appropriate
repopermissions for both public and private repositories. - Permission errors: Ensure that the
.envfile is correctly set up with valid GitHub credentials. - Missing dependencies: If the script complains about missing libraries, make sure you have installed the required Python packages (
pygit2,python-dotenv).
This project is licensed under the MIT License. See the LICENSE file for more details.