Working on GiteaClient
This commit is contained in:
parent
35d0ff0b63
commit
ba82eaa1f5
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -18,3 +18,4 @@ __pycache__/
|
|||
/.coverage
|
||||
/.hypothesis/
|
||||
/htmlcov/
|
||||
.aider*
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
This script downloads issues from a given Gitea repository and produces a pull request for each issue.
|
||||
It assumes that the default branch (default "main") exists and that you have a valid API token if authentication is required.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import requests
|
||||
import sys
|
||||
|
||||
def get_default_branch_sha(gitea_url, owner, repo, branch, token):
|
||||
"""Retrieve the commit SHA of the default branch."""
|
||||
headers = {"Authorization": f"token {token}"} if token else {}
|
||||
url = f"{gitea_url}/repos/{owner}/{repo}/branches/{branch}"
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data['commit']['sha']
|
||||
|
||||
def create_branch(gitea_url, owner, repo, new_branch, sha, token):
|
||||
"""Create a new branch from the provided SHA."""
|
||||
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"} if token else {"Content-Type": "application/json"}
|
||||
url = f"{gitea_url}/repos/{owner}/{repo}/git/refs"
|
||||
json_data = {"ref": f"refs/heads/{new_branch}", "sha": sha}
|
||||
response = requests.post(url, headers=headers, json=json_data)
|
||||
if response.status_code == 422:
|
||||
print(f"Branch {new_branch} already exists.")
|
||||
return False
|
||||
response.raise_for_status()
|
||||
return True
|
||||
|
||||
def get_issues(gitea_url, owner, repo, token):
|
||||
"""Download issues from the specified repository."""
|
||||
headers = {"Authorization": f"token {token}"} if token else {}
|
||||
url = f"{gitea_url}/repos/{owner}/{repo}/issues"
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def create_pull_request(gitea_url, owner, repo, title, head, base, body, token):
|
||||
"""Create a pull request for the given branch."""
|
||||
headers = {"Authorization": f"token {token}", "Content-Type": "application/json"} if token else {"Content-Type": "application/json"}
|
||||
url = f"{gitea_url}/repos/{owner}/{repo}/pulls"
|
||||
json_data = {"title": title, "head": head, "base": base, "body": body}
|
||||
response = requests.post(url, headers=headers, json=json_data)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Download issues and create pull requests for a Gitea repository.")
|
||||
parser.add_argument("--gitea-url", required=True, help="Base URL for the Gitea instance, e.g., https://gitfub.space/api/v1")
|
||||
parser.add_argument("--owner", required=True, help="Owner of the repository")
|
||||
parser.add_argument("--repo", required=True, help="Repository name")
|
||||
parser.add_argument("--base-branch", default="main", help="Base branch to use for new branches (default: main)")
|
||||
parser.add_argument("--token", default="", help="Authentication token if required")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
issues = get_issues(args.gitea_url, args.owner, args.repo, args.token)
|
||||
except Exception as e:
|
||||
sys.exit(f"Failed to retrieve issues: {e}")
|
||||
|
||||
if not issues:
|
||||
print("No issues found.")
|
||||
return
|
||||
|
||||
try:
|
||||
base_sha = get_default_branch_sha(args.gitea_url, args.owner, args.repo, args.base_branch, args.token)
|
||||
except Exception as e:
|
||||
sys.exit(f"Failed to retrieve base branch SHA: {e}")
|
||||
|
||||
for issue in issues:
|
||||
issue_number = issue.get("number")
|
||||
title = issue.get("title", f"Issue {issue_number}")
|
||||
branch_name = f"issue-{issue_number}"
|
||||
try:
|
||||
created = create_branch(args.gitea_url, args.owner, args.repo, branch_name, base_sha, args.token)
|
||||
if created:
|
||||
print(f"Created branch {branch_name} for issue {issue_number}.")
|
||||
except Exception as e:
|
||||
print(f"Error creating branch {branch_name}: {e}")
|
||||
body = f"Automatically generated pull request for issue: {issue.get('html_url', 'unknown')}"
|
||||
try:
|
||||
pr = create_pull_request(args.gitea_url, args.owner, args.repo, f"[Issue {issue_number}] {title}", branch_name, args.base_branch, body, args.token)
|
||||
print(f"Created pull request: {pr.get('html_url', 'unknown')} for issue {issue_number}.")
|
||||
except Exception as e:
|
||||
print(f"Error creating pull request for branch {branch_name}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1
aider_gitea/__init__.py
Normal file
1
aider_gitea/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# TODO
|
|
@ -1,3 +1,110 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
This script downloads issues from a given Gitea repository and produces a pull request for each issue.
|
||||
It assumes that the default branch (default "main") exists and that you have a valid API token if authentication is required.
|
||||
"""
|
||||
|
||||
# TODO
|
||||
import logging
|
||||
import argparse
|
||||
import requests
|
||||
import sys
|
||||
import dataclasses
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GiteaClient:
|
||||
|
||||
def __init__(self, gitea_url: str, token: str) -> None:
|
||||
self.gitea_url = gitea_url
|
||||
self.session = requests.Session()
|
||||
self.session.headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
self.session.headers["Authorization"] = f"token {token}"
|
||||
|
||||
def get_default_branch_sha(self, owner, repo, branch):
|
||||
"""Retrieve the commit SHA of the default branch."""
|
||||
url = f"{self.gitea_url}/repos/{owner}/{repo}/branches/{branch}"
|
||||
response = self.session.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
print(data)
|
||||
return data['commit']['sha']
|
||||
|
||||
def create_branch(self, owner, repo, new_branch, sha):
|
||||
"""Create a new branch from the provided SHA."""
|
||||
url = f"{self.gitea_url}/repos/{owner}/{repo}/git/refs"
|
||||
json_data = {"ref": f"refs/heads/{new_branch}", "sha": sha}
|
||||
response = self.session.post(url, json=json_data)
|
||||
if response.status_code == 422:
|
||||
print(f"Branch {new_branch} already exists.")
|
||||
return False
|
||||
response.raise_for_status()
|
||||
return True
|
||||
|
||||
def get_issues(self, owner, repo):
|
||||
"""Download issues from the specified repository."""
|
||||
url = f"{self.gitea_url}/repos/{owner}/{repo}/issues"
|
||||
response = self.session.get(url)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def create_pull_request(self, owner, repo, title, head, base, body):
|
||||
"""Create a pull request for the given branch."""
|
||||
url = f"{self.gitea_url}/repos/{owner}/{repo}/pulls"
|
||||
json_data = {"title": title, "head": head, "base": base, "body": body}
|
||||
response = self.session.post(url, json=json_data)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description="Download issues and create pull requests for a Gitea repository.")
|
||||
parser.add_argument("--gitea-url", required=True, help="Base URL for the Gitea instance, e.g., https://gitfub.space/api/v1")
|
||||
parser.add_argument("--owner", required=True, help="Owner of the repository")
|
||||
parser.add_argument("--repo", required=True, help="Repository name")
|
||||
parser.add_argument("--base-branch", default="main", help="Base branch to use for new branches (default: main)")
|
||||
parser.add_argument("--token", default="", help="Authentication token if required")
|
||||
return parser.parse_args()
|
||||
|
||||
def main():
|
||||
logging.basicConfig()
|
||||
args = parse_args()
|
||||
|
||||
client = GiteaClient(args.gitea_url, args.token)
|
||||
|
||||
|
||||
try:
|
||||
issues = client.get_issues(args.owner, args.repo)
|
||||
except Exception as e:
|
||||
logger.exception('Failed to retrieve issues')
|
||||
sys.exit(1)
|
||||
|
||||
if not issues:
|
||||
print("No issues found.")
|
||||
return
|
||||
|
||||
try:
|
||||
base_sha = client.get_default_branch_sha(args.owner, args.repo, args.base_branch)
|
||||
except Exception as e:
|
||||
logger.exception('Failed to retrieve base branch SHA')
|
||||
sys.exit(1)
|
||||
|
||||
for issue in issues:
|
||||
issue_number = issue.get("number")
|
||||
title = issue.get("title", f"Issue {issue_number}")
|
||||
branch_name = f"issue-{issue_number}"
|
||||
try:
|
||||
created = client.create_branch(args.owner, args.repo, branch_name, base_sha)
|
||||
if created:
|
||||
print(f"Created branch {branch_name} for issue {issue_number}.")
|
||||
except Exception as e:
|
||||
print(f"Error creating branch {branch_name}: {e}")
|
||||
body = f"Automatically generated pull request for issue: {issue.get('html_url', 'unknown')}"
|
||||
try:
|
||||
pr = client.create_pull_request(args.owner, args.repo, f"[Issue {issue_number}] {title}", branch_name, args.base_branch, body)
|
||||
print(f"Created pull request: {pr.get('html_url', 'unknown')} for issue {issue_number}.")
|
||||
except Exception as e:
|
||||
print(f"Error creating pull request for branch {branch_name}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user