From fb56fdc0dc18d277ccfae2cdb948e9da367377ea Mon Sep 17 00:00:00 2001
From: Elena Vilchik
Date: Tue, 12 Jun 2018 13:34:33 +0200
Subject: Create CSS language and set up trivial ITs (#39)
---
Jenkinsfile | 127 +++++++++++++++++++++
its/plugin/pom.xml | 88 ++++++++++++++
.../projects/css-plugin-test-project/src/file1.css | 7 ++
.../css-plugin-test-project/src/file2.less | 8 ++
.../css-plugin-test-project/src/file3.scss | 9 ++
.../css-plugin-test-project/src/file4.html | 10 ++
.../test/java/org/sonar/css/its/MetricsTest.java | 47 ++++++++
.../src/test/java/org/sonar/css/its/Tests.java | 91 +++++++++++++++
its/pom.xml | 20 ++++
pom.xml | 19 +--
sonar-css-plugin/pom.xml | 4 -
.../java/org/sonar/css/plugin/CssLanguage.java | 41 +++++++
.../main/java/org/sonar/css/plugin/CssPlugin.java | 20 ++++
.../java/org/sonar/css/plugin/SonarWayProfile.java | 35 ++++++
.../java/org/sonar/css/plugin/package-info.java | 21 ++++
.../java/org/sonar/css/plugin/CssPluginTest.java | 42 +++++++
16 files changed, 567 insertions(+), 22 deletions(-)
create mode 100644 Jenkinsfile
create mode 100644 its/plugin/pom.xml
create mode 100644 its/plugin/projects/css-plugin-test-project/src/file1.css
create mode 100644 its/plugin/projects/css-plugin-test-project/src/file2.less
create mode 100644 its/plugin/projects/css-plugin-test-project/src/file3.scss
create mode 100644 its/plugin/projects/css-plugin-test-project/src/file4.html
create mode 100644 its/plugin/src/test/java/org/sonar/css/its/MetricsTest.java
create mode 100644 its/plugin/src/test/java/org/sonar/css/its/Tests.java
create mode 100644 its/pom.xml
create mode 100644 sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssLanguage.java
create mode 100644 sonar-css-plugin/src/main/java/org/sonar/css/plugin/SonarWayProfile.java
create mode 100644 sonar-css-plugin/src/main/java/org/sonar/css/plugin/package-info.java
create mode 100644 sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssPluginTest.java
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..0503600
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,127 @@
+@Library('SonarSource@1.11') _
+
+pipeline {
+ agent {
+ label 'linux'
+ }
+ parameters {
+ string(name: 'GIT_SHA1', description: 'Git SHA1 (provided by travisci hook job)')
+ string(name: 'CI_BUILD_NAME', defaultValue: 'sonar-css', description: 'Build Name (provided by travisci hook job)')
+ string(name: 'CI_BUILD_NUMBER', description: 'Build Number (provided by travisci hook job)')
+ string(name: 'GITHUB_BRANCH', defaultValue: 'master', description: 'Git branch (provided by travisci hook job)')
+ string(name: 'GITHUB_REPOSITORY_OWNER', defaultValue: 'SonarSource', description: 'Github repository owner(provided by travisci hook job)')
+ }
+ environment {
+ SONARSOURCE_QA = 'true'
+ MAVEN_TOOL = 'Maven 3.3.x'
+ }
+ stages {
+ stage('Notify') {
+ steps {
+ sendAllNotificationQaStarted()
+ }
+ }
+ stage('QA') {
+ parallel {
+ stage('ITs-lts') {
+ agent {
+ label 'linux'
+ }
+ steps {
+ runITs "LATEST_RELEASE[6.7]"
+ }
+ }
+
+ stage('ITs-latest') {
+ agent {
+ label 'linux'
+ }
+ steps {
+ runITs "LATEST_RELEASE"
+ }
+ }
+
+ stage('ITs-windows') {
+ agent {
+ label 'windows'
+ }
+ steps {
+ runITsWindows "LATEST_RELEASE"
+ }
+ }
+
+ stage('ITs-dev') {
+ agent {
+ label 'linux'
+ }
+ steps {
+ runITs "DEV"
+ }
+ }
+
+ stage('CI-windows') {
+ agent {
+ label 'windows'
+ }
+ steps {
+ withMaven(maven: MAVEN_TOOL) {
+ sh 'mvn.cmd clean test'
+ }
+ }
+ }
+ }
+ post {
+ always {
+ sendAllNotificationQaResult()
+ }
+ }
+ }
+ stage('Promote') {
+ steps {
+ repoxPromoteBuild()
+ }
+ post {
+ always {
+ sendAllNotificationPromote()
+ }
+ }
+ }
+ }
+}
+
+def runITs(String sqRuntimeVersion) {
+ withQAEnv {
+ withMaven(maven: MAVEN_TOOL) {
+ mavenSetBuildVersion()
+ dir('its') {
+ sh "mvn ${itBuildArguments sqRuntimeVersion}"
+ }
+ }
+ }
+}
+
+def runITsWindows(String sqRuntimeVersion) {
+ withQAEnv {
+ withMaven(maven: MAVEN_TOOL) {
+ mavenSetBuildVersion()
+ dir('its') {
+ sh "mvn.cmd ${itBuildArguments sqRuntimeVersion}"
+ }
+ }
+ }
+}
+
+def withQAEnv(def body) {
+ checkout scm
+ withCredentials([string(credentialsId: 'ARTIFACTORY_PRIVATE_API_KEY', variable: 'ARTIFACTORY_PRIVATE_API_KEY'),
+ usernamePassword(credentialsId: 'ARTIFACTORY_PRIVATE_USER', passwordVariable: 'ARTIFACTORY_PRIVATE_PASSWORD', usernameVariable: 'ARTIFACTORY_PRIVATE_USERNAME')]) {
+ wrap([$class: 'Xvfb']) {
+ body.call()
+ }
+ }
+}
+
+String itBuildArguments(String sqRuntimeVersion) {
+ "-Pits -Dsonar.runtimeVersion=${sqRuntimeVersion} -Dorchestrator.artifactory.apiKey=${env.ARTIFACTORY_PRIVATE_API_KEY} " +
+ "-Dorchestrator.configUrl=http://infra.internal.sonarsource.com/jenkins/orch-h2.properties -Dmaven.test.redirectTestOutputToFile=false clean verify -e -V"
+}
diff --git a/its/plugin/pom.xml b/its/plugin/pom.xml
new file mode 100644
index 0000000..24aa42c
--- /dev/null
+++ b/its/plugin/pom.xml
@@ -0,0 +1,88 @@
+
+
Hello World!
diff --git a/its/plugin/src/test/java/org/sonar/css/its/MetricsTest.java b/its/plugin/src/test/java/org/sonar/css/its/MetricsTest.java new file mode 100644 index 0000000..232e955 --- /dev/null +++ b/its/plugin/src/test/java/org/sonar/css/its/MetricsTest.java @@ -0,0 +1,47 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 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. + */ +package org.sonar.css.its; + +import com.sonar.orchestrator.Orchestrator; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.css.its.Tests.getProjectMeasureAsDouble; + +public class MetricsTest { + + private static String PROJECT_KEY = "css-plugin-test-project"; + + @ClassRule + public static Orchestrator orchestrator = Tests.ORCHESTRATOR; + + @BeforeClass + public static void prepare() { + orchestrator.executeBuild(Tests.createScanner(PROJECT_KEY)); + } + + @Test + public void test() { + assertThat(getProjectMeasureAsDouble("lines", PROJECT_KEY)).isEqualTo(27); + } + +} diff --git a/its/plugin/src/test/java/org/sonar/css/its/Tests.java b/its/plugin/src/test/java/org/sonar/css/its/Tests.java new file mode 100644 index 0000000..4e38073 --- /dev/null +++ b/its/plugin/src/test/java/org/sonar/css/its/Tests.java @@ -0,0 +1,91 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 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. + */ +package org.sonar.css.its; + +import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.OrchestratorBuilder; +import com.sonar.orchestrator.build.SonarScanner; +import com.sonar.orchestrator.locator.FileLocation; +import java.io.File; +import java.util.Collections; +import java.util.List; +import org.junit.ClassRule; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.sonarqube.ws.Measures.ComponentWsResponse; +import org.sonarqube.ws.Measures.Measure; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsClientFactories; +import org.sonarqube.ws.client.measures.ComponentRequest; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + MetricsTest.class +}) +public class Tests { + + private static final FileLocation PLUGIN_LOCATION = FileLocation.byWildcardMavenFilename( + new File("../../sonar-css-plugin/target"), "sonar-css-plugin-*.jar"); + + @ClassRule + public static final Orchestrator ORCHESTRATOR; + + static { + String defaultRuntimeVersion = "true".equals(System.getenv("SONARSOURCE_QA")) ? null : "7.2.0.13185"; // TODO LATEST_RELEASE[7.2] + String sqVersion = System.getProperty("sonar.runtimeVersion", defaultRuntimeVersion); + OrchestratorBuilder orchestratorBuilder = Orchestrator.builderEnv() + .addPlugin(PLUGIN_LOCATION) + .setSonarVersion(sqVersion); + ORCHESTRATOR = orchestratorBuilder.build(); + } + + public static WsClient newWsClient() { + return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() + .url(ORCHESTRATOR.getServer().getUrl()) + .build()); + } + + public static Double getProjectMeasureAsDouble(String metricKey, String projectKey) { + Measure measure = getMeasure(metricKey, projectKey); + return (measure == null) ? null : Double.parseDouble(measure.getValue()); + } + + private static Measure getMeasure(String metricKey, String projectKey) { + ComponentWsResponse response = newWsClient().measures().component(new ComponentRequest() + .setComponent(projectKey) + .setMetricKeys(Collections.singletonList(metricKey))); + List