Job Restrictions Plugin, Regular Expression(Job Name) - jenkins

Target: Restrict all jobs from my node, except jobs that include "it_test_patch" part in job name.
Solutions that doesn't work for me:
/^.*(it_test_patch)/ (and any sort of regexp, all regular expressions I checked in regexp generators)
Variant 1
Variant 2
Variant 3
Variant 4

Related

In a tree-sitter grammar, how do I match strings except for reserved keywords in identifiers?

This might be related to me not understanding the Keyword Extraction feature, which from the docs seems to be about avoiding an issue where no space exists between a keyword and the following expression. But say I have a fairly standard identifier regex for variable names, function names, etc.:
/\w*[A-Za-z]\w*/
How do I keep this from matching a reserved keyword like IF or ELSE or something like that? So this expression would produce an error:
int IF = 5;
while this would not:
int x = 5;
There is a pull request pending since 2019 to add an EXCLUDE feature, but this is not currently implemented as of time of writing this (April 2021 - if some time has passed and you're reading this, please do re-check this!). And since treesitter also does not support negative lookbehind in its regular expressions, this has to be handled at the semantic level. One thing you can do to make this check easier is to enumerate all your reserved words then add them as an alternative to your identifier regex:
keyword: $ => choice('IF', 'THEN', 'ELSE'),
name: $ => /\w*[A-Za-z]\w*/,
identifier: $ => choice($.keyword, $.name)
According to rule 4 of treesitter's match rules, in the expression int IF = 5; the IF token would match (identifier keyword) rather than (identifier name) since it is a more specific match. This means you can do an easy query for illegal (identifier keyword) nodes and surface the error to the user in your language server or from wherever it is you're using the treesitter grammar.
Note that this approach does run the risk of creating many conflicts between your (identifier keyword) match and the actual language constructs that use those keywords. If so, you'll have to handle the whole thing at the semantic level: scan all identifiers to check whether they're a reserved word.

Ruby regular expression for number that should respect some rules per number groups

I need to build an expression (num) but the construction should respect some rules
I need to check a number (13digits) that is buld from some groups
I succeed to build one (see below) but I need to add some rules
[1-9]{1}.?[0-9]{2}.?[0-9]{2}.?[0-9]{2}.?[0-9]{2}.?[0-9]{3}.?[0-9][1]{0,20}
Rules are the following :
Group 3 => [0-9]{2} should be only like 1,2,3,4,5,6,7,8,9,10,11,12 (months)
Group 4 => [0-9]{2} should be only like 1,2, … 31 (days)
Group 5 => [0-9]{2} should be only like 1,2,3, ….. 53
How can I define these rules ?
thank you
https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/
Break up the values by splitting on the .
Use code to validate the values after converting to integers
Not all problems are regex's forte: implementing (relatively) complex business rules is one of those things. This is not regex's forte: regex here would be harder to modify, understand, and debug.

Set node label in Jenkins pipeline

Is it possible to set a label instead of a name for nodes Groovy? We want to define labels outside the script to easily access them from the Jenkins Dashboard.
Idea:
Instead of:
Groovy Script
node('Slave_1 || Slave_2'){ echo 'Hello world' }
We want something like this:
Pipeline configuration
Node Label Name: slaveGroup
Node Label Value: Slave_1 || Slave_2
Groovy Script
node(slaveGroup){echo 'Hello world'}
Or is it possible to use the labels you can set in slave configuration directly in the Groovy script?
Just found out that the Pipline Syntax (Generator) gives this option:
Valid Operators
The following operators are supported, in the order of precedence.
(expr)
parenthesis
!expr
negation
expr&&expr
and
expr||expr
or
a -> b
"implies" operator. Equivalent to !a|b. For example, windows->x64
could be thought of as "if run on a Windows slave, that slave must be
64bit." It still allows Jenkins to run this build on linux.
a <-> b
"if and only if" operator. Equivalent to a&&b || !a&&!b. For example,
windows<->sfbay could be thought of as "if run on a Windows slave,
that slave must be in the SF bay area, but if not on Windows, it must
not be in the bay area."
All operators are left-associative (i.e., a->b->c <-> (a->b)->c ) An
expression can contain whitespace for better readability, and it'll be
ignored.
Label names or slave names can be quoted if they contain unsafe
characters. For example, "jenkins-solaris (Solaris)" || "Windows 2008"
More in the documentation.

Jenkins: avoid build due to commit message

Is it possible to cancel or skip a job in Jenkins due to special commit-message patterns? I thought the option "Excluded Commit comments" in the job configuration does this for me out of the box, like mentioned here. But no matter which regular expression i write in this field, the build is performed nevertheless.
For example:
I want to perform the build job only if the commit message includes the expression "release". So i write the regular expression [^(?:release)] in the Excluded Commit comments field. I thought if i commit a revision with, for example "test commit" the build-job does not perform, right? Is this the right way to do when not using a post-commit hook?
Jenkins Git plugin exposes you the environment variable GIT_COMMIT which of course, contains the current git commit hash.
Use [Jenkins Conditional Step] and build a step which execute the following bash shell:
echo "==========================="
if [ "git show $GIT_COMMIT | grep "your-pattern-here" == false ] ; then
echo "pattern failed";
exit 1
else
echo "ok"
fi
echo "==========================="
And mark that if the shell fails, fail the build.
Late reply but may help some one in future,
There is a plugin to skip build depending on git commit message, just include a [ci-skip] in the commit message junkin will skip the build
jenkins-ci-skip-plugin
TL;DR
To trigger builds only for commits with "release" word (case-insensitive) set this in the "Excluded Commit comments" field in job configuration:
(?i)(?s)(?!.*\brelease\b.*)^.*$
Better still, use a trigger phrase which is unlikely to be added to a commit message accidentally. For example, use "[ci build]":
(?i)(?s)(?!.*\[ci build\].*)^.*$
How does this work?
(?i) tells regex do do case-insensitive match. This is optional, but useful if you want to match "Release" and "RELEASE" as well as "release".
(?s) makes dot to match line-ends (aka dotall option), so that we look for matches within the entire commit message. By default dot doesn't match line-ends, so if there is no "release" keyword on one of the lines in the commit message, the pattern would match on that line, and commit would be incorrectly ignored by Jenkins. With dotall, we look at the entire commit message, ignoring any line ends.
(?!.*\brelease\b.*) - negative look-ahead pattern. Any match is discarded if this pattern is found within it. In this pattern:
.* matches anything before our trigger phrase and after it. We need this because of the way java regex matching works (quote from the tutorial):
myString.matches("regex") returns true or false depending whether the string can be matched entirely by the regular expression. It is important to remember that String.matches() only returns true if the entire string can be matched. In other words: "regex" is applied as if you had written "^regex$" with start and end of string anchors. This is different from most other regex libraries, where the "quick match test" method returns true if the regex can be matched anywhere in the string. If myString is abc then myString.matches("bc") returns false. bc matches abc, but ^bc$ (which is really being used here) does not.
\b makes sure that there is a word boundary before and after the keyword, as you probably don't want to match "unreleased" etc.
^.*$ is the actual matching pattern we are looking for. Note that ^ and $ match start of the string and end of the string, not the start/end of lines within that string. This is default behavior for java regex, unless multi-line mode is enabled. In other words, this pattern matches the entire commit message, because dotall mode was enabled by (?s) and dot matches newlines.
So matching algorithm would match the entire commit message, and then discard it or not depending on whether it finds negative look-ahead pattern anywhere in it.
Why your expression didn't work?
There were two problems with your suggested regex expression. First, you used incorrect regex syntax for excluding a pattern. Second, you didn't tell what your pattern should include, only what it should exclude. Therefore it would never match anything even if you used correct syntax. And because it doesn't match anything, then nothing is excluded from triggering jobs, i.e. any commits would trigger.
References
If you need more information, look for java.util.regex package which is used by Jenkins uses for matching. I used this online java regex tester to test my expressions. I've also found a nice tutorial - learned about (?m), (?s) and (?i) there.

IMAP criteria with multiple ORs

I am using gmail's IMAP API to search for mails.
I use the 'OR' criteria to search for different keywords. This works well if I go one level only, i.e. something like
'UID SEARCH OR (FROM "somebody#email.com") (TO "somebody#email.com")'
however, it does not work when I try a longer expression like
criteria OR criteria OR criteria or criteria
which translates to (as far as I understand the syntax)
'UID SEARCH OR ( OR (FROM "somebodyelse#email.com") (TO "somebodyelse#email.com")) ( OR (FROM "somebody#email.com") (TO "somebody#email.com"))'
to make it clear I basically want all messages that are either sent from or sent to ANY of a given list of emails
The error I get from the libray is
[ 'A34', 'BAD', 'Could', 'not', 'parse', 'command' ]
any suggestions?
Do not use parenthesis.
IMAP uses polish notation which does not need them:
UID SEARCH OR OR FROM one#mail.com TO one#mail.com OR FROM two#mail.com TO two#mail.com
There are cases where parenthesis are needed (as IMAP AND operator can take more than 2 operands):
https://www.limilabs.com/blog/imap-search-requires-parentheses
So if you have N expressions that you want to "OR" together, you would prefix those N expressions by N-1 "OR"s.
In Perl, for example, that would be:
$search = join " ", ("OR") x (#exprs - 1), #exprs;
Just to clarify Pawel's answer (posting since I lack the points to comment), as I didn't quite pick up the nuance:
OR is a prefix operator and can only take two operands (not more). So this translates into:
UID SEARCH OR FROM "somebodyelse#email.com" TO "somebodyelse#email.com"
If you have more than two, you need to add an OR:
e.g., 3 operands
UID SEARCH OR OR FROM "somebodyelse#email.com" TO "somebodyelse#email.com" FROM "somebody#email.com"
e.g., 4 operands
UID SEARCH OR OR OR FROM "somebodyelse#email.com" TO "somebodyelse#email.com" FROM "somebody#email.com" TO "somebody#email.com"
In other words, for four operands either:
Pawel's answer of :
OR (OR x x) (OR x x)
or what I outlined above:
(OR (OR (OR x x) x) x)
Should work.

Resources