Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow undefined variables in facts templates #307

Open
Gliptal opened this issue May 5, 2023 · 4 comments
Open

Allow undefined variables in facts templates #307

Gliptal opened this issue May 5, 2023 · 4 comments

Comments

@Gliptal
Copy link

Gliptal commented May 5, 2023

What feature do you want to see added?

Currently, the connector crashes if any fact has a template with a variable that is undefined when the connector runs. It would be nice if templates could handle undefined variables instead: simply replacing it with an empty "" string should be enough.

My jobs have a mildly complicated set of actions, some of which dynamically set variables; some of the steps that do this may not run if some conditions are met, which means when that happens I don't get a Teams notification because the connector crashes. I could set all the variables initially and then simply change their value, but Jenkins tends to behave erratically when doing this.

Upstream changes

No response

@damianszczepanik
Copy link
Member

Share the stacktrace to handle undefined value

@Gliptal
Copy link
Author

Gliptal commented May 9, 2023

Here's an example:

May 09, 2023 10:35:36 AM WARNING jenkins.util.Listeners lambda$notify$0
null
org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'BENCHMARK_RESULTS_1' in 'LakeTown: ${BENCHMARK_RESULTS_1}'
at org.jenkinsci.plugins.tokenmacro.Parser.processToken(Parser.java:492)
at org.jenkinsci.plugins.tokenmacro.Parser.parseDelimitedToken(Parser.java:178)
at org.jenkinsci.plugins.tokenmacro.Parser.parseToken(Parser.java:111)
at org.jenkinsci.plugins.tokenmacro.Parser.parse(Parser.java:87)
Caused: org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Error processing tokens
at org.jenkinsci.plugins.tokenmacro.Parser.parse(Parser.java:97)
at org.jenkinsci.plugins.tokenmacro.Parser.process(Parser.java:75)
at org.jenkinsci.plugins.tokenmacro.Parser.process(Parser.java:68)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expand(TokenMacro.java:196)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:234)
at org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(TokenMacro.java:204)
at jenkins.plugins.office365connector.FactsBuilder.evaluateMacro(FactsBuilder.java:134)
Caused: java.lang.IllegalArgumentException
at jenkins.plugins.office365connector.FactsBuilder.evaluateMacro(FactsBuilder.java:136)
at jenkins.plugins.office365connector.FactsBuilder.addUserFacts(FactsBuilder.java:125)
at jenkins.plugins.office365connector.CardBuilder.createCompletedCard(CardBuilder.java:85)
at jenkins.plugins.office365connector.Office365ConnectorWebhookNotifier.sendBuildCompletedNotification(Office365ConnectorWebhookNotifier.java:80)
at jenkins.plugins.office365connector.workflow.Office365ConnectorBuildListener.onCompleted(Office365ConnectorBuildListener.java:45)
at hudson.model.listeners.RunListener.lambda$fireCompleted$0(RunListener.java:207)
at jenkins.util.Listeners.lambda$notify$0(Listeners.java:59)
at jenkins.util.Listeners.notify(Listeners.java:67)
at hudson.model.listeners.RunListener.fireCompleted(RunListener.java:205)
at hudson.model.Run.execute(Run.java:1945)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44)
at hudson.model.ResourceController.execute(ResourceController.java:101)
at hudson.model.Executor.run(Executor.java:442)

This is the fact:

fact

As you can see $(BENCHMARK_MAP_1} was correctly replaced with LakeTown, since that variable is set as one of the job's paramenters; $(BENCHMARK_RESULTS_1} is undefined because the step of the job that sets it hasn't run yet, but the build stopped for some other reason.

@damianszczepanik
Copy link
Member

I'd suggest to make this variable defined anyway. If this hasn't been defined yet by the step then make sure this is defined other way before evaluation is started

@Gliptal
Copy link
Author

Gliptal commented May 10, 2023

That was my first reaction, but it's not working with my setup (for whatever reason).

  • C is loaded from an .env file;
  • A is set to C;
  • C is loaded from an .env file again (it has a different value now);
  • B is set to C.

If I don't define A and B, their values end up correct; if I define them beforehand, only the second assignment to B works, while A remains at whatever default value I set it at. This is despite Jenkins telling me in the log it's setting both A and B to their respective values correctly.

I understand handling env variables correctly is not this plugin's domain, but I'd get more consistent Teams notifications if it was robust with regards to undefined variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants