Skip to content

Commit

Permalink
Add linters to CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropombeiro committed Oct 1, 2024
1 parent 2cf7efc commit 84f134c
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 77 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/publishBranchesPRs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: PR - Create and publish a Docker image
on:
pull_request:
#push:
# branches:
# branches:
# - '*'
# - '!master'

Expand All @@ -22,7 +22,7 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -36,7 +36,7 @@ jobs:
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_branch
- name: Extract metadata (tags, labels) for Docker
id: meta
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publishMaster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Master - Create and publish a Docker image
# Configures this workflow to run every time a change is pushed to the branch called `master`.
on:
push:
branches: ['master']
branches: ["master"]

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
Expand All @@ -19,7 +19,7 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publishTags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ name: Tags - Create and publish a Docker image
# Build when a tag is created
on:
push:
tags:
- '**'
tags:
- "**"

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
Expand All @@ -20,7 +20,7 @@ jobs:
permissions:
contents: read
packages: write
#
#
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/superLinter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# This workflow executes several linters on changed files based on languages used in your code base whenever
# you push a code or open a pull request.
#
# You can adjust the behavior by modifying this file.
# For more information, see:
# https://github.com/github/super-linter
name: Lint Code Base

on: # yamllint disable-line rule:truthy
push:
branches: ["master"]
pull_request:
branches: ["master"]

permissions:
contents: read

jobs:
run-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
# Full git history is needed to get a proper list of changed files within `super-linter`
fetch-depth: 0

- name: Lint Code Base
uses: github/super-linter/slim@v7
env:
DISABLE_ERRORS: true
VALIDATE_ALL_CODEBASE: false
VALIDATE_CHECKOV: false
VALIDATE_EDITORCONFIG: false
VALIDATE_JSCPD: false
VALIDATE_MARKDOWN: false
VALIDATE_PYTHON: true
VALIDATE_PYTHON_PYLINT: false
VALIDATE_YAML: false
VALIDATE_YAML_PRETTIER: false
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LINTER_RULES_PATH: /
DOCKERFILE_HADOLINT_FILE_NAME: .hadolint.yml
MARKDOWN_CONFIG_FILE: .markdown-lint.yml
YAML_LINTER_RULES: .yamllint.yml
3 changes: 3 additions & 0 deletions .hadolint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignored:
- DL3008 # Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
- DL3018 # Pin versions in apk add. Instead of `apk add <package>` use `apk add <package>=<version>`
11 changes: 11 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends: default

rules:
document-start: disable
line-length:
max: 180
level: warning
comments:
# Changed this to stop a mess between linters from Prettier (vscode) to yamllint
# - https://github.com/prettier/prettier/pull/10926
min-spaces-from-content: 1
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ RUN wget --progress=dot:giga https://download.brother.com/welcome/dlf006652/brsc
dpkg -i /tmp/brscan-skey-0.3.1-2.amd64.deb && \
rm /tmp/brscan-skey-0.3.1-2.amd64.deb

ADD files/runScanner.sh /opt/brother/runScanner.sh
ADD files/brscan-skey.config /opt/brother/scanner/brscan-skey/brscan-skey.config
COPY files/runScanner.sh /opt/brother/runScanner.sh
COPY files/brscan-skey.config /opt/brother/scanner/brscan-skey/brscan-skey.config
COPY script /opt/brother/scanner/brscan-skey/script

RUN cp /etc/lighttpd/conf-available/05-auth.conf /etc/lighttpd/conf-enabled/
Expand Down
34 changes: 17 additions & 17 deletions html/active.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?php

function isProcessRunning($processName) {
// Execute the pgrep command
$command = "pgrep $processName";
exec($command, $output, $status);
// Check if pgrep returned a status of 0, which means the process was found
if ($status === 0) {
// Process is running
return true;
} else {
// Process is not running
return false;
}
// Execute the pgrep command
$command = "pgrep $processName";
exec($command, $output, $status);

// Check if pgrep returned a status of 0, which means the process was found
if ($status === 0) {
// Process is running
return true;
} else {
// Process is not running
return false;
}
}

// Check if the scanimage, sleep, and curl processes are running
$result = array(
'scan' => isProcessRunning('scanimage'),
'waiting' => isProcessRunning('sleep'),
'ocr' => isProcessRunning('curl')
'scan' => isProcessRunning('scanimage'),
'waiting' => isProcessRunning('sleep'),
'ocr' => isProcessRunning('curl')
);


// Output the result as JSON
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);

?>
?>
38 changes: 20 additions & 18 deletions html/download.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if(array_key_exists("file", $_GET)) {
$file = $_GET["file"];
if(str_contains($file, "..") || str_contains($file, "/")) {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Dont't be evil!");
}
$filename="/scans/".$file;
if(file_exists($filename)) {
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=\"$file\"");
readfile($filename);
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: File does not exist!");
}
if(array_key_exists("file", $_GET)) {
$file = $_GET["file"];
if(str_contains($file, "..") || str_contains($file, "/")) {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Dont't be evil!");
}
$filename="/scans/".$file;
if(file_exists($filename)) {
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=\"$file\"");
readfile($filename);
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No file provided!");
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: File does not exist!");
}
} else {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No file provided!");
}
}
?>

?>
20 changes: 10 additions & 10 deletions html/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
include 'config.php';
<?php
include 'config.php';

if (isset($RENAME_GUI_SCANTOFILE) && $RENAME_GUI_SCANTOFILE) {
$button_file = $RENAME_GUI_SCANTOFILE;
Expand Down Expand Up @@ -36,7 +36,7 @@
<link rel="icon" href="favicon.ico">
<link rel="stylesheet" href="/assets/bootstrap.5.1.3/bootstrap.min.css ">
<link rel="stylesheet" href="/assets/fontawesome.5.15.4/css/all.min.css">

<style>
/* prevent persistent highlight after click to scan */
.trigger-scan:focus, .trigger-scan:active:focus {
Expand Down Expand Up @@ -64,7 +64,7 @@ class="far fa-smile fa-fw fa-10x"></i></span>
<h1 class=" text-light mb-2 mt-5"><strong><?php echo($MODEL); ?></strong> </h1>

Check failure on line 64 in html/index.php

View workflow job for this annotation

GitHub Actions / run-lint

Variable $MODEL might not be defined.
<p class="lead text-light mb-5" id="status-text">Ready to scan</p>

<?php
<?php
if (!isset($DISABLE_GUI_SCANTOFILE) || $DISABLE_GUI_SCANTOFILE != true) {
echo('<p><a href="#" class="btn btn-outline-light btn-lg d-block trigger-scan" data-trigger="file">'.$button_file.'</a></p>');
}
Expand All @@ -85,7 +85,7 @@ class="far fa-smile fa-fw fa-10x"></i></span>


<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasFiles" aria-labelledby="offcanvasFilesLabel">

<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasFilesLabel">Last scanned</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
Expand Down Expand Up @@ -169,7 +169,7 @@ function set_state(state) {


let state = 'idle';


if (data.ocr && data.waiting && !data.scan) {
state = 'ocr';
Expand All @@ -192,14 +192,14 @@ function set_state(state) {
/**
* Event handler for the click event on the element with ID 'triggerFiles'.
* Prevents the default action and performs an AJAX GET request to '/list.php'.
*
*
* On successful response:
* - Populates the Offcanvas element with ID 'offcanvasContent' with the response content.
* - Displays the Offcanvas element with ID 'offcanvasFiles'.
*
*
* On error:
* - Logs an error message to the console.
*
*
* @param {Event} e - The click event object.
*/
$('#triggerFiles').on('click', function(e) {
Expand Down Expand Up @@ -228,4 +228,4 @@ function set_state(state) {

</body>

</html>
</html>
17 changes: 9 additions & 8 deletions html/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div class="list-group list-group-flush border-bottom scrollarea">

<?php

// Directory path
$directory = '/scans';

Expand All @@ -17,13 +18,13 @@
foreach ($files as $file) {
$filePath = $directory . '/' . $file;
if (is_file($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) === 'pdf') { // Filter PDF files
$filesWithMtime[$file] = array(
'mtime' => filemtime($filePath),
'size' => filesize($filePath),
'permissions' => substr(sprintf('%o', fileperms($filePath)), -4),
'owner' => posix_getpwuid(fileowner($filePath))['name'],
'group' => posix_getgrgid(filegroup($filePath))['name'],
);
$filesWithMtime[$file] = array(
'mtime' => filemtime($filePath),
'size' => filesize($filePath),
'permissions' => substr(sprintf('%o', fileperms($filePath)), -4),
'owner' => posix_getpwuid(fileowner($filePath))['name'],
'group' => posix_getgrgid(filegroup($filePath))['name'],
);
}
}

Expand All @@ -48,7 +49,7 @@
?>



</div>
</div>

Expand Down
29 changes: 15 additions & 14 deletions html/scan.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target = $_POST["target"];
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$target = $_GET["target"];
$target = $_POST["target"];
} elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
$target = $_GET["target"];
}

if (empty($target)) {
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No scanning function selected (try append: ?target=<file|email|image|ocr>)");
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: No scanning function selected (try append: ?target=<file|email|image|ocr>)");
}
if (in_array($target, array('file','email','image','ocr'))) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//return immediately
$handle = popen('sudo -b -u \#'.$UID.' /opt/brother/scanner/brscan-skey/script/scanto'.$target.'.py', 'r');
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
//wait for completion
$output=shell_exec('sudo -u \#'.$UID.' /opt/brother/scanner/brscan-skey/script/scanto'.$target.'.py');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// return immediately
$handle = popen('sudo -b -u \#'.$UID.' /opt/brother/scanner/brscan-skey/script/scanto'.$target.'.py', 'r');

Check failure on line 22 in html/scan.php

View workflow job for this annotation

GitHub Actions / run-lint

Variable $UID might not be defined.
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// wait for completion
$output=shell_exec('sudo -u \#'.$UID.' /opt/brother/scanner/brscan-skey/script/scanto'.$target.'.py');

Check failure on line 25 in html/scan.php

View workflow job for this annotation

GitHub Actions / run-lint

Variable $UID might not be defined.
}
}
else
{
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Thou shalt not inject unknown script names!");
header($_SERVER["SERVER_PROTOCOL"] . " 400 OK");
die("Error: Thou shalt not inject unknown script names!");
}

//TODO: Fix serving of file on get
Expand Down

0 comments on commit 84f134c

Please sign in to comment.