0

我是 Jenkins 和 Groovy 的新手。我需要能够检测是否添加或删除了签入 Jenkins 的文件。但是,我似乎无法构造类权限来返回编辑值。

Changeset.Item 类的链接在这里

我尝试使用的代码是:

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

我相信这是一个非常容易解决的问题。我尝试了很多不同的方法,似乎离解决方案越来越远。

4

2 回答 2

1

这行似乎引发了错误:

Item typeItems = items

问题items是一个数组 ifItem

并且您正在尝试将数组分配给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()}"
}
于 2018-04-25T15:01:06.063 回答
1

你可以使用类似下面的东西

  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())
      }
    }

学分:https ://github.com/frank1rouse/jenkins_build_avoidance/blob/5723d5f7457cf4993def513deed21950eb67d398/build_avoidance.groovy

于 2018-04-25T16:45:33.513 回答