Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Jenkins plugin forms with Ruby

I'm developing a Jenkins plugin in Ruby. You're supposed to be able to configure every node that connects to the server so that an email is sent to a specified address when the node loses its connection to the master. EmailNodeProperty adds a field to enter an email address:

# 
# 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

When you configure a node, there's a field named email where you can enter an email address. I want this field to be validated when you enter an address.

When you save the configuration, an EmailNodeProperty is created whence (that's right) you can access the email address.

MyComputerListener's offline gets called when a node loses its connection:

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 finds the email address and sends an email.

Does anybody know if it is possible to validate the form in Ruby? According to the Jenkins wiki, this is what's supposed to be implemented (FIELD is supposed to be exchanged for the field name, so I guess it should be doCheckEmail):

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

How would you do this in Ruby? Where should the method be implemented? In EmailNodeProperty or in MyComputerListener? How do you handle the QueryParameter? The @ would make it an intstance variable in Ruby. (What is a Queryparameter?)

Any help would be much appreciated!

/Jonatan

like image 396
JonatanEkstedt Avatar asked Feb 23 '12 09:02

JonatanEkstedt


1 Answers

This simply doesn't exist today, and we badly need to add it. This has been brought up a couple of times already in Thursday morning's hack session, so it's high on the TODO list. But as of ruby-runtime plugin 0.10, this just isn't possible. Sorry to let you down.

like image 116
Kohsuke Kawaguchi Avatar answered Oct 18 '22 12:10

Kohsuke Kawaguchi