diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..65c5ca8 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/README.md b/README.md index b51a96a..dc75636 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,203 @@ -# sonarqube-scan-action +# Scan your code with SonarQube [![QA](https://github.com/SonarSource/sonarqube-scan-action/actions/workflows/qa.yml/badge.svg)](https://github.com/SonarSource/sonarqube-scan-action/actions/workflows/qa.yml) +This SonarSource project, available as a GitHub Action, scans your projects with SonarQube [Server](https://www.sonarsource.com/products/sonarqube/) or [Cloud](https://www.sonarsource.com/products/sonarcloud/). + +![Logo](./images/SQ_Logo_Server_Cloud_Dark_Backgrounds.png#gh-dark-mode-only) +![Logo](./images/SQ_Logo_Server_Cloud_Light_Backgrounds.png#gh-light-mode-only) + +SonarQube [Server](https://www.sonarsource.com/products/sonarqube/) and [Cloud](https://www.sonarsource.com/products/sonarcloud/) (formerly SonarQube and SonarCloud) is a widely used static analysis solution for continuous code quality and security inspection. + +It helps developers detect coding issues in 30+ languages, frameworks, and IaC platforms, including Java, JavaScript, TypeScript, C#, Python, C, C++, and [many more](https://www.sonarsource.com/knowledge/languages/). + +The solution also provides fix recommendations leveraging AI with Sonar's AI CodeFix capability. + +## Requirements + +### Server + +To run an analysis on your code, you first need to set up your project on SonarQube Server. Your SonarQube Server instance must be accessible from GitHub, and you will need an access token to run the analysis (more information below under **Environment variables**). + +Read more information on how to analyze your code [here](https://docs.sonarsource.com/sonarqube-server/latest/devops-platform-integration/github-integration/introduction/). + +### Cloud + +* Create your account on SonarQube Cloud. [Sign up for free](https://www.sonarsource.com/products/sonarcloud/signup/?utm_medium=referral&utm_source=github&utm_campaign=sc-signup&utm_content=signup-sonarcloud-listing-x-x&utm_term=ww-psp-x) now if it's not already the case! +* The repository to analyze is set up on SonarQube Cloud. [Set it up](https://sonarcloud.io/projects/create) in just one click. + +## Usage + +Project metadata, including the location of the sources to be analyzed, must be declared in the file `sonar-project.properties` in the base directory: + +### Server + +```properties +sonar.projectKey= + +# relative paths to source directories. More details and properties are described +# at https://docs.sonarsource.com/sonarqube-server/latest/project-administration/analysis-scope/ +sonar.sources=. +``` + +The workflow, usually declared under `.github/workflows`, looks like: + +```yaml +on: + # Trigger analysis when pushing to your main branches, and when creating a pull request. + push: + branches: + - main + - master + - develop + - 'releases/**' + pull_request: + types: [opened, synchronize, reopened] + +name: Main Workflow +jobs: + sonarqube: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + # Disabling shallow clones is recommended for improving the relevancy of reporting + fetch-depth: 0 + - name: SonarQube Scan + uses: sonarsource/sonarqube-scan-action@ # Ex: v4.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }} +``` + +### Cloud + +```properties +sonar.organization= +sonar.projectKey= + +# relative paths to source directories. More details and properties are described +# at https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/analysis-scope/ +sonar.sources=. +``` + +The workflow, usually declared under `.github/workflows`, looks like: + +```yaml +on: + # Trigger analysis when pushing to your main branches, and when creating a pull request. + push: + branches: + - main + - master + - develop + - 'releases/**' + pull_request: + types: [opened, synchronize, reopened] + +name: Main Workflow +jobs: + sonarqube: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + # Disabling shallow clones is recommended for improving the relevancy of reporting + fetch-depth: 0 + - name: SonarQube Scan + uses: sonarsource/sonarqube-scan-action@ # Ex: v4.1.0, See the latest version at https://github.com/marketplace/actions/official-sonarqube-scan + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} +``` + +## Action parameters + +You can change the analysis base directory by using the optional input `projectBaseDir` like this: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + with: + projectBaseDir: app/src +``` + +In case you need to specify the version of the Sonar Scanner, you can use the `scannerVersion` option: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + with: + scannerVersion: 6.2.0.4584 +``` + +In case you need to add additional analysis parameters, and you do not wish to set them in the `sonar-project.properties` file, you can use the `args` option: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + with: + projectBaseDir: app/src + args: > + -Dsonar.organization=my-organization # For SonarQube Cloud only + -Dsonar.projectKey=my-projectkey + -Dsonar.python.coverage.reportPaths=coverage.xml + -Dsonar.sources=lib/ + -Dsonar.tests=tests/ + -Dsonar.test.exclusions=tests/** + -Dsonar.verbose=true +``` + +You can also specify the URL where to retrieve the SonarScanner CLI from. +The specified URL overrides the default address: `https://binaries.sonarsource.com/Distribution/sonar-scanner-cli`. +This can be useful when the runner executing the action is self-hosted and has regulated or no access to the Internet: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + with: + scannerBinariesUrl: https://my.custom.binaries.url.com/Distribution/sonar-scanner-cli/ +``` + +More information about possible analysis parameters can be found: +* in the [Analysis parameters page](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/analysis-parameters/) of the SonarQube Server documentation +* in the [Analysis parameters page](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/analysis-parameters/) of the SonarQube Cloud documentation + +### Environment variables + +- `SONAR_TOKEN` – **Required** this is the token used to authenticate access to SonarQube. You can read more about security tokens in the documentation of SonarQube [Server](https://docs.sonarsource.com/sonarqube-server/latest/user-guide/managing-tokens/) and [Cloud](https://docs.sonarsource.com/sonarqube-cloud/managing-your-account/managing-tokens/). You can set the `SONAR_TOKEN` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). +- *`GITHUB_TOKEN` – Provided by Github (see [Authenticating with the GITHUB_TOKEN](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token)).* +- `SONAR_HOST_URL` – this tells the scanner where SonarQube Server is hosted. You can set the `SONAR_HOST_URL` environment variable in the "Variables" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). Not needed for SonarQube Cloud. +- `SONAR_ROOT_CERT` – Holds an additional certificate (in PEM format) that is used to validate the certificate of SonarQube Server or of a secured proxy to SonarQube (Server or Cloud). You can set the `SONAR_ROOT_CERT` environment variable in the "Secrets" settings page of your repository, or you can add them at the level of your GitHub organization (recommended). + +Here is an example of how you can pass a certificate (in PEM format) to the Scanner truststore: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }} + SONAR_ROOT_CERT: ${{ secrets.SONAR_ROOT_CERT }} +``` + +If your source code file names contain special characters that are not covered by the locale range of `en_US.UTF-8`, you can configure your desired locale like this: + +```yaml +- uses: sonarsource/sonarqube-scan-action@ + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }} # or https://sonarcloud.io + LC_ALL: "ru_RU.UTF-8" +``` + +## Alternatives for Java, .NET, and C/C++ projects + +This GitHub Action will not work for all technologies. If you are in one of the following situations, you should use the following alternatives: + +* Your code is built with Maven. Read the documentation about our SonarScanner for Maven in SonarQube [Server](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/scanners/sonarscanner-for-maven/) and [Cloud](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-maven/). +* Your code is built with Gradle. Read the documentation about our SonarScanner for Gradle in SonarQube [Server](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/scanners/sonarscanner-for-gradle/) and [Cloud](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-gradle/). +* You want to analyze a .NET solution. Read the documentation about our SonarScanner for .NET in SonarQube [Server](https://docs.sonarsource.com/sonarqube-server/latest/analyzing-source-code/scanners/dotnet/introduction/) and [Cloud](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-for-dotnet/introduction/). +* You want to analyze C or C++ code. Starting from SonarQube 10.6, this GitHub Action will scan C and C++ out of the box. If you want to have better control over the scan configuration/setup, you can switch to: + * the [SonarQube Server Scan for C and C++](https://github.com/marketplace/actions/sonarqube-scan-for-c-and-c) GitHub Action, for projects on SonarQube Server + * the [SonarQube Cloud Scan for C and C++](https://github.com/marketplace/actions/sonarcloud-scan-for-c-and-c) GitHub Action, for projects on SonarQube Cloud - look at [our sample C and C++ project](https://github.com/sonarsource-cfamily-examples?q=gh-actions-sc&type=all&language=&sort=). + +## Have questions or feedback? + +To provide feedback (requesting a feature or reporting a bug) please post on the SonarSource Community Forum page for SonarQube [Server](https://community.sonarsource.com/tags/c/help/sq/github-actions) or [Cloud](https://community.sonarsource.com/tags/c/help/sc/9/github-actions). + +## License + +Container images built with this project include third-party materials. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..3989705 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,13 @@ +# Reporting Security Issues + +A mature software vulnerability treatment process is a cornerstone of a robust information security management system. Contributions from the community play an important role in the evolution and security of our products, and in safeguarding the security and privacy of our users. + +If you believe you have discovered a security vulnerability in Sonar's products, we encourage you to report it immediately. + +To responsibly report a security issue, please email us at [security@sonarsource.com](mailto:security@sonarsource.com). Sonar’s security team will acknowledge your report, guide you through the next steps, or request additional information if necessary. Customers with a support contract can also report the vulnerability directly through the support channel. + +For security vulnerabilities found in third-party libraries, please also contact the library's owner or maintainer directly. + +## Responsible Disclosure Policy + +For more information about disclosing a security vulnerability to Sonar, please refer to our community post: [Responsible Vulnerability Disclosure](https://community.sonarsource.com/t/responsible-vulnerability-disclosure/9317). \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..5b0c4ca --- /dev/null +++ b/action.yml @@ -0,0 +1,52 @@ +name: Official SonarQube (Server, Cloud) Scan +description: > + Scan your code with SonarQube Server and Cloud to detect + issues in 30+ languages. (Formerly SonarQube and SonarCloud) +branding: + icon: check + color: green +inputs: + args: + description: Additional arguments to the Sonar Scanner CLI + required: false + projectBaseDir: + description: Set the sonar.projectBaseDir analysis property + required: false + scannerVersion: + description: Version of the Sonar Scanner CLI to use + required: false + default: 6.2.1.4610 + scannerBinariesUrl: + description: URL to download the Sonar Scanner CLI binaries from + required: false + default: https://binaries.sonarsource.com/Distribution/sonar-scanner-cli +runs: + using: "composite" + steps: + - name: Sanity checks + run: ${GITHUB_ACTION_PATH}/sanity-checks.sh + shell: bash + env: + INPUT_PROJECTBASEDIR: ${{ inputs.projectBaseDir }} + - name: Load Sonar Scanner CLI from cache + id: sonar-scanner-cli + uses: actions/cache@v4.0.2 + with: + path: ${{ runner.temp }}/sonar-scanner-cli-${{ inputs.scannerVersion }}-${{ runner.os }}-${{ runner.arch }} + key: sonar-scanner-cli-${{ inputs.scannerVersion }}-${{ runner.os }}-${{ runner.arch }} + - name: Install Sonar Scanner CLI + if: ${{ env.NO_CACHE == 'true' || steps.sonar-scanner-cli.outputs.cache-hit != 'true' }} + run: ${GITHUB_ACTION_PATH}/install-sonar-scanner-cli.sh + shell: bash + env: + INPUT_SCANNERVERSION: ${{ inputs.scannerVersion }} + INPUT_SCANNERBINARIESURL: ${{ inputs.scannerBinariesUrl }} + - name: Add SonarScanner CLI to the PATH + run: echo "${RUNNER_TEMP}/sonar-scanner-cli-${{ inputs.scannerVersion }}-${{ runner.os }}-${{ runner.arch }}/bin" >> $GITHUB_PATH + shell: bash + - name: Run SonarScanner + run: ${GITHUB_ACTION_PATH}/run-sonar-scanner.sh ${{ inputs.args }} + shell: bash + env: + INPUT_PROJECTBASEDIR: ${{ inputs.projectBaseDir }} + SONAR_SCANNER_JRE: ${{ runner.temp }}/sonar-scanner-cli-${{ inputs.scannerVersion }}-${{ runner.os }}-${{ runner.arch }}/jre \ No newline at end of file diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..0fcc4d4 --- /dev/null +++ b/contributing.md @@ -0,0 +1,26 @@ +Contributing +============ + +If you would like to see a new feature, please create a new thread in the forum ["Suggest new features"](https://community.sonarsource.com/c/suggestions/features). + +Please be aware that we are not actively looking for feature contributions. The truth is that it's extremely difficult for someone outside SonarSource to comply with our roadmap and expectations. Therefore, we typically only accept minor cosmetic changes and typo fixes. + +## Submitting a pull request + +With that in mind, if you would like to submit a code contribution, please create a pull request for this repository. Please explain your motives to contribute this change: what problem you are trying to fix, what improvement you are trying to make. + +Make sure that you follow our [code style](https://github.com/SonarSource/sonar-developer-toolset#code-style) and all tests are passing (Travis build is executed for each pull request). + +## Next steps + +One of the members of our team will carefully review your pull request. You might be asked at this point for clarifications or your pull request might be rejected if we decide that it doesn't fit our roadmap and vision for the product. +If your contribution looks promising then either we will decide: + +- it is good to go and merge your pull request to the master branch + +or + +- that we need to think over your change and modify it to adhere to our roadmap and internal standards. We will close your pull request at this point, but we might come back to your changes later in the future when we decide it is the right time to work on it. + +Thank You! +The SonarSource Team diff --git a/images/SQ_Logo_Server_Cloud_Dark_Backgrounds.png b/images/SQ_Logo_Server_Cloud_Dark_Backgrounds.png new file mode 100644 index 0000000..6f019d5 Binary files /dev/null and b/images/SQ_Logo_Server_Cloud_Dark_Backgrounds.png differ diff --git a/images/SQ_Logo_Server_Cloud_Light_Backgrounds.png b/images/SQ_Logo_Server_Cloud_Light_Backgrounds.png new file mode 100644 index 0000000..e574231 Binary files /dev/null and b/images/SQ_Logo_Server_Cloud_Light_Backgrounds.png differ diff --git a/install-sonar-scanner-cli.sh b/install-sonar-scanner-cli.sh new file mode 100755 index 0000000..57e458a --- /dev/null +++ b/install-sonar-scanner-cli.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +set -eou pipefail + +# See https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables +# +# Script-specific variables required: +# - INPUT_SCANNERVERSION: e.g. 6.2.1.4610 +# - INPUT_SCANNERBINARIESURL: e.g. https://github.com/me/my-repo/raw/refs/heads/main/binaries + +if [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "AMD64" ]]; then + FLAVOR="linux-x64" +elif [[ "$RUNNER_OS" == "Linux" && "$RUNNER_ARCH" == "ARM64" ]]; then + FLAVOR="linux-aarch64" +elif [[ "$RUNNER_OS" == "Windows" && "$RUNNER_ARCH" == "X64" ]]; then + FLAVOR="windows-x64" +elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "X64" ]]; then + FLAVOR="macosx-x64" +elif [[ "$RUNNER_OS" == "macOS" && "$RUNNER_ARCH" == "ARM64" ]]; then + FLAVOR="macosx-aarch64" +else + echo "::error title=SonarScanner::$RUNNER_OS $RUNNER_ARCH not supported" + exit 1 +fi + +set -x + +mkdir -p $RUNNER_TEMP/sonarscanner +cd $RUNNER_TEMP/sonarscanner + +SCANNER_FILE_NAME="sonar-scanner-cli-$INPUT_SCANNERVERSION-$FLAVOR.zip" +SCANNER_URI="${INPUT_SCANNERBINARIESURL%/}/$SCANNER_FILE_NAME" + +if command -v wget &> /dev/null; then + wget --no-verbose --user-agent=sonarqube-scan-action "$SCANNER_URI" +elif command -v curl &> /dev/null; then + curl --fail --silent --show-error --user-agent sonarqube-scan-action \ + --location --output "$SCANNER_FILE_NAME" "$SCANNER_URI" +elif [ "$RUNNER_OS" == "Windows" ] && [ -t "C:\\msys64\\usr\\bin\\wget.exe" ]; then + "C:\\msys64\\usr\\bin\\wget.exe" --no-verbose --user-agent=sonarqube-scan-action "$SCANNER_URI" +elif [ "$RUNNER_OS" == "Windows" ] && [ -t "C:\\msys64\\usr\\bin\\curl.exe" ]; then + "C:\\msys64\\usr\\bin\\curl.exe" --fail --silent --show-error --user-agent sonarqube-scan-action \ + --location --output "$SCANNER_FILE_NAME" "$SCANNER_URI" +else + echo "::error title=SonarScanner::Neither wget nor curl found on the machine" + exit 1 +fi + +unzip -q $SCANNER_FILE_NAME + +# Folder name should correspond to the directory cached by the actions/cache +mv sonar-scanner-$INPUT_SCANNERVERSION-$FLAVOR $RUNNER_TEMP/sonar-scanner-cli-$INPUT_SCANNERVERSION-$RUNNER_OS-$RUNNER_ARCH diff --git a/run-sonar-scanner.sh b/run-sonar-scanner.sh new file mode 100755 index 0000000..14caadc --- /dev/null +++ b/run-sonar-scanner.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -eo pipefail + +if [[ "$RUNNER_OS" == "Windows" ]]; then + SCANNER_BIN="sonar-scanner.bat" +else + SCANNER_BIN="sonar-scanner" +fi + +scanner_args=() +if [[ ${SONARCLOUD_URL} ]]; then + scanner_args+=("-Dsonar.scanner.sonarcloudUrl=${SONARCLOUD_URL}") +fi + +if [[ "$RUNNER_DEBUG" == '1' ]]; then + scanner_args+=('--debug') +fi + +if [[ -n "${INPUT_PROJECTBASEDIR}" ]]; then + scanner_args+=("-Dsonar.projectBaseDir=${INPUT_PROJECTBASEDIR}") +fi + +if [[ -n "${SONAR_ROOT_CERT}" ]]; then + echo "Adding SSL certificate to the Scanner truststore" + rm -f $RUNNER_TEMP/tmpcert.pem + echo "${SONAR_ROOT_CERT}" > $RUNNER_TEMP/tmpcert.pem + # Use keytool for now, as SonarQube 10.6 and below doesn't support openssl generated keystores + # keytool require a password > 6 characters, so we wan't use the default password 'sonar' + store_pass=changeit + mkdir -p ~/.sonar/ssl + $SONAR_SCANNER_JRE/bin/java sun.security.tools.keytool.Main -storetype PKCS12 -keystore ~/.sonar/ssl/truststore.p12 -storepass $store_pass -noprompt -trustcacerts -importcert -alias sonar -file $RUNNER_TEMP/tmpcert.pem + scanner_args+=("-Dsonar.scanner.truststorePassword=$store_pass") +fi + +scanner_args+=("$@") + +set -ux + +$SCANNER_BIN "${scanner_args[@]}" + diff --git a/sanity-checks.sh b/sanity-checks.sh new file mode 100755 index 0000000..6fd8b97 --- /dev/null +++ b/sanity-checks.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -eo pipefail + +if [[ -z "${SONAR_TOKEN}" ]]; then + echo "::warning title=SonarScanner::Running this GitHub Action without SONAR_TOKEN is not recommended" +fi + +if [[ -f "${INPUT_PROJECTBASEDIR%/}/pom.xml" ]]; then + echo "::warning title=SonarScanner::Maven project detected. Sonar recommends running the 'org.sonarsource.scanner.maven:sonar-maven-plugin:sonar' goal during the build process instead of using this GitHub Action + to get more accurate results." +fi + +if [[ -f "${INPUT_PROJECTBASEDIR%/}/build.gradle" || -f "${INPUT_PROJECTBASEDIR%/}/build.gradle.kts" ]]; then + echo "::warning title=SonarScanner::Gradle project detected. Sonar recommends using the SonarQube plugin for Gradle during the build process instead of using this GitHub Action + to get more accurate results." +fi + diff --git a/test/assertFileContains b/test/assertFileContains new file mode 100755 index 0000000..69380e1 --- /dev/null +++ b/test/assertFileContains @@ -0,0 +1,10 @@ +#!/bin/bash + +error() { echo -e "\\e[31m✗ $*\\e[0m"; } + +assertFileExists $1 + +if ! grep -q $2 $1; then + error "'$2' not found in '$1'" + exit 1 +fi \ No newline at end of file diff --git a/test/assertFileDoesntExist b/test/assertFileDoesntExist new file mode 100755 index 0000000..032a07c --- /dev/null +++ b/test/assertFileDoesntExist @@ -0,0 +1,8 @@ +#!/bin/bash + +error() { echo -e "\\e[31m✗ $*\\e[0m"; } + +if [ -f $1 ]; then + error "File '$1' found" + exit 1 +fi \ No newline at end of file diff --git a/test/assertFileExists b/test/assertFileExists new file mode 100755 index 0000000..8f04686 --- /dev/null +++ b/test/assertFileExists @@ -0,0 +1,8 @@ +#!/bin/bash + +error() { echo -e "\\e[31m✗ $*\\e[0m"; } + +if [ ! -f $1 ]; then + error "File '$1' not found" + exit 1 +fi \ No newline at end of file diff --git a/test/example-project/sonar-project.properties b/test/example-project/sonar-project.properties new file mode 100644 index 0000000..4ed4ee7 --- /dev/null +++ b/test/example-project/sonar-project.properties @@ -0,0 +1,2 @@ +sonar.projectKey=foo +sonar.sources=src/ diff --git a/test/example-project/src/main.js b/test/example-project/src/main.js new file mode 100644 index 0000000..909cb02 --- /dev/null +++ b/test/example-project/src/main.js @@ -0,0 +1,5 @@ +function main() { + console.log("Hello World"); +} + +main(); diff --git a/test/gradle-project-kotlin/build.gradle.kts b/test/gradle-project-kotlin/build.gradle.kts new file mode 100644 index 0000000..e69de29 diff --git a/test/gradle-project/build.gradle b/test/gradle-project/build.gradle new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/gradle-project/build.gradle @@ -0,0 +1 @@ + diff --git a/test/maven-project/pom.xml b/test/maven-project/pom.xml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/maven-project/pom.xml @@ -0,0 +1 @@ +