n39librarian/createMR.py
2024-09-13 18:53:35 +02:00

55 lines
1.5 KiB
Python

import os
import json
import argparse
from git import Repo
from pyforgejo import AuthenticatedClient
from dotenv import load_dotenv
from pyforgejo.api.issue import issue_list_issues
from pyforgejo.api.repository import repo_create_branch, repo_create_pull_request
from pyforgejo.models import CreateBranchRepoOption, CreatePullRequestOption
load_dotenv()
parser = argparse.ArgumentParser(description="")
parser.add_argument("-i", "--issue")
args = parser.parse_args()
FORJEGO_ACCESS_TOKEN = os.getenv("FORJEGO_ACCESS_TOKEN")
OWNER = os.getenv("OWNER")
USER = os.getenv("USER")
REPO = os.getenv("REPO")
client = AuthenticatedClient(
base_url="https://git.n39.eu/api/v1/", token=FORJEGO_ACCESS_TOKEN
)
issues = json.loads(issue_list_issues.sync_detailed(OWNER, REPO, client=client).content)
def filterIssues(issue):
return args.issue in issue["title"]
issues = list(filter(filterIssues, issues))
branchName = issues[0]["title"].replace(" ", "-")
if len(issues) > 1:
raise ValueError("found more than one ticket")
if len(issues) == 0:
raise ValueError("found more than one ticket")
else:
repo_create_branch.sync_detailed(
OWNER, REPO, body=CreateBranchRepoOption(branchName, "main"), client=client
)
res = json.loads(
repo_create_pull_request.sync_detailed(
OWNER,
REPO,
body=CreatePullRequestOption(
base=branchName, head="main", title="merge" + branchName
),
client=client,
).content
)
print(res)