Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #47822 to 22.3: Place short return before big block, improve logging #47841

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 39 additions & 42 deletions tests/ci/merge_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,55 +100,52 @@ def is_approved(self, team: List[NamedUser]) -> bool:
if review.state == "APPROVED"
}

if approved:
if not approved:
logging.info(
"The following users from %s team approved the PR: %s",
"The PR #%s is not approved by any of %s team member",
self.pr.number,
TEAM_NAME,
", ".join(user.login for user in approved.keys()),
)
# The only reliable place to get the 100% accurate last_modified
# info is when the commit was pushed to GitHub. The info is
# available as a header 'last-modified' of /{org}/{repo}/commits/{sha}.
# Unfortunately, it's formatted as 'Wed, 04 Jan 2023 11:05:13 GMT'

commit = self.pr.head.repo.get_commit(self.pr.head.sha)
if commit.stats.last_modified is None:
logging.warning(
"Unable to get info about the commit %s", self.pr.head.sha
)
return False

last_changed = datetime.strptime(
commit.stats.last_modified, "%a, %d %b %Y %H:%M:%S GMT"
)

approved_at = max(review.submitted_at for review in approved.values())
if approved_at == datetime.fromtimestamp(0):
logging.info(
"Unable to get `datetime.fromtimestamp(0)`, "
"here's debug info about reviews: %s",
"\n".join(pformat(review) for review in self.reviews.values()),
)
else:
logging.info(
"The PR is approved at %s",
approved_at.isoformat(),
)

if approved_at < last_changed:
logging.info(
"There are changes after approve at %s",
approved_at.isoformat(),
)
return False
return True
return False

logging.info(
"The PR #%s is not approved by any of %s team member",
self.pr.number,
"The following users from %s team approved the PR: %s",
TEAM_NAME,
", ".join(user.login for user in approved.keys()),
)

# The only reliable place to get the 100% accurate last_modified
# info is when the commit was pushed to GitHub. The info is
# available as a header 'last-modified' of /{org}/{repo}/commits/{sha}.
# Unfortunately, it's formatted as 'Wed, 04 Jan 2023 11:05:13 GMT'
commit = self.pr.head.repo.get_commit(self.pr.head.sha)
if commit.stats.last_modified is None:
logging.warning("Unable to get info about the commit %s", self.pr.head.sha)
return False

last_changed = datetime.strptime(
commit.stats.last_modified, "%a, %d %b %Y %H:%M:%S GMT"
)
return False
logging.info("The PR is changed at %s", last_changed.isoformat())

approved_at = max(review.submitted_at for review in approved.values())
if approved_at == datetime.fromtimestamp(0):
logging.info(
"Unable to get `datetime.fromtimestamp(0)`, "
"here's debug info about reviews: %s",
"\n".join(pformat(review) for review in self.reviews.values()),
)
else:
logging.info("The PR is approved at %s", approved_at.isoformat())

if approved_at < last_changed:
logging.info(
"There are changes done at %s after approval at %s",
last_changed.isoformat(),
approved_at.isoformat(),
)
return False
return True


def get_workflows_for_head(repo: Repository, head_sha: str) -> List[WorkflowRun]:
Expand Down