-
Notifications
You must be signed in to change notification settings - Fork 35
Handle singleton artifact objects per FAIR spec #497
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
kasparsd
wants to merge
5
commits into
fairpm:main
Choose a base branch
from
kasparsd:fix-package-artifacts-types
base: main
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.
+155
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a28a06
Normalize to arrays of artifact objects since spec supports both formats
kasparsd 7bff913
Pick the best artifact for locale
kasparsd 85afc80
Account for lang property being optional
kasparsd d5ca380
Add unit tests for artifact picker
kasparsd 0e3a367
Describe the current state
kasparsd 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -460,15 +460,19 @@ function pick_artifact_by_lang( array $artifacts, ?string $locale = null ) { | |
| // Score artifacts based on match. | ||
| $score_artifact = function ( $artifact ) use ( $langs ) { | ||
| $score = 0; | ||
| $lang = strtolower( $artifact->lang ?? '' ); | ||
|
|
||
| // Check for lang match. | ||
| $idx = array_search( strtolower( $artifact->lang ), $langs, true ); | ||
| $idx = array_search( $lang, $langs, true ); | ||
| if ( $idx !== false ) { | ||
| $score += ( count( $langs ) - $idx ) * 100; | ||
| } | ||
|
|
||
| return $score; | ||
| }; | ||
|
|
||
| // Minimal behavior only: missing lang is tolerated, but equal-score ordering | ||
| // and partial-locale fallback precedence are intentionally unspecified. | ||
| usort( $artifacts, function ( $a, $b ) use ( $score_artifact ) { | ||
| $a_score = $score_artifact( $a ); | ||
| $b_score = $score_artifact( $b ); | ||
|
|
@@ -726,6 +730,7 @@ function get_package_data( $did ) { | |
| $type = str_replace( 'wp-', '', $metadata->type ); | ||
| $sections = (array) $metadata->sections; | ||
| $description = trim( $sections['description'] ?? '' ); | ||
| $package_artifact = isset( $release->artifacts->package ) ? pick_artifact_by_lang( $release->artifacts->package ) : null; | ||
|
|
||
| $response = [ | ||
| 'name' => $metadata->name, | ||
|
|
@@ -747,8 +752,8 @@ function get_package_data( $did ) { | |
| 'new_version' => $release->version, | ||
| 'version' => $release->version, | ||
| 'remote_version' => $release->version, | ||
| 'package' => $release->artifacts->package[0]->url, | ||
| 'download_link' => $release->artifacts->package[0]->url, | ||
| 'package' => $package_artifact->url ?? '', | ||
| 'download_link' => $package_artifact->url ?? '', | ||
| 'tested' => $required_versions['tested_to'] ?? '', | ||
| 'external' => 'xxx', | ||
| 'last_updated' => $metadata->last_updated ?? '', | ||
|
|
@@ -791,10 +796,7 @@ function cache_did_for_install( array $options ): array { | |
| $did = array_find_key( | ||
| $releases, | ||
| function ( $release ) use ( $options ) { | ||
| if ( ! is_array( $release->artifacts->package ) ) { | ||
| return false; | ||
| } | ||
| $artifact = pick_artifact_by_lang( $release->artifacts->package ); | ||
| $artifact = isset( $release->artifacts->package ) ? pick_artifact_by_lang( $release->artifacts->package ) : null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this return |
||
| return $artifact && $artifact->url === $options['package']; | ||
| } | ||
| ); | ||
|
|
@@ -964,8 +966,12 @@ function maybe_add_accept_header( $args, $url ) : array { | |
| } | ||
|
|
||
| foreach ( $releases as $release ) { | ||
| if ( $url === $release->artifacts->package[0]->url ) { | ||
| $content_type = $release->artifacts->package[0]->{'content-type'}; | ||
| $artifact = array_find( | ||
| $release->artifacts->package ?? [], | ||
| fn ( $package_artifact ) => $url === ( $package_artifact->url ?? '' ) | ||
| ); | ||
| if ( $artifact ) { | ||
| $content_type = $artifact->{'content-type'} ?? ''; | ||
| if ( $content_type === 'application/octet-stream' ) { | ||
| $args = array_merge( $args, [ 'headers' => [ 'Accept' => $content_type ] ] ); | ||
| break; | ||
|
|
||
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,53 @@ | ||
| <?php | ||
| /** | ||
| * Tests for FAIR\Packages\pick_artifact_by_lang(). | ||
| * | ||
| * @package FAIR | ||
| */ | ||
|
|
||
| use function FAIR\Packages\pick_artifact_by_lang; | ||
|
|
||
| /** | ||
| * Tests for FAIR\Packages\pick_artifact_by_lang(). | ||
| * | ||
| * @covers FAIR\Packages\pick_artifact_by_lang | ||
| */ | ||
| class PickArtifactByLangTest extends WP_UnitTestCase { | ||
|
|
||
| // Edge cases intentionally not pinned down here: tie-breaking across multiple | ||
| // matching lang values, artifacts without lang, and fallback precedence across | ||
| // partial locale matches. | ||
|
|
||
| /** | ||
| * Test should prefer an exact locale match over artifacts without lang. | ||
| */ | ||
| public function test_should_prefer_exact_locale_match_over_artifact_without_lang() { | ||
| $fallback_artifact = (object) [ | ||
| 'url' => 'https://example.com/no-lang.zip', | ||
| ]; | ||
| $matching_artifact = (object) [ | ||
| 'url' => 'https://example.com/de-de.zip', | ||
| 'lang' => 'de-DE', | ||
| ]; | ||
|
|
||
| $selected = pick_artifact_by_lang( [ $fallback_artifact, $matching_artifact ], 'de-DE' ); | ||
|
|
||
| $this->assertSame( $matching_artifact, $selected, 'The exact locale match should be selected.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test should not fail when artifacts do not specify lang. | ||
| */ | ||
| public function test_should_return_an_artifact_when_lang_is_missing() { | ||
| $first_artifact = (object) [ | ||
| 'url' => 'https://example.com/first.zip', | ||
| ]; | ||
| $second_artifact = (object) [ | ||
| 'url' => 'https://example.com/second.zip', | ||
| ]; | ||
|
|
||
| $selected = pick_artifact_by_lang( [ $first_artifact, $second_artifact ], 'de-DE' ); | ||
|
|
||
| $this->assertContains( $selected, [ $first_artifact, $second_artifact ], 'Artifacts without lang should still return a valid artifact.' ); | ||
| } | ||
| } |
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,78 @@ | ||
| <?php | ||
| /** | ||
| * Tests for FAIR\Packages\ReleaseDocument. | ||
| * | ||
| * @package FAIR | ||
| */ | ||
|
|
||
| use FAIR\Packages\ReleaseDocument; | ||
|
|
||
| /** | ||
| * Tests for FAIR\Packages\ReleaseDocument. | ||
| * | ||
| * @covers FAIR\Packages\ReleaseDocument::from_data | ||
| */ | ||
| class ReleaseDocumentTest extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Test should normalize singleton artifact objects to arrays. | ||
| */ | ||
| public function test_should_normalize_singleton_artifact_objects_to_arrays() { | ||
| $package_artifact = (object) [ | ||
| 'url' => 'https://example.com/plugin.zip', | ||
| ]; | ||
| $icon_artifact = (object) [ | ||
| 'url' => 'https://example.com/icon.png', | ||
| ]; | ||
| $custom_artifact = (object) [ | ||
| 'url' => 'https://example.com/extra.json', | ||
| ]; | ||
|
|
||
| $release = ReleaseDocument::from_data( | ||
| (object) [ | ||
| 'version' => '1.2.3', | ||
| 'artifacts' => (object) [ | ||
| 'package' => $package_artifact, | ||
| 'icon' => $icon_artifact, | ||
| 'x-extra' => $custom_artifact, | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertNotWPError( $release, 'Expected a valid release document.' ); | ||
| $this->assertIsArray( $release->artifacts->package, 'Package artifacts should be normalized to an array.' ); | ||
| $this->assertIsArray( $release->artifacts->icon, 'Icon artifacts should be normalized to an array.' ); | ||
| $this->assertIsArray( $release->artifacts->{'x-extra'}, 'Custom artifact types should be normalized to an array.' ); | ||
| $this->assertSame( $package_artifact, $release->artifacts->package[0], 'The original package artifact should be preserved.' ); | ||
| $this->assertSame( $icon_artifact, $release->artifacts->icon[0], 'The original icon artifact should be preserved.' ); | ||
| $this->assertSame( $custom_artifact, $release->artifacts->{'x-extra'}[0], 'The original custom artifact should be preserved.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test should preserve artifact arrays. | ||
| */ | ||
| public function test_should_preserve_artifact_arrays() { | ||
| $package_artifact = (object) [ | ||
| 'url' => 'https://example.com/plugin.zip', | ||
| ]; | ||
| $banner_artifact = (object) [ | ||
| 'url' => 'https://example.com/banner.png', | ||
| ]; | ||
|
|
||
| $release = ReleaseDocument::from_data( | ||
| (object) [ | ||
| 'version' => '1.2.3', | ||
| 'artifacts' => (object) [ | ||
| 'package' => [ $package_artifact ], | ||
| 'banner' => [ $banner_artifact ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertNotWPError( $release, 'Expected a valid release document.' ); | ||
| $this->assertCount( 1, $release->artifacts->package, 'Package artifact arrays should be preserved.' ); | ||
| $this->assertCount( 1, $release->artifacts->banner, 'Banner artifact arrays should be preserved.' ); | ||
| $this->assertSame( $package_artifact, $release->artifacts->package[0], 'Existing package array entries should be preserved.' ); | ||
| $this->assertSame( $banner_artifact, $release->artifacts->banner[0], 'Existing banner array entries should be preserved.' ); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Account for missing
langproperty which is allowed per spec.