diff --git a/.gitignore b/.gitignore index 41180b8..3449270 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ __pycache__/ /.coverage /.hypothesis/ /htmlcov/ +.aider* diff --git a/New file: gitea_issues_pr.py b/New file: gitea_issues_pr.py deleted file mode 100644 index be30f51..0000000 --- a/New file: gitea_issues_pr.py +++ /dev/null @@ -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() diff --git a/aider_gitea/__init__.py b/aider_gitea/__init__.py new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/aider_gitea/__init__.py @@ -0,0 +1 @@ +# TODO diff --git a/aider_gitea/__main__.py b/aider_gitea/__main__.py index 9c49fad..4ed24fe 100644 --- a/aider_gitea/__main__.py +++ b/aider_gitea/__main__.py @@ -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()