From 6802bee0dbfcb9baeecb18489bc969c004e6050a Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 20 May 2021 11:02:08 +0000
Subject: [PATCH] ci(gitlabci): add easy-coding-standard check for code-style +
 fix quality scripts

---
 .gitlab-ci.yml                            | 67 ++++++++++-------------
 .releaserc.json                           |  2 +-
 Dockerfile                                |  4 +-
 app/Views/install/instance_config.php     |  2 +-
 app/Views/pager/default_full.php          |  5 +-
 scripts/bundle-prepare.sh                 | 12 ++++
 scripts/{prepare-release.sh => bundle.sh} | 11 ++--
 scripts/package.sh                        |  7 +++
 8 files changed, 59 insertions(+), 51 deletions(-)
 create mode 100644 scripts/bundle-prepare.sh
 rename scripts/{prepare-release.sh => bundle.sh} (85%)
 create mode 100644 scripts/package.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 62cc6540dc..75d7fcd6b9 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -13,74 +13,62 @@ cache:
 before_script:
   - apt-get update -y
 
-  # Install git (the php image doesn't have it) which is required by composer
+  # Install git which is required by composer (the php image doesn't have it)
   - apt-get install git -y
 
   - apt-get install -y libicu-dev
 
-  # Install intl PHP extension
+  # Install intl PHP extension for tests
   - docker-php-ext-install intl
 
-  # install rsync for file transfers
-  - apt-get install rsync -y
-
-  # install wget to download archives
-  - apt-get install wget
-
   # Install composer
-  - apt-get install zip unzip
   - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
   - php composer-setup.php
   - php -r "unlink('composer-setup.php');"
 
   # Install NodeJS for NPM
-  - curl -sL https://deb.nodesource.com/setup_12.x | bash -
+  - curl -sL https://deb.nodesource.com/setup_lts.x | bash -
   - apt-get update && apt-get install -y nodejs
 
-  # Install all php and js dependencies
+  # Install all php dependencies
   - php composer.phar install --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs
-  - npm install
 
 tests:
   stage: quality
   script:
-    - composer test
+    # run phpunit without code coverage
+    # TODO: add code coverage
+    - vendor/bin/phpunit --no-coverage
+
+code-style:
+  stage: quality
+  script:
+    - vendor/bin/ecs check --ansi
 
-analysis:
+static-analysis:
   stage: quality
   script:
-    # run phpstan
-    - composer analyse
+    - vendor/bin/phpstan analyse --ansi
 
 code-review:
   stage: quality
   script:
-    - composer rector
+    - vendor/bin/rector process --dry-run --ansi
 
 bundle_app:
   stage: bundle
   script:
-    # delete installed git hooks because captainhooks (dev dependency) will be removed
-    - rm -rf ./.git/hooks
-
-    # remove dev dependencies using the --no-dev option
-    - php composer.phar install --no-dev --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs
-
-    # build all UI assets
-    - npm run build
-
-    # download GeoLite2-City archive and extract it to writable/uploads
-    - wget -c "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=$MAXMIND_LICENCE_KEY&suffix=tar.gz" -O - | tar -xz -C ./writable/uploads/
-
-    # rename extracted archives' folders
-    - mv ./writable/uploads/GeoLite2-City* ./writable/uploads/GeoLite2-City
+    # make scripts/bundle.sh executable
+    - chmod +x ./scripts/bundle-prepare.sh
+    - chmod +x ./scripts/bundle.sh
 
-    # create bundle folder: uses .rsync-filter (-F) file to copy only needed files
-    - rsync -aF --progress . ./castopod-host
+    # bundle castopod-host with commit ref as version
+    - ./scripts/bundle-prepare.sh
+    - ./scripts/bundle.sh ${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}
   artifacts:
     name: "castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
     paths:
-      - castopod-host
+      - castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}
   except:
     - main
     - beta
@@ -89,12 +77,13 @@ bundle_app:
 release_app:
   stage: release
   script:
-    # install required packages for prepare-release.sh script
-    - apt-get install jq -y
-    - apt-get install zip -y
+    # make release scripts executable
+    - chmod +x ./scripts/bundle-prepare.sh
+    - chmod +x ./scripts/bundle.sh
+    - chmod +x ./scripts/package.sh
 
-    # make scripts/prepare-release.sh executable
-    - chmod +x ./scripts/prepare-release.sh
+    # prepare bundle before release, both bundle and package scripts will be run by semantic-release
+    - ./scripts/bundle-prepare.sh
 
     # IMPORTANT: delete local git tags before release to prevent eventual script failure (ie. tag already exists)
     - git tag | xargs git tag -d
diff --git a/.releaserc.json b/.releaserc.json
index 018006c294..b355dac248 100644
--- a/.releaserc.json
+++ b/.releaserc.json
@@ -11,7 +11,7 @@
     [
       "@semantic-release/exec",
       {
-        "prepareCmd": "./scripts/prepare-release.sh ${nextRelease.version}"
+        "prepareCmd": "./scripts/bundle.sh ${nextRelease.version} && ./scripts/package.sh"
       }
     ],
     "@semantic-release/npm",
diff --git a/Dockerfile b/Dockerfile
index 44e3f9a5c2..8d87e30d72 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,7 +7,7 @@
 
 FROM php:8.0-fpm
 
-LABEL maintainer="Yassine Doghri<yassine@podlibre.org>"
+LABEL maintainer="Yassine Doghri <yassine@doghri.fr>"
 
 COPY . /castopod-host
 WORKDIR /castopod-host
@@ -38,7 +38,7 @@ RUN apt-get update && apt-get install -y \
     && docker-php-ext-install intl
 
 RUN docker-php-ext-configure gd --with-jpeg \
-    && docker-php-ext-install gd    
+    && docker-php-ext-install gd
 
 RUN pecl install -o -f redis \
     &&  rm -rf /tmp/pear \
diff --git a/app/Views/install/instance_config.php b/app/Views/install/instance_config.php
index 4b99ff8d2e..18e3e63ee2 100644
--- a/app/Views/install/instance_config.php
+++ b/app/Views/install/instance_config.php
@@ -17,7 +17,7 @@
     'class' => 'form-input mb-4',
     'value' => old(
         'hostname',
-        host_url( === null) ? config('App')->baseURL : host_url(),
+        host_url() === null ? config('App')->baseURL : host_url(),
     ),
     'required' => 'required',
 ]) ?>
diff --git a/app/Views/pager/default_full.php b/app/Views/pager/default_full.php
index 7c4c64cb24..2f002792c0 100644
--- a/app/Views/pager/default_full.php
+++ b/app/Views/pager/default_full.php
@@ -1,11 +1,8 @@
 <?php
 
 use CodeIgniter\Pager\PagerRenderer;
+
 /**
- * @copyright  2020 Podlibre
- * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
- * @link       https://castopod.org/
- *
  * @var PagerRenderer $pager
  */
 $pager->setSurroundCount(2);
diff --git a/scripts/bundle-prepare.sh b/scripts/bundle-prepare.sh
new file mode 100644
index 0000000000..71042a3e81
--- /dev/null
+++ b/scripts/bundle-prepare.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# delete possibly installed git hooks because captainhooks (dev dependency)
+# isn't included in the production dependencies
+rm -rf ./.git/hooks
+
+# install only dev dependencies using the --no-dev option
+php composer.phar install --no-dev --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs
+
+# install js dependencies and build all production UI assets
+npm install
+npm run build
diff --git a/scripts/prepare-release.sh b/scripts/bundle.sh
similarity index 85%
rename from scripts/prepare-release.sh
rename to scripts/bundle.sh
index 7446be4bf4..d3e4893f9c 100644
--- a/scripts/prepare-release.sh
+++ b/scripts/bundle.sh
@@ -4,20 +4,23 @@ VERSION=$1
 COMPOSER_VERSION=$(echo "$VERSION" | perl -pe 's/(?<=[alpha|beta])\.//g')
 
 # replace composer.json version using jq
+apt-get install jq -y
 echo "$( jq '.version = "'$COMPOSER_VERSION'"' composer.json )" > composer.json
 
 # replace CP_VERSION constant in app/config/constants
 sed -i "s/^defined('CP_VERSION').*/defined('CP_VERSION') || define('CP_VERSION', '$VERSION');/" ./app/Config/Constants.php
 
+# install wget to download archives
+apt-get install wget
+
 # download GeoLite2-City archive and extract it to writable/uploads
 wget -c "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=$MAXMIND_LICENCE_KEY&suffix=tar.gz" -O - | tar -xz -C ./writable/uploads/
 
 # rename extracted archives' folders
 mv ./writable/uploads/GeoLite2-City* ./writable/uploads/GeoLite2-City
 
+# install rsync for file transfers
+apt-get install rsync -y
+
 # create castopo-host folder bundle: uses .rsync-filter (-F) file to copy only needed files
 rsync -aF --progress . ./castopod-host
-
-# create zip and tar.gz packages for release upload
-zip -r castopod-host-$VERSION.zip castopod-host
-tar -zcvf castopod-host-$VERSION.tar.gz castopod-host
diff --git a/scripts/package.sh b/scripts/package.sh
new file mode 100644
index 0000000000..d8eff83143
--- /dev/null
+++ b/scripts/package.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+apt-get install zip -y
+
+# create zip and tar.gz packages for release upload
+zip -r castopod-host-$VERSION.zip castopod-host
+tar -zcvf castopod-host-$VERSION.tar.gz castopod-host
-- 
GitLab