Files
pytorch/.github/scripts/test_tryrebase.py
Catherine Lee 0bd8e237b5 rebase via comment
Fixes #ISSUE_NUMBER

start rebase bot

random testing on my fork, using `on: issue_comment` https://github.com/clee2000/pytorch/pull/6

stealing both code and an identity from mergebot

corresponding pr on test-infra https://github.com/pytorch/test-infra/pull/305

doesnt do ghstack

should accept "@pytorchbot rebase me", "@pytorchbot rebase this", "@pytorchmergebot rebase this"
Pull Request resolved: https://github.com/pytorch/pytorch/pull/76262
Approved by: https://github.com/janeyx99, https://github.com/seemethere
2022-04-27 00:55:05 +00:00

43 lines
2.1 KiB
Python

from unittest import TestCase, mock, main
from test_trymerge import mocked_gh_graphql
from trymerge import GitHubPR
from gitutils import get_git_remote_name, get_git_repo_dir, GitRepo
from typing import Any
from tryrebase import rebase_onto
class TestRebase(TestCase):
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@mock.patch('gitutils.GitRepo._run_git')
@mock.patch('tryrebase.gh_post_comment')
def test_rebase(self, mocked_post_comment: Any, mocked_run_git: Any, mocked_gql: Any) -> None:
"Tests rebase successfully"
pr = GitHubPR("pytorch", "pytorch", 31093)
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
rebase_onto(pr, repo)
calls = [mock.call('fetch', 'origin', 'pull/31093/head:pull/31093/head'),
mock.call('rebase', 'master', 'pull/31093/head'),
mock.call('push', '-f', 'https://github.com/mingxiaoh/pytorch.git', 'pull/31093/head:master')]
mocked_run_git.assert_has_calls(calls)
self.assertTrue("Successfully rebased `master` onto `master`" in mocked_post_comment.call_args[0][3])
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@mock.patch('gitutils.GitRepo._run_git', return_value="Everything up-to-date")
@mock.patch('tryrebase.gh_post_comment')
def test_no_need_to_rebase(self, mocked_post_comment: Any, mocked_run_git: Any, mocked_gql: Any) -> None:
"Tests branch already up to date"
pr = GitHubPR("pytorch", "pytorch", 31093)
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
rebase_onto(pr, repo)
calls = [mock.call('fetch', 'origin', 'pull/31093/head:pull/31093/head'),
mock.call('rebase', 'master', 'pull/31093/head'),
mock.call('push', '-f', 'https://github.com/mingxiaoh/pytorch.git', 'pull/31093/head:master')]
mocked_run_git.assert_has_calls(calls)
self.assertTrue(
"Tried to rebase and push PR #31093, but it was already up to date" in mocked_post_comment.call_args[0][3])
if __name__ == "__main__":
main()