Skip to content

Commit e89bae1

Browse files
authored
Code samples usage (#2438)
1 parent 086be4b commit e89bae1

File tree

7 files changed

+405
-18
lines changed

7 files changed

+405
-18
lines changed

.github/workflows/build.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,100 @@ jobs:
104104

105105
- name: Run PHPStan analysis
106106
run: composer phpstan
107+
108+
code-samples-inclusion-check:
109+
name: Check code samples inclusion
110+
runs-on: ubuntu-latest
111+
if: github.event_name == 'pull_request'
112+
permissions:
113+
# Needed to manage the comment
114+
pull-requests: write
115+
116+
steps:
117+
- name: List modified files
118+
id: list
119+
run: |
120+
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files"
121+
echo 'CODE_SAMPLES_CHANGE<<CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
122+
curl -s -X GET -G $URL | jq -r '.[] | .filename,.previous_filename' | grep '^code_samples/' | tr '\n' ' ' >> "$GITHUB_OUTPUT"
123+
echo '' >> "$GITHUB_OUTPUT"
124+
echo 'CODE_SAMPLES_CHANGE_DELIMITER' >> "$GITHUB_OUTPUT"
125+
126+
- name: Checkout target branch (base_ref)
127+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
128+
uses: actions/checkout@v3
129+
with:
130+
ref: ${{ github.base_ref }}
131+
- name: Log target branch code_samples usage
132+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
133+
run: |
134+
git fetch origin
135+
git checkout origin/${{ github.head_ref }} -- tools/code_samples/code_samples_usage.php
136+
php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_target.txt
137+
138+
- name: Checkout source branch (head_ref)
139+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
140+
uses: actions/checkout@v3
141+
with:
142+
ref: ${{ github.head_ref }}
143+
- name: Log source branch code_samples usage
144+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
145+
run: php tools/code_samples/code_samples_usage.php ${{ steps.list.outputs.CODE_SAMPLES_CHANGE }} > $HOME/code_samples_usage_source.txt
146+
147+
- name: Compare code_samples usages (diff --unified)
148+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
149+
# diff returns 1 if there is a difference, this is normal but seen as an error by the job.
150+
continue-on-error: true
151+
run: |
152+
source_length=`wc -l < $HOME/code_samples_usage_source.txt`
153+
target_length=`wc -l < $HOME/code_samples_usage_target.txt`
154+
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
155+
- name: Check for differences
156+
id: diff
157+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != ''
158+
run: |
159+
echo "CODE_SAMPLES_DIFF=$(wc -l < $HOME/code_samples_usage.diff | xargs)" >> "$GITHUB_OUTPUT"
160+
- name: Convert code_samples usages differences (diff2html)
161+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
162+
run: |
163+
npm install -g diff2html-cli
164+
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
165+
- name: Upload code_samples usages differences artifact
166+
id: artifact
167+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
168+
uses: actions/upload-artifact@v4
169+
with:
170+
name: code_samples_usage.diff.html
171+
path: ~/code_samples_usage.diff.html
172+
overwrite: true
173+
- name: Convert code_samples usages for comment
174+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
175+
run: |
176+
echo '# code_samples/ change report' >> code_samples_usage.diff.md
177+
echo '' >> code_samples_usage.diff.md
178+
php tools/code_samples/code_samples_usage_diff2html.php $HOME/code_samples_usage.diff >> code_samples_usage.diff.md
179+
echo '<a href="${{ steps.artifact.outputs.artifact-url }}">Download colorized diff</a>' >> code_samples_usage.diff.md
180+
- name: Find Comment
181+
id: find-comment
182+
uses: peter-evans/find-comment@v3
183+
with:
184+
issue-number: ${{ github.event.pull_request.number }}
185+
comment-author: 'github-actions[bot]'
186+
body-includes: 'code_samples/ change report'
187+
- name: Delete comment
188+
if: steps.find-comment.outputs.comment-id != ''
189+
uses: actions/github-script@v6
190+
with:
191+
script: |
192+
github.rest.issues.deleteComment({
193+
owner: context.repo.owner,
194+
repo: context.repo.repo,
195+
comment_id: ${{ steps.find-comment.outputs.comment-id }}
196+
})
197+
- name: Create comment
198+
if: steps.list.outputs.CODE_SAMPLES_CHANGE != '' && steps.diff.outputs.CODE_SAMPLES_DIFF != '0'
199+
uses: peter-evans/create-or-update-comment@v4
200+
with:
201+
issue-number: ${{ github.event.pull_request.number }}
202+
body-path: code_samples_usage.diff.md
203+
edit-mode: replace

code_samples/api/product_catalog/src/Command/AttributeCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ final class AttributeCommand extends Command
2929

3030
private PermissionResolver $permissionResolver;
3131

32-
public function __construct(LocalAttributeDefinitionServiceInterface $localAttributeDefinitionService, AttributeDefinitionServiceInterface $attributeDefinitionService, AttributeGroupServiceInterface $attributeGroupService, LocalAttributeGroupServiceInterface $localAttributeGroupService, AttributeTypeServiceInterface $attributeTypeService, UserService $userService, PermissionResolver $permissionResolver)
33-
{
32+
public function __construct(
33+
LocalAttributeDefinitionServiceInterface $localAttributeDefinitionService,
34+
AttributeDefinitionServiceInterface $attributeDefinitionService,
35+
AttributeGroupServiceInterface $attributeGroupService,
36+
LocalAttributeGroupServiceInterface $localAttributeGroupService,
37+
AttributeTypeServiceInterface $attributeTypeService,
38+
UserService $userService,
39+
PermissionResolver $permissionResolver
40+
) {
3441
$this->localAttributeGroupService = $localAttributeGroupService;
3542
$this->attributeGroupService = $attributeGroupService;
3643
$this->attributeTypeService = $attributeTypeService;

code_samples/api/product_catalog/src/Command/ProductCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ final class ProductCommand extends Command
3232

3333
private ProductAvailabilityServiceInterface $productAvailabilityService;
3434

35-
public function __construct(UserService $userService, PermissionResolver $permissionResolver, ProductTypeServiceInterface $productTypeService, ProductServiceInterface $productService, LocalProductServiceInterface $localProductService, ProductAvailabilityServiceInterface $productAvailabilityService)
36-
{
35+
public function __construct(
36+
UserService $userService,
37+
PermissionResolver $permissionResolver,
38+
ProductTypeServiceInterface $productTypeService,
39+
ProductServiceInterface $productService,
40+
LocalProductServiceInterface $localProductService,
41+
ProductAvailabilityServiceInterface $productAvailabilityService
42+
) {
3743
$this->userService = $userService;
3844
$this->permissionResolver = $permissionResolver;
3945
$this->productService = $productService;

docs/pim/price_api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ To load price definitions that match given criteria, use `ProductPriceServiceInt
4848

4949
``` php
5050
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 12, 16) =]]
51-
5251
// ...
5352
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 100, 110) =]]
5453
```
@@ -81,7 +80,6 @@ To resolve a price of a product in the currency for the current context, use eit
8180

8281
``` php
8382
[[= 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) =]]
84-
8583
// ...
8684
[[= include_file('code_samples/api/product_catalog/src/Command/ProductPriceCommand.php', 111, 115) =]]
8785
```

docs/pim/product_api.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,36 @@ description: Use PHP API to manage products in PIM, their attributes, availabili
2020
Get an individual product by using the `productService::getProduct()` method:
2121

2222
``` php
23-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 62, 65) =]]
23+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 68, 71) =]]
2424
```
2525

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

2929
``` php
30-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 66, 76) =]]
30+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 72, 82) =]]
3131
```
3232

3333
### Modifying products
3434

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

3737
``` php
38-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 87, 91) =]]
38+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 93, 97) =]]
3939
```
4040

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

4545
``` php
46-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 77, 84) =]]
46+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 83, 90) =]]
4747
```
4848

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

5151
``` php
52-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 114, 115) =]]
52+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 120, 121) =]]
5353
```
5454

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

127127
```php
128-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 98, 103) =]] }
128+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 104, 109) =]] }
129129
```
130130

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

135135
``` php
136-
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 106, 109) =]]
136+
[[= include_file('code_samples/api/product_catalog/src/Command/ProductCommand.php', 112, 115) =]]
137137
```
138138

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

147147
``` php
148-
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 64, 65) =]]
149-
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 85, 90) =]]
148+
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 71, 72) =]]
149+
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 92, 97) =]]
150150
```
151151

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

155155
``` php
156-
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 59, 63) =]]
156+
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 66, 70) =]]
157157
```
158158

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

162162
``` php
163-
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 71, 73) =]]
163+
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 78, 80) =]]
164164
```
165165

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

169169
``` php
170-
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 76, 82) =]]
170+
[[= include_file('code_samples/api/product_catalog/src/Command/AttributeCommand.php', 83, 89) =]]
171171
```

0 commit comments

Comments
 (0)