Skip to content

Commit

Permalink
ITKDev: Added code style checking
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 25, 2024
1 parent b061683 commit ea939a2
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 7 deletions.
137 changes: 137 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
on: pull_request
name: Review
jobs:
changelog:
runs-on: ubuntu-latest
name: Changelog should be updated
strategy:
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Git fetch
run: git fetch

- name: Check that changelog has been updated.
run: git diff --exit-code origin/${{ github.base_ref }} -- CHANGELOG.md && exit 1 || exit 0

test-composer-files:
name: Validate composer
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Validate composer files
run: |
composer validate --strict composer.json
# Check that dependencies resolve.
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
php-coding-standards:
name: PHP coding standards
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: |
composer install --no-interaction --no-progress
- name: PHPCS
run: |
composer coding-standards-check/phpcs
php-code-analysis:
name: PHP code analysis
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1' ]
steps:
- uses: actions/checkout@master
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json
coverage: none
tools: composer:v2
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Code analysis
run: |
./scripts/code-analysis
markdownlint:
runs-on: ubuntu-latest
name: markdownlint
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Cache yarn packages
uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Yarn install
uses: actions/setup-node@v2
with:
node-version: '20'
- run: yarn install
- name: markdownlint
run: yarn coding-standards-check/markdownlint
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor/
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- markdownlint-disable MD024 -->
# OS2 Audit Change Log

All notable changes to this project should be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

See ["how do I make a good changelog record?"](https://keepachangelog.com/en/1.0.0/#how)
before starting to add changes. Use example [placed in the end of the page](#example-of-change-log-record)

## [Unreleased]

- First version of the module

[Unreleased]: https://github.com/OS2web/os2web_audit/compare/develop...HEAD
62 changes: 62 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "os2web/os2web_audit",
"type": "drupal-module",
"description": "Drupal OS2 module that provides audit logging for Danish Municipalities",
"minimum-stability": "dev",
"prefer-stable": true,
"license": "EUPL-1.2",
"repositories": {
"drupal": {
"type": "composer",
"url": "https://packages.drupal.org/8"
},
"assets": {
"type": "composer",
"url": "https://asset-packagist.org"
}
},
"require": {
"php": "^8.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"drupal/coder": "^8.3",
"mglaman/phpstan-drupal": "^1.1",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpunit/phpunit": "^9.5"
},
"extra" : {
"composer-exit-on-patch-failure": false,
"enable-patching" : true,
"patches": {
}
},
"scripts": {
"code-analysis/phpstan": [
"phpstan analyse"
],
"code-analysis": [
"@code-analysis/phpstan"
],
"coding-standards-check/phpcs": [
"phpcs --standard=phpcs.xml.dist"
],
"coding-standards-check": [
"@coding-standards-check/phpcs"
],
"coding-standards-apply/phpcs": [
"phpcbf --standard=phpcs.xml.dist"
],
"coding-standards-apply": [
"@coding-standards-apply/phpcs"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"license": "UNLICENSED",
"private": true,
"devDependencies": {
"markdownlint-cli": "^0.32.2"
},
"scripts": {
"coding-standards-check/markdownlint": "yarn markdownlint --ignore LICENSE.md --ignore vendor --ignore node_modules '*.md' 'modules/os2forms_digital_post/**/*.md'",
"coding-standards-check": "yarn coding-standards-check/markdownlint",
"coding-standards-apply/markdownlint": "yarn markdownlint --ignore LICENSE.md --ignore vendor --ignore node_modules '*.md' 'modules/os2forms_digital_post/**/*.md' --fix",
"coding-standards-apply": "yarn coding-standards-apply/markdownlint"
}
}
27 changes: 27 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="PHP_CodeSniffer">
<description>OS2Forms PHP Code Sniffer configuration</description>

<file>.</file>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>node_modules/</exclude-pattern>
<!-- Exclude our copy of the abandoned https://www.drupal.org/project/webform_embed -->
<exclude-pattern>modules/webform_embed/</exclude-pattern>
<!-- Exclude our copy of the abandoned https://www.drupal.org/project/field_color -->
<exclude-pattern>modules/os2forms_webform_maps/modules/field_color/</exclude-pattern>

<!-- Show progress of the run -->
<arg value="p"/>

<arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,yml"/>
<config name="drupal_core_version" value="9"/>


<rule ref="Drupal">
<!-- We want to be able to use "package" and "version" in our custom modules -->
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Project"/>
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Version"/>
</rule>

<rule ref="DrupalPractice"/>
</ruleset>
20 changes: 20 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
level: 6
paths:
- modules/os2forms_digital_post/
excludePaths:
# @see https://github.com/mglaman/drupal-check/issues/261#issuecomment-1030141772/
- vendor
- '*/node_modules/*'
ignoreErrors:
-
# Ignore some weird errors reported by PHPStan
# @todo Investigate further
messages:
# These errors may be related to classes that don't add anything to their base class (e.g. `class BrugerFlerRelationType extends FlerRelationType {}`)
- '#expects (DataGovDk\\Model\\Core\\)(.+)(\|null)?, \1\2\\\2AType given.#'
- '#expects (DigitalPost\\MeMo\\)(.+)(\|null)?, \1\2\\\2AType given.#'
- '#expects (Oio\\(?:.+\\)*)(.+)(\|null)?, \1\2Type given.#'
- '#expects array<(DigitalPost\\MeMo\\)(.+)>(\|null)?, array<int, \1\2\\\2AType> given.#'
- '#should return (DigitalPost\\MeMo\\)(.+) but returns \1\2\\\2AType.#'
- '#should return (Oio\\Fjernprint\\)(.+) but returns \1\2Type.#'
35 changes: 35 additions & 0 deletions scripts/code-analysis
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
script_dir=$(pwd)
module_name=$(basename "$script_dir")
drupal_dir=vendor/drupal-module-code-analysis
# Relative to $drupal_dir
module_path=web/modules/contrib/$module_name

cd "$script_dir" || exit

drupal_composer() {
composer --working-dir="$drupal_dir" --no-interaction "$@"
}

# Create new Drupal 9 project
if [ ! -f "$drupal_dir/composer.json" ]; then
composer --no-interaction create-project drupal/recommended-project:^9 "$drupal_dir"
fi
# Copy our code into the modules folder
mkdir -p "$drupal_dir/$module_path"
# https://stackoverflow.com/a/15373763
rsync --archive --compress . --filter=':- .gitignore' --exclude "$drupal_dir" --exclude .git "$drupal_dir/$module_path"

drupal_composer config minimum-stability dev

# Allow ALL plugins
# https://getcomposer.org/doc/06-config.md#allow-plugins
drupal_composer config --no-plugins allow-plugins true

drupal_composer require wikimedia/composer-merge-plugin
drupal_composer config extra.merge-plugin.include "$module_path/composer.json"
# https://www.drupal.org/project/drupal/issues/3220043#comment-14845434
drupal_composer require --dev symfony/phpunit-bridge

# Run PHPStan
(cd "$drupal_dir" && vendor/bin/phpstan --configuration="$module_path/phpstan.neon")
15 changes: 8 additions & 7 deletions src/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\os2web_audit\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\os2web_audit\Form\PluginSettingsForm;
use Drupal\os2web_audit\Form\SettingsForm;
use Drupal\os2web_audit\Plugin\LoggerManager;
Expand All @@ -17,6 +18,7 @@ class Logger {
public function __construct(
private readonly LoggerManager $loggerManager,
private readonly ConfigFactoryInterface $configFactory,
private readonly AccountProxyInterface $currentUser,
) {
}

Expand All @@ -37,7 +39,7 @@ public function __construct(
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function info(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void {
public function info(string $type, int $timestamp, string $line, bool $logUser = FALSE, array $metadata = []): void {
$this->log($type, $timestamp, $line, $logUser, $metadata + ['level' => 'info']);
}

Expand All @@ -58,7 +60,7 @@ public function info(string $type, int $timestamp, string $line, bool $logUser =
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function error(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void {
public function error(string $type, int $timestamp, string $line, bool $logUser = FALSE, array $metadata = []): void {
$this->log($type, $timestamp, $line, $logUser, $metadata + ['level' => 'error']);
}

Expand All @@ -79,19 +81,18 @@ public function error(string $type, int $timestamp, string $line, bool $logUser
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
private function log(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void {
private function log(string $type, int $timestamp, string $line, bool $logUser = FALSE, array $metadata = []): void {
$config = $this->configFactory->get(SettingsForm::$configName);
$plugin_id = $config->get('provider');

// @todo: default logger (file)
// @todo: Fallback logger on error.
// @todo default logger (file)
// @todo Fallback logger on error.
$configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id);
$logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []);

if ($logUser) {
// Add user id to the log message metadata.
$user = \Drupal::currentUser();
$metadata['userId'] = $user->id();
$metadata['userId'] = $this->currentUser->id();
}

$logger->log($type, $timestamp, $line, $metadata);
Expand Down

0 comments on commit ea939a2

Please sign in to comment.