Jenkins groovy init script for sonarqube configuration - java

I am trying to set sonarqube settings in Jenkins system property using groovy init script but I am getting below error. Can somebody help me to resolve this?
Error
+++++
groovy.lang.GroovyRuntimeException: Could not find matching constructor for:
hudson.plugins.sonar.SonarInstallation(java.lang.String, java.lang.String,
java.lang.String, hudson.plugins.sonar.model.TriggersConfig,
java.lang.String)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1732)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1532)
This is the script that I am using
import hudson.model.*
import jenkins.model.*
import hudson.plugins.sonar.SonarGlobalConfiguration
import hudson.plugins.sonar.*
import hudson.plugins.sonar.model.TriggersConfig
import hudson.tools.*
def inst = Jenkins.getInstance()
println "--> Configuring SonarQube"
SonarGlobalConfiguration global = Hudson.instance.getDescriptorByType(SonarGlobalConfiguration.class)
def sonar_inst = new SonarInstallation(
"SonarQ",
"http://localhost:9000",
"yy", // Token
new TriggersConfig(),
""
)
// Only add ADOP Sonar if it does not exist - do not overwrite existing config
def sonar_installations = sonar_conf.getInstallations()
def sonar_inst_exists = false
sonar_installations.each {
installation = (SonarInstallation) it
if (sonar_inst.getName() == installation.getName()) {
sonar_inst_exists = true
println("Found existing installation: " + installation.getName())
}
}
if (!sonar_inst_exists) {
sonar_installations += sonar_inst
sonar_conf.setInstallations((SonarInstallation[]) sonar_installations)
sonar_conf.save()
}

You missed some parameters. SonarInstallation constructor needs 7 parameters, not 5:
#DataBoundConstructor
public SonarInstallation(String name,
String serverUrl, String serverAuthenticationToken,
String mojoVersion, String additionalProperties, TriggersConfig triggers,
String additionalAnalysisProperties) {
this.name = name;
this.serverUrl = serverUrl;
this.serverAuthenticationToken = serverAuthenticationToken;
this.additionalAnalysisProperties = additionalAnalysisProperties;
this.mojoVersion = mojoVersion;
this.additionalProperties = additionalProperties;
this.triggers = triggers;
}

Related

Problem while authenticating URL in spock testing

I have a groovy file gitClone.groovy which has a function call.
def call(credentials, url, project, branch, path, refs, noTags=false,
timeout=20)
{
}
I am writing a test for validating the 'url'. I want to write a test case which will validate if the url is correct or not. My test file is as follows:
gitCloneSpec.groovy
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification;
import spock.lang.*
import java.net.URL;
public class gitCloneSpec extends JenkinsPipelineSpecification {
def gitClone = null
def check = 0
def setup() {
gitClone = loadPipelineScriptForTest("vars/gitClone.groovy")
gitClone.getBinding().setVariable( "url", "https://www.google.com/")
}
def "validate url"(){
when:
try {
URL u = new URL(url)
u.toURI()
check = 1
}
// If there was an Exception
// while creating URL object
catch (Exception e) {
check = 2;
}
then:
check == 1
}
}
Somehow url is not able to store the string "http://www.google.com" and it is throwing the exception where 'check' is getting updated with value '2'
How can I perform this test?

hadoop distributed copy overwrite not working

I am trying to use the org.apache.hadoop.tools.DistCp class to copy some files over into a S3 bucket. However overwrite functionality is not working in spite of explicitly setting the overwrite flag to true
Copying works fine but it does not overwrite if there are existing files. The copy mapper skips those files. I have explicitly set the "overwrite" option to true.
import com.typesafe.scalalogging.LazyLogging
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.apache.hadoop.tools.{DistCp, DistCpOptions}
import org.apache.hadoop.util.ToolRunner
import scala.collection.JavaConverters._
object distcptest extends App with LazyLogging {
def copytoS3( hdfsSrcFilePathStr: String, s3DestPathStr: String) = {
val hdfsSrcPathList = List(new Path(hdfsSrcFilePathStr))
val s3DestPath = new Path(s3DestPathStr)
val distcpOpt = new DistCpOptions(hdfsSrcPathList.asJava, s3DestPath)
// Overwriting is not working inspite of explicitly setting it to true.
distcpOpt.setOverwrite(true)
val conf: Configuration = new Configuration()
conf.set("fs.s3n.awsSecretAccessKey", "secret key")
conf.set("fs.s3n.awsAccessKeyId", "access key")
conf.set("fs.s3n.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem")
val distCp: DistCp = new DistCp(conf, distcpOpt)
val filepaths: Array[String] = Array(hdfsSrcFilePathStr, s3DestPathStr)
try {
val distCp_result = ToolRunner.run(distCp, filepaths)
if (distCp_result != 0) {
logger.error(s"DistCP has failed with - error code = $distCp_result")
}
}
catch {
case e: Exception => {
e.printStackTrace()
}
}
}
copytoS3("hdfs://abc/pqr", "s3n://xyz/wst")
}
I think the problem is you called ToolRunner.run(distCp, filepaths).
If you check the source code of DistCp, in run method will overwrite inputOptions, so the DistCpOptions passed to constructor will not work.
#Override
public int run(String[] argv) {
...
try {
inputOptions = (OptionsParser.parse(argv));
...
} catch (Throwable e) {
...
}
...
}

How to get version attribute from a gradle build to be included in runtime Swing application

I have a simple parent project with modules/applications within it. My build tool of choice is gradle. The parent build.gradle is defined below.
apply plugin: 'groovy'
dependencies {
compile gradleApi()
compile localGroovy()
}
allprojects {
repositories {
mavenCentral()
}
version "0.1.0-SNAPSHOT"
}
What I would like to do is utilize the version attribute (0.1.0-SNAPSHOT) within my swing application. Specifically, I'd like it to display in the titlebar of the main JFrame. I expect to be able to do something like this.setTitle("My Application - v." + ???.version);
The application is a plain java project, but I'm not opposed to adding groovy support it it will help.
I like creating a properties file during the build. Here's a way to do that from Gradle directly:
task createProperties(dependsOn: processResources) {
doLast {
new File("$buildDir/resources/main/version.properties").withWriter { w ->
Properties p = new Properties()
p['version'] = project.version.toString()
p.store w, null
}
}
}
classes {
dependsOn createProperties
}
You can always use brute force as somebody suggested and generate properties file during build. More elegant answer, which works only partially would be to use
getClass().getPackage().getImplementationVersion()
Problem is that this will work only if you run your application from generated jar - if you run it directly from IDE/expanded classes, getPackage above will return null. It is good enough for many cases - just display 'DEVELOPMENT' if you run from IDE(geting null package) and will work for actual client deployments.
Better idea is to keep the project version in gradle.properties file. All the properties from this file will be automatically loaded and can be used in build.gradle script.
Then if you need the version in your swing application, add a version.properties file under src/main/resources folder and filter this file during application build, here is a post that shows how it should be done.
version.properties will be included in the final jar, hence can be read and via ClassLoader and properties from this file can be displayed in application.
Simpler and updated solution of #Craig Trader (ready for Gradle 4.0/5.0)
task createProperties {
doLast {
def version = project.version.toString()
def file = new File("$buildDir/resources/main/version.txt")
file.write(version)
}
}
war {
dependsOn createProperties
}
I used #Craig Trader's answer, but had to add quite some changes to make it work (it also adds git-details):
task createProperties() {
doLast {
def details = versionDetails()
new File("$buildDir/resources/main/version.properties").withWriter { w ->
Properties p = new Properties()
p['version'] = project.version.toString()
p['gitLastTag'] = details.lastTag
p['gitCommitDistance'] = details.commitDistance.toString()
p['gitHash'] = details.gitHash.toString()
p['gitHashFull'] = details.gitHashFull.toString() // full 40-character Git commit hash
p['gitBranchName'] = details.branchName // is null if the repository in detached HEAD mode
p['gitIsCleanTag'] = details.isCleanTag.toString()
p.store w, null
}
// copy needed, otherwise the bean VersionController can't load the file at startup when running complete-app tests.
copy {
from "$buildDir/resources/main/version.properties"
into "bin/main/"
}
}
}
classes {
dependsOn createProperties
}
And load it from the constructor of class: VersionController
import static net.logstash.logback.argument.StructuredArguments.v;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
#RestController
public class VersionController {
final static Logger log = LoggerFactory.getLogger(AppInfoController.class);
private Properties versionProperties = new Properties();
private String gitLastTag;
private String gitHash;
private String gitBranchName;
private String gitIsCleanTag;
VersionController()
{
String AllGitVersionProperties = "";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("classpath:/version.properties");
if(inputStream == null)
{
// When running unit tests, no jar is built, so we load a copy of the file that we saved during build.gradle.
// Possibly this also is the case during debugging, therefore we save in bin/main instead of bin/test.
try {
inputStream = new FileInputStream("bin/main/version.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
versionProperties.load(inputStream);
} catch (IOException e) {
AllGitVersionProperties += e.getMessage()+":";
log.error("Could not load classpath:/version.properties",e);
}
gitLastTag = versionProperties.getProperty("gitLastTag","last-tag-not-found");
gitHash = versionProperties.getProperty("gitHash","git-hash-not-found");
gitBranchName = versionProperties.getProperty("gitBranchName","git-branch-name-not-found");
gitIsCleanTag = versionProperties.getProperty("gitIsCleanTag","git-isCleanTag-not-found");
Set<Map.Entry<Object, Object>> mainPropertiesSet = versionProperties.entrySet();
for(Map.Entry oneEntry : mainPropertiesSet){
AllGitVersionProperties += "+" + oneEntry.getKey()+":"+oneEntry.getValue();
}
log.info("All Git Version-Properties:",v("GitVersionProperties", AllGitVersionProperties));
}
}
Using #Craig Trader's solution to save the properties in a version.properties file. Add to build.gradle:
task createProperties() {
doLast {
def details = versionDetails()
new File("$buildDir/resources/main/version.properties").withWriter { w ->
Properties p = new Properties()
p['version'] = project.version.toString()
p['gitLastTag'] = details.lastTag
p['gitCommitDistance'] = details.commitDistance.toString()
p['gitHash'] = details.gitHash.toString()
p['gitHashFull'] = details.gitHashFull.toString() // full 40-character Git commit hash
p['gitBranchName'] = details.branchName // is null if the repository in detached HEAD mode
p['gitIsCleanTag'] = details.isCleanTag.toString()
p.store w, null
}
// copy needed, otherwise the bean VersionController can't load the file at startup when running complete-app tests.
copy {
from "$buildDir/resources/main/version.properties"
into "bin/main/"
}
}
}
classes {
dependsOn createProperties
}
To load the properties runtime in version.properties you need to annotate your class with #PropertySource({"classpath:version.properties"})
Then you can assign a property to a private variable with annotation like:
#Value("${gitLastTag}")
private String gitLastTag;
Full example:
package com.versioncontroller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
import java.util.Properties;
#PropertySource({"classpath:version.properties"})
public class VersionController {
#Value("${gitLastTag}")
private String gitLastTag;
#Value("${gitHash}")
private String gitHash;
#Value("${gitBranchName}")
private String gitBranchName;
#Value("${gitIsCleanTag}")
private String gitIsCleanTag;
#PostConstruct // properties are only set after the constructor has run
private void logVersion(){
// when called during the constructor, all values are null.
System.out.println("All Git Version-Properties:");
System.out.println("gitLastTag: " + gitLastTag),
System.out.println("gitHash: " + gitHash),
System.out.println("gitBranchName: " + gitBranchName),
System.out.println("gitIsCleanTag: " + gitIsCleanTag));
}
}

Aspect-Oriented programming issue in grails . Point-cut methods are not executing

I am using Grails 2.4.3 . In resource.groovy I have added component-scan
xmlns aop:"http://www.springframework.org/schema/aop"
context.'component-scan'('base-package': 'com')
Then in src/groovy I have created groovy class
package com.demo.aspects.mongo.history;
import grails.transaction.Transactional
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.After
import org.springframework.stereotype.Component
#Aspect
#Component
#Transactional
public class MongoAspectAdvice {
#After("execution(* com.demo.global.BaseOptionService.save(..))")
def afterMethod(){
println "after method execution"
}
#Before("execution(* com.demo.global.BaseOptionService.save(..))")
def beforeMethod(){
println "before method execution"
}
}
And save funtion in com.demo.global.BaseOptionService is defined as
def save(def entity){
if(entity.id == null){
entity.createdDate = new Date()
}
entity.lastUpdatedDate = new Date()
log.debug(entity)
neo4jTemplate.save(entity)
}
And BaseOptionService is extended by UserService in which BaseOptionService method is called.
UserService.groovy
class UserService extends BaseOptionService{
def addUser(username,email,role,phonenumber){
log.debug "user ===== "+username
UserCommand userCommand = new UserCommand()
userCommand.username = username
userCommand.email = email
userCommand.phonenumber = phonenumber
userCommand.role = role
if(userCommand.labels?.empty == true || userCommand.labels == null){
if(role == null){
userCommand.addLabel(null)
}else{
userCommand.addLabel("_USER")
userCommand.addLabel(role)
}
}
userCommand.isActive = true
userCommand.token = null
UserDomain user = userCommand.getUser()
log.debug "user == "+user
save(user)
return user
}
def removeLabels(id,label){
UserDomain user = findOne(id,UserDomain)
if(user.labels?.contains(label)){
user.labels.remove(label)
}
save(user)
return user
}
def serviceMethod() {
}
}
When save function is executing , I haven't seen println statement of afterMethod and beforeMethod in console and there is no error . I am not sure what wrong I am doing. Please help.
when your application start, the component scan will scan all packages for check what beans are present.
This process anyway don't instantiate the bean if it's not referenced by any other class. In other way you are telling that yes i have a MongoAspectAdvice class that is annotated with #Component so it's also a singleton but where you use it?
Try to import it in UserService.
Then if stil not working ( for check that you can put a breakpoint in that class ) add the following code to you resources.groovy:
// Place your Spring DSL code here
beans = {
mongoAspectAdvice(MongoAspectAdvice) { bean ->
bean.autowire = "byName"
}
And then import in your service:
def mongoAspectAdvice
However you can replicate this behaviour by using some grails ad-hc closures:
def beforeInterceptor = {
println "Tracing action ${actionUri}"
}
And
def afterInterceptor = {
println "Tracing action ${actionUri}"
}
Put them in BaseOptionService

Groovy sandbox and Java: execute Groovy script

Task: execute groovy script with Groovy sandbox:
http://groovy.codehaus.org/api/overview-summary.html
http://groovy-sandbox.kohsuke.org/
Groovy Script to execute:
query.reverse(); // QUERY is a some string that should be reversed
File "GroovyScriptSandbox.groovy" should get two parameters(script and values for this script):
package test.my.groovy.sandbox
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ImportCustomizer
import org.codehaus.groovy.control.customizers.SecureASTCustomizer
import org.springframework.stereotype.Component
#Component
class GroovyScriptSandbox {
def config
def shell
public String runScript(final String script, final String query) {
final ImportCustomizer imports = new ImportCustomizer()
.addStarImports('groovyx.net.http')
.addStaticStars('groovyx.net.http.ContentType', 'groovyx.net.http.Method')
config = new CompilerConfiguration()
config.addCompilationCustomizers(imports)
def newScript = "{ query -> " + script + "}"
shell = new GroovyShell(config)
def clos = shell.evaluate(newScript)
return clos.call(query)
}
}
Java method that executes "GroovyScriptSandbox.groovy":
#Resource
private GroovyScriptSandbox groovyScriptSandbox;
#RequestMapping(value = "/run", method = RequestMethod.POST)
#ResponseBody
public String runScript(#RequestParam("script") final String script,
#RequestParam("query") final String query) {
return groovyScriptSandbox.runScript(script, query);
}
In that case all works fine:
Java controller getting script parameter equal "query.reverse()" and query parameter equals "0123"
Groovy file executes script "query.reverse()" in sandbox where query equals "0123"
Result equals "3210"
Question:
I'm trying to replace "GroovyScriptSandbox.groovy" file with "GroovyScriptSandbox.java" and I don't know how to write the same groovy code in Java.
Finally found solution:
public String scriptRunner(final String script, final String query) {
final ImportCustomizer imports = new ImportCustomizer();
imports.addStaticStars("java.lang.Math");
imports.addStarImports("groovyx.net.http");
imports.addStaticStars("groovyx.net.http.ContentType", "groovyx.net.http.Method");
final SecureASTCustomizer secure = new SecureASTCustomizer();
secure.setClosuresAllowed(true);
final CompilerConfiguration config = new CompilerConfiguration();
config.addCompilationCustomizers(imports, secure);
final Binding intBinding = new Binding(); // alow parameters in the script
intBinding.setVariable("query", query);
final GroovyShell shell = new GroovyShell(intBinding, config); // create shall
// code execution
final Object clos = shell.evaluate(script);
if (clos == null) {
return "No result avalible!";
}
return clos.toString();
}

Categories

Resources