-
Notifications
You must be signed in to change notification settings - Fork 90
Keep WordPress auto-updates on for sites created before the setting existed #4715
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
Open
gcsecsey
wants to merge
8
commits into
trunk
Choose a base branch
from
stu-2348-fix-stale-wordpress-on-site-create
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1ea551f
Install the current WordPress release when creating an auto-updating …
gcsecsey e8186f0
Keep the 24 hour dependency check interval
gcsecsey aa74be9
Treat an unset auto-update flag as auto-updating
gcsecsey 6931aa4
Merge branch 'trunk' into stu-2348-fix-stale-wordpress-on-site-create
gcsecsey 2323761
Revert the WordPress version check restructure and its lock
gcsecsey 21af72c
Check the cached WordPress copy is current when creating a latest site
gcsecsey 024d479
Merge branch 'trunk' into stu-2348-fix-stale-wordpress-on-site-create
gcsecsey 4536605
Merge branch 'trunk' into stu-2348-fix-stale-wordpress-on-site-create
gcsecsey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { mkdir } from 'fs/promises'; | ||
| import path from 'path'; | ||
| import { lockFileAsync, unlockFileAsync } from '@studio/common/lib/lockfile'; | ||
| import { getServerFilesPath } from '@studio/common/lib/well-known-paths'; | ||
|
|
||
| // Far more patient than the config-file locks: the guarded work downloads and | ||
| // copies a full WordPress release, which takes minutes on a slow connection. | ||
| const STALE_TIME = 10 * 60 * 1000; | ||
| const WAIT_TIME = 2 * 60 * 1000; | ||
|
gcsecsey marked this conversation as resolved.
Outdated
|
||
|
|
||
| function getLockfilePath(): string { | ||
| return path.join( getServerFilesPath(), 'wordpress-versions.lock' ); | ||
| } | ||
|
|
||
| /** | ||
| * Serialize access to `wordpress-versions/`. The `latest` directory is shared | ||
| * mutable state: a refresh rewrites it in place, while a site create may be | ||
| * copying files out of it at the same time. Without this, a create started | ||
| * just as a new WordPress release lands can copy a half-written directory. | ||
|
gcsecsey marked this conversation as resolved.
Outdated
|
||
| */ | ||
| export async function withWordPressVersionsLock< T >( run: () => Promise< T > ): Promise< T > { | ||
| const lockfilePath = getLockfilePath(); | ||
| await mkdir( path.dirname( lockfilePath ), { recursive: true } ); | ||
| await lockFileAsync( lockfilePath, { stale: STALE_TIME, wait: WAIT_TIME } ); | ||
|
gcsecsey marked this conversation as resolved.
Outdated
|
||
|
|
||
| try { | ||
| return await run(); | ||
| } finally { | ||
| await unlockFileAsync( lockfilePath ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
apps/cli/lib/dependency-management/tests/wordpress.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import fs from 'fs'; | ||
| import { downloadFile } from '@studio/common/lib/download-file'; | ||
| import { extractZip } from '@studio/common/lib/extract-zip'; | ||
| import { recursiveCopyDirectory } from '@studio/common/lib/fs-utils'; | ||
| import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import { getWordPressVersionPath } from '../paths'; | ||
| import { updateLatestWordPressVersion } from '../wordpress'; | ||
|
|
||
| vi.mock( 'fs', () => ( { | ||
| default: { | ||
| existsSync: vi.fn(), | ||
| promises: { | ||
| readdir: vi.fn(), | ||
| readFile: vi.fn(), | ||
| mkdir: vi.fn(), | ||
| cp: vi.fn(), | ||
| rm: vi.fn(), | ||
| }, | ||
| }, | ||
| } ) ); | ||
|
|
||
| vi.mock( '@studio/common/lib/download-file', () => ( { downloadFile: vi.fn() } ) ); | ||
| vi.mock( '@studio/common/lib/extract-zip', () => ( { extractZip: vi.fn() } ) ); | ||
| vi.mock( '@studio/common/lib/fs-utils', () => ( { recursiveCopyDirectory: vi.fn() } ) ); | ||
| vi.mock( '../paths', () => ( { | ||
| getWordPressVersionPath: vi.fn( | ||
| ( version: string ) => `/server-files/wordpress-versions/${ version }` | ||
| ), | ||
| } ) ); | ||
|
|
||
| const STABLE_CHECK_URL = 'https://api.wordpress.org/core/stable-check/1.0/'; | ||
|
|
||
| function mockStableCheck( body: Record< string, string > ) { | ||
| vi.spyOn( global, 'fetch' ).mockResolvedValue( { | ||
| json: async () => body, | ||
| } as Response ); | ||
| } | ||
|
|
||
| describe( 'updateLatestWordPressVersion', () => { | ||
| beforeEach( () => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked( fs.existsSync ).mockReturnValue( true ); | ||
| vi.mocked( fs.promises.readdir ).mockResolvedValue( [ 'wp-includes' ] as never ); | ||
| vi.mocked( fs.promises.readFile ).mockResolvedValue( "<?php $wp_version = '6.9.5';" as never ); | ||
| mockStableCheck( { '6.9.5': 'latest' } ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| vi.restoreAllMocks(); | ||
| } ); | ||
|
|
||
| it( 'replaces the cached copy when wordpress.org reports a newer release', async () => { | ||
| mockStableCheck( { '6.9.5': 'outdated', '6.9.7': 'latest' } ); | ||
|
|
||
| await updateLatestWordPressVersion(); | ||
|
|
||
| // The outgoing release is kept under its own version number, so a site | ||
| // pinned to it doesn't have to download it again. | ||
| expect( recursiveCopyDirectory ).toHaveBeenCalledWith( | ||
| getWordPressVersionPath( 'latest' ), | ||
| getWordPressVersionPath( '6.9.5' ) | ||
| ); | ||
| expect( downloadFile ).toHaveBeenCalledTimes( 1 ); | ||
| expect( extractZip ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
|
|
||
| it( 'leaves the cached copy alone when it is already the current release', async () => { | ||
| mockStableCheck( { '6.9.5': 'latest' } ); | ||
|
|
||
| await updateLatestWordPressVersion(); | ||
|
|
||
| expect( recursiveCopyDirectory ).not.toHaveBeenCalled(); | ||
| expect( downloadFile ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'throws when wordpress.org is unreachable instead of serving the stale copy', async () => { | ||
| vi.spyOn( global, 'fetch' ).mockRejectedValue( new Error( 'network' ) ); | ||
|
|
||
| await expect( updateLatestWordPressVersion() ).rejects.toThrow( 'network' ); | ||
| expect( downloadFile ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'throws when wordpress.org reports no latest release', async () => { | ||
| mockStableCheck( { '6.9.5': 'outdated' } ); | ||
|
|
||
| await expect( updateLatestWordPressVersion() ).rejects.toThrow( | ||
| 'did not report a latest WordPress version' | ||
| ); | ||
| expect( downloadFile ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'downloads without a version check when nothing is cached yet', async () => { | ||
| vi.mocked( fs.promises.readdir ).mockResolvedValue( [] as never ); | ||
| vi.mocked( fs.existsSync ).mockReturnValue( false ); | ||
|
|
||
| await updateLatestWordPressVersion(); | ||
|
|
||
| expect( global.fetch ).not.toHaveBeenCalled(); | ||
| expect( downloadFile ).toHaveBeenCalledTimes( 1 ); | ||
| } ); | ||
|
|
||
| it( 'queries the documented stable-check endpoint', async () => { | ||
| mockStableCheck( { '6.9.5': 'latest' } ); | ||
|
|
||
| await updateLatestWordPressVersion(); | ||
|
|
||
| expect( global.fetch ).toHaveBeenCalledWith( STABLE_CHECK_URL ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.