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

feat: pin down commits to apps #482

Merged
merged 2 commits into from
Jun 3, 2024
Merged

Conversation

giovanni-guidini
Copy link
Contributor

extends NotificationResult to record the app that was used. After notifications go through we save them to CommitNotification, including the app. This is the "permanent pindown"
Previously this would only happen for pull requests, but not it also saves a CommitNotification everytime we have a record of an app_id being used (for a successful notification)

The Notify task after selecting an app will possibly pindown the commit to that selected app, or refresh the permanent pindown in Redis (faster to access). Notice then, that if a notifier is to use a specific app this will certainly be in redis. Put there by the NotifyTask.

I've found some errors in the redis get/set though (through manual testing). It turns out that the return value is a binary string, not a "regular" string. Hence the changes to the "get" method.

Only notifiers that notify on a specific commit (status and checks) check for a specific app. The comment notifier might use any app. But that's OK because an app can edit the comment of another app.

👀 This is PR 4/4 for pinning commits to apps

extends NotificationResult to record the app that was used.
After notifications go through we save them to `CommitNotification`, including the app.
This is the "permanent pindown"
Previously this would only happen for pull requests, but not it _also_ saves a `CommitNotification` everytime
we have a record of an app_id being used (for a successful notification)

The `Notify` task after selecting an app will possibly pindown the
commit to that selected app, or refresh the permanent pindown in Redis (faster to access).
Notice then, that if a notifier is to use a specific app this will certainly
be in redis. Put there by the `NotifyTask`.

I've found some errors in the redis get/set though (through manual testing).
It turns out that the return value is a binary string, not a "regular" string.
Hence the changes to the "get" method.

Only notifiers that notify on a specific commit (status and checks) check for a specific app.
The comment notifier might use any app. But that's OK because an app _can_ edit
the comment of another app.
@giovanni-guidini giovanni-guidini requested a review from a team June 3, 2024 12:30
@codecov-notifications
Copy link

codecov-notifications bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 91.57303% with 15 lines in your changes are missing coverage. Please review.

✅ All tests successful. No failed tests found.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #482      +/-   ##
==========================================
- Coverage   97.26%   97.24%   -0.03%     
==========================================
  Files         412      412              
  Lines       34150    34271     +121     
==========================================
+ Hits        33217    33326     +109     
- Misses        933      945      +12     
Flag Coverage Δ
integration 97.24% <91.57%> (-0.03%) ⬇️
latest-uploader-overall 97.24% <91.57%> (-0.03%) ⬇️
unit 97.24% <91.57%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 94.38% <94.02%> (+<0.01%) ⬆️
OutsideTasks 97.54% <100.00%> (+0.01%) ⬆️
Files Coverage Δ
conftest.py 95.00% <ø> (ø)
services/github.py 94.87% <100.00%> (+0.13%) ⬆️
services/notification/__init__.py 100.00% <100.00%> (+2.11%) ⬆️
services/notification/commit_notifications.py 95.00% <100.00%> (+1.25%) ⬆️
services/notification/notifiers/base.py 92.59% <100.00%> (+0.13%) ⬆️
services/notification/notifiers/checks/base.py 98.12% <100.00%> (-0.08%) ⬇️
services/notification/notifiers/status/base.py 98.70% <100.00%> (+0.07%) ⬆️
...s/notification/notifiers/tests/unit/test_status.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_commit_notifications.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_notification_service.py 100.00% <100.00%> (ø)
... and 5 more

@codecov-qa
Copy link

codecov-qa bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 91.57303% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 97.24%. Comparing base (2ff1c1d) to head (df85504).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #482      +/-   ##
==========================================
- Coverage   97.26%   97.24%   -0.03%     
==========================================
  Files         412      412              
  Lines       34150    34271     +121     
==========================================
+ Hits        33217    33326     +109     
- Misses        933      945      +12     
Flag Coverage Δ
integration 97.24% <91.57%> (-0.03%) ⬇️
latest-uploader-overall 97.24% <91.57%> (-0.03%) ⬇️
unit 97.24% <91.57%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 94.38% <94.02%> (+<0.01%) ⬆️
OutsideTasks 97.54% <100.00%> (+0.01%) ⬆️
Files Coverage Δ
conftest.py 95.00% <ø> (ø)
services/github.py 94.87% <100.00%> (+0.13%) ⬆️
services/notification/__init__.py 100.00% <100.00%> (+2.11%) ⬆️
services/notification/commit_notifications.py 95.00% <100.00%> (+1.25%) ⬆️
services/notification/notifiers/base.py 92.59% <100.00%> (+0.13%) ⬆️
services/notification/notifiers/checks/base.py 98.12% <100.00%> (-0.08%) ⬇️
services/notification/notifiers/status/base.py 98.70% <100.00%> (+0.07%) ⬆️
...s/notification/notifiers/tests/unit/test_status.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_commit_notifications.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_notification_service.py 100.00% <100.00%> (ø)
... and 5 more

Copy link

codecov-public-qa bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 91.57303% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 97.24%. Comparing base (2ff1c1d) to head (df85504).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found ☺️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #482      +/-   ##
==========================================
- Coverage   97.26%   97.24%   -0.03%     
==========================================
  Files         412      412              
  Lines       34150    34271     +121     
==========================================
+ Hits        33217    33326     +109     
- Misses        933      945      +12     
Flag Coverage Δ
integration 97.24% <91.57%> (-0.03%) ⬇️
latest-uploader-overall 97.24% <91.57%> (-0.03%) ⬇️
unit 97.24% <91.57%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 94.38% <94.02%> (+<0.01%) ⬆️
OutsideTasks 97.54% <100.00%> (+0.01%) ⬆️
Files Coverage Δ
conftest.py 95.00% <ø> (ø)
services/github.py 94.87% <100.00%> (+0.13%) ⬆️
services/notification/__init__.py 100.00% <100.00%> (+2.11%) ⬆️
services/notification/commit_notifications.py 95.00% <100.00%> (+1.25%) ⬆️
services/notification/notifiers/base.py 92.59% <100.00%> (+0.13%) ⬆️
services/notification/notifiers/checks/base.py 98.12% <100.00%> (-0.08%) ⬇️
services/notification/notifiers/status/base.py 98.70% <100.00%> (+0.07%) ⬆️
...s/notification/notifiers/tests/unit/test_status.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_commit_notifications.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_notification_service.py 100.00% <100.00%> (ø)
... and 5 more

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

Attention: Patch coverage is 91.57303% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 97.27%. Comparing base (2ff1c1d) to head (df85504).
Report is 1 commits behind head on main.

Changes have been made to critical files, which contain lines commonly executed in production. Learn more

✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #482      +/-   ##
==========================================
- Coverage   97.29%   97.27%   -0.03%     
==========================================
  Files         443      443              
  Lines       34879    35000     +121     
==========================================
+ Hits        33936    34045     +109     
- Misses        943      955      +12     
Flag Coverage Δ
integration 97.24% <91.57%> (-0.03%) ⬇️
latest-uploader-overall 97.24% <91.57%> (-0.03%) ⬇️
unit 97.24% <91.57%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
NonTestCode 94.43% <94.02%> (+<0.01%) ⬆️
OutsideTasks 97.54% <100.00%> (+0.01%) ⬆️
Files Coverage Δ
conftest.py 95.00% <ø> (ø)
services/github.py 100.00% <100.00%> (ø)
services/notification/__init__.py 100.00% <100.00%> (+2.09%) ⬆️
services/notification/commit_notifications.py 95.00% <100.00%> (+1.25%) ⬆️
services/notification/notifiers/base.py 92.72% <100.00%> (+0.13%) ⬆️
services/notification/notifiers/checks/base.py 98.12% <100.00%> (-0.08%) ⬇️
services/notification/notifiers/status/base.py 98.70% <100.00%> (+0.07%) ⬆️
...s/notification/notifiers/tests/unit/test_status.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_commit_notifications.py 100.00% <100.00%> (ø)
...tification/tests/unit/test_notification_service.py 100.00% <100.00%> (ø)
... and 5 more
Related Entrypoints
run/app.tasks.notify.Notify

Copy link
Contributor

@michelletran-codecov michelletran-codecov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments.

pull: Pull, notifier: AbstractBaseNotifier, result_dict
comparison: ComparisonProxy,
notifier: AbstractBaseNotifier,
notification_result: NotificationResult | None,
) -> CommitNotification:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From below, it looks like we can return None type as well.

Suggested change
) -> CommitNotification:
) -> CommitNotification | None:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks

Comment on lines 351 to 354
if not self._repository_service:
self._repository_service = get_repo_provider_service(
self.repository, installation_name_to_use=self.gh_installation_name
self._repository_service = get_repo_provider_service_for_specific_commit(
commit, fallback_installation_name=self.gh_installation_name
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function also exists in StatusNotifier, which is the superclass. Can we remove the duplication?

self._repository_service = get_repo_provider_service(
self.repository, installation_name_to_use=self.gh_installation_name
self._repository_service = get_repo_provider_service_for_specific_commit(
commit, fallback_installation_name=self.gh_installation_name
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we save the repo provider to the class object, which means that the repo service for the first commit that calls this function will be returned here for the lifecycle of StatusNotifier. Is it possible for a repo service for a commit to be stale for a subsequent commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every NotifyTask has its own NotificationService. That in turn creates new instances of all the notifiers (

for notifier in self.get_notifiers_instances():
). A NotifyTask runs for a single commit, so I don't think it is possible for the commit to be stale

return True
return False

def _possibly_pin_commit_to_github_app(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
def _possibly_pin_commit_to_github_app(
def _maybe_pin_commit_to_github_app(

@@ -399,6 +402,55 @@ def run_impl_within_lock(
)
return {"notified": False, "notifications": None}

def _possibly_refresh_previous_selection(self, commit: Commit) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
def _possibly_refresh_previous_selection(self, commit: Commit) -> bool:
def _maybe_refresh_previous_selection(self, commit: Commit) -> bool:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change this because (1) I don't feel "maybe" is an improvement to "possibly" and (2, most important) we use "possibly_" in this context in other parts of the code, so it keeps consistent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, maybe doesn't necessarily fit here because it's more commonly used for Optional types (see monads). So I'm fine leaving it.

tasks/notify.py Outdated
def _possibly_pin_commit_to_github_app(
self, commit: Commit, torngit: TorngitBaseAdapter
) -> int | str | None:
"""Pins down the github app to use when emitting notifications for this commit, as needed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Pins down the github app to use when emitting notifications for this commit, as needed.
"""Pin the GitHub app to use when emitting notifications for this commit, as needed.

@@ -103,11 +106,10 @@ def flag_coverage_was_uploaded(self, comparison) -> bool:
len(report_uploaded_flags.intersection(flags_included_in_status_check)) > 0
)

@property
def repository_service(self):
def repository_service(self, commit: Commit):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a type annotation for the return value and self._repository_service.

if not self._repository_service:
self._repository_service = get_repo_provider_service(
self.repository, installation_name_to_use=self.gh_installation_name
self._repository_service = get_repo_provider_service_for_specific_commit(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to confirm my understanding of the code: does this pull the commit specific app that was set at the beginning of the app? Specifically, _possibly_pin_commit_to_github_app sets the app to use, then this function will retrieve it if it exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the idea, yes

Copy link
Contributor

@michelletran-codecov michelletran-codecov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@giovanni-guidini giovanni-guidini added this pull request to the merge queue Jun 3, 2024
Merged via the queue into main with commit 36fc900 Jun 3, 2024
21 of 30 checks passed
@giovanni-guidini giovanni-guidini deleted the gio/pin-commit-to-ghapp branch June 3, 2024 18:10
adrian-codecov pushed a commit that referenced this pull request Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants