[BE][GHF] Add retries_decorator (#101227)

I've noticed that 3-4 functions in trymerge are trying to implement similar tail recursion for flaky network retries.

Unify them using single wrapper in `gitutils.py`

<!--
copilot:poem
-->
### <samp>🤖 Generated by Copilot at 8d40631</samp>

> _`retries_decorator`_
> _adds resilience to GitHub scripts_
> _autumn of errors_
Pull Request resolved: https://github.com/pytorch/pytorch/pull/101227
Approved by: https://github.com/kit1980
This commit is contained in:
Nikita Shulga
2023-05-12 20:29:06 +00:00
committed by PyTorch MergeBot
parent 2fcc2002fa
commit 568bac7961
3 changed files with 59 additions and 45 deletions

View File

@ -8,6 +8,7 @@ from gitutils import (
GitRepo,
patterns_to_regex,
PeekableIterator,
retries_decorator,
)
@ -49,6 +50,22 @@ class TestPattern(TestCase):
self.assertTrue(patterns_re.match(filename))
class TestRetriesDecorator(TestCase):
def test_simple(self) -> None:
@retries_decorator()
def foo(x: int, y: int) -> int:
return x + y
self.assertEqual(foo(3, 4), 7)
def test_fails(self) -> None:
@retries_decorator(rc=0)
def foo(x: int, y: int) -> int:
return x + y
self.assertEqual(foo("a", 4), 0)
class TestGitRepo(TestCase):
def setUp(self) -> None:
repo_dir = BASE_DIR.parent.parent.absolute()