Shared libraries "vars" folder structure - can I add subfolders? - jenkins-pipeline

From Extending with Shared Libraries - Directory structure I created a shared library where I have multiple *.groovy files implementing global variables in the vars folder.
Can I add subfolders to vars to organize better my files? I tried, with no luck at the moment of consuming the global variable. Is there a specific syntax I need to use to reference a file in a subfolder? or subfolder are just not supported?

Unfortunately, no, you cannot. There is a declined improvement request at Jenkins' issue tracker. The given reason is that filenames are mapped directly to variable names.
Other approaches typical in Groovy like
evaluate(new File("../tools/Tools.groovy"))
do not work as well, because the Jenkins global vars files are not native Groovy code but processed.
However, there is something you can use to better organize helper functions for those which are not custom pipeline steps.
I have an includes.groovy file containing different functions like
def doSomething() {
}
def doSomethingElse() {
}
In a customPipelineStep.groovy file I can then access them with
def call() {
includes.doSomethingElse()
}
So includes works somehow like a namespace, and you could have multiple such utility files. They are no folders, but help organizing stuff.
Instead of defining custom steps in individual files, you could also group them together in files, but then you would have to wrap them in a script block within your pipeline to access them, as pointed out in the documentation. In the same way, include-functions are also publicly available in script-blocks, so be aware that they are not private.

Related

Read Karate config from optional YAML file - is there a check if a file exists?

In a similar use case as in Read Karate config from YAML I want to read my environment config for Karate from a YAML file. This works well with karate.read. My extended use case now would be the following:
read environment config of common environments from a YAML file which is in version control
have a file with custom environments not in version control and read from that file too
for all environments (based on a ID field) that are defined in both files the custom environment definition overwrites the common one
I now have to read two files but for the file with the custom environments I don't know if it will exist because the user might choose to not have any custom environments defined. Is there a way to check if the second file exists before attempting to read it? I have checked the documentation for the karate object but have not found anything like that.
If that wouldn't be possible, is there another way how my use case could be implemented?
Karate has a built-in way to use dev-env specifc config that may not exist: https://github.com/intuit/karate#environment-specific-config
That said, note that you can catch exceptions in JS, so that gives you some more options: https://stackoverflow.com/a/54554175/143475
try {
// read
} catch(e) {
// print e if needed and ignore
}

I am looking for code policy enforcement tool for xml and Python

I have projects that are developed with xml and python code mostly (Odoo modules). There is a bit of .po files for translation and csv fields for data.
I would like to enforce specific policies in xml files, for example:
No duplicate id attributes.
A specific attribute must be present if child elements contain a specific tags.
On python, I want to enforce rules like:
Look for SQL queries, and make sure that they use specific parameter methods to prevent SQL injection
Follow a specific naming convention
Some attributes are required in classes that inherit a specific class
I hope that the idea is clear.
Is there any open source solution for this? Preferably linked with github and checks on every commit!
I found a python package made specifically for this, pylint-odoo, here.
It can also be installed with pip install pylint-odoo.
An example .pylintrc config file can be found at the web OCA module, here. They also have another file named .pylintrc-mandatory.
There is even a warning for duplicate xml id attribute W7902.

Get content of module file from Ruby custom type in Puppet

I am currently writing a Ruby custom type for Puppet and I must load the content of a file which is located in the same module in the 'files' folder. Is there a function accessible from the provider that can give me the content of the file addressed by "puppet:///modules/my_module/test.yaml"?
I found a way to get it working... Is this a proper solution?
file = Puppet::Parser::Files.find_file("my_module/my_file",
Puppet::Module.find('my_module').environment)
File.open('/tmp/test', 'w') { |f| f.write(File.read(file)) }
No, I don't think there is.
The manifest of your module should take care of copying the file to the agent through a file resource, so that the provider can use it from a known location.
Functions run on the server, types in the agent. If you create a function that returns the content of the file at the server, using standard Ruby code, you could use that function to inject the value in your type.
There are some caveats about what you can do with functions though.

Utility Classes In Ruby on Rails

This is probably a stupid question, but I'm new to Ruby on Rails and I could use a little guidance. I want to have a helper/utility class that performs a group of network operations and returns results. Where do I put that class and how do I use it.
I've created network_helper.rb in my app/modulename/helpers directory. In my controller when I try to do
myNetworkHelper = ModuleName::NetworkHelper.new
results = myNetworkHelper.getResults
I get an error
undefined method `new' for MyModule::NetworkHelper:Module
I'm sure this is just a misunderstanding of how ruby on rails works. Can I get some clarification?
Would it be better to make this a class instead of a module and put it in libs? And can I add subfolders in libs and have them automatically loaded?
Lib or Classes
Little utility classes like this typically go in the lib folder, though some people prefer to create a folder called classes. Whichever you choose, make sure you import the folder in config/application.rb, as the lib folder is not autoloaded:
config.autoload_paths += %W(#{config.root}/lib)
Concerns
If instead of a utility class, you want to extend some of your models with reusable code, you may also wish to look at the new Rails 4 concerns folders which encourage you to extract reusable modules:
see: How to use concerns in Rails 4
To use new, the thing your calling it on must be a class, not a module. You're using a module. Change module to class in lib/utilities/network_utility.rb.
I cannot verify this at the moment, however I believe one place you can store your custom modules and classes is the lib directory. Alternatively, you should be able to store them in the app directory in the manner you have indicated by adding the following line to your environment.rb:
config.load_paths << File.join(Rails.root, "app", "modulename")
Also, check out Yehuda Katz's answer, which I think not only answers your question better, but also contains some very interesting and useful information and concepts relating to your situation. Hope that helps!
Add your class to app/lib folder instead of lib, so that you don't change autoload paths!
Explanations:
The accepted answer suggests adding the classes to lib.
But according to this discussion:
The lib folder does not belong to the autoload paths since Rails 3.
So it's discouraged to add lib under autoload path. Use app/lib instead.

Can I extend the core helpers within a package in Code Igniter?

I'm building my own base to use on multiple sites that I will be building. And I've made a package for that. But I want to extend the CI helpers in that package (not in the app) - helpers such as url_helper, html_helper etc.
I've put a config folder (although I don't really understant what it does) in the package folder and a config file in it (so structure is packages/app_package/config/config.php).
I've put the $config['subclass_prefix'] = 'app_'; (different from the application one preferably) and still not loading the helpers app_url_helper etc
Did anybody do that?
Have a look at CodeIgniter Helpers. Specifically the section labelled "Extending" Helpers.
I'm not quite sure what you mean by:
I've put a config folder (although I don't really understant what it
does) in the package folder and a config file in it (so structure is
packages/app_package/config/config.php).
You're not required to 'create' any config folders or files at all. The config file already exists and is located in application/config/config.php of your CodeIgniter project. The Class Extension Prefix is located ~ line 110 (version dependent). Set it to _app
Now create app_url_helper.php and app_html_helper.php in application/helpers and away you go.

Resources