Skip to content

Commit 13b71a2

Browse files
Merge pull request #1 from matomo-org/PG-4491-schedule-report-slack
Initial PR to send scheduled reports to a Slack channel, #PG-4491
2 parents 87a11b1 + 37fd322 commit 13b71a2

46 files changed

Lines changed: 2001 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.git-hooks-matomo/pre-push

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
3+
# This hook is called with the following parameters:
4+
#
5+
# $1 -- Name of the remote to which the push is being done
6+
# $2 -- URL to which the push is being done
7+
#
8+
# If pushing without using a named remote those arguments will be equal.
9+
#
10+
# Information about the commits which are being pushed is supplied as lines to
11+
# the standard input in the form:
12+
#
13+
# <local ref> <local oid> <remote ref> <remote oid>
14+
15+
16+
17+
### Check we're running in the context of a plugin and get helpful dir variables ###
18+
19+
REPO_DIR="$(git rev-parse --show-toplevel)"
20+
echo "Running pre-commit hook in repo: $REPO_DIR"
21+
22+
if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then
23+
PLUGIN_PATH="plugins/${BASH_REMATCH[1]}/"
24+
else
25+
echo "Not a plugin, not running any further checks"
26+
exit 1
27+
fi
28+
MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||')
29+
30+
31+
32+
### Figure out how to run PHPStan - ddev or not. ###
33+
34+
COMMAND=""
35+
# Use local PHP if setup
36+
if command -v php >/dev/null 2>&1; then
37+
if [ -f "${MATOMO_DIR}/vendor/bin/phpstan" ]; then
38+
COMMAND="${MATOMO_DIR}/vendor/bin/phpstan"
39+
PLUGIN_PATH=''
40+
fi
41+
fi
42+
# Use ddev if setup (overridding local setup)
43+
if command -v ddev >/dev/null 2>&1; then
44+
if [ -d "$MATOMO_DIR/.ddev" ]; then
45+
cd "$MATOMO_DIR" || exit 1
46+
if ddev status 2>&1 > /dev/null; then
47+
COMMAND="ddev exec phpstan"
48+
fi
49+
fi
50+
fi
51+
# If no command, exit
52+
if [[ -z "$COMMAND" ]]; then
53+
echo "No way to run phpstan found."
54+
exit 1
55+
fi
56+
57+
58+
59+
# Basic setup
60+
cd "$REPO_DIR"
61+
STATUS=0
62+
63+
64+
65+
66+
### Run PHPStan on newly created files. ###
67+
68+
PHPSTAN_CREATED_CONFIG=phpstan/phpstan.created.neon
69+
MAIN_BRANCH='5.x-dev'
70+
if [[ -f "$PHPSTAN_CREATED_CONFIG" ]]; then
71+
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=A | grep '\.php$' || true)
72+
if [ -z "$CHANGED_FILES" ]; then
73+
echo "No created PHP files"
74+
else
75+
echo "Running PHPstan at a very high level on new files"
76+
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
77+
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_CREATED_CONFIG} || STATUS=1
78+
fi
79+
fi
80+
81+
82+
83+
### Run PHPStan on modified files. ###
84+
PHPSTAN_MODIFIED_CONFIG=phpstan/phpstan.modified.neon
85+
if [[ -f "$PHPSTAN_MODIFIED_CONFIG" ]]; then
86+
CHANGED_FILES=$(git diff --name-only ${MAIN_BRANCH} --diff-filter=CM | grep '\.php$' || true)
87+
if [ -z "$CHANGED_FILES" ]; then
88+
echo "No changed PHP files"
89+
else
90+
echo "Running PHPstan on modified files"
91+
CHANGED_FILES=`echo "$CHANGED_FILES" | sed -e 's/^\(.*\)$/"\1"/' | xargs -I{} echo "${PLUGIN_PATH}{}"`
92+
echo "$CHANGED_FILES" | xargs $COMMAND analyse -c ${PLUGIN_PATH}${PHPSTAN_MODIFIED_CONFIG} || STATUS=1
93+
fi
94+
fi
95+
96+
# Don't bother running the full check, as we check changes files already, and
97+
# can assume that the unchanged files don't need rechecking.
98+
#
99+
# Github will check this anyway.
100+
#
101+
# PHPSTAN_BASE_CONFIG=phpstan.neon
102+
# if [[ -f "$PHPSTAN_BASE_CONFIG" ]]; then
103+
# echo "Running PHPstan at a base level on all plugin files"
104+
# $COMMAND analyse -c ${PLUGIN_PATH}/${PHPSTAN_BASE_CONFIG} || STATUS=1
105+
# fi
106+
107+
exit $STATUS

.github/workflows/matomo-tests.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Action for running tests
2+
# This file has been automatically created.
3+
# To recreate it you can run this command
4+
# ./console generate:test-action --plugin="Slack" --php-versions="7.2,8.4" --schedule-cron="0 5 * * 6"
5+
6+
name: Plugin Slack Tests
7+
8+
on:
9+
pull_request:
10+
types: [opened, synchronize]
11+
push:
12+
branches:
13+
- '**.x-dev'
14+
workflow_dispatch:
15+
schedule:
16+
- cron: "0 5 * * 6"
17+
18+
permissions:
19+
actions: read
20+
checks: none
21+
contents: read
22+
deployments: none
23+
issues: read
24+
packages: none
25+
pull-requests: read
26+
repository-projects: none
27+
security-events: none
28+
statuses: none
29+
30+
concurrency:
31+
group: php-${{ github.ref }}
32+
cancel-in-progress: true
33+
34+
jobs:
35+
PluginTests:
36+
runs-on: ubuntu-24.04
37+
strategy:
38+
fail-fast: false
39+
matrix:
40+
php: [ '7.2', '8.4' ]
41+
target: ['minimum_required_matomo', 'maximum_supported_matomo']
42+
steps:
43+
- uses: actions/checkout@v3
44+
with:
45+
lfs: true
46+
persist-credentials: false
47+
- name: Install package ripgrep
48+
run: sudo apt-get install ripgrep
49+
- name: Run tests
50+
uses: matomo-org/github-action-tests@main
51+
with:
52+
plugin-name: 'Slack'
53+
php-version: ${{ matrix.php }}
54+
test-type: 'PluginTests'
55+
matomo-test-branch: ${{ matrix.target }}
56+
redis-service: true
57+
artifacts-pass: ${{ secrets.ARTIFACTS_PASS }}
58+
upload-artifacts: ${{ matrix.php == '7.2' && matrix.target == 'maximum_supported_matomo' }}
59+
UI:
60+
runs-on: ubuntu-24.04
61+
steps:
62+
- uses: actions/checkout@v3
63+
with:
64+
lfs: true
65+
persist-credentials: false
66+
- name: running tests
67+
uses: matomo-org/github-action-tests@main
68+
with:
69+
plugin-name: 'Slack'
70+
matomo-test-branch: 'maximum_supported_matomo'
71+
test-type: 'UI'
72+
php-version: '7.2'
73+
node-version: '16'
74+
redis-service: true
75+
artifacts-pass: ${{ secrets.ARTIFACTS_PASS }}
76+
upload-artifacts: true

.github/workflows/phpcs.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: PHPCS check
2+
3+
on: pull_request
4+
5+
permissions:
6+
actions: read
7+
checks: read
8+
contents: read
9+
deployments: none
10+
issues: read
11+
packages: none
12+
pull-requests: read
13+
repository-projects: none
14+
security-events: none
15+
statuses: read
16+
17+
jobs:
18+
phpcs:
19+
name: PHPCS
20+
runs-on: ubuntu-24.04
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
lfs: false
25+
persist-credentials: false
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: '7.4'
30+
tools: cs2pr
31+
- name: Install dependencies
32+
run:
33+
composer init --name=matomo/slack --quiet;
34+
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
35+
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
36+
composer require matomo-org/matomo-coding-standards:dev-master;
37+
composer install --dev --prefer-dist --no-progress --no-suggest
38+
- name: Check PHP code styles
39+
id: phpcs
40+
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
41+
- name: Show PHPCS results in PR
42+
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
43+
run: cs2pr ./phpcs-report.xml --prepend-filename

.github/workflows/phpstan.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: PHPStan check
2+
3+
on: pull_request
4+
5+
permissions:
6+
actions: read
7+
checks: read
8+
contents: read
9+
deployments: none
10+
issues: read
11+
packages: none
12+
pull-requests: read
13+
repository-projects: none
14+
security-events: none
15+
statuses: read
16+
17+
env:
18+
PLUGIN_NAME: Slack
19+
20+
jobs:
21+
phpstan:
22+
name: PHPStan
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v4
26+
with:
27+
lfs: false
28+
persist-credentials: false
29+
- name: Setup PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: '7.2'
33+
34+
- name: Check out github-action-tests repository
35+
uses: actions/checkout@v4
36+
with:
37+
repository: matomo-org/github-action-tests
38+
ref: main
39+
path: github-action-tests
40+
41+
- name: checkout matomo for plugin builds
42+
shell: bash
43+
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_matomo.sh
44+
env:
45+
PLUGIN_NAME: ${{ env.PLUGIN_NAME }}
46+
WORKSPACE: ${{ github.workspace }}
47+
ACTION_PATH: ${{ github.workspace }}/github-action-tests
48+
MATOMO_TEST_TARGET: maximum_supported_matomo
49+
50+
- name: prepare setup
51+
shell: bash
52+
run: |
53+
cd ${{ github.workspace }}/matomo
54+
echo -e "composer install"
55+
composer install --ignore-platform-reqs
56+
57+
- name: checkout additional plugins
58+
if: ${{ env.DEPENDENT_PLUGINS != '' }}
59+
shell: bash
60+
working-directory: ${{ github.workspace }}/matomo
61+
run: ${{ github.workspace }}/github-action-tests/scripts/bash/checkout_dependent_plugins.sh
62+
63+
env:
64+
GITHUB_USER_TOKEN: ${{ secrets.TESTS_ACCESS_TOKEN || secrets.GITHUB_TOKEN }}
65+
66+
- name: "Restore result cache"
67+
uses: actions/cache/restore@v4
68+
with:
69+
path: /tmp/phpstan # same as in phpstan.neon
70+
key: "phpstan-result-cache-${{ github.run_id }}"
71+
restore-keys: |
72+
phpstan-result-cache-
73+
74+
- name: PHPStan whole repo
75+
id: phpstan-all
76+
run: cd ${{ github.workspace }}/matomo && composer run phpstan -- -vvv -c plugins/${{ env.PLUGIN_NAME }}/phpstan.neon
77+
78+
- name: "Save result cache"
79+
uses: actions/cache/save@v4
80+
if: ${{ !cancelled() }}
81+
with:
82+
path: /tmp/phpstan # same as in phpstan.neon
83+
key: "phpstan-result-cache-${{ github.run_id }}"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/tests/System/processed/
2+
/tests/Integration/Importers/processed/
3+
/tests/UI/processed-ui-screenshots/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Changelog
2+
3+
5.0.0 - 2025-09-15
4+
- Initial release to send scheduled reports to a Slack channel

ScheduleReportSlack.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\Slack;
13+
14+
class ScheduleReportSlack
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $subject;
20+
/**
21+
* @var string
22+
*/
23+
private $fileName;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $fileContents;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $channel;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $token;
39+
40+
public function __construct(
41+
string $subject,
42+
string $fileName,
43+
string $fileContents,
44+
string $channel,
45+
#[\SensitiveParameter]
46+
string $token
47+
) {
48+
$this->subject = $subject;
49+
$this->fileName = $fileName;
50+
$this->fileContents = $fileContents;
51+
$this->channel = $channel;
52+
$this->token = $token;
53+
}
54+
55+
public function send(): bool
56+
{
57+
$slackApi = new SlackApi($this->token);
58+
return $slackApi->uploadFile($this->subject, $this->fileName, $this->fileContents, $this->channel);
59+
}
60+
}

0 commit comments

Comments
 (0)