8

我正在用 Ruby 开发一个 Jenkins 插件。您应该能够配置连接到服务器的每个节点,以便在节点失去与主节点的连接时将电子邮件发送到指定地址。EmailNodeProperty添加一个字段以输入电子邮件地址:

# 
# Save an email property for every node
#
class EmailNodeProperty < Jenkins::Slaves::NodeProperty
    require 'java'
    import 'hudson.util.FormValidation'  

    display_name "Email notification"

    attr_accessor :email

    def initialize(attrs = {})
        @email = attrs['email']
    end

    def doCheckEmail value
        puts "  ENP.doCheckEmail:#{value}"
    end
end

配置节点时,有一个名为的字段email,您可以在其中输入电子邮件地址。我希望在您输入地址时验证此字段。

当您保存配置时,将EmailNodeProperty创建一个(没错)您可以访问电子邮件地址的位置。

MyComputerListeneroffline当节点失去连接时调用's :

class MyComputerListener
    include Jenkins::Slaves::ComputerListener
    include Jenkins::Plugin::Proxy

    def online(computer, listener)
    end

    def offline(computer)
        #Do nothing when the Master shuts down
        if computer.to_s.match('Master') == nil
            list = computer.native.getNode().getNodeProperties()
            proxy = list.find {"EmailNodeProperty"}
            if proxy.is_a?(Jenkins::Plugin::Proxy)
                rubyObject = proxy.getTarget()
                email = rubyObject.email #<= Accesses the email from EmailNodeProperty
                [...]
            end
        end
    end
end

MyComputerListener找到电子邮件地址并发送电子邮件。

有人知道是否可以在 Ruby 中验证表单吗?根据Jenkins wiki,这是应该实现的(FIELD 应该被交换为字段名称,所以我想它应该是doCheckEmail):

public FormValidation doCheckFIELD(@QueryParameter String value) {
    if(looksOk(value))
        return FormValidation.ok();
    else
        return FormValidation.error("There's a problem here");
}

你会如何在 Ruby 中做到这一点?该方法应该在哪里实现?在EmailNodeProperty还是在MyComputerListener?你如何处理查询参数?@ 将使其成为 Ruby 中的实例变量。(什么是查询参数?)

任何帮助将非常感激!

/乔纳坦

4

1 回答 1

1

这在今天根本不存在,我们非常需要添加它。这已经在周四早上的 hack session中提到过几次,所以它在 TODO 列表中排名靠前。但是从ruby​​-runtime 插件 0.10开始,这是不可能的。很抱歉让你失望了。

于 2012-06-30T00:02:01.593 回答