From 8df2487cffd1d54d88667a7197af2a2d7ab4a450 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Wed, 16 Jun 2021 10:36:31 +0200 Subject: [PATCH] SONAR-15028 C-Family config for GitHub Actions --- .../BitbucketPipelinesTutorial-test.tsx.snap | 4 +- .../tutorials/components/YamlFileStep.tsx | 16 +- .../__tests__/YamlFileStep-test.tsx | 9 +- .../__snapshots__/YamlFileStep-test.tsx.snap | 28 +- .../github-action/AnalysisCommand.tsx | 3 + .../AnalysisCommand-test.tsx.snap | 27 +- .../GitHubActionTutorial-test.tsx.snap | 4 +- .../github-action/commands/CFamily.tsx | 162 +++++++++ .../commands/__tests__/CFamily-test.tsx | 48 +++ .../__snapshots__/CFamily-test.tsx.snap | 326 ++++++++++++++++++ 10 files changed, 615 insertions(+), 12 deletions(-) create mode 100644 server/sonar-web/src/main/js/components/tutorials/github-action/commands/CFamily.tsx create mode 100644 server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/CFamily-test.tsx create mode 100644 server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/__snapshots__/CFamily-test.tsx.snap diff --git a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-test.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-test.tsx.snap index d3e72b5eb64..45e2b22a367 100644 --- a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-test.tsx.snap @@ -73,9 +73,9 @@ exports[`should render correctly: repo variable step content 1`] = ` `; exports[`should render correctly: yaml file step content 1`] = ` - [Function] - + `; diff --git a/server/sonar-web/src/main/js/components/tutorials/components/YamlFileStep.tsx b/server/sonar-web/src/main/js/components/tutorials/components/YamlFileStep.tsx index 88026e76c98..1c28586d3e8 100644 --- a/server/sonar-web/src/main/js/components/tutorials/components/YamlFileStep.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/components/YamlFileStep.tsx @@ -20,6 +20,7 @@ import * as React from 'react'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { AlmKeys } from '../../../types/alm-settings'; +import { withCLanguageFeature } from '../../hoc/withCLanguageFeature'; import RenderOptions from '../components/RenderOptions'; import { BuildTools } from '../types'; import AllSet from './AllSet'; @@ -27,6 +28,7 @@ import AllSet from './AllSet'; export interface YamlFileStepProps { alm: AlmKeys; children?: (buildTool?: BuildTools) => React.ReactElement<{}>; + hasCLanguageFeature: boolean; } export interface AnalysisCommandProps { @@ -35,9 +37,15 @@ export interface AnalysisCommandProps { component: T.Component; } -export default function YamlFileStep(props: YamlFileStepProps) { - const { alm, children } = props; - const buildTools = [BuildTools.Maven, BuildTools.Gradle, BuildTools.DotNet, BuildTools.Other]; +export function YamlFileStep(props: YamlFileStepProps) { + const { alm, children, hasCLanguageFeature } = props; + + const buildTools = [BuildTools.Maven, BuildTools.Gradle, BuildTools.DotNet]; + if (hasCLanguageFeature) { + buildTools.push(BuildTools.CFamily); + } + buildTools.push(BuildTools.Other); + const [buildToolSelected, setBuildToolSelected] = React.useState(); return ( @@ -65,3 +73,5 @@ export default function YamlFileStep(props: YamlFileStepProps) { ); } + +export default withCLanguageFeature(YamlFileStep); diff --git a/server/sonar-web/src/main/js/components/tutorials/components/__tests__/YamlFileStep-test.tsx b/server/sonar-web/src/main/js/components/tutorials/components/__tests__/YamlFileStep-test.tsx index 94bb3e9c714..936287cac55 100644 --- a/server/sonar-web/src/main/js/components/tutorials/components/__tests__/YamlFileStep-test.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/components/__tests__/YamlFileStep-test.tsx @@ -20,12 +20,15 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { AlmKeys } from '../../../../types/alm-settings'; -import YamlFileStep, { YamlFileStepProps } from '../YamlFileStep'; +import { YamlFileStep, YamlFileStepProps } from '../YamlFileStep'; it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot(); + expect(shallowRender()).toMatchSnapshot('C unavailable'); + expect(shallowRender({ hasCLanguageFeature: true })).toMatchSnapshot('C available'); }); function shallowRender(props: Partial = {}) { - return shallow(); + return shallow( + + ); } diff --git a/server/sonar-web/src/main/js/components/tutorials/components/__tests__/__snapshots__/YamlFileStep-test.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/components/__tests__/__snapshots__/YamlFileStep-test.tsx.snap index 4682c79068d..c33c7635df3 100644 --- a/server/sonar-web/src/main/js/components/tutorials/components/__tests__/__snapshots__/YamlFileStep-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/components/__tests__/__snapshots__/YamlFileStep-test.tsx.snap @@ -1,6 +1,32 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should render correctly 1`] = ` +exports[`should render correctly: C available 1`] = ` + +
    +
  1. + onboarding.build + +
  2. +
+
+`; + +exports[`should render correctly: C unavailable 1`] = `
    ; case BuildTools.DotNet: return ; + case BuildTools.CFamily: + return ; case BuildTools.Other: return ; } diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/AnalysisCommand-test.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/AnalysisCommand-test.tsx.snap index 4f86247edbf..99f415c28cb 100644 --- a/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/AnalysisCommand-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/AnalysisCommand-test.tsx.snap @@ -1,6 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should render correctly for "cfamily" 1`] = `""`; +exports[`should render correctly for "cfamily" 1`] = ` + +`; exports[`should render correctly for "dotnet" 1`] = ` [Function] - + `; diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/commands/CFamily.tsx b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/CFamily.tsx new file mode 100644 index 00000000000..f673d58461b --- /dev/null +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/CFamily.tsx @@ -0,0 +1,162 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import * as React from 'react'; +import { translate } from 'sonar-ui-common/helpers/l10n'; +import { CompilationInfo } from '../../components/CompilationInfo'; +import CreateYmlFile from '../../components/CreateYmlFile'; +import DefaultProjectKey from '../../components/DefaultProjectKey'; +import RenderOptions from '../../components/RenderOptions'; +import { OSs } from '../../types'; + +export interface CFamilyProps { + branchesEnabled?: boolean; + component: T.Component; +} + +const STEPS = { + [OSs.Linux]: `steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Download and install the build wrapper, build the project + run: | + mkdir $HOME/.sonar + curl -sSLo $HOME/.sonar/build-wrapper-linux-x86.zip \${{ secrets.SONAR_HOST_URL }}/static/cpp/build-wrapper-linux-x86.zip + unzip -o $HOME/.sonar/build-wrapper-linux-x86.zip -d $HOME/.sonar/ + $HOME/.sonar/build-wrapper-linux-x86/build-wrapper-linux-x86-64 --out-dir bw-output + env: + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }} + + - name: SonarQube analysis + uses: SonarSource/sonarqube-scan-action@v1.0.0 + with: + args: -Dsonar.cfamily.build-wrapper-output=bw-output + env: + SONAR_TOKEN: \${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }}`, + [OSs.MacOS]: `steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Download and install the build wrapper + run: | + mkdir $HOME/.sonar + curl -sSLo $HOME/.sonar/build-wrapper-macosx-x86.zip \${{ secrets.SONAR_HOST_URL }}/static/cpp/build-wrapper-macosx-x86.zip + unzip -o $HOME/.sonar/build-wrapper-macosx-x86.zip -d $HOME/.sonar/ + env: + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }} + + - name: Download and install the SonarScanner + run: | + curl -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-macosx.zip + unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/ + + - name: Build and analyse the project + run: | + # Potential improvement : add these paths to the PATH env var. + $HOME/.sonar/build-wrapper-macosx-x86/build-wrapper-macosx-x86 --out-dir bw-output + $HOME/.sonar/sonar-scanner-4.6.2.2472-macosx/bin/sonar-scanner -Dsonar.cfamily.build-wrapper-output=bw-output + env: + SONAR_TOKEN: \${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }}`, + [OSs.Windows]: `steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Download and install the build wrapper + shell: powershell + run: | + $path = "$HOME/.sonar/build-wrapper-win-x86.zip" + mkdir $HOME/.sonar + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + (New-Object System.Net.WebClient).DownloadFile("\${{ secrets.SONAR_HOST_URL }}/static/cpp/build-wrapper-win-x86.zip", $path) + Add-Type -AssemblyName System.IO.Compression.FileSystem + [System.IO.Compression.ZipFile]::ExtractToDirectory($path, "$HOME/.sonar") + env: + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }} + + - name: Download and install the SonarScanner + shell: powershell + run: | + $path = "$HOME/.sonar/sonar-scanner-cli-4.6.2.2472-windows.zip" + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + (New-Object System.Net.WebClient).DownloadFile("https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-windows.zip", $path) + Add-Type -AssemblyName System.IO.Compression.FileSystem + [System.IO.Compression.ZipFile]::ExtractToDirectory($path, "$HOME/.sonar") + + - name: Build and analyse the project + shell: powershell + run: | + $env:Path += ";$HOME/.sonar/build-wrapper-win-x86;$HOME/.sonar/sonar-scanner-4.6.2.2472-windows/bin" + build-wrapper-win-x86-64 --out-dir bw-output + sonar-scanner.bat "-Dsonar.cfamily.build-wrapper-output=bw-output" + env: + SONAR_TOKEN: \${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }}` +}; + +const cfamilyYamlTemplate = (branchesEnabled: boolean, os: OSs) => `name: Build +on: + push: + branches: + - master +${branchesEnabled ? ' pull_request:\n types: [opened, synchronize, reopened]' : ''} + +jobs: + build: + runs-on: + ${STEPS[os]} +`; + +export default function CFamily(props: CFamilyProps) { + const { component, branchesEnabled } = props; + const [os, setOs] = React.useState(); + + return ( + <> + +
  1. + {translate('onboarding.build.other.os')} + setOs(value)} + optionLabelKey="onboarding.build.other.os" + options={Object.values(OSs)} + /> +
  2. + {os && ( + <> + + + + )} + + ); +} diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/CFamily-test.tsx b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/CFamily-test.tsx new file mode 100644 index 00000000000..c809742bb89 --- /dev/null +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/CFamily-test.tsx @@ -0,0 +1,48 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { shallow } from 'enzyme'; +import * as React from 'react'; +import { mockComponent } from '../../../../../helpers/testMocks'; +import RenderOptions from '../../../components/RenderOptions'; +import { OSs } from '../../../types'; +import CFamily, { CFamilyProps } from '../CFamily'; + +it('should render correctly', () => { + expect(shallowRender()).toMatchSnapshot(); +}); + +it.each([ + [OSs.Linux, false], + [OSs.MacOS, true], + [OSs.Windows, true] +])('should render correctly for %s', (os: OSs, branchesEnabled: boolean) => { + const wrapper = shallowRender({ branchesEnabled }); + wrapper + .find(RenderOptions) + .props() + .onCheck(os); + expect(wrapper).toMatchSnapshot(`branches ${branchesEnabled ? 'enabled' : 'disabled'}`); +}); + +function shallowRender(props: Partial = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/__snapshots__/CFamily-test.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/__snapshots__/CFamily-test.tsx.snap new file mode 100644 index 00000000000..6e48693b2a4 --- /dev/null +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/__tests__/__snapshots__/CFamily-test.tsx.snap @@ -0,0 +1,326 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + + +
  3. + + onboarding.build.other.os + + +
  4. +
    +`; + +exports[`should render correctly for linux: branches disabled 1`] = ` + + +
  5. + + onboarding.build.other.os + + +
  6. + + +
    +`; + +exports[`should render correctly for mac: branches enabled 1`] = ` + + +
  7. + + onboarding.build.other.os + + +
  8. + + +
    +`; + +exports[`should render correctly for win: branches enabled 1`] = ` + + +
  9. + + onboarding.build.other.os + + +
  10. + + sonar-scanner.bat \\"-Dsonar.cfamily.build-wrapper-output=bw-output\\" + env: + SONAR_TOKEN: \${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: \${{ secrets.SONAR_HOST_URL }} +" + /> + +
    +`; -- 2.39.5