feat: add script to download Gitea issues and create pull requests
This commit is contained in:
parent
d9b4af54bd
commit
35d0ff0b63
90
New file: gitea_issues_pr.py
Normal file
90
New file: gitea_issues_pr.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
#!/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()
|
Loading…
Reference in New Issue
Block a user