Fix trymerge changed files count (#95720)

The value from the PR info includes only unique files != The number of files changed (both are technically correct, depending on how you view it)

I'm trying to merge this PR https://github.com/pytorch/pytorch/pull/95233 which makes `.github/ci_commit_pins/triton.txt` a softlink.  So the PR includes 2 changes to that file 1) to delete the file and 2) to add it as a symlink.

```
[
  ".ci/docker/build.sh",
  ".ci/docker/ci_commit_pins/triton.txt",
  ".ci/docker/common/common_utils.sh",
  ".ci/docker/common/install_triton.sh",
  ".ci/docker/requirements-ci.txt",
  ".ci/docker/ubuntu-cuda/Dockerfile",
  ".ci/docker/ubuntu/Dockerfile",
  ".github/ci_commit_pins/triton.txt", <--
  ".github/ci_commit_pins/triton.txt", <--
  ".github/workflows/build-triton-wheel.yml"
]
```

Trymerge doesn't like that and rejects the merge due to `Changed file count mismatch` https://github.com/pytorch/pytorch/actions/runs/4295438799/jobs/7485853815 . This is because the PRInfo GraphQL result from GitHub only counts 9 of them https://paste.sh/zVsOnWoT#p_3RKX_VMjj-e71vwsTeA01W (search for `changedFiles`).  It means that the name are dedup, so that only unique file names are counted.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/95720
Approved by: https://github.com/kit1980, https://github.com/malfet, https://github.com/ZainRizvi
This commit is contained in:
Huy Do
2023-02-28 21:55:21 +00:00
committed by PyTorch MergeBot
parent 2cc845eb1a
commit b55d0d2aef
3 changed files with 1258 additions and 2 deletions

1242
.github/scripts/gql_mocks.json generated vendored

File diff suppressed because it is too large Load Diff

View File

@ -420,6 +420,19 @@ class TestGitHubPR(TestCase):
repo = DummyGitRepo()
self.assertIsNotNone(validate_revert(repo, pr, comment_id=1189459845))
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
def test_get_changed_files(self, mock_gql: Any, *args: Any) -> None:
"""
Tests that the list changed files in a PR doesn't include duplicates
"""
pr = GitHubPR("pytorch", "pytorch", 95233)
try:
changed_files = pr.get_changed_files()
except RuntimeError as error:
self.fail(f"get_changed_files throws an exception: {error}")
self.assertEqual(len(changed_files), pr.get_changed_files_count())
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
def test_revert_codev_fails(self, mock_gql: Any, *args: Any) -> None:
pr = GitHubPR("pytorch", "pytorch", 91340)

View File

@ -774,10 +774,10 @@ class GitHubPR:
def get_changed_files(self) -> List[str]:
if self.changed_files is None:
info = self.info
self.changed_files = []
unique_changed_files = set()
# Do not try to fetch more than 10K files
for _ in range(100):
self.changed_files += [x["path"] for x in info["files"]["nodes"]]
unique_changed_files.update([x["path"] for x in info["files"]["nodes"]])
if not info["files"]["pageInfo"]["hasNextPage"]:
break
rc = gh_graphql(GH_GET_PR_NEXT_FILES_QUERY,
@ -786,6 +786,7 @@ class GitHubPR:
number=self.pr_num,
cursor=info["files"]["pageInfo"]["endCursor"])
info = rc["data"]["repository"]["pullRequest"]
self.changed_files = list(unique_changed_files)
if len(self.changed_files) != self.get_changed_files_count():
raise RuntimeError("Changed file count mismatch")