Skip to content
Open
139 changes: 139 additions & 0 deletions .github/workflows/bash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
name: Code Style Check in Bash

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
workflow_dispatch:

jobs:
test-bash-script:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Validate Bash script
run: |
chmod +x ./coding-style.sh
shellcheck ./coding-style.sh || echo "ShellCheck found issues, but continuing workflow"

test-c-repo:
runs-on: ubuntu-latest
needs: test-bash-script
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Docker
run: |
if ! command -v docker &> /dev/null; then
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
fi

- name: Clone C repository
run: |
git clone https://github.com/examplehub/C.git c-project

- name: Run coding style checker on C code
id: run-c-check
run: |
chmod +x ./coding-style.sh
mkdir -p reports
output=$(./coding-style.sh c-project reports 2>&1)
echo "$output"

if echo "$output" | grep -q "📌 Running C Coding Style ..."; then
echo "C Style check started correctly"
else
echo "Error: C Style check didn't start correctly"
exit 1
fi

if [[ -f "/tmp/noge/coding-style-reports.log" ]]; then
errors=$(grep -c ":" /tmp/noge/coding-style-reports.log || echo "0")
echo "Found $errors coding style errors in C code"
echo "errors=$errors" >> $GITHUB_OUTPUT
else
echo "No coding style errors found or report not generated"
echo "errors=0" >> $GITHUB_OUTPUT
fi

- name: Upload C style check results
uses: actions/upload-artifact@v4
with:
name: c-style-check-report
path: |
reports/
/tmp/noge/coding-style-reports.log
if-no-files-found: warn
retention-days: 5

test-haskell-repo:
runs-on: ubuntu-latest
needs: test-bash-script
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Docker
run: |
if ! command -v docker &> /dev/null; then
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
fi

- name: Clone Haskell repository
run: |
git clone https://github.com/marklnichols/haskell-examples.git haskell-project

- name: Run coding style checker on Haskell code
id: run-haskell-check
run: |
chmod +x ./coding-style.sh
output=$(./coding-style.sh -hs haskell-project 2>&1)
echo "$output"

if echo "$output" | grep -q "📌 Running Haskell Coding Style ..."; then
echo "Haskell Style check started correctly"
else
echo "Error: Haskell Style check didn't start correctly"
exit 1
fi

if echo "$output" | grep -q "error"; then
echo "Found coding style errors in Haskell code"
echo "has_errors=true" >> $GITHUB_OUTPUT
else
echo "No coding style errors found in Haskell code"
echo "has_errors=false" >> $GITHUB_OUTPUT
fi

- name: Upload Haskell style check results
uses: actions/upload-artifact@v4
with:
name: haskell-style-check-report
path: /tmp/noge/
if-no-files-found: warn
retention-days: 5

notify:
runs-on: ubuntu-latest
needs: [test-c-repo, test-haskell-repo]
if: always()
steps:
- name: Check workflow status
run: |
if [[ "${{ needs.test-c-repo.result }}" == "success" && "${{ needs.test-haskell-repo.result }}" == "success" ]]; then
echo "All checks passed! Style check completed successfully."
echo "Note: Even if coding style errors were found, the workflow is considered successful if the checks ran properly."
else
echo "Some checks failed to execute properly. Check the logs for details."
exit 1
fi
165 changes: 113 additions & 52 deletions coding-style.sh
Original file line number Diff line number Diff line change
@@ -1,71 +1,132 @@
#!/bin/bash


##
## EPITECH PROJECT, 2025
## Epitech
## File description:
## coding-style-checker
##

BASE_EXEC_CMD="docker"

function my_readlink() {
cd $1
cd "$1"
pwd
cd - > /dev/null
}

function cat_readme() {
echo ""
echo "Usage: $(basename $0) DELIVERY_DIR REPORTS_DIR"
echo -e "\tDELIVERY_DIR\tShould be the directory where your project files are"
echo -e "\tREPORTS_DIR\tShould be the directory where we output the reports"
echo -e "\t\t\tTake note that existing reports will be overriden"
echo "Usage: $(basename "$0") [OPTIONS] DELIVERY_DIR REPORTS_DIR"
echo ""
echo "Arguments:"
echo -e "\tDELIVERY_DIR\tPath to the directory containing your project files."
echo -e "\tREPORTS_DIR\tPath to the directory where reports will be saved."
echo ""
echo "Options:"
echo -e "\t-hs, --haskell DELIVERY_DIR \t(Optional) Run lambdananas on the specified Haskell directory."
echo -e "\t--help\t\t\tShow this help message and exit."
echo ""
echo "Notes:"
echo -e "\t- Running with the Haskell option (-hs)"
echo -e "\t- Existing reports in the output directory will be overwritten."
echo ""
echo "© 2025 Epitech. Tous droits réservés."
}

if [ $# == 1 ] && [ $1 == "--help" ]; then


if [[ "$#" -eq 1 && "$1" == "--help" ]]; then
cat_readme
elif [ $# == 1 ] || [ $# = 2 ];
then
if [ $# == 1 ];
then
REPORTS_DIR=$(my_readlink ".")
elif [ $# = 2 ];
then
REPORTS_DIR=$(my_readlink "$2")
fi

exit 0
fi

if [[ "$#" -gt 2 ]]; then
cat_readme
exit 1
fi

REPORTS_DIR=$(my_readlink .)
EXPORT_FILE="$REPORTS_DIR"/coding-style-reports.log

function pull_docker() {
DELIVERY_DIR=$(my_readlink "$1")
DOCKER_SOCKET_PATH=/var/run/docker.sock
HAS_SOCKET_ACCESS=$(test -r $DOCKER_SOCKET_PATH; echo "$?")
GHCR_REGISTRY_TOKEN=$(curl -s "https://ghcr.io/token?service=ghcr.io&scope=repository:epitech/coding-style-checker:pull" | grep -o '"token":"[^"]*' | grep -o '[^"]*$')
GHCR_REPOSITORY_STATUS=$(curl -I -f -s -o /dev/null -H "Authorization: Bearer $GHCR_REGISTRY_TOKEN" "https://ghcr.io/v2/epitech/coding-style-checker/manifests/latest" && echo 0 || echo 1)
BASE_EXEC_CMD="docker"
EXPORT_FILE="$REPORTS_DIR"/coding-style-reports.log
### delete existing report file
DOCKER_SOCKET_PATH="/var/run/docker.sock"
HAS_SOCKET_ACCESS=$(test -r "$DOCKER_SOCKET_PATH"; echo "$?")
IMAGE_NAME="ghcr.io/epitech/coding-style-checker:latest"
#EXPORT_FILE="/tmp/noge/coding-style-reports.log"

rm -f "$EXPORT_FILE"

### Pull new version of docker image and clean olds

if [ $HAS_SOCKET_ACCESS -ne 0 ]; then
#chown -R "$USER:$USER" "$REPORTS_DIR"
#chmod -R 777 "$REPORTS_DIR"
if [[ "$HAS_SOCKET_ACCESS" -ne 0 ]]; then
echo "WARNING: Socket access is denied"
echo "To fix this we will add the current user to docker group with : sudo usermod -a -G docker $USER"
read -p "Do you want to proceed? (yes/no) " yn
echo "To fix this, add the current user to the docker group: sudo usermod -a -G docker $USER"
read -rp "Do you want to proceed? (yes/no) " yn
case $yn in
yes | Y | y | Yes | YES) echo "ok, we will proceed";
sudo usermod -a -G docker $USER;
echo "You must reboot your computer for the changes to take effect";;
no | N | n | No | NO) echo "ok, Skipping";;
* ) echo "invalid response, Skipping";;
yes | Y | y | Yes | YES)
sudo usermod -a -G docker "$USER"
echo "You must reboot your computer for the changes to take effect."
;;
*)
echo "Skipping..."
;;
esac
BASE_EXEC_CMD="sudo ${BASE_EXEC_CMD}"
fi


if [ $GHCR_REPOSITORY_STATUS -eq 0 ]; then
echo "Downloading new image and cleaning old one..."
$BASE_EXEC_CMD pull ghcr.io/epitech/coding-style-checker:latest && $BASE_EXEC_CMD image prune -f
echo "Download OK"
else
echo "WARNING: Skipping image download"

LOCAL_IMAGE_DATE=$(docker inspect --format='{{.Created}}' "$IMAGE_NAME" 2>/dev/null)
REMOTE_IMAGE_DATE=$(curl -sI "https://ghcr.io/v2/epitech/coding-style-checker/manifests/latest" | grep -i "last-modified" | cut -d' ' -f2-)

if [[ -n "$REMOTE_IMAGE_DATE" && -n "$LOCAL_IMAGE_DATE" ]]; then
LOCAL_TIMESTAMP=$(date -d "$LOCAL_IMAGE_DATE" +%s 2>/dev/null || echo 0)
REMOTE_TIMESTAMP=$(date -d "$REMOTE_IMAGE_DATE" +%s 2>/dev/null || echo 0)

if [[ "$REMOTE_TIMESTAMP" -gt "$LOCAL_TIMESTAMP" ]]; then
echo "Downloading new image and cleaning old one..."
$BASE_EXEC_CMD pull "$IMAGE_NAME" && $BASE_EXEC_CMD image prune -f
echo "Download OK"
fi
fi
}

tput setaf 2
echo "**/*/*/*/*/*/*/*/*/*/*/* CODING STYLE CHECKER EPITECH */*/*/*/*/*/*/*/*/*/*/*/**"
tput sgr0

if [[ "$#" -eq 2 && ( "$1" == "-hs" || "$1" == "--haskell" ) ]]; then
echo ""
echo "📌 Running Haskell Coding Style ..."
echo ""

pull_docker "$2"

HASKELL_DIR=$(my_readlink "$2")

excluded_dirs="Setup.hs:setup.hs:.git:.stack-work:test:tests:bonus"

LAMBDA_PATH=$($BASE_EXEC_CMD run --rm --entrypoint /bin/bash ghcr.io/epitech/coding-style-checker:latest -c "which lambdananas")

# $BASE_EXEC_CMD run --rm --entrypoint /bin/bash ghcr.io/epitech/coding-style-checker:latest -c "which lambdananas"
$BASE_EXEC_CMD run --rm --entrypoint $LAMBDA_PATH -v "$HASKELL_DIR:/mnt/haskell" "$IMAGE_NAME" "/mnt/haskell"
exit 0
fi

if [[ "$#" -eq 2 || "$#" -eq 1 ]]; then
echo ""
echo "📌 Running C Coding Style ..."
echo ""

pull_docker "$1"

$BASE_EXEC_CMD run --rm -i -v "$DELIVERY_DIR:/mnt/delivery" -v "$REPORTS_DIR:/mnt/reports" "$IMAGE_NAME" "/mnt/delivery" "/mnt/reports"

#$BASE_EXEC_CMD run --rm --security-opt "label:disable" -i -v "$DELIVERY_DIR":"/mnt/delivery" -v "$REPORTS_DIR":"/mnt/reports" ghcr.io/epitech/coding-style-checker:latest "/mnt/delivery" "/mnt/reports"
[[ -f "$EXPORT_FILE" ]] && echo "$(wc -l < "$EXPORT_FILE") coding style error(s) reported in "$EXPORT_FILE", $(tput sgr0; tput setaf 9; grep -c ": MAJOR:" "$EXPORT_FILE") major, $(tput sgr0; tput setaf 27; grep -c ": MINOR:" "$EXPORT_FILE") minor, $(tput sgr0; tput setaf 11; grep -c ": INFO:" "$EXPORT_FILE") info"
tput sgr0; echo ""

if [[ -f "./coding-style-reports.log" ]]; then
cat ./coding-style-reports.log
rm -f ./coding-style-reports.log
fi


### generate reports
$BASE_EXEC_CMD run --rm --security-opt "label:disable" -i -v "$DELIVERY_DIR":"/mnt/delivery" -v "$REPORTS_DIR":"/mnt/reports" ghcr.io/epitech/coding-style-checker:latest "/mnt/delivery" "/mnt/reports"
[[ -f "$EXPORT_FILE" ]] && echo "$(wc -l < "$EXPORT_FILE") coding style error(s) reported in "$EXPORT_FILE", $(grep -c ": MAJOR:" "$EXPORT_FILE") major, $(grep -c ": MINOR:" "$EXPORT_FILE") minor, $(grep -c ": INFO:" "$EXPORT_FILE") info"
else
cat_readme
fi