Install Snyk in Jenkins "Global Tool Configuration" using groovy - jenkins

I'm trying to add a Snyk installation to Jenkins using groovy. The plugin is installed and I can see the installation option in Global Tool Configuration:
The problem is the Descriptor is not available until I manually add the installer and click Save. If I don't do this task manually, which I want to prevent, it causes my code to fail with the following error message "Cannot invoke method setInstallations() on null object"
My code:
import hudson.model.*
import jenkins.model.*
import hudson.tools.*
import hudson.tasks.*
import io.snyk.jenkins.tools.SnykInstaller
import io.snyk.jenkins.tools.SnykInstallation
def snyk_name = "Snyk"
def snyk_home = ""
def snyk_installer = new SnykInstaller("", "latest", 24)
def snyk_properties = new InstallSourceProperty([snyk_installer])
def instance = Jenkins.getInstance()
println("[init.groovy.d] START Configuring Snyk Installation...")
// Get the GlobalConfiguration descriptor of Snyk plugin.
def snyk_conf = instance.getDescriptor("io.snyk.jenkins.SnykStepBuilder.SnykStepBuilderDescriptor")
def snyk_inst = new SnykInstallation(
snyk_name,
snyk_home,
[snyk_properties]
)
// Only add the new Snyk setting if it does not exist - do not overwrite existing config
def snyk_installations = snyk_conf.getInstallations()
def snyk_inst_exists = false
snyk_installations.each {
installation = (SnykInstallation) it
if (snyk_inst.getName() == installation.getName()) {
snyk_inst_exists = true
println("Found existing installation: " + installation.getName())
}
}
if (!snyk_inst_exists) {
snyk_installations += snyk_inst
snyk_conf.setInstallations((SnykInstallation[]) snyk_installations)
snyk_conf.save()
}
// Save the state
instance.save()
println("[init.groovy.d] END")
Is there any way to do what I want programmatically?

After testing your groovy on my local Jenkins (v 2.263.1) I came up with the below which then worked for me:
import hudson.model.*
import jenkins.model.*
import hudson.tools.*
import hudson.tasks.*
import io.snyk.jenkins.tools.*
def instance = Jenkins.getInstance()
def snyk_name = "SnykLatest"
def snyk_home = ""
def snyk_installer = new SnykInstaller("", "latest", 24L, null)
def snyk_properties = new InstallSourceProperty([snyk_installer])
println("[init.groovy.d] START Configuring Snyk Installation...")
// Get the GlobalConfiguration descriptor of Snyk plugin.
def snyk_conf = instance.getDescriptor("io.snyk.jenkins.tools.SnykInstallation")
def snyk_inst = new SnykInstallation(
snyk_name,
snyk_home,
[snyk_properties]
)
// Only add the new Snyk setting if it does not exist - do not overwrite existing config
def snyk_installations = snyk_conf.getInstallations()
def snyk_inst_exists = false
snyk_installations.each {
installation = (SnykInstallation) it
if (snyk_inst.getName() == installation.getName()) {
snyk_inst_exists = true
println("Found existing installation: " + installation.getName())
}
}
if (!snyk_inst_exists) {
snyk_installations += snyk_inst
snyk_conf.setInstallations((SnykInstallation[]) snyk_installations)
snyk_conf.save()
}
// Save the state
instance.save()
println("[init.groovy.d] END")
In basic Terms the SnykInstaller was expecting 4 values not 3. Groovy also took the 3rd value as an Integer when it was expecting a Long Value.
References:
https://javadoc.jenkins.io/plugin/snyk-security-scanner/io/snyk/jenkins/tools/SnykInstaller.html
https://github.com/jenkinsci/snyk-security-scanner-plugin/blob/master/src/main/java/io/snyk/jenkins/tools/SnykInstaller.java
https://github.com/jenkinsci/snyk-security-scanner-plugin/blob/master/src/main/java/io/snyk/jenkins/tools/PlatformItem.java

Related

How to pass parameter to groovy post build in jenkins?

I have the following Groovy Postbuild script in a jenkins job:
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
def sendPostRequest(urlString, paramString) {
def url = new URL(urlString)
def conn = url.openConnection()
conn.setDoOutput(true)
def writer = new OutputStreamWriter(conn.getOutputStream())
writer.write(paramString)
writer.flush()
String line
def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
while ((line = reader.readLine()) != null) {
println line
}
writer.close()
reader.close()
}
def result =" Project: *Autotests* \n Version *manager.build.buildVariables.get("VERSION_CURR")* \n"
sendPostRequest("https://api.telegram.org/bot{token}/sendMessage", "parse_mode=Markdown&chat_id={id_Chat}&reply_to_message_id=&text=${result}")
But I am getting an error: "groovy.lang.MissingPropertyException: No such property: VERSION_CURR for class: Script1"
However, I have a string parameter:
i also tried: def result =' Project: *Autotests* \n Version *${VERSION_CURR}* \n'

Groovy script to create masked password in Jenkins

I am looking for a Groovy script that can create a masked password pair in Jenkins.
I've tried the following but it doesn't work.
import java.util.logging.Logger
import jenkins.model.*
import hudson.logging.*
//import com.michelin.cio.hudson.plugins.maskpasswords.*
import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsConfig
import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper.VarPasswordPair
maskPasswordsConfig = MaskPasswordsConfig.getInstance()
varPasswordPairs = maskPasswordsConfig.getGlobalVarPasswordPairs()
MaskPasswordsConfig plugin = new MaskPasswordsConfig()
VarPasswordPair pwdPair = new VarPasswordPair("PWD", "myPassword")
plugin.addGlobalVarPasswordPair(pwdPair)
plugin.save()
I'm getting no results in the script console. Any help is appreciated.
#mweish, Just come across your post as I was trying to setup something similar, this should do what you are looking for:
import java.util.logging.Logger
import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsConfig
import com.michelin.cio.hudson.plugins.maskpasswords.MaskPasswordsBuildWrapper.VarPasswordPair
import jenkins.model.*
import hudson.logging.*
Logger logger = Logger.getLogger("")
Jenkins jenkins = Jenkins.getInstance()
MaskPasswordsConfig plugin = new MaskPasswordsConfig()
MASKED_PARAMETER_CLASSES = [
'com.michelin.cio.hudson.plugins.passwordparam.PasswordParameterDefinition',
'hudson.model.PasswordParameterDefinition'
]
def NAME_PASSWORD_PAIRS = [
[NAME:'PWD', PASSWORD:'myPassword']
]
// Add classes that should automatically be masked
MASKED_PARAMETER_CLASSES.each { maskedClass ->
plugin.addMaskedPasswordParameterDefinition(maskedClass)
}
// Add Global name/password pairs
NAME_PASSWORD_PAIRS.each { namePassPair ->
VarPasswordPair passwordPair = new VarPasswordPair(namePassPair.NAME, namePassPair.PASSWORD)
plugin.addGlobalVarPasswordPair(passwordPair)
}
plugin.save(plugin)
logger.info('Successfully Configured the Mask Passwords plugin')

How to minimize the complexity of this code ,by avoiding it to traverse all the jenkins job(lets say 5000 jobs?)

I'm changing my jenkins job's SCM url from, lets say gitlab to github by using Groovy script. Is there any way to minimize the complexity of this code ?
I have used Jenkins.instance.items.each {} which traverses through all the jobs in my jenkins
import hudson.plugins.git.*
import jenkins.*
import jenkins.model.*
def modifyGitUrl(url) {
def newurl = url.replace("source", "destination")
return newurl
}
def modifyGitID(credentialsId) {
def newID = ("Jenkins User ID")
return newID
}
Jenkins.instance.items.each {
if(it.name == "expected name"){
if (it.scm instanceof GitSCM) {
def oldScm = it.scm
def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, modifyGitID(it.credentialsId))
}
def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
it.scm = newScm
it.save()
}
}
}

issues with groovy script

I have the following groovy script that does the following
- checks all the builds for a specific job to see if it has the same build parameter as the current build
- stops the build with the same parameter as the current build
import hudson.tasks.Ant
def myBuildNumber = build.getEnvVars()['BUILD_NUMBER'].toInteger();
def myFractureNO = build.getEnvVars()['FRACTURE_NO'].toInteger();
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
projectXml.build.each {
if(it.number.toInteger() < myBuildNumber)
{
def jobXml = new XmlSlurper().parseText(("curl http://test.corp.test.com:8080/job/FractureAUT/" + it.number + "/api/xml").execute().text);
def myparams = jobXml.getAction(hudson.model.ParametersAction.class);
for( p in myparams ) {
printlin p.name.toString();
if (p.name.toString == "FRACTURE_NO") {
if (p.value.toString() == myFractureNO.toString()) {
"curl http://test.corp.adobe.com:8080/job/FractureAUT/" + it.number + "/stop".execute().waitFor();
break;
}
}
}
}
}
the script outputs a bunch of urls to the screen because of the line:
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
and it doesn't do what it suppose to do. How can I make it so that the line
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
doesn't output the urls to the screen?
I get the following output
189http://test.corp.test.com:8080/job/FractureAUT/189/188http://test.corp.test.com:8080/job/FractureAUT/188/187http://test.corp.test.com:8080/job/FractureAUT/187/186http://test.corp.test.com:8080/job/FractureAUT/186/185http://test.corp.test.com:8080/job/FractureAUT/185/184http://test.corp.test.com:8080/job/FractureAUT/184/183http://test.corp.test.com:8080/job/FractureAUT/183/182http://test.corp.test.com:8080/job/FractureAUT/182/181http://test.corp.test.com:8080/job/FractureAUT/181/179http://test.corp.test.com:8080/job/FractureAUT/179/178http://test.corp.test.com:8080/job/FractureAUT/178/177http://test.corp.test.com:8080/job/FractureAUT/177/176http://test.corp.test.com:8080/job/FractureAUT/176/174http://test.corp.test.com:8080/job/FractureAUT/174/173http://test.corp.test.com:8080/job/FractureAUT/173/172http://test.corp.test.com:8080/job/FractureAUT/172/171http://test.corp.test.com:8080/job/FractureAUT/171/170http://test.corp.test.com:8080/job/FractureAUT/170/169http://test.corp.test.com:8080/job/FractureAUT/169/168http://test.corp.test.com:8080/job/FractureAUT/168/167http://test.corp.test.com:8080/job/FractureAUT/167/166http://test.corp.test.com:8080/job/FractureAUT/166/165http://test.corp.test.com:8080/job/FractureAUT/165/164http://test.corp.test.com:8080/job/FractureAUT/164/163http://test.corp.test.com:8080/job/FractureAUT/163/162http://test.corp.test.com:8080/job/FractureAUT/162/161http://test.corp.test.com:8080/job/FractureAUT/161/160http://test.corp.test.com:8080/job/FractureAUT/160/159http://test.corp.test.com:8080/job/FractureAUT/159/158http://test.corp.test.com:8080/job/FractureAUT/158/157http://test.corp.test.com:8080/job/FractureAUT/157/156http://test.corp.test.com:8080/job/FractureAUT/156/155http://test.corp.test.com:8080/job/FractureAUT/155/154http://test.corp.test.com:8080/job/FractureAUT/154/153http://test.corp.test.com:8080/job/FractureAUT/153/152http://test.corp.test.com:8080/job/FractureAUT/152/151http://test.corp.test.com:8080/job/FractureAUT/151/150http://test.corp.test.com:8080/job/FractureAUT/150/148http://test.corp.test.com:8080/job/FractureAUT/148/146http://test.corp.test.com:8080/job/FractureAUT/146/144http://test.corp.test.com:8080/job/FractureAUT/144/143http://test.corp.test.com:8080/job/FractureAUT/143/142http://test.corp.test.com:8080/job/FractureAUT/142/141http://test.corp.test.com:8080/job/FractureAUT/141/140http://test.corp.test.com:8080/job/FractureAUT/140/139http://test.corp.test.com:8080/job/FractureAUT/139/138http://test.corp.test.com:8080/job/FractureAUT/138/137http://test.corp.test.com:8080/job/FractureAUT/137/136http://test.corp.test.com:8080/job/FractureAUT/136/135http://test.corp.test.com:8080/job/FractureAUT/135/134http://test.corp.test.com:8080/job/FractureAUT/134/133http://test.corp.test.com:8080/job/FractureAUT/133/132http://test.corp.test.com:8080/job/FractureAUT/132/131http://test.corp.test.com:8080/job/FractureAUT/131/130http://test.corp.test.com:8080/job/FractureAUT/130/129http://test.corp.test.com:8080/job/FractureAUT/129/128http://test.corp.test.com:8080/job/FractureAUT/128/127http://test.corp.test.com:8080/job/FractureAUT/127/126http://test.corp.test.com:8080/job/FractureAUT/126/125http://test.corp.test.com:8080/job/FractureAUT/125/124http://test.corp.test.com:8080/job/FractureAUT/124/123http://test.corp.test.com:8080/job/FractureAUT/123/122http://test.corp.test.com:8080/job/FractureAUT/122/121http://test.corp.test.com:8080/job/FractureAUT/121/120http://test.corp.test.com:8080/job/FractureAUT/120/119http://test.corp.test.com:8080/job/FractureAUT/119/118http://test.corp.test.com:8080/job/FractureAUT/118/117http://test.corp.test.com:8080/job/FractureAUT/117/116http://test.corp.test.com:8080/job/FractureAUT/116/115http://test.corp.test.com:8080/job/FractureAUT/115/114http://test.corp.test.com:8080/job/FractureAUT/114/113http://test.corp.test.com:8080/job/FractureAUT/113/112http://test.corp.test.com:8080/job/FractureAUT/112/111http://test.corp.test.com:8080/job/FractureAUT/111/110http://test.corp.test.com:8080/job/FractureAUT/110/109http://test.corp.test.com:8080/job/FractureAUT/109/108http://test.corp.test.com:8080/job/FractureAUT/108/107http://test.corp.test.com:8080/job/FractureAUT/107/106http://test.corp.test.com:8080/job/FractureAUT/106/105http://test.corp.test.com:8080/job/FractureAUT/105/104http://test.corp.test.com:8080/job/FractureAUT/104/103http://test.corp.test.com:8080/job/FractureAUT/103/102http://test.corp.test.com:8080/job/FractureAUT/102/101http://test.corp.test.com:8080/job/FractureAUT/101/100http://test.corp.test.com:8080/job/FractureAUT/100/99http://test.corp.test.com:8080/job/FractureAUT/99/98http://test.corp.test.com:8080/job/FractureAUT/98/97http://test.corp.test.com:8080/job/FractureAUT/97/96http://test.corp.test.com:8080/job/FractureAUT/96/95http://test.corp.test.com:8080/job/FractureAUT/95/94http://test.corp.test.com:8080/job/FractureAUT/94/93http://test.corp.test.com:8080/job/FractureAUT/93/92http://test.corp.test.com:8080/job/FractureAUT/92/91http://test.corp.test.com:8080/job/FractureAUT/91/90http://test.corp.test.com:8080/job/FractureAUT/90/89http://test.corp.test.com:8080/job/FractureAUT/89/88http://test.corp.test.com:8080/job/FractureAUT/88/87http://test.corp.test.com:8080/job/FractureAUT/87/86http://test.corp.test.com:8080/job/FractureAUT/86/85http://test.corp.test.com:8080/job/FractureAUT/85/
Why do I get the output above?
Why are you invoking curl at all? Just retrieve the contents of the URL from groovy:
def xmlString = new URL("http://test.corp.test.com:8080/job/FractureAUT/api/xml").getText()
def projectXML = new XMLSlurper().parseText(xmlString)
XmlSlurper can parse the URL directly:
def projectXml = new XmlSlurper().parse('http://test.corp.test.com:8080/job/FractureAUT/api/xml')
I think the output on screen is because of curl. So can try to silence it with -s or --silent option:
def projectXml = new XmlSlurper().parseText("curl -s 'http://test.corp.test.com:8080/job/FractureAUT/api/xml'".execute().text);

Is there a way to automatically change epic state to done when all the linked stories and tasks are complete

I am new to JIRA and Kanban. I was expecting that when I create an epic and link some stories and tasks to it. The status of the epic will automatically change (e.g. to done) when all the stories and tasks linked to it are done. But it seems this is not the case. I can move the epic from the Backlog to the Done column even when its linked tasks and stories are still in the backlog. Is there a way to make JIRA prevent that from happening?
I have been working on something similar. My intention was to set assigne of all linked issues of another one to a specific user when the status changes to a specific state.
I did this with a postfunction of the workflow of type: "Set Field Value to constant or Groovy expression"
In your situation I would do the following:
go to "Close" transition, and click configure.
select postfunctions, and add the type i told you.
mark the chekbox that says execute only if condition is true
set your condition. Probably something like issue.epic=your epic.
Then you add your script, where you recover alll the issues linked to the epic, and check their status.
Create your logic so that if everithing is as it should be, you just change the status, using MutableIssue object.
remember that a field is going to be modified by this script, and i guess you cant choose status as field to be set. If this happens, choose summary, and store the current value, and use it to end your script, and set the summary value, whtih the same you had.
Publish your workflow.
Excuse me if it is not clear, but is difficult to explain. Let me know if you need somenthing else.
PD: If you just want to do this at some specific moment and not for every epics automatically, just add Script Runner plugin, and run your script in the console. Much easier.
Regards
Maybe it helps you:
I used jira with system language set to "Russian" (and i'm not good in groovy), that's why script below contains language dependencies (you should edit code if you use differ from my jira system language! At least change )
Use Scrip Runner plugin
Create "Custom listener" and paste code (code is not so good as can be but it's working):
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.ComponentManager;
ComponentManager componentManager = ComponentManager.getInstance();
def groupMan = ComponentAccessor.getGroupManager()
def authCon = ComponentAccessor.getJiraAuthenticationContext()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder();
IssueManager issueManager = ComponentAccessor.getIssueManager();
OptionsManager optionsManager = componentManager.getComponentInstanceOfType(OptionsManager.class);
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
def curUser = authCon.getUser()
def issue = event.issue
def epicLinkCf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Epic Link'}
if(!epicLinkCf) {log.warn "No Epic Link field"; return}
log.warn "Existing Epic Link: ${epicLinkCf.getValue(issue)}"
String epicIssue = epicLinkCf.getValue(issue)
Issue epic = issueManager.getIssueObject(epicIssue) // epicKey is passed into your script
// Check if Epic link is exist
if(!epic)
return true
def newEpicState = "Сделано"
log.warn "Epic: " + epic
List<IssueLink> allOutIssueLink = issueLinkManager.getOutwardLinks(epic.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
log.warn "child link type: " + issueLink.getIssueLinkType().getName()
// Check status of all issues from epic
if (issueLink.getIssueLinkType().getName() == "Epic-Story Link") {
Issue chIssue = issueLink.getDestinationObject();
log.warn "child state: " + chIssue.getStatusObject().getName()
if(chIssue.getStatusObject().getName() == "В процессе") {
newEpicState = "В процессе"
} else if (chIssue.getStatusObject().getName() != "Закрыто" && newEpicState != "В процессе") {
newEpicState = "Сделать"
}
}
}
def epicStatusCf = customFieldManager.getCustomFieldObjects(epic).find {it.name == 'Epic Status'}
log.warn "Current epic status: " + epicStatusCf.getValue(epic)
FieldConfig epicStatusFieldConfig = epicStatusCf.getRelevantConfig(epic);
String oldStatus = epicStatusCf.getValue(epic)
log.warn "New epic status: " + newEpicState
// Set new status if it necessary
if (oldStatus != newEpicState) {
Options epicStatusOptions = optionsManager.getOptions(epicStatusFieldConfig);
Option epicStatusDoneOption = epicStatusOptions.getOptionForValue(newEpicState, null);
epicStatusCf.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(epicStatusCf),epicStatusDoneOption),changeHolder)
log.warn "Epic status is updated!"
}
If you're using Scriptrunner then you should be good to use the scriptrunner code. Please check this code from scriptrunner:
// Add the next line as a condition to the script listener, so it gets executed only for epic issues, the line must be written uncommented:
// issue.isEpic
// Check if the resolution has been changed
def resolutionChange = changelog.items.find {
(it as Map).field == 'resolution'
} as Map
logger.info("The resolution change of issue '${issue.key}': ${resolutionChange}.")
if (!resolutionChange) {
logger.info("The resolution didn't change.")
return
}
// Compute the 'Epic Status' value to set based on the resolution value
def newEpicStatusValue = (resolutionChange.toString == 'Done') ? 'Done' : 'To Do'
// Get the 'Epic Status' field ID
final epicStatusField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Epic Status'
} as Map
def epicStatusFieldId = epicStatusField.id
logger.info("Updating Epic Status field (${epicStatusFieldId}) to '${newEpicStatusValue}'.")
// Update the 'Epic Status' field value
put("/rest/api/2/issue/${issue.key}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
.body([
fields: [
(epicStatusFieldId): [value: newEpicStatusValue]
]
])
.asString()
You can automate this code your post-function or automation in jira. Please find the further details in this link.

Resources