Skip to content
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

Code samples usage #2438

Merged
merged 104 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
ce7096a
code_samples_usage.php
adriendupuis Jul 15, 2024
0984d82
AttributeCommand.php: New lines in __construct arg list
adriendupuis Jul 15, 2024
77dfdea
build.yaml: Display in PR changes in modified code samples' usages
adriendupuis Jul 15, 2024
0f59611
build.yaml: Fix "a step cannot have both the `uses` and `run` keys"
adriendupuis Jul 15, 2024
0fe80e2
PHP CS Fixes
adriendupuis Jul 15, 2024
1b10bb9
build.yaml: Fix github.head_ref & github.base_ref
adriendupuis Jul 15, 2024
1d84c80
Merge remote-tracking branch 'origin/code_sample_usage' into code_sam…
adriendupuis Jul 15, 2024
d620e43
build.yaml: Fix code_samples_usage.php path
adriendupuis Jul 15, 2024
673cc2a
build.yaml: clean up
adriendupuis Jul 15, 2024
7831578
build.yaml: clean up
adriendupuis Jul 15, 2024
4fff977
build.yaml: Fix code_samples_usage.php usage
adriendupuis Jul 15, 2024
8c13b5d
build.yaml: Try to fix git diff
adriendupuis Jul 15, 2024
d814e78
ProductCommand.php: New lines in __construct arg list
adriendupuis Jul 15, 2024
66eb299
code_samples_usage.php: Modified code_samples as $argv
adriendupuis Jul 15, 2024
c958b83
build.yaml: Get file list
adriendupuis Jul 15, 2024
417f81f
build.yaml: EOF?
adriendupuis Jul 15, 2024
de5b59c
PHP CS Fixes
adriendupuis Jul 15, 2024
ce2f928
build.yaml: Fix $GITHUB_OUTPUT usage
adriendupuis Jul 15, 2024
d37d29a
Merge remote-tracking branch 'origin/code_sample_usage' into code_sam…
adriendupuis Jul 15, 2024
b495b4c
build.yaml: Remove new lines
adriendupuis Jul 15, 2024
74284fe
build.yaml: Remove new lines
adriendupuis Jul 15, 2024
adaf752
Revert "build.yaml: Remove new lines"
adriendupuis Jul 15, 2024
f29bb76
build.yaml: Remove new lines
adriendupuis Jul 15, 2024
101b19f
build.yaml: Remove new lines
adriendupuis Jul 15, 2024
2a02c8f
build.yaml: Get same code_samples_usage.php than source
adriendupuis Jul 15, 2024
9798f67
build.yaml: Fix sed
adriendupuis Jul 15, 2024
7b038bc
code_samples_usage.php: Fix loop
adriendupuis Jul 15, 2024
c792230
build.yaml: try using file to avoid truncated env
adriendupuis Jul 15, 2024
3d55501
code_samples_usage.php: Format for GitHub Markdown
adriendupuis Jul 16, 2024
2089d12
build.yaml: Fix comparison
adriendupuis Jul 16, 2024
2273def
code_samples_usage.php: Enh. output
adriendupuis Jul 16, 2024
857cbe0
code_samples_usage.php: Use exec to not fail on not used
adriendupuis Jul 16, 2024
3a8fb60
build.yaml: Enh. output
adriendupuis Jul 16, 2024
746ce95
build.yaml: HTML diff
adriendupuis Jul 16, 2024
68429cb
build.yaml: HTML diff
adriendupuis Jul 16, 2024
22ebf9b
build.yaml: HTML diff
adriendupuis Jul 16, 2024
39690cb
build.yaml: HTML diff
adriendupuis Jul 16, 2024
c4a9211
build.yaml: HTML diff
adriendupuis Jul 16, 2024
b0481bf
build.yaml: HTML diff
adriendupuis Jul 16, 2024
ea54942
build.yaml: HTML diff
adriendupuis Jul 16, 2024
73eeed4
build.yaml: HTML diff
adriendupuis Jul 16, 2024
d39c4f4
build.yaml: HTML diff
adriendupuis Jul 16, 2024
e4b8438
build.yaml: HTML diff
adriendupuis Jul 16, 2024
3340034
build.yaml: HTML diff
adriendupuis Jul 16, 2024
e77fa75
build.yaml: HTML diff
adriendupuis Jul 16, 2024
61efbe1
build.yaml: HTML diff
adriendupuis Jul 16, 2024
52200ef
build.yaml: HTML diff
adriendupuis Jul 16, 2024
2780250
build.yaml: HTML diff
adriendupuis Jul 16, 2024
93b8236
build.yaml: HTML diff
adriendupuis Jul 16, 2024
265edf3
build.yaml: HTML diff
adriendupuis Jul 16, 2024
3327209
build.yaml: HTML diff
adriendupuis Jul 16, 2024
7f1cb71
build.yaml: HTML diff
adriendupuis Jul 16, 2024
71ea41d
build.yaml: HTML diff
adriendupuis Jul 16, 2024
afbde10
build.yaml: HTML diff
adriendupuis Jul 16, 2024
3a49355
build.yaml: HTML diff
adriendupuis Jul 16, 2024
8dda311
build.yaml: HTML diff
adriendupuis Jul 16, 2024
8fd689e
build.yaml: HTML diff
adriendupuis Jul 16, 2024
e17ce34
build.yaml: HTML diff
adriendupuis Jul 16, 2024
f853e37
build.yaml: HTML diff
adriendupuis Jul 16, 2024
46c70e9
build.yaml: HTML diff
adriendupuis Jul 17, 2024
170b535
Merge remote-tracking branch 'origin/master' into code_sample_usage
adriendupuis Jul 17, 2024
0501e78
build.yaml: HTML diff
adriendupuis Jul 17, 2024
0fa1dd9
build.yaml: HTML diff
adriendupuis Jul 17, 2024
f12a460
build.yaml: HTML diff
adriendupuis Jul 17, 2024
990e3df
build.yaml: HTML diff
adriendupuis Jul 17, 2024
f355a12
build.yaml: HTML diff
adriendupuis Jul 17, 2024
1e076f0
build.yaml: HTML diff
adriendupuis Jul 17, 2024
2ac3b57
build.yaml: HTML diff
adriendupuis Jul 17, 2024
5295c17
build.yaml: HTML diff
adriendupuis Jul 17, 2024
e105b68
build.yaml: HTML diff
adriendupuis Jul 17, 2024
bdb4419
product_api.md: Fix ProductCommand.php included lines
adriendupuis Jul 17, 2024
c401bd4
build.yaml: HTML diff
adriendupuis Jul 17, 2024
d5dfa5c
build.yaml: HTML diff
adriendupuis Jul 17, 2024
fde6b8a
build.yaml: HTML diff
adriendupuis Jul 17, 2024
6634420
product_api.md: Fix ProductCommand.php included lines
adriendupuis Jul 17, 2024
b2cc214
code_samples_usage.php: include_file add a new line after itself
adriendupuis Jul 17, 2024
e5d3b22
price_api.md: deduplicate empty line
adriendupuis Jul 17, 2024
64e6d02
build.yaml: HTML diff
adriendupuis Jul 17, 2024
0291aa4
build.yaml: HTML diff
adriendupuis Jul 17, 2024
99b20ef
product_api.md: Fix AttributeCommand.php included lines
adriendupuis Jul 26, 2024
758aed2
code_samples_usage.php: Trim empty lines
adriendupuis Jul 26, 2024
2c6c4f5
build.yaml: Remove and recreate code_samples comment
adriendupuis Jul 26, 2024
3f50549
build.yaml: Remove and recreate code_samples comment
adriendupuis Jul 26, 2024
4b2c351
Temporary file to test code_samples_usage on creation
adriendupuis Jul 26, 2024
7f786b1
PHP CS Fixes
adriendupuis Jul 26, 2024
31e7fe0
Temporary file to test code_samples_usage on creation
adriendupuis Jul 26, 2024
2b05fa2
Remove temporary test
adriendupuis Jul 26, 2024
271c562
code_samples_usage.php: Handle file included outside a code block eve…
adriendupuis Jul 29, 2024
ca98bc2
code_samples_usage.php: What happens when there is no change?
adriendupuis Jul 29, 2024
ae1961b
build.yaml: Skip if there is no code_samples/ change
adriendupuis Jul 29, 2024
ad02680
build.yaml: Skip if there is no code_samples/ change
adriendupuis Jul 29, 2024
5c7896c
Revert "code_samples_usage.php: What happens when there is no change?"
adriendupuis Jul 29, 2024
df005ea
build.yaml: Skip if there is no code_samples/ change
adriendupuis Jul 29, 2024
b3f39dc
code_samples_usage.php: Catch missing file error
adriendupuis Aug 26, 2024
40cf07e
build.yaml code-samples-inclusion-check previous_filename
adriendupuis Aug 26, 2024
aa12a80
build.yaml: name code-samples-inclusion-check
adriendupuis Aug 26, 2024
4a4d3bd
build.yaml: Restrict code-samples-inclusion-check permissions
adriendupuis Aug 29, 2024
b064eff
build.yaml: Handle "no diff" case
adriendupuis Aug 31, 2024
d09fd94
code_samples_usage.php: Rm unused getModifiedCodeSampleFileList
adriendupuis Sep 2, 2024
ebc14f5
code_samples_usage.php: escapeshellarg
adriendupuis Sep 2, 2024
08fe785
build.yaml: Restrict code-samples-inclusion-check permissions a bit more
adriendupuis Sep 2, 2024
72801ef
build.yaml: Restrict code-samples-inclusion-check permissions a bit more
adriendupuis Sep 2, 2024
8d192d4
build.yaml: Restrict code-samples-inclusion-check permissions a bit more
adriendupuis Sep 2, 2024
a6ddaa6
price_api.md: deduplicate empty line
adriendupuis Sep 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,100 @@ jobs:

- name: Run PHPStan analysis
run: composer phpstan

code-samples-inclusion-check:
name: Check code samples inclusion
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
# Needed to manage the comment
pull-requests: write

steps:
- name: List modified files
id: list
run: |
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files"
echo 'CODE_SAMPLES_CHANGE<<CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
curl -s -X GET -G $URL | jq -r '.[] | .filename,.previous_filename' | grep '^code_samples/' | tr '\n' ' ' >> "$GITHUB_OUTPUT"
echo '' >> "$GITHUB_OUTPUT"
echo 'CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"

- name: Checkout target branch (base_ref)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
uses: actions/checkout@v3
with:
ref: ${{ github.base_ref }}
- name: Log target branch code_samples usage
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: |
git fetch origin
git checkout origin/${{ github.head_ref }} -- tools/code_samples/code_samples_usage.php
php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_target.txt

- name: Checkout source branch (head_ref)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Log source branch code_samples usage
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_source.txt

- name: Compare code_samples usages (diff --unified)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
# diff returns 1 if there is a difference, this is normal but seen as an error by the job.
continue-on-error: true
run: |
source_length=`wc -l < $HOME/code_samples_usage_source.txt`
target_length=`wc -l < $HOME/code_samples_usage_target.txt`
diff -U $(( source_length > target_length ? source_length : target_length )) $HOME/code_samples_usage_target.txt $HOME/code_samples_usage_source.txt > $HOME/code_samples_usage.diff
- name: Check for differences
id: diff
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
run: |
echo "CODE_SAMPLES_DIFF=$(wc -l < $HOME/code_samples_usage.diff | xargs)" >> "$GITHUB_OUTPUT"
- name: Convert code_samples usages differences (diff2html)
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
run: |
npm install -g diff2html-cli
diff2html -f html -s side -t 'code_samples/ changes report' --su hidden --fct false -o stdout -i file -- $HOME/code_samples_usage.diff > $HOME/code_samples_usage.diff.html
- name: Upload code_samples usages differences artifact
id: artifact
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
uses: actions/upload-artifact@v4
with:
name: code_samples_usage.diff.html
path: ~/code_samples_usage.diff.html
overwrite: true
- name: Convert code_samples usages for comment
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
run: |
echo '# code_samples/ change report' >> code_samples_usage.diff.md
echo '' >> code_samples_usage.diff.md
php tools/code_samples/code_samples_usage_diff2html.php $HOME/code_samples_usage.diff >> code_samples_usage.diff.md
echo '<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>' >> code_samples_usage.diff.md
- name: Find Comment
id: find-comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: 'code_samples/ change report'
- name: Delete comment
if: steps.find-comment.outputs.comment-id != ''
uses: actions/github-script@v6
with:
script: |
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.find-comment.outputs.comment-id }}
})
- name: Create comment
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: code_samples_usage.diff.md
edit-mode: replace
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ final class AttributeCommand extends Command

private PermissionResolver $permissionResolver;

public function __construct(LocalAttributeDefinitionServiceInterface $localAttributeDefinitionService, AttributeDefinitionServiceInterface $attributeDefinitionService, AttributeGroupServiceInterface $attributeGroupService, LocalAttributeGroupServiceInterface $localAttributeGroupService, AttributeTypeServiceInterface $attributeTypeService, UserService $userService, PermissionResolver $permissionResolver)
{
public function __construct(
LocalAttributeDefinitionServiceInterface $localAttributeDefinitionService,
AttributeDefinitionServiceInterface $attributeDefinitionService,
AttributeGroupServiceInterface $attributeGroupService,
LocalAttributeGroupServiceInterface $localAttributeGroupService,
AttributeTypeServiceInterface $attributeTypeService,
UserService $userService,
PermissionResolver $permissionResolver
) {
$this->localAttributeGroupService = $localAttributeGroupService;
$this->attributeGroupService = $attributeGroupService;
$this->attributeTypeService = $attributeTypeService;
Expand Down
10 changes: 8 additions & 2 deletions code_samples/api/product_catalog/src/Command/ProductCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ final class ProductCommand extends Command

private ProductAvailabilityServiceInterface $productAvailabilityService;

public function __construct(UserService $userService, PermissionResolver $permissionResolver, ProductTypeServiceInterface $productTypeService, ProductServiceInterface $productService, LocalProductServiceInterface $localProductService, ProductAvailabilityServiceInterface $productAvailabilityService)
{
public function __construct(
UserService $userService,
PermissionResolver $permissionResolver,
ProductTypeServiceInterface $productTypeService,
ProductServiceInterface $productService,
LocalProductServiceInterface $localProductService,
ProductAvailabilityServiceInterface $productAvailabilityService
) {
$this->userService = $userService;
$this->permissionResolver = $permissionResolver;
$this->productService = $productService;
Expand Down
2 changes: 0 additions & 2 deletions docs/pim/price_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ To load price definitions that match given criteria, use `ProductPriceServiceInt

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 12, 16) =]]

// ...
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 100, 110) =]]
```
Expand Down Expand Up @@ -81,7 +80,6 @@ To resolve a price of a product in the currency for the current context, use eit

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 7, 8) =]][[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 11, 12) =]]

// ...
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 111, 115) =]]
```
Expand Down
24 changes: 12 additions & 12 deletions docs/pim/product_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@ description: Use PHP API to manage products in PIM, their attributes, availabili
Get an individual product by using the `productService::getProduct()` method:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 62, 65) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 68, 71) =]]
```

Find multiple products with `productService::findProducts()`.
Provide the method with optional filter, query or Sort Clauses.

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 66, 76) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 72, 82) =]]
```

### Modifying products

To create, update and delete products, use the `LocalProductServiceInterface`.

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 87, 91) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 93, 97) =]]
```

To create a product, use `LocalProductService::newProductCreateStruct()`.
Provide the method with the product type object and the main language code.
You also need to set (at least) the code for the product and the required Field of the underlying content type, `name`:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 77, 84) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 83, 90) =]]
```

To delete a product, use `LocalProductService::deleteProduct()`:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 114, 115) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 120, 121) =]]
```

### Product variants
Expand Down Expand Up @@ -125,15 +125,15 @@ Get the availability object with `getAvailability()`.
You can then use `ProductAvailabilityServiceInterface::getStock` to get the stock number for the product:

```php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 98, 103) =]] }
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 104, 109) =]] }
```

To change availability for a product, use `updateProductAvailability()` with a `ProductAvailabilityUpdateStruct`
and provide it with the product object. The second parameter defines whether product is available,
and the third whether its stock is infinite. The fourth parameter is the stock number:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 106, 109) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 112, 115) =]]
```

## Attributes
Expand All @@ -145,27 +145,27 @@ or `LocalAttributeGroupServiceInterface` to modify attribute groups.
`AttributeGroupServiceInterface::findAttributeGroups()` get all attribute groups, base on optional query:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 64, 65) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 85, 90) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 71, 72) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 92, 97) =]]
```

To create an attribute group, use `LocalAttributeGroupServiceinterface::createAttributeGroup`
and provide it with an `AttributeGroupCreateStruct`:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 59, 63) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 66, 70) =]]
```

To get information about product attributes, use the `AttributeDefinitionServiceInterface`,
or `LocalAttributeDefinitionServiceInterface` to modify attributes.

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 71, 73) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 78, 80) =]]
```

To create an attribute, use `LocalAttributeGroupServiceinterface::createAttributeDefinition`
and provide it with an `AttributeDefinitionCreateStruct`:

``` php
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 76, 82) =]]
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 83, 89) =]]
```
Loading
Loading