How can I programatically change the (D8 core) workflow state of an entity with Content Moderation? I'm using Drupal 8.4-rc2.  When you enable the Workflow and Content Moderation modules, you automatically get the Editorial Workflow as a demonstration.

The states are as follows:


Archived
Draft
Published


And the transitions are as follows:


Create new draft (from draft/published to draft)
Publish (from draft/published to published)


How can I programatically call a transition?  Or, how can I programatically change the state without calling a transition (although this seems to violate the spirit of having transitions defined in the first place)?

Specifically, I have a node subject to content moderation (states: archived, draft, published).  By default, on the edit node page, the node is saved in the original state (so a draft node is saved as a draft).  I want to add a second button "save as published" with a submit handler, but I don't understand how I can programatically change the state.

What I do understand so far is that workflows are entities and so are states, so it's different than Drupal 7 in which most workflow-type modules used fields to track what was going on.

I checked the Examples module but there's nothing on configuration management/workflows yet.
A great example could be found in Content Moderation module's code.

$entity-&gt;set('moderation_state', $new_state);
if ($entity instanceof RevisionLogInterface) {
  $entity-&gt;setRevisionLogMessage($form_state-&gt;getValue('revision_log'));
  $entity-&gt;setRevisionUserId($this-&gt;currentUser()-&gt;id());
}
$entity-&gt;save();