Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 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
107 changes: 107 additions & 0 deletions .git-hooks-matomo/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/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
fi
# Use ddev if setup (overridding local setup)
if command -v ddev >/dev/null 2>&1; then
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing issues for me since I have both php and ddev installed.

Suggested change
fi
# Use ddev if setup (overridding local setup)
if command -v ddev >/dev/null 2>&1; then
# Use ddev if setup (overridding local setup)
elif command -v ddev >/dev/null 2>&1; then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@james-hill-matomo Looks good to me this suggestion, what about you ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AltamashShaikh @snake14 I'd rather ddev was the first choice, but even more than that I'm happy to merge this as is, as it'll probably never matter anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything works for me, having ddev first should also work

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
76 changes: 76 additions & 0 deletions .github/workflows/matomo-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Action for running tests
# This file has been automatically created.
# To recreate it you can run this command
# ./console generate:test-action --plugin="Slack" --php-versions="7.2,8.4" --schedule-cron="0 5 * * 6"

name: Plugin Slack Tests

on:
pull_request:
types: [opened, synchronize]
push:
branches:
- '**.x-dev'
workflow_dispatch:
schedule:
- cron: "0 5 * * 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: 'Slack'
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' }}
UI:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
with:
lfs: true
persist-credentials: false
- name: running tests
uses: matomo-org/github-action-tests@main
with:
plugin-name: 'Slack'
matomo-test-branch: 'maximum_supported_matomo'
test-type: 'UI'
php-version: '7.2'
node-version: '16'
redis-service: true
artifacts-pass: ${{ secrets.ARTIFACTS_PASS }}
upload-artifacts: true
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/slack --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
83 changes: 83 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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: Slack

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 }}"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/tests/System/processed/
/tests/Integration/Importers/processed/
/tests/UI/processed-ui-screenshots/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Changelog

5.0.0 - 2025-09-01
- Initial release to send scheduled reports to a Slack channel
60 changes: 60 additions & 0 deletions ScheduleReportSlack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

declare(strict_types=1);

namespace Piwik\Plugins\Slack;

class ScheduleReportSlack
{
/**
* @var string
*/
private $subject;
/**
* @var string
*/
private $fileName;

/**
* @var string
*/
private $fileContents;

/**
* @var string
*/
private $channel;

/**
* @var string
*/
private $token;

public function __construct(
string $subject,
string $fileName,
string $fileContents,
string $channel,
#[\SensitiveParameter]
string $token
) {
$this->subject = $subject;
$this->fileName = $fileName;
$this->fileContents = $fileContents;
$this->channel = $channel;
$this->token = $token;
}

public function send(): bool
{
$slackApi = new SlackApi($this->token);
return $slackApi->uploadFile($this->subject, $this->fileName, $this->fileContents, $this->channel);
}
}
Loading