1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-12-04 09:31:11 +01:00

Extend gitea_api.PullRequest with methods that work with 'PR:' references

This commit is contained in:
2025-10-17 09:55:38 +02:00
parent 66e98819b2
commit ad96f3926d
2 changed files with 111 additions and 0 deletions

View File

@@ -87,6 +87,46 @@ class PullRequest(GiteaModel):
return result
@classmethod
def add_pr_references(cls, text: str, pr_id_list: List[Tuple[str, str, int]]) -> str:
pos = 0
add_newline = False
for match in re.finditer(r"^PR: *(.*)$", text, re.M):
pos = match.end()
if pos < len(text) and text[pos] == "\n":
pos += 1
else:
add_newline = True
references_str = ""
if add_newline:
references_str += "\n"
for owner, repo, number in pr_id_list:
references_str += f"PR: {owner}/{repo}!{number}\n"
text = text[:pos] + references_str + text[pos:]
return text
@classmethod
def remove_pr_references(cls, text: str, pr_id_list: List[Tuple[str, str, int]]) -> str:
# HACK: reverse matches so we can modify the text from end without breaking any indexes
for match in reversed(list(re.finditer(r"^PR: *(.*)$", text, re.M))):
try:
pr_id = PullRequest.split_id(match.group(1))
if pr_id not in pr_id_list:
continue
except ValueError:
continue
start_pos = match.start()
end_pos = match.end()
if end_pos < len(text) and text[end_pos] == "\n":
end_pos += 1
text = text[:start_pos] + text[end_pos:]
return text
@property
def is_pull_request(self):
# determine if we're working with a proper pull request or an issue without pull request details