def test_get_last_comment(self, mocked_gql: Any) -> None: "Tests that last comment can be fetched" pr = GitHubPR("pytorch", "pytorch", 71759) comment = pr.get_last_comment() self.assertEqual(comment.author_login, "github-actions") self.assertIsNone(comment.editor_login) self.assertTrue("You've committed this PR" in comment.body_text)
def main() -> None: args = parse_args() repo = GitRepo(get_git_repo_dir(), get_git_remote_name(), debug=True) org, project = repo.gh_owner_and_name() pr = GitHubPR(org, project, args.pr_num) if pr.is_closed(): gh_post_comment(org, project, args.pr_num, f"PR #{args.pr_num} is closed, won't rebase", dry_run=args.dry_run) return try: if pr.is_ghstack_pr(): rebase_ghstack_onto(pr, repo, dry_run=args.dry_run, stable=args.stable) return rebase_onto(pr, repo, dry_run=args.dry_run, stable=args.stable) except Exception as e: msg = f"Rebase failed due to {e}" run_url = os.getenv("GH_RUN_URL") if run_url is not None: msg += f"\nRaised by {run_url}" gh_post_comment(org, project, args.pr_num, msg, dry_run=args.dry_run)
def main() -> None: args = parse_args() repo = GitRepo(get_git_repo_dir(), get_git_remote_name(), debug=True) org, project = repo.gh_owner_and_name() pr = GitHubPR(org, project, args.pr_num) onto_branch = args.branch if args.branch else pr.default_branch() msg = "@pytorchbot successfully started a rebase job." msg += f" Check the current status [here]({os.getenv('GH_RUN_URL')})" gh_post_comment(org, project, args.pr_num, msg, dry_run=args.dry_run) if pr.is_closed(): gh_post_comment(org, project, args.pr_num, f"PR #{args.pr_num} is closed, won't rebase", dry_run=args.dry_run) return try: if pr.is_ghstack_pr(): rebase_ghstack_onto(pr, repo, onto_branch, dry_run=args.dry_run) return rebase_onto(pr, repo, onto_branch, dry_run=args.dry_run) except Exception as e: msg = f"Rebase failed due to {e}" run_url = os.getenv("GH_RUN_URL") if run_url is not None: msg += f"\nRaised by {run_url}" gh_post_comment(org, project, args.pr_num, msg, dry_run=args.dry_run)
def test_get_checkruns_many_runs(self, mocked_gql: Any) -> None: """ Tests that all checkruns can be fetched """ pr = GitHubPR("pytorch", "pytorch", 77700) conclusions = pr.get_checkrun_conclusions() self.assertEqual(len(conclusions), 83) self.assertTrue("pull / linux-docs / build-docs (cpp)" in conclusions.keys())
def test_cancelled_gets_ignored(self, mocked_gql: Any) -> None: """ Tests that cancelled workflow does not override existing successfull status """ pr = GitHubPR("pytorch", "pytorch", 82169) conclusions = pr.get_checkrun_conclusions() self.assertTrue("Lint" in conclusions.keys()) self.assertEqual(conclusions["Lint"][0], "SUCCESS")
def test_gql_complexity(self, mocked_gql: Any) -> None: "Fetch comments and conclusions for PR with 60 commits" # Previous version of GrapQL query used to cause HTTP/502 error # see https://gist.github.com/malfet/9b93bc7eeddeaf1d84546efc4f0c577f pr = GitHubPR("pytorch", "pytorch", 68111) self.assertGreater(len(pr.get_comments()), 20) self.assertGreater(len(pr.get_checkrun_conclusions()), 3) self.assertGreater(pr.get_commit_count(), 60)
def test_get_author_many_commits(self, mocked_gql: Any) -> None: """ Tests that authors for all commits can be fetched """ pr = GitHubPR("pytorch", "pytorch", 76118) authors = pr.get_authors() self.assertGreater(pr.get_commit_count(), 100) self.assertGreater(len(authors), 50) self.assertTrue("@" in pr.get_author())
def test_get_author_null(self, mocked_gql: Any) -> None: """ Tests that PR author can be computed If reply contains NULL """ pr = GitHubPR("pytorch", "pytorch", 71759) author = pr.get_author() self.assertTrue(author is not None) self.assertTrue("@" in author)
def test_get_author_many_reviews(self, mocked_gql: Any) -> None: """ Tests that all reviews can be fetched """ pr = GitHubPR("pytorch", "pytorch", 76123) approved_by = pr.get_approved_by() self.assertGreater(len(approved_by), 0) assert pr._reviews is not None # to pacify mypy self.assertGreater(len(pr._reviews), 100)
def rebase_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False, stable: bool = False) -> None: branch = f"pull/{pr.pr_num}/head" onto_branch = "refs/remotes/origin/viable/strict" if stable else pr.default_branch( ) remote_url = f"https://github.com/{pr.info['headRepository']['nameWithOwner']}.git" refspec = f"{branch}:{pr.head_ref()}" repo.fetch(branch, branch) repo._run_git("rebase", onto_branch, branch) if dry_run: push_result = repo._run_git("push", "--dry-run", "-f", remote_url, refspec) else: push_result = repo._run_git("push", "-f", remote_url, refspec) if "Everything up-to-date" in push_result: gh_post_comment( pr.org, pr.project, pr.pr_num, f"Tried to rebase and push PR #{pr.pr_num}, but it was already up to date", dry_run=dry_run) else: gh_post_comment( pr.org, pr.project, pr.pr_num, f"Successfully rebased `{pr.head_ref()}` onto `{onto_branch}`, please pull locally " + f"before adding more changes (for example, via `git checkout {pr.head_ref()} && " + "git pull --rebase`)", dry_run=dry_run)
def test_pending_status_check(self, mocked_gql: Any, mocked_read_merge_rules: Any) -> None: """ Tests that PR with nonexistent/pending status checks fails with the right reason. """ pr = GitHubPR("pytorch", "pytorch", 76118) repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) self.assertRaisesRegex(MandatoryChecksMissingError, ".*are pending/not yet run.*", lambda: find_matching_merge_rule(pr, repo))
def test_get_author_null(self, mocked_gql: Any) -> None: """ Tests that PR author can be computed If reply contains NULL """ pr = GitHubPR("pytorch", "pytorch", 71759) author = pr.get_author() self.assertTrue(author is not None) self.assertTrue("@" in author) self.assertTrue(pr.get_diff_revision() is None) # PR with multiple contributors, but creator id is not among authors pr = GitHubPR("pytorch", "pytorch", 75095) self.assertEqual(pr.get_pr_creator_login(), "mruberry") author = pr.get_author() self.assertTrue(author is not None)
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])
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])
def test_match_rules(self, mocked_gql: Any) -> None: "Tests that PR passes merge rules" pr = GitHubPR("pytorch", "pytorch", 77700) repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) self.assertTrue(find_matching_merge_rule(pr, repo) is not None)
def test_lint_fails(self, mocked_gql: Any, mocked_rmr: Any) -> None: "Tests that PR fails mandatory lint check" pr = GitHubPR("pytorch", "pytorch", 74649) repo = DummyGitRepo() self.assertRaises(RuntimeError, lambda: find_matching_merge_rule(pr, repo))
def test_comments_pagination(self, mocked_gql: Any) -> None: "Tests that PR with 50+ comments can be fetched" pr = GitHubPR("pytorch", "pytorch", 31093) self.assertGreater(len(pr.get_comments()), 50)
def test_checksuites_pagination(self, mocked_gql: Any) -> None: "Tests that PR with lots of checksuits can be fetched" pr = GitHubPR("pytorch", "pytorch", 73811) self.assertGreater(len(pr.get_checkrun_conclusions()), 0)
def test_internal_changes(self, mocked_gql: Any) -> None: "Tests that PR with internal changes is detected" pr = GitHubPR("pytorch", "pytorch", 73969) self.assertTrue(pr.has_internal_changes())
def test_large_diff(self, mocked_gql: Any) -> None: "Tests that PR with 100+ files can be fetched" pr = GitHubPR("pytorch", "pytorch", 73099) self.assertTrue(pr.get_changed_files_count() > 100) flist = pr.get_changed_files() self.assertEqual(len(flist), pr.get_changed_files_count())
def test_match_rules(self, mocked_gql: Any, mocked_rmr: Any) -> None: "Tests that PR passes merge rules" pr = GitHubPR("pytorch", "pytorch", 77700) repo = DummyGitRepo() self.assertTrue(find_matching_merge_rule(pr, repo) is not None)
def test_lint_fails(self, mocked_gql: Any) -> None: "Tests that PR fails mandatory lint check" pr = GitHubPR("pytorch", "pytorch", 74649) repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) self.assertRaises(RuntimeError, lambda: find_matching_merge_rule(pr, repo))
def test_revert_rules(self, mock_gql: Any) -> None: """ Tests that reverts from collaborators are allowed """ pr = GitHubPR("pytorch", "pytorch", 79694) repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) self.assertIsNotNone(validate_revert(repo, pr, comment_id=1189459845))
def rebase_ghstack_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False, stable: bool = False) -> None: if subprocess.run([sys.executable, "-m", "ghstack", "--help"], capture_output=True).returncode != 0: subprocess.run([sys.executable, "-m", "pip", "install", "ghstack"]) orig_ref = f"{re.sub(r'/head$', '/orig', pr.head_ref())}" onto_branch = "refs/remotes/origin/viable/strict" if stable else pr.default_branch( ) repo.fetch(orig_ref, orig_ref) repo._run_git("rebase", onto_branch, orig_ref) if dry_run: print("Don't know how to dry-run ghstack") else: ghstack_result = subprocess.run(["ghstack"], capture_output=True) push_result = ghstack_result.stdout.decode("utf-8") print(push_result) if ghstack_result.returncode != 0: raise Exception(f"\n```{push_result}```") # The contents of a successful push result should look like: # Summary of changes (ghstack 0.6.0) # - Updated https://github.com/clee2000/random-testing/pull/2 # - Updated https://github.com/clee2000/random-testing/pull/1 # Facebook employees can import your changes by running # (on a Facebook machine): # ghimport -s https://github.com/clee2000/random-testing/pull/2 # If you want to work on this diff stack on another machine: # ghstack checkout https://github.com/clee2000/random-testing/pull/2 org, project = repo.gh_owner_and_name() for line in push_result.splitlines(): if "Updated" in line: pr_num = int(line.split("/")[-1]) if pr_num != pr.pr_num: gh_post_comment( pr.org, pr.project, pr_num, f"Rebased `{orig_ref}` onto `{onto_branch}` because #{pr.pr_num} was rebased, " "please pull locally before adding more changes (for example, via `git checkout " f"{orig_ref} && git pull --rebase`)", dry_run=dry_run) else: gh_post_comment( pr.org, pr.project, pr_num, f"Successfully rebased `{orig_ref}` onto `{onto_branch}`, please pull locally " + f"before adding more changes (for example, via `git checkout {orig_ref} && " + "git pull --rebase`)", dry_run=dry_run) if f"Skipped https://github.com/{org}/{project}/pull/{pr.pr_num}" in push_result: gh_post_comment( pr.org, pr.project, pr.pr_num, f"Tried to rebase and push PR #{pr.pr_num}, but it was already up to date", dry_run=dry_run)