使用Groovy在Jenkins中初始化审计试用插件

问题描述 投票:1回答:1

我的方法:

import hudson.plugins.audit_trail.AuditTrailPlugin
import hudson.plugins.jobConfigHistory.JobConfigHistory
import net.sf.json.JSONObject

def auditTrialPlugin = Jenkins.getInstance().getPlugin(AuditTrailPlugin.class)

// println(auditTrialPlugin.getConfigXml().asString())

println("Going to configure...")

def logger = new JSONObject()
logger.put("log", "Vibin")
logger.put("limit", "1")
logger.put("count", "2")
logger.put("stapler-class", "hudson.plugins.audit_trail.LogFileAuditLogger")
logger.put("\$class", "hudson.plugins.audit_trail.LogFileAuditLogger")

def plugin = new JSONObject()
plugin.put("name", "audit-trail")
plugin.put("pattern", "")
plugin.put("logBuildCause", true)
plugin.put("loggers", logger)

auditTrialPlugin.configure(null, plugin)

Error happening:

java.lang.NoSuchMethodException: hudson.plugins.audit_trail.LogFileAuditLogger.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
Caused: java.lang.InstantiationException: hudson.plugins.audit_trail.LogFileAuditLogger
    at java.lang.Class.newInstance(Class.java:427)
    at hudson.model.Descriptor.newInstance(Descriptor.java:578)
Caused: java.lang.Error: Failed to instantiate class hudson.plugins.audit_trail.LogFileAuditLogger from {"log":"Vibin","limit":"1","count":"2","stapler-class":"hudson.plugins.audit_trail.LogFileAuditLogger","$class":"hudson.plugins.audit_trail.LogFileAuditLogger"}
    at hudson.model.Descriptor.newInstance(Descriptor.java:600)
    at hudson.model.Descriptor.newInstancesFromHeteroList(Descriptor.java:1055)
    at hudson.model.Descriptor.newInstancesFromHeteroList(Descriptor.java:1017)
    at hudson.plugins.audit_trail.AuditTrailPlugin.configure(AuditTrailPlugin.java:78)
    at hudson.plugins.audit_trail.AuditTrailPlugin$configure$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at Script1.run(Script1.groovy:24)

我试图遵循我在GitHub上找到的插件的代码库。这个插件有一个'configure'函数,我尝试使用它不起作用。

插件的代码库:GitHub link

jenkins jenkins-plugins jenkins-pipeline
1个回答
0
投票

试试这个:

import jenkins.model.*;
import hudson.plugins.audit_trail.AuditTrailPlugin;
import hudson.plugins.audit_trail.LogFileAuditLogger;

def log   = "Vibin"
def limit = 1
def count = 2

LogFileAuditLogger logFileAuditLogger = new LogFileAuditLogger(log, limit, count)

Jenkins j = Jenkins.getInstance();
AuditTrailPlugin plugin = j.getPlugin(AuditTrailPlugin.class);
plugin.loggers.clear()
plugin.loggers.add(logFileAuditLogger)
plugin.pattern = "" // empty pattern?

plugin.save()
plugin.start()

我认为使用LogFileAuditLogger class比'configure'功能更清晰。

© www.soinside.com 2019 - 2024. All rights reserved.