1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-11-27 23:19:49 +01:00

Move get_editor() and related functions from command-line module to gitea_api.common

This commit is contained in:
2025-07-01 13:25:09 +02:00
parent cd92782d58
commit 666e9ab7c0
5 changed files with 63 additions and 66 deletions

View File

@@ -1,69 +1,9 @@
import os
import re
import subprocess
import sys
from typing import Optional
from typing import List
import osc.commandline_git
def get_editor() -> List[str]:
import shutil
import shlex
editor = os.getenv("EDITOR", None)
if editor:
candidates = [editor]
else:
candidates = ["vim", "vi"]
editor_path = None
args = None
for i in candidates:
i, *args = shlex.split(i)
if i.startswith("/"):
editor_path = i
else:
editor_path = shutil.which(i)
if editor_path:
break
if not editor_path:
raise RuntimeError(f"Unable to start editor '{candidates[0]}'")
res = [editor_path]
if args:
res += args
return res
def get_editor_command(file_path: str) -> List[str]:
res = get_editor()
res.append(file_path)
return res
def run_editor(file_path: str):
subprocess.run(get_editor_command(file_path))
def edit_message(template: Optional[str] = None) -> str:
import tempfile
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", prefix="git_obs_message_") as f:
if template:
f.write(template)
f.flush()
run_editor(f.name)
f.seek(0)
return f.read()
NEW_PULL_REQUEST_TEMPLATE = """
# title
{title}
@@ -197,7 +137,7 @@ class PullRequestCreateCommand(osc.commandline_git.GitObsCommand):
else:
git_status = "#"
message = edit_message(template=NEW_PULL_REQUEST_TEMPLATE.format(**locals()))
message = gitea_api.edit_message(template=NEW_PULL_REQUEST_TEMPLATE.format(**locals()))
# remove comments
message = "\n".join([i for i in message.splitlines() if not i.startswith("#")])