mirror of
https://github.com/openSUSE/osc.git
synced 2025-12-05 09:54:47 +01:00
Extend gitea_api.PullRequest with methods that work with 'PR:' references
This commit is contained in:
@@ -87,6 +87,46 @@ class PullRequest(GiteaModel):
|
|||||||
|
|
||||||
return result
|
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
|
@property
|
||||||
def is_pull_request(self):
|
def is_pull_request(self):
|
||||||
# determine if we're working with a proper pull request or an issue without pull request details
|
# determine if we're working with a proper pull request or an issue without pull request details
|
||||||
|
|||||||
@@ -164,5 +164,76 @@ class TestGiteaApiPullRequestUrlParsing(unittest.TestCase):
|
|||||||
PullRequest.get_host_owner_repo_number(url)
|
PullRequest.get_host_owner_repo_number(url)
|
||||||
|
|
||||||
|
|
||||||
|
class TestGiteaApiPullRequestReferences(unittest.TestCase):
|
||||||
|
PR_BODY = """
|
||||||
|
PR: foo/bar!1
|
||||||
|
PR: foo/bar#2
|
||||||
|
text
|
||||||
|
PR: bar/baz#3
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
text
|
||||||
|
""".strip()
|
||||||
|
|
||||||
|
def test_parse_pr_references(self):
|
||||||
|
pr_obj = PullRequest({"body": self.PR_BODY})
|
||||||
|
actual = pr_obj.parse_pr_references()
|
||||||
|
expected = [
|
||||||
|
('foo', 'bar', 1),
|
||||||
|
('foo', 'bar', 2),
|
||||||
|
('bar', 'baz', 3),
|
||||||
|
]
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_add_pr_references(self):
|
||||||
|
actual = PullRequest.add_pr_references(self.PR_BODY, [('xxx', 'xxx', 4), ('yyy', 'yyy', 5)])
|
||||||
|
expected = """
|
||||||
|
PR: foo/bar!1
|
||||||
|
PR: foo/bar#2
|
||||||
|
text
|
||||||
|
PR: bar/baz#3
|
||||||
|
PR: xxx/xxx!4
|
||||||
|
PR: yyy/yyy!5
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
text
|
||||||
|
""".strip()
|
||||||
|
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_add_pr_references_end(self):
|
||||||
|
actual = PullRequest.add_pr_references(self.PR_BODY + "\nPR: a/b#123", [('xxx', 'xxx', 4), ('yyy', 'yyy', 5)])
|
||||||
|
expected = """
|
||||||
|
PR: foo/bar!1
|
||||||
|
PR: foo/bar#2
|
||||||
|
text
|
||||||
|
PR: bar/baz#3
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
text
|
||||||
|
PR: a/b#123
|
||||||
|
PR: xxx/xxx!4
|
||||||
|
PR: yyy/yyy!5
|
||||||
|
""".lstrip()
|
||||||
|
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_remove_pr_references(self):
|
||||||
|
actual = PullRequest.remove_pr_references(self.PR_BODY, [("foo", "bar", 2)])
|
||||||
|
expected = """
|
||||||
|
PR: foo/bar!1
|
||||||
|
text
|
||||||
|
PR: bar/baz#3
|
||||||
|
text
|
||||||
|
|
||||||
|
text
|
||||||
|
text
|
||||||
|
""".strip()
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user