我是Jenkins和Groovy的新手。我需要能够检测是否添加或删除了检入Jenkins的文件。但是,我似乎无法构造正确返回编辑值的类。
Changeset.Item类的链接是here
我尝试使用的代码是:
import java.lang.*
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import hudson.util.*
import hudson.scm.*
import hudson.scm.SubversionChangeLogSet.LogEntry
import com.microsoft.tfs.core.clients.versioncontrol.specs.version.ChangesetVersionSpec
import hudson.model.User
import hudson.plugins.tfs.commands.RemoteChangesetVersionCommand
import hudson.plugins.tfs.model.ChangeSet.Item
import groovy.util.slurpersupport.GPathResult
import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.MarkupBuilder
import java.io.Serializable
import java.lang.Cloneable
// work with current build
def build = Thread.currentThread()?.executable
// get ChangesSets with all changed items
def changeSet = build.getChangeSet()
def items = changeSet.getItems()
println "Affected Paths"
def affectedFiles = items.collect {
it.getAffectedPaths()
}
println affectedFiles
Item typeItems = items
def kind = typeItems .collect {
it.getEditType()
}
println kind
我得到的错误是:
08:49:00 ERROR: Build step failed with exception
08:49:00 org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[Ljava.lang.Object;@11890e4' with class '[Ljava.lang.Object;' to class 'hudson.plugins.tfs.model.ChangeSet$Item' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: hudson.plugins.tfs.model.ChangeSet$Item(hudson.plugins.tfs.model.ChangeSet)
08:49:00 at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:403)
08:49:00 at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
08:49:00 at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
08:49:00 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
08:49:00 at Script1.run(Script1.groovy:38)
08:49:00 at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
08:49:00 at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
08:49:00 at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
08:49:00 at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:343)
08:49:00 at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
08:49:00 at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
08:49:00 at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
08:49:00 at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
08:49:00 at hudson.model.Build$BuildExecution.build(Build.java:206)
08:49:00 at hudson.model.Build$BuildExecution.doRun(Build.java:163)
08:49:00 at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
08:49:00 at hudson.model.Run.execute(Run.java:1727)
08:49:00 at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
08:49:00 at hudson.model.ResourceController.execute(ResourceController.java:97)
08:49:00 at hudson.model.Executor.run(Executor.java:429)
08:49:00 Build step 'Execute system Groovy script' marked build as failure
我确信这是一个非常简单的修复方法。我尝试了很多不同的方法,似乎从解决方案中得到了进一步的发展。
答案 0 :(得分:1)
这一行似乎错误:
Item typeItems = items
如果items
Item
是一个数组的问题
并且您正尝试将数组分配到Item
变量
来自您的评论和代码,似乎变量changeSet
包含hudson.plugins.tfs.model.ChangeLogSet
对象
请使用以下代码进行检查:
println ( changeSet.getClass() )
如果是,那么:
changeSet.getItems()
应该返回hudson.plugins.tfs.model.ChangeSet
这应该有效:
//collect all ChangeSet.Items into single array
def childItems = items.collectMany { it.getItems() }
childItems.each{
println " path = ${it.getPath()} kind = ${it.getEditType()}"
}
答案 1 :(得分:0)
您可以使用以下内容
println(" Changeset: ")
def changeSet = build.getChangeSet()
def items = changeSet.getItems()
for(int i = 0 ; i < items.length; i++) {
gitChangeSet = items[i]
author = gitChangeSet.getAuthorName()
comment = gitChangeSet.getComment().replaceAll("\\n", " ")
rev = gitChangeSet.getRevision()
paths = gitChangeSet.getPaths()
println(" " + (i+1) + ". " + comment)
println(" Commit: " + rev + " by (" + author + ")")
for(hudson.plugins.git.GitChangeSet.Path path : paths) {
editType = path.getEditType()
if(editType == hudson.scm.EditType.ADD) {
println(" + " + path.getPath())
} else if(editType == hudson.scm.EditType.DELETE) {
println(" - " + path.getPath())
} else if(editType == hudson.scm.EditType.EDIT) {
println(" E " + path.getPath())
}
}