173 lines
5.3 KiB
Python
173 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Commit using Gitea API directly (bypassing git.exe issues)
|
|
This uses Gitea's REST API to create commits directly.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import base64
|
|
import requests
|
|
from datetime import datetime
|
|
|
|
# Gitea configuration
|
|
GITEA_URL = "https://git.mtrcompute.com"
|
|
USERNAME = "jleu3482"
|
|
PASSWORD = "jleu1212"
|
|
REPO_OWNER = "jleu3482"
|
|
REPO_NAME = "railseek6"
|
|
|
|
def get_auth_token():
|
|
"""Get authentication token from Gitea"""
|
|
auth_url = f"{GITEA_URL}/api/v1/users/{USERNAME}/tokens"
|
|
auth_data = {
|
|
"name": f"auto-commit-{datetime.now().strftime('%Y%m%d-%H%M%S')}",
|
|
"scopes": ["repo", "write:repo"]
|
|
}
|
|
|
|
try:
|
|
response = requests.post(
|
|
auth_url,
|
|
json=auth_data,
|
|
auth=(USERNAME, PASSWORD)
|
|
)
|
|
|
|
if response.status_code == 201:
|
|
token = response.json()["sha1"]
|
|
print(f"✓ Authentication token created")
|
|
return token
|
|
else:
|
|
print(f"❌ Failed to create token: {response.status_code} - {response.text}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"❌ Error creating token: {e}")
|
|
return None
|
|
|
|
def get_file_content(filepath):
|
|
"""Read file content and encode as base64"""
|
|
try:
|
|
with open(filepath, 'rb') as f:
|
|
content = f.read()
|
|
return base64.b64encode(content).decode('utf-8')
|
|
except Exception as e:
|
|
print(f"❌ Error reading file {filepath}: {e}")
|
|
return None
|
|
|
|
def create_commit_via_api(token, description):
|
|
"""Create a commit using Gitea API"""
|
|
# First, get the current tree
|
|
repo_url = f"{GITEA_URL}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}"
|
|
|
|
# Get list of files to add
|
|
files_to_add = []
|
|
for root, dirs, files in os.walk("."):
|
|
# Skip .git directory and other hidden directories
|
|
if '.git' in root:
|
|
continue
|
|
|
|
for file in files:
|
|
filepath = os.path.join(root, file)
|
|
rel_path = os.path.relpath(filepath, ".")
|
|
|
|
# Skip large files and binary files
|
|
if os.path.getsize(filepath) > 10 * 1024 * 1024: # 10MB limit
|
|
print(f"⚠ Skipping large file: {rel_path}")
|
|
continue
|
|
|
|
# Check if it's a text file
|
|
try:
|
|
with open(filepath, 'rb') as f:
|
|
content = f.read(1024)
|
|
# Simple binary detection
|
|
if b'\x00' in content:
|
|
print(f"⚠ Skipping binary file: {rel_path}")
|
|
continue
|
|
except:
|
|
continue
|
|
|
|
content_b64 = get_file_content(filepath)
|
|
if content_b64:
|
|
files_to_add.append({
|
|
"path": rel_path.replace("\\", "/"),
|
|
"content": content_b64
|
|
})
|
|
|
|
print(f"Found {len(files_to_add)} files to commit")
|
|
|
|
# Create commit via API
|
|
commit_url = f"{repo_url}/contents"
|
|
headers = {
|
|
"Authorization": f"token {token}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
commit_message = f"{description}\n\nAuto-commit at: {timestamp}"
|
|
|
|
successful_commits = 0
|
|
|
|
for file_info in files_to_add[:10]: # Limit to 10 files for testing
|
|
file_data = {
|
|
"message": commit_message,
|
|
"content": file_info["content"],
|
|
"branch": "main"
|
|
}
|
|
|
|
try:
|
|
response = requests.put(
|
|
f"{commit_url}/{file_info['path']}",
|
|
json=file_data,
|
|
headers=headers
|
|
)
|
|
|
|
if response.status_code in [201, 200]:
|
|
successful_commits += 1
|
|
print(f"✓ Committed: {file_info['path']}")
|
|
else:
|
|
print(f"❌ Failed to commit {file_info['path']}: {response.status_code}")
|
|
except Exception as e:
|
|
print(f"❌ Error committing {file_info['path']}: {e}")
|
|
|
|
return successful_commits
|
|
|
|
def main():
|
|
"""Main function"""
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python gitea_api_commit.py \"Description of changes\"")
|
|
print("Example: python gitea_api_commit.py \"Added document download endpoint\"")
|
|
sys.exit(1)
|
|
|
|
description = sys.argv[1]
|
|
|
|
print("=" * 60)
|
|
print("GITEA API AUTO-COMMIT")
|
|
print("=" * 60)
|
|
print(f"Description: {description}")
|
|
print(f"Repository: {GITEA_URL}/{REPO_OWNER}/{REPO_NAME}")
|
|
print()
|
|
|
|
# Get authentication token
|
|
print("Authenticating with Gitea...")
|
|
token = get_auth_token()
|
|
if not token:
|
|
print("❌ Authentication failed")
|
|
sys.exit(1)
|
|
|
|
# Create commit via API
|
|
print("\nCreating commit via API...")
|
|
successful = create_commit_via_api(token, description)
|
|
|
|
print()
|
|
print("=" * 60)
|
|
print("SUMMARY")
|
|
print("=" * 60)
|
|
print(f"✓ Repository: {GITEA_URL}/{REPO_OWNER}/{REPO_NAME}")
|
|
print(f"✓ Commit: {description}")
|
|
print(f"✓ Files committed: {successful}")
|
|
print(f"✓ Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
print()
|
|
print("View repository at: https://git.mtrcompute.com/jleu3482/railseek6")
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main() |