Problem/Motivation
@tedbow in Slack:
I had a thought regarding one of our discussions yesterday. Maybe we should add a package manager changes longer. It would log on post apply all the Drupal projects that had changed. It could also keep a record of what was asked for in require so that it could report on what were the housework changes and what Drupal projects were updated because of dependencies. Also dependencies that were added or removed
This would also definitely help a lot with #3355463: Looking for Alpha tester for future functionality!
Steps to reproduce
N/A
Proposed resolution
- A new
package_manager_audit_loglogger channel - A new subscriber that listens to
PostRequireEventto internally store which package changes were requested
It is important that this isPostRequireEventand notPreRequireEventbecause in PreRequireEvent another validator may still stop the operation or composer stager might throw an error before the composer operation is actually made.More than 1 require call to
\Drupal\package_manager\StageBase::requiremay be made during the stage life cycle - That same method should also introspect each changed package to observe which Drupal extensions (modules/themes/profiles) are added/changed/removed
- That same subscriber should also listen to
PostApplyEventto observe what the actual changes are on the Drupal site. The reason is because there could be several require operations taking place in a stage, with certain things changed in each one; you'd have to go back through the logs and do a lot of mental grunt work to piece together the final picture of what was actually deployed. PostApplyEvent, on the other hand, has a complete picture of what happened right off the bat.In
PostApplyEventthe subscriber should finally log all the changes in 1 log entry.something like
Requested changes
Updated drupal/webform 8.0.1 -> 8.5.3
New drupal/other:10.0.1Changes to other Drupal packages:
Remove drupal/webform_incompatible
Added drupal/new_dependency:1.2.3
Updated drupal/updated_dependency 1.1.1 -> 2.0.1 - 100% of build tests should be updated to use it — that means 5 classes should gain a
::assertPackageManagerAuditLog()all the way at the end.
Remaining tasks
@phenaproxima to clarify point 5Implement the above 5-step planImplement the 6th step: test coverage that simultaneously makes ourBuildtests easier to maintainCreate a follow-up for a Drush command that makes it easy to inspect the audit log, based on what we learn here along the way→ usedrush watchdog:list --type👍
User interface changes
None.
API changes
None. Internal API addition: a package_manager_audit_log logger channel that may be used for more purposes in the future.
StageBase's getMetadata() and setMetadata() methods will be public, since this is a very clear case where it makes sense to attach persistent metadata to a particular stage, and it makes the code simpler.
Data model changes
None.
Issue fork automatic_updates-3355628
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
wim leersComment #5
phenaproximaComment #6
phenaproximaComment #7
phenaproximaComment #8
tedbowI don't there should be any logging until PostApply. Update until point we don't know if these change will be actually be applied to the site. The operations may be canceled or not validate on Pre-Apply. We can subscribe to PreRequire and keep a record of what was request to compare against what the final packages that are changed..
I think it would confusing to have the PreRequireEvent logging package changes that never actually get applied to the site.
I think this follow-up should the priority of "minor" because this won't be part of the core module. So anything we can do for the core module should be be a higher priority. We could file core issue to provide a
drupal logcommand. Could have a channel option so we could dodrupal log -channel=package_manager_audit_logComment #9
wim leers👍
💯
Sure. But … it will be critical for adoption/support by hosting companies, because this would make their job of supporting customers actually manageable.
Comment #10
tedbowComment #11
tedbowComment #12
wim leersThis would also really help with debugging/writing the Build test over at #3355446: [PP-1] Implement experimental cron updates for contrib in automatic_updates_extensions.
Comment #13
kunal.sachdev commentedComment #15
kunal.sachdev commentedComment #16
wim leersThis is on the right track!
High-level next steps:
UpdateStageinstances and not allStageBaseinstances. Explained on the MR how that can be supported 😊As for how to adopt this and start using it in tests:\ColinODell\PsrTestLogger\TestLoggerto the rescue! This MR should updatePackageManagerKernelTestBaseto add an additional logger to the newlogger.channel.package_manager_audit_logchannel that this MR introduces, just like we already do at the end of\Drupal\Tests\package_manager\Kernel\PackageManagerKernelTestBase::setUp():The preceding point should not cause any changes in existing tests. It just makes it possible for tests to add additional assertions. Let's start by adding a few.Nope, that can't work, because none of theKerneltests execute a whole stage life cycle 🙈\Drupal\Tests\package_manager\Build\TemplateProjectTestBase::assertExpectedStageEventsFired()provides an example! That is using the test-onlypackage_manager_test_event_logger(which is still valuable on its own). This MR needs to add aTemplateProjectTestBase::assertPackageManagerAuditLog()method with logic modeled after::assertExpectedStageEventsFired().::assertPackageManagerAuditLog()all the way at the end.Comment #17
wim leers#16 helped me discover the need for #3359609: Document that validators comparing state between stage life cycle operations must store that state out-of-band, not on the object 👍😄
Comment #18
kunal.sachdev commentedSome of the failures are because of the exception
Typed property Drupal\package_manager\EventSubscriber\ChangeLogSubscriber::$installedPackagesBeforeApply must not be accessed before initializationduring post-apply as I am accessing it in\Drupal\package_manager\EventSubscriber\logAppliedChanges. But it is getting set in\Drupal\package_manager\EventSubscriber\recordInstalledPackagesBeforeApplywhich runs on pre-create which definitely runs before post-apply and we shouldn’t get this error. But I think the error is because the post-apply is running on different request and it can't access that property. And actually this property is also accessed in\Drupal\package_manager\EventSubscriber\logRequestedChangeswhich runs on post-require and it works fine.Comment #19
kunal.sachdev commentedComment #20
omkar.podey commentedComment #21
omkar.podey commentedTwo nits, otherwise looks good, nice work✌️
Comment #22
kunal.sachdev commentedComment #23
wim leersLooking great! 👏 😄
I requested some changes (pun intended 😜) to help improve test code clarity. But this is definitely close to being done!
Credited @omkar.podey for the review.
Comment #24
kunal.sachdev commentedComment #25
wim leersI found more room for simplification!
Comment #26
kunal.sachdev commentedComment #27
wim leers99% ready!
Comment #28
kunal.sachdev commentedComment #29
wim leersNo more need for a follow-up for a Drush command: I discovered
drush watchdog:list --typethanks to @tedbow's comment #8.Great to have this done!
Comment #30
wim leersComment #31
phenaproximaI really like the idea here, but I think some clean-up is warranted.
Comment #32
wim leersNice catches from @phenaproxima. Found a few new inconsistencies now — see the MR.
Comment #33
kunal.sachdev commentedThe build tests are failing because of #3362110: Since #3361983 was committed to Drupal core, psr/http-message needed to be explicitly required for build tests
Comment #34
wim leersOnce tests are passing again (after #3362110: Since #3361983 was committed to Drupal core, psr/http-message needed to be explicitly required for build tests lands), this can be committed 👍
Comment #35
tedbowI would like to review and commit this. Will be great to get this in!
Comment #36
tedbowSee MR comment
Comment #37
wim leers🏓 See MR comment: https://git.drupalcode.org/project/automatic_updates/-/merge_requests/86...
Comment #38
tedbowUnassigning from myself since I responded to @Wim Leers' points.
I don't think we should expand this issue any further and don't think the current log messages give enough context to the user reading the logs to understand that the "required" changes may or may not actually related to changes that made to the active site(see my MR comments). I think we would need to expand this issue a lot to make including the messages from updates that did not succeed or were canceled to be understandable by people were not involved in developing this module.
for that reason I think in this issue we should only every add a long message in PostApply. We can use information we gathered from subscribe to the other events but we should not log anything in those other events.
That will fulfill the primary purpose of the of the issue which is let the admin know what projects have been changed by Package Manager.
I updated the title to match this
Comment #39
tedbowComment #40
kunal.sachdev commentedComment #41
kunal.sachdev commentedComment #42
wim leersFair!
WFM
Found some problems during re-reviewing.
Comment #43
kunal.sachdev commentedComment #44
phenaproximaI think we still have some clarifying to do here, and I'm wondering if we can't make the whole thing slightly simpler.
Comment #45
wim leersFor @phenaproxima's feedback.
Comment #46
kunal.sachdev commentedComment #47
wim leersOne last piece of feedback.
P.S.: Those new commit messages are WAY better! 👏
Comment #48
kunal.sachdev commentedComment #49
wim leers🚀
Comment #50
kunal.sachdev commentedComment #51
kunal.sachdev commentedComment #52
tedbowI wondering if this would be easier to test in a kernel.
We can use our fixture manipulators to fake which versions get updated.
The reason I was wondering is because right now we don't test that nothing gets logged if the process is stopped in preapply or before. maybe we don't need to test this but it would be pretty easy in kernel test but very hard in a build test. Maybe we could that in follow-up
@phenaproxima had mentioned in wanted to review before I committed so assigning him.
Comment #53
wim leersIf this were critical logic, I'd get it. But it's just logging.
I don't think we need to test this.
The complexity of testing would far outweigh the complexity of the logging. Plus, a bug in the logging cannot even break the functionality itself. Hence no tests for edge cases necessary, only for The Happy Path.
Comment #55
phenaproximaCrediting @wendyZ for #3365422: [DrupalCon] Automatic Updates alpha test result.
Comment #56
wim leersTo clarify #55: @wendyZ specifically requested this behavior; they were surprised it did not already happen, thus confirming the need for this functionality — here's what they wrote:
Comment #57
wim leersComment #58
phenaproximaThere are some core things here that I don't think I'm understanding.
Comment #59
phenaproximaComment #60
tedbowLooking good!
A few points in the MR
Comment #61
phenaproximaComment #62
tedbowJust 2 more things
Comment #63
tedbow🤣I didn't mean to RTBC it before but now it looks done!
thanks everyone!
Comment #65
phenaproximaComment #66
tedbowuggh. we have phpstan error only in 10.0.x for some reason https://www.drupal.org/pift-ci-job/2692141
The error is real not sure why phpstan didn't catch it
Comment #69
phenaproximaComment #70
wim leers🤩