Get project label expression in RunListener

23 views
Skip to first unread message

Gary Yao

unread,
Aug 8, 2018, 8:00:40 AM8/8/18
to Jenkins Developers
Hi Jenkins Dev

I inherited a plugin which implements RunListener to onComplete.

I have a new requirement to get the label expression defined in the project where build is restricted to run from.

How can I go from onCompleted(Run build, TaskListener listener) to the information of "Restrict where this project can be run => Label Expression"?

Thanks very much!

Gary

Jesse Glick

unread,
Aug 8, 2018, 10:34:01 AM8/8/18
to Jenkins Dev
On Wed, Aug 8, 2018 at 8:00 AM Gary Yao <gary...@gmail.com> wrote:
> I have a new requirement to get the label expression defined in the project where build is restricted to run from.
>
> How can I go from onCompleted(Run build, TaskListener listener) to the information of "Restrict where this project can be run => Label Expression"?

If the `build instanceof AbstractBuild` (i.e., not Pipeline), then you
will find a method for that.

Gary Yao

unread,
Aug 9, 2018, 2:44:49 PM8/9/18
to Jenkins Developers
Thanks Jessi, actually build is instanceof hudson.model.Run, 

What kind of method should I be looking for? Sorry, I am new to the jenkins class hierarchy.

Jesse Glick

unread,
Aug 9, 2018, 4:50:30 PM8/9/18
to Jenkins Dev
On Thu, Aug 9, 2018 at 2:44 PM Gary Yao <gary...@gmail.com> wrote:
> actually build is instanceof hudson.model.Run

Of course—that is the defined supertype. I noted that you must check
an `instanceof` expression to see if it is not a Pipeline build (which
would work entirely differently).

> What kind of method should I be looking for?

http://javadoc.jenkins.io/hudson/model/AbstractProject.html#getAssignedLabelString--

Gary Yao

unread,
Aug 10, 2018, 1:35:18 AM8/10/18
to jenkin...@googlegroups.com
Thanks Jesse, that solved the problem.

labelExpression = ((AbstractProject)run.getParent()).getAssignedLabelString()
:thumbup:


--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr0D6w4YOwjZKtmjDObrdE4ASfW6zjKeSysdtRzj%3DLoZYA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jesse Glick

unread,
Aug 10, 2018, 9:45:56 AM8/10/18
to Jenkins Dev
On Fri, Aug 10, 2018 at 1:35 AM Gary Yao <gary...@gmail.com> wrote:
> labelExpression = ((AbstractProject)run.getParent()).getAssignedLabelString()

Again this will throw a `ClassCastException` whenever a Pipeline build
completes unless you guard it with

if (run instanceof AbstractBuild) {
Reply all
Reply to author
Forward
0 new messages