constantcontact / jenkins_pipeline_builder

YAML/JSON driven jenkins job generator that lets you version your artifact pipelines alongside with the artifact source itself.
MIT License
62 stars 43 forks source link

trouble writing custom extension #125

Closed donahuetrevor closed 6 years ago

donahuetrevor commented 6 years ago

First off, I want to say: amazing job! Great project! Definitely something I looked for a long time.

This is not really an issue, and sorry, I know this is for reporting legitimate issues. So feel free to close this if I'm intruding in some way. But please point me in the right direction.

I'm trying to write my extension for this jenkins plugin: https://github.com/jenkinsci/nodelabelparameter-plugin

Here's what I have so far:

/extensions/build_on.rb
----
require 'jenkins_pipeline_builder/extensions'

job_attribute do
    name :build_on
    plugin_id 'builtin'
    description 'Allow the job to restrict what nodes it runs on'
    jenkins_name 'Build on specific nodes'
    announced false

    xml path: '//properties' do |params|
        send('hudson.model.ParametersDefinitionProperty') do
            parameterDefinitions do

                # nil means:
                # - allow multi node selection
                # - trigger concurrent builds
                triggerIfResult = nil;

                if params[:multinode] then
                    if params[:concurrent] then
                        triggerIfResult = :allowMultiSelectionForConcurrentBuilds;
                    end
                else
                    triggerIfResult = :multiSelectionDisallowed;
                end

                eligibility = send('org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreTempOfflineNodeEligibility')

                send('org.jvnet.jenkins.plugins.nodelabelparameter.NodeParameterDefinition') do
                    name params[:name]
                    description params[:description]
                    defaultSlaves ['master'] # doesn't work
                    allowedSlaves ['master'] # doesn't work
                    triggerIfResult triggerIfResult
                    nodeEligibility eligibility # doesn't work
                end

            end
        end
    end
end

My Ruby is not too strong, so I'm hopinh someone can just point me in the right direction. The 3 fields with comments on them don't work, the rest do. So: defaultSlaves and allowedSlaves are of type List<String>. and nodeEligibility is even harder - is of type NodeEligibility a custom type.

From what I understand, send will send a signal to the current instance and return whatever that new class has?

what am I doing wrong here?

Thank you in advance and apologies again!

crimsonknave commented 6 years ago

So, first, a note. The xml section of the extension is actually not straight up ruby. You can use ruby in it, but the basic keywords are then translated into xml. That's where send comes in, the send function basically calls the method corresponding to the string passed. So, you will need to use send for any ruby keywords that you want to be xml nodes and not ruby or when you have periods in the xml node.

Without seeing the output I can't really diagnose what's wrong with those three. But, you can run the builder and have it write the files locally. The -d flag for debug might do it, but there's also the dump command (you'd use it instead of bootstrap). Once you have the xml it's writing you can compare it to the xml you are expecting to be there.

donahuetrevor commented 6 years ago

this is awesome! thank you @crimsonknave. I think I have everything I need to debug this.