Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
106 changes: 106 additions & 0 deletions .git-hooks-matomo/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash

# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local oid> <remote ref> <remote oid>



### Check we're running in the context of a plugin and get helpful dir variables ###

REPO_DIR="$(git rev-parse --show-toplevel)"
echo "Running pre-commit hook in repo: $REPO_DIR"

if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then
PLUGIN_PATH="plugins/${BASH_REMATCH[1]}/"
else
echo "Not a plugin, not running any further checks"
exit 1
fi
MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||')



### Figure out how to run PHPStan - ddev or not. ###

COMMAND=""
# Use local PHP if setup
if command -v php >/dev/null 2>&1; then
if [ -f "${MATOMO_DIR}/vendor/bin/phpstan" ]; then
COMMAND="${MATOMO_DIR}/vendor/bin/phpstan"
PLUGIN_PATH=''
fi
# Use ddev if setup (overridding local setup)
elif command -v ddev >/dev/null 2>&1; then
if [ -d "$MATOMO_DIR/.ddev" ]; then
cd "$MATOMO_DIR" || exit 1
if ddev status 2>&1 > /dev/null; then
COMMAND="ddev exec phpstan"
fi
fi
fi
# If no command, exit
if [[ -z "$COMMAND" ]]; then
echo "No way to run phpstan found."
exit 1
fi



# Basic setup
cd "$REPO_DIR"
STATUS=0




### Run PHPStan on newly created files. ###

PHPSTAN_CREATED_CONFIG=phpstan/phpstan.created.neon
MAIN_BRANCH='5.x-dev'
if [[ -f "$PHPSTAN_CREATED_CONFIG" ]]; then
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=A | grep '\.php$' || true)
if [ -z "$CHANGED_FILES" ]; then
echo "No created PHP files"
else
echo "Running PHPstan at a very high level on new files"
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_CREATED_CONFIG} || STATUS=1
fi
fi



### Run PHPStan on modified files. ###
PHPSTAN_MODIFIED_CONFIG=phpstan/phpstan.modified.neon
if [[ -f "$PHPSTAN_MODIFIED_CONFIG" ]]; then
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=CM | grep '\.php$' || true)
if [ -z "$CHANGED_FILES" ]; then
echo "No changed PHP files"
else
echo "Running PHPstan on modified files"
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_MODIFIED_CONFIG} || STATUS=1
fi
fi

# Don't bother running the full check, as we check changes files already, and
# can assume that the unchanged files don't need rechecking.
#
# Github will check this anyway.
#
# PHPSTAN_BASE_CONFIG=phpstan.neon
# if [[ -f "$PHPSTAN_BASE_CONFIG" ]]; then
# echo "Running PHPstan at a base level on all plugin files"
# $COMMAND analyse -c ${PLUGIN_PATH}/${PHPSTAN_BASE_CONFIG} || STATUS=1
# fi

exit $STATUS
58 changes: 58 additions & 0 deletions .github/workflows/matomo-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Action for running tests
# This file has been automatically created.
# To recreate it you can run this command
# ./console generate:test-action --plugin="OpenApiDocs" --php-versions="7.2,8.4" --schedule-cron="0 19 * * 6"

name: Plugin OpenApiDocs Tests

on:
pull_request:
types: [opened, synchronize]
push:
branches:
- '**.x-dev'
workflow_dispatch:
schedule:
- cron: "0 19 * * 6"

permissions:
actions: read
checks: none
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: none

concurrency:
group: php-${{ github.ref }}
cancel-in-progress: true

jobs:
PluginTests:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
php: [ '7.2', '8.4' ]
target: ['minimum_required_matomo', 'maximum_supported_matomo']
steps:
- uses: actions/checkout@v3
with:
lfs: true
persist-credentials: false
- name: Install package ripgrep
run: sudo apt-get install ripgrep
- name: Run tests
uses: matomo-org/github-action-tests@main
with:
plugin-name: 'OpenApiDocs'
php-version: ${{ matrix.php }}
test-type: 'PluginTests'
matomo-test-branch: ${{ matrix.target }}
redis-service: true
artifacts-pass: ${{ secrets.ARTIFACTS_PASS }}
upload-artifacts: ${{ matrix.php == '7.2' && matrix.target == 'maximum_supported_matomo' }}
43 changes: 43 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPCS check

on: pull_request

permissions:
actions: read
checks: read
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: read

jobs:
phpcs:
name: PHPCS
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
lfs: false
persist-credentials: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: cs2pr
- name: Install dependencies
run:
composer init --name=matomo/openapidocs --quiet;
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
composer require matomo-org/matomo-coding-standards:dev-master;
composer install --dev --prefer-dist --no-progress --no-suggest
- name: Check PHP code styles
id: phpcs
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml --prepend-filename
81 changes: 81 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: PHPStan check

on: pull_request

permissions:
actions: read
checks: read
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: read

env:
PLUGIN_NAME: OpenApiDocs

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: false
persist-credentials: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'

- name: Check out github-action-tests repository
uses: actions/checkout@v4
with:
repository: matomo-org/github-action-tests
ref: main
path: github-action-tests

- name: checkout matomo for plugin builds
shell: bash
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_matomo.sh
env:
PLUGIN_NAME: ${{ env.PLUGIN_NAME }}
WORKSPACE: ${{ github.workspace }}
ACTION_PATH: ${{ github.workspace }}/github-action-tests
MATOMO_TEST_TARGET: maximum_supported_matomo

- name: prepare setup
shell: bash
run: |
cd ${{ github.workspace }}/matomo
echo -e "composer install"
composer install --ignore-platform-reqs
- name: checkout additional plugins
if: ${{ env.DEPENDENT_PLUGINS != '' }}
shell: bash
working-directory: ${{ github.workspace }}/matomo
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_dependent_plugins.sh

env:
GITHUB_USER_TOKEN: ${{ secrets.TESTS_ACCESS_TOKEN || secrets.GITHUB_TOKEN }}

- name: "Restore result cache"
uses: actions/cache/restore@v4
with:
path: /tmp/phpstan # same as in phpstan.neon
key: "phpstan-result-cache-${{ github.run_id }}"
restore-keys: |
phpstan-result-cache-
- name: PHPStan whole repo
id: phpstan-all
run: cd ${{ github.workspace }}/matomo && composer run phpstan -- -vvv -c plugins/${{ env.PLUGIN_NAME }}/phpstan.neon

- name: "Save result cache"
uses: actions/cache/save@v4
if: ${{ !cancelled() }}
with:
path: /tmp/phpstan # same as in phpstan.neon
key: "phpstan-result-cache-${{ github.run_id }}"
16 changes: 11 additions & 5 deletions Annotations/AnnotationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use function _PHPStan_3d4486d07\RingCentral\Psr7\str;

class AnnotationGenerator
{
Expand Down Expand Up @@ -123,8 +122,10 @@ protected function buildAnnotationForMethod(array $rules, string $pluginName, \R
{
$existing = $reflectionMethod->getDocComment();
// Skip methods which have been marked as internal or auto annotations disabled
if ($existing !== false && (stripos($existing, 'OA-AUTO:OFF') !== false
|| stripos($existing, '@internal') !== false)) {
if (
$existing !== false && (stripos($existing, 'OA-AUTO:OFF') !== false
|| stripos($existing, '@internal') !== false)
) {
return [];
}

Expand All @@ -145,7 +146,8 @@ protected function buildAnnotationForMethod(array $rules, string $pluginName, \R
return $this->compileOperationLines($path, $opId, $pluginName, $methodName, $params, $responses, $isPost);
}

protected function getParamInfoFromDocBlock(string $docBlock): array {
protected function getParamInfoFromDocBlock(string $docBlock): array
{
$lexer = new Lexer();
$tokens = $lexer->tokenize($docBlock);
$expressionParser = new ConstExprParser();
Expand Down Expand Up @@ -241,14 +243,18 @@ protected function determineParameters(array $rules, string $plugin, string $met
* @param string $type The PHP type from the method signature or doc-block
* @return string The normalised Data Type to be used in the swagger-php annotation
*/
protected function getOpenApiTypeFromPhpType(string $type): string
public function getOpenApiTypeFromPhpType(string $type): string
{
// TODO - Is there a good way to handle object type or should that always be ref?
// TODO - Eventually handle the Data Type Formats: https://spec.openapis.org/oas/v3.1.1.html#data-type-format
switch (strtolower($type)) {
case 'array':
case '[]':
case 'int[]':
case 'string[]':
case 'bool[]':
case 'float[]':
case 'double[]':
$type = 'array';
break;
case 'int':
Expand Down
3 changes: 1 addition & 2 deletions Annotations/GlobalApiComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,4 @@
*/
class GlobalApiComponents
{

}
}
3 changes: 1 addition & 2 deletions Renderer/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Piwik\API\ApiRenderer;
use Piwik\Common;
use Piwik\DataTable\Renderer;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;

class Yaml extends ApiRenderer
Expand Down Expand Up @@ -45,4 +44,4 @@ public function renderDataTable($dataTable)
{
return $dataTable->getFirstRow()->getColumn(0);
}
}
}
2 changes: 1 addition & 1 deletion Specs/SpecGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SpecGenerator
public function __construct()
{
// Set the constant for the current instance's URL
if(!defined('LOCAL_MATOMO_SERVER_URL')) {
if (!defined('LOCAL_MATOMO_SERVER_URL')) {
define('LOCAL_MATOMO_SERVER_URL', SettingsPiwik::getPiwikUrl());
}
}
Expand Down
Loading