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

  1. A new package_manager_audit_log logger channel
  2. A new subscriber that listens to PostRequireEvent to internally store which package changes were requested
    It is important that this is PostRequireEvent and not PreRequireEvent because 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::require may be made during the stage life cycle

  3. That same method should also introspect each changed package to observe which Drupal extensions (modules/themes/profiles) are added/changed/removed
  4. That same subscriber should also listen to PostApplyEvent to 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 PostApplyEvent the 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.1

    Changes 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

  5. 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

  1. @phenaproxima to clarify point 5
  2. Implement the above 5-step plan
  3. Implement the 6th step: test coverage that simultaneously makes our Build tests easier to maintain
  4. Create 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 → use drush 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.

Command icon 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

Wim Leers created an issue. See original summary.

Wim Leers credited tedbow.

wim leers’s picture

Assigned: Unassigned » phenaproxima
phenaproxima’s picture

Assigned: phenaproxima » Unassigned
Issue summary: View changes
phenaproxima’s picture

Issue summary: View changes
phenaproxima’s picture

Issue summary: View changes
tedbow’s picture

Issue tags: +sprint

A new subscriber that listens to PreRequireEvent to log which package changes were requested

I 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.

Create 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

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 log command. Could have a channel option so we could do drupal log -channel=package_manager_audit_log

wim leers’s picture

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"

Sure. But … it will be critical for adoption/support by hosting companies, because this would make their job of supporting customers actually manageable.

tedbow’s picture

Issue summary: View changes
tedbow’s picture

Issue tags: +core-post-mvp
wim leers’s picture

Title: Package Manger should keep an audit log of changes it applied » Package Manager should keep an audit log of changes it applied
Issue tags: +Contributed project soft blocker
Related issues: +#3355446: [PP-1] Implement experimental cron updates for contrib in automatic_updates_extensions

This 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.

kunal.sachdev’s picture

Assigned: Unassigned » kunal.sachdev

kunal.sachdev’s picture

Assigned: kunal.sachdev » wim leers
Status: Active » Needs review
wim leers’s picture

Assigned: wim leers » kunal.sachdev
Status: Needs review » Needs work

This is on the right track!

High-level next steps:

  1. I think the current MR is interpreting the issue summary a bit too literally, which is why it currently only works for UpdateStage instances and not all StageBase instances. Explained on the MR how that can be supported 😊
  2. As for how to adopt this and start using it in tests: \ColinODell\PsrTestLogger\TestLogger to the rescue! This MR should update PackageManagerKernelTestBase to add an additional logger to the new logger.channel.package_manager_audit_log channel that this MR introduces, just like we already do at the end of \Drupal\Tests\package_manager\Kernel\PackageManagerKernelTestBase::setUp():
        $this->auditLogger = new TestLogger();
        $this->container->get('logger.channel.package_manager_audit_log')
          ->addLogger($this->auditLogger);
    
  3. 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 the Kernel tests execute a whole stage life cycle 🙈
  4. Attempt #2! As for how to adopt this and start using it in tests: \Drupal\Tests\package_manager\Build\TemplateProjectTestBase::assertExpectedStageEventsFired() provides an example! That is using the test-only package_manager_test_event_logger (which is still valuable on its own). This MR needs to add a TemplateProjectTestBase::assertPackageManagerAuditLog() method with logic modeled after ::assertExpectedStageEventsFired().
  5. 100% of build tests should be updated to use it — that means 5 classes should gain a ::assertPackageManagerAuditLog() all the way at the end.
wim leers’s picture

kunal.sachdev’s picture

Some of the failures are because of the exception Typed property Drupal\package_manager\EventSubscriber\ChangeLogSubscriber::$installedPackagesBeforeApply must not be accessed before initialization during post-apply as I am accessing it in \Drupal\package_manager\EventSubscriber\logAppliedChanges. But it is getting set in \Drupal\package_manager\EventSubscriber\recordInstalledPackagesBeforeApply which 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\logRequestedChanges which runs on post-require and it works fine.

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
omkar.podey’s picture

Assigned: Unassigned » omkar.podey
omkar.podey’s picture

Assigned: omkar.podey » Unassigned
Status: Needs review » Needs work

Two nits, otherwise looks good, nice work✌️

kunal.sachdev’s picture

Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » kunal.sachdev
Status: Needs review » Needs work

Looking 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.

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » kunal.sachdev
Status: Needs review » Needs work

I found more room for simplification!

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » kunal.sachdev
Status: Needs review » Needs work

99% ready!

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs followup

No more need for a follow-up for a Drush command: I discovered drush watchdog:list --type thanks to @tedbow's comment #8.

Great to have this done!

wim leers’s picture

Issue summary: View changes
phenaproxima’s picture

Status: Reviewed & tested by the community » Needs work

I really like the idea here, but I think some clean-up is warranted.

wim leers’s picture

Assigned: Unassigned » kunal.sachdev

Nice catches from @phenaproxima. Found a few new inconsistencies now — see the MR.

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Reviewed & tested by the community
tedbow’s picture

Assigned: Unassigned » tedbow

I would like to review and commit this. Will be great to get this in!

tedbow’s picture

Assigned: tedbow » Unassigned
Status: Reviewed & tested by the community » Needs work

See MR comment

wim leers’s picture

Assigned: Unassigned » tedbow
Status: Needs work » Needs review
tedbow’s picture

Title: Package Manager should keep an audit log of changes it applied » Package Manager should keep an audit log of changes it applied to the active codebase
Assigned: tedbow » Unassigned

Unassigning 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

tedbow’s picture

Status: Needs review » Needs work
kunal.sachdev’s picture

Assigned: Unassigned » kunal.sachdev
kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » kunal.sachdev
Status: Needs review » Needs work

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.

Fair!

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.

WFM

Found some problems during re-reviewing.

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
phenaproxima’s picture

Status: Needs review » Needs work

I think we still have some clarifying to do here, and I'm wondering if we can't make the whole thing slightly simpler.

wim leers’s picture

Assigned: Unassigned » kunal.sachdev

For @phenaproxima's feedback.

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » kunal.sachdev
Status: Needs review » Needs work

One last piece of feedback.

P.S.: Those new commit messages are WAY better! 👏

kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
wim leers’s picture

Assigned: Unassigned » phenaproxima
Status: Needs review » Reviewed & tested by the community

🚀

kunal.sachdev’s picture

Assigned: phenaproxima » kunal.sachdev
Status: Reviewed & tested by the community » Needs work
kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
Status: Needs work » Needs review
tedbow’s picture

Assigned: Unassigned » phenaproxima

I 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.

wim leers’s picture

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

If this were critical logic, I'd get it. But it's just logging.

maybe we don't need to test this but […]

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.

phenaproxima’s picture

wim leers’s picture

To 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:

But the whole update process was not logged at all. By looking at the log report, it only shows the 2 auto_updates modules are installed. It should also log the core was upgraded from which version to version.

wim leers’s picture

Issue tags: +Pittsburgh2023
phenaproxima’s picture

Status: Needs review » Needs work

There are some core things here that I don't think I'm understanding.

phenaproxima’s picture

Assigned: phenaproxima » tedbow
Issue summary: View changes
Status: Needs work » Needs review
tedbow’s picture

Assigned: tedbow » phenaproxima
Status: Needs review » Needs work

Looking good!

A few points in the MR

phenaproxima’s picture

Assigned: phenaproxima » tedbow
Status: Needs work » Needs review
tedbow’s picture

Assigned: tedbow » phenaproxima
Status: Needs review » Reviewed & tested by the community

Just 2 more things

tedbow’s picture

🤣I didn't mean to RTBC it before but now it looks done!

thanks everyone!

phenaproxima’s picture

Status: Reviewed & tested by the community » Fixed
tedbow’s picture

Status: Fixed » Needs work

uggh. 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

  • phenaproxima committed cb221304 on 3.0.x
    Issue #3355628 follow-up by tedbow, phenaproxima: Fix incorrect type...
phenaproxima’s picture

Status: Needs work » Fixed
wim leers’s picture


    // The first record should be of the requested changes.
    $expected_message = <<<END
Requested changes:
- Update drupal/core-recommended from 9.8.0 to ^9.8.1
- Install drupal/semver_test * (any version)
END;
    $this->assertSame($expected_message, (string) $records[0]['message']);

    // The second record should be of the actual changes.
    $expected_message = <<<END
Applied changes:
- Updated drupal/core from 9.8.0 to 9.8.1
- Updated drupal/core-dev from 9.8.0 to 9.8.1
- Updated drupal/core-recommended from 9.8.0 to 9.8.1
- Installed drupal/semver_test 8.1.1
- Uninstalled package/removed
END;
    $this->assertSame($expected_message, (string) $records[1]['message']);

🤩

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.