From 5e8928031a05f624f433c4b5301c122acabfdb0c Mon Sep 17 00:00:00 2001 From: Giovanni Pellerano Date: Mon, 18 Nov 2024 18:17:16 +0100 Subject: [PATCH] Refactor README.md badges in order to host more metrics --- .github/workflows/build.yml | 2 +- .github/workflows/scripts/run-tests.sh | 73 ------------------- .../scripts/{run-build.sh => run_build.sh} | 0 .../workflows/scripts/run_tests_backend.sh | 32 ++++++++ .github/workflows/scripts/run_tests_client.sh | 41 +++++++++++ .github/workflows/test-backend.yml | 28 +++++++ .../workflows/{test.yml => test-client.yml} | 4 +- README.md | 18 +++-- backend/globaleaks/settings.py | 10 ++- 9 files changed, 121 insertions(+), 87 deletions(-) delete mode 100755 .github/workflows/scripts/run-tests.sh rename .github/workflows/scripts/{run-build.sh => run_build.sh} (100%) create mode 100755 .github/workflows/scripts/run_tests_backend.sh create mode 100755 .github/workflows/scripts/run_tests_client.sh create mode 100644 .github/workflows/test-backend.yml rename .github/workflows/{test.yml => test-client.yml} (88%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dd919b228c..3709719823 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,4 +22,4 @@ jobs: - name: Run tests run: | cd $GITHUB_WORKSPACE - ./.github/workflows/scripts/run-build.sh + ./.github/workflows/scripts/run_build.sh diff --git a/.github/workflows/scripts/run-tests.sh b/.github/workflows/scripts/run-tests.sh deleted file mode 100755 index 85eace7010..0000000000 --- a/.github/workflows/scripts/run-tests.sh +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/bash - -backend_test_failed=0 -browser_test_failed=0 - -setupClient() { - cd $GITHUB_WORKSPACE/client # to install frontend dependencies - npm install -d - ./node_modules/grunt/bin/grunt build_and_instrument -} - -setupBackend() { - cd $GITHUB_WORKSPACE/backend # to install backend dependencies - pip3 install -r requirements/requirements-$(lsb_release -cs).txt -} - -echo "Running setup" -sudo apt-get update -sudo apt-get install -y tor -npm install -g grunt grunt-cli -pip install coverage -setupClient -setupBackend - -# Running Backend Unit Tests -echo "Running backend unit tests" -cd $GITHUB_WORKSPACE/backend && coverage run setup.py test -if [ $? -ne 0 ]; then - echo "Backend unit tests failed!" - backend_test_failed=1 -else - echo "Backend unit tests succeeded!" -fi - -$GITHUB_WORKSPACE/backend/bin/globaleaks -z -sleep 5 - -# Running BrowserTesting Locally -echo "Running BrowserTesting locally collecting code coverage" -cd $GITHUB_WORKSPACE/client && npm test -if [ $? -ne 0 ]; then - echo "Browser tests failed!" - browser_test_failed=1 -else - echo "Browser tests succeeded!" -fi - -cd $GITHUB_WORKSPACE/backend && coverage xml -bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l Python -r $GITHUB_WORKSPACE/backend/coverage.xml -bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l TypeScript -r $GITHUB_WORKSPACE/client/cypress/coverage/lcov.info - -# At the end, check if any of the critical tests failed -echo "Test Results Summary:" -if [ $backend_test_failed -eq 1 ]; then - echo " - Backend unit tests: FAILED" -else - echo " - Backend unit tests: PASSED" -fi - -if [ $browser_test_failed -eq 1 ]; then - echo " - Browser tests: FAILED" -else - echo " - Browser tests: PASSED" -fi - -# Final exit based on the test results -if [ $backend_test_failed -eq 1 ] || [ $browser_test_failed -eq 1 ]; then - echo "One or both critical tests failed." - exit 1 # Exit with failure if either critical test failed -else - echo "Script completed successfully." - exit 0 # Exit successfully if no critical test failed -fi diff --git a/.github/workflows/scripts/run-build.sh b/.github/workflows/scripts/run_build.sh similarity index 100% rename from .github/workflows/scripts/run-build.sh rename to .github/workflows/scripts/run_build.sh diff --git a/.github/workflows/scripts/run_tests_backend.sh b/.github/workflows/scripts/run_tests_backend.sh new file mode 100755 index 0000000000..43b4b91a76 --- /dev/null +++ b/.github/workflows/scripts/run_tests_backend.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +backend_test_failed=0 + +setupBackend() { + cd $GITHUB_WORKSPACE/backend # to install backend dependencies + pip3 install -r requirements/requirements-$(lsb_release -cs).txt +} + +echo "Running setup" +sudo apt-get update +sudo apt-get install -y tor +pip install coverage +setupBackend + +# Running backend tests +echo "Running backend tests" +cd $GITHUB_WORKSPACE/backend && coverage run setup.py test +if [ $? -ne 0 ]; then + backend_test_failed=1 +fi + +cd $GITHUB_WORKSPACE/backend && coverage xml +bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l Python -r $GITHUB_WORKSPACE/backend/coverage.xml + +if [ $backend_test_failed -eq 1 ]; then + echo "Backend unit tests: FAILED" + exit 1 +else + echo "Backend unit tests: PASSED" + exit 0 +fi diff --git a/.github/workflows/scripts/run_tests_client.sh b/.github/workflows/scripts/run_tests_client.sh new file mode 100755 index 0000000000..c6be9e8ccf --- /dev/null +++ b/.github/workflows/scripts/run_tests_client.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +client_test_failed=0 + +setupBackend() { + cd $GITHUB_WORKSPACE/backend # to install backend dependencies + pip3 install -r requirements/requirements-$(lsb_release -cs).txt +} + +setupClient() { + cd $GITHUB_WORKSPACE/client # to install frontend dependencies + npm install -d + ./node_modules/grunt/bin/grunt build_and_instrument +} + +echo "Running setup" +sudo apt-get update +sudo apt-get install -y tor +npm install -g grunt grunt-cli +setupBackend +setupClient + +$GITHUB_WORKSPACE/backend/bin/globaleaks -z +sleep 5 + +# Running client tests locally +echo "Running client tests locally collecting code coverage" +cd $GITHUB_WORKSPACE/client && npm test +if [ $? -ne 0 ]; then + client_test_failed=1 +fi + +bash <(curl -Ls https://coverage.codacy.com/get.sh) report -l TypeScript -r $GITHUB_WORKSPACE/client/cypress/coverage/lcov.info + +if [ $client_test_failed -eq 1 ]; then + echo "Client tests: FAILED" + exit 1 +else + echo "Client tests: PASSED" + exit 0 +fi diff --git a/.github/workflows/test-backend.yml b/.github/workflows/test-backend.yml new file mode 100644 index 0000000000..e447b9c90e --- /dev/null +++ b/.github/workflows/test-backend.yml @@ -0,0 +1,28 @@ +name: Tests (B) + +on: [ push, pull_request ] + +# Declare default permissions as read only. +permissions: read-all + +env: + CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }} + +jobs: + run_tests: + runs-on: "ubuntu-latest" + steps: + - name: Check out repository code + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + fetch-depth: 1 + + - name: Install git + run: | + sudo apt-get update -q + sudo apt-get install -y git + + - name: Run tests + run: | + cd $GITHUB_WORKSPACE + ./.github/workflows/scripts/run_tests_backend.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test-client.yml similarity index 88% rename from .github/workflows/test.yml rename to .github/workflows/test-client.yml index 499cfb257b..7b9810b686 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test-client.yml @@ -1,4 +1,4 @@ -name: Test +name: Tests (C) on: [ push, pull_request ] @@ -25,4 +25,4 @@ jobs: - name: Run tests run: | cd $GITHUB_WORKSPACE - ./.github/workflows/scripts/run-tests.sh + ./.github/workflows/scripts/run_tests_client.sh diff --git a/README.md b/README.md index 4f9a41b77e..4a3bdffa06 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,18 @@ The software is recognized by the [Digital Public Good Alliance](https://digitalpublicgoods.net) as a [Digital Public Good](https://app.digitalpublicgoods.net/a/11113). ## Continuous integration and testing -| Branch | Build Status | Test Status | Quality | Coverage | Documentation -| :---: | :---: | :---: | :---: | :---: | :---: | -| [stable](https://github.com/globaleaks/globaleaks-whistleblowing-software/tree/stable) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml/badge.svg?branch=stable)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml?query=branch%3Astable) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test.yml/badge.svg?branch=stable)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test.yml?query=branch%3Astable) | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/c09f1ec9607f4546924d19798a98dd7d?branch=stable)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/c09f1ec9607f4546924d19798a98dd7d?branch=stable)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | [![Build Status](https://readthedocs.org/projects/globaleaks/badge/?version=stable&style=flat)](https://docs.globaleaks.org/en/stable/) -| [devel](https://github.com/globaleaks/globaleaks-whistleblowing-software/tree/devel) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml/badge.svg?branch=devel)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml?query=branch%3Adevel) | [![test workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test.yml/badge.svg?branch=devel)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test.yml?query=branch%3Adevel) | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/c09f1ec9607f4546924d19798a98dd7d?branch=devel)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard?branch=devel) | [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/c09f1ec9607f4546924d19798a98dd7d?branch=devel)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard?branch=devel) | [![Build Status](https://readthedocs.org/projects/globaleaks/badge/?version=devel&style=flat)](https://docs.globaleaks.org/en/devel/) - -Project best practices and scores: +| Metric | [Stable branch](https://github.com/globaleaks/globaleaks-whistleblowing-software/tree/stable) | [Devel branch](https://github.com/globaleaks/globaleaks-whistleblowing-software/tree/devel) | +| :---- | :---- | :---- | +| Build Status | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml/badge.svg?branch=stable)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml?query=branch%3Astable) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml/badge.svg?branch=devel)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/build.yml?query=branch%3Adevel) | +| Tests (Backend): | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-backend.yml/badge.svg?branch=stable)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-backend.yml?query=branch%3Astable) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-backend.yml/badge.svg?branch=devel)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-backend.yml?query=branch%3Adevel) | +| Tests (Client): | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-client.yml/badge.svg?branch=stable)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-client.yml?query=branch%3Astable) | [![build workflow](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-client.yml/badge.svg?branch=devel)](https://github.com/globaleaks/globaleaks-whistleblowing-software/actions/workflows/test-client.yml?query=branch%3Adevel) | +| Tests Coverage: | [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/c09f1ec9607f4546924d19798a98dd7d?branch=stable)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/c09f1ec9607f4546924d19798a98dd7d?branch=devel)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | +| Code Quality: | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/c09f1ec9607f4546924d19798a98dd7d?branch=stable)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/c09f1ec9607f4546924d19798a98dd7d?branch=devel)](https://app.codacy.com/gh/globaleaks/globaleaks-whistleblowing-software/dashboard) | +| Documentation | [![Build Status](https://readthedocs.org/projects/globaleaks/badge/?version=stable&style=flat)](https://docs.globaleaks.org/en/stable/) | [![Build Status](https://readthedocs.org/projects/globaleaks/badge/?version=devel&style=flat)](https://docs.globaleaks.org/en/devel/) | + +## Project best practices and scores: | Metric | Score -| :---: | :---: | +| :---- | :---- | | [OpenSSF Scorecard](https://scorecard.dev/) | [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/globaleaks/globaleaks-whistleblowing-software/badge)](https://scorecard.dev/viewer/?uri=github.com/globaleaks/globaleaks-whistleblowing-software) | [OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/) | [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/3816/badge)](https://bestpractices.coreinfrastructure.org/projects/3816) | [MDN HTTP Observatory](https://developer.mozilla.org/en-US/observatory/analyze?host=try.globaleaks.org) | ![Status](https://img.shields.io/badge/observatory-A%2B-brightgreen) diff --git a/backend/globaleaks/settings.py b/backend/globaleaks/settings.py index f0272395b6..19a6d718d7 100644 --- a/backend/globaleaks/settings.py +++ b/backend/globaleaks/settings.py @@ -8,8 +8,8 @@ this_directory = os.path.dirname(__file__) possible_client_paths = [ - os.path.abspath(os.path.join(this_directory, '../../client/build/')), - '/usr/share/globaleaks/client' + '/usr/share/globaleaks/client', + os.path.abspath(os.path.join(this_directory, '../../client/build/')) ] @@ -98,14 +98,16 @@ def eval_paths(self): self.accesslogfile = os.path.abspath(os.path.join(self.log_path, "access.log")) # Client path detection + client_found=False + self.client_path = possible_client_paths[0] for path in possible_client_paths: if os.path.isfile(os.path.join(path, 'index.html')): self.client_path = path + client_found=True break - if self.client_path is None: + if not client_found: print("Unable to find a directory to load the client from") - sys.exit(1) self.appdata_file = os.path.join(self.client_path, 'data/appdata.json') self.questionnaires_path = os.path.join(self.client_path, 'data/questionnaires')