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

Update config access through lookupSingleton method #85

Merged
merged 2 commits into from
Jul 2, 2020

Conversation

mgarabed
Copy link
Contributor

What does this PR do?

Implements more unified access to global configuration through the single utility function.

What inspired you to submit this pull request?
#59 (comment)

Description of the Change

All configuration requests now go through the utility class, which performs the standard approach of lookupSingleton for retrieving the class.

This has the drawback of requiring a Jenkins instance during tests (because of the DatadogGlobalConfiguration.load() operation), else an exception gets raised, so we still wrap the access in a try/catch to maintain support for existing test scenarios.

Alternate Designs

Adding a get method on the DatadogGlobalConfiguration class was the first approach, but in order to maintain support for the existing test suite, this required keeping the existing utility method as well, thus creating a confusing interface. It also made the accesses more verbose versus DatadogUtilities.getDatadogGlobalDescriptor().

Possible Drawbacks

Maintaining the try/catch and returning null in some scenarios can hide some potential errors since the other utility methods that check for null return default values which may not be expected and would be hard to troubleshoot.

Verification Process

mvn test

Additional Notes

Release Notes

Review checklist (to be filled by reviewers)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

Copy link
Collaborator

@sarah-witt sarah-witt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

return ExtensionList.lookup(DatadogGlobalConfiguration.class).get(DatadogGlobalConfiguration.class);
} catch(NullPointerException e){
return ExtensionList.lookupSingleton(DatadogGlobalConfiguration.class);
} catch (IllegalStateException e) {
// It can only throw a NullPointerException when running tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove/update this comment?

@@ -646,10 +646,4 @@ private String getJenkinsVersion(){
}
return this.jenkinsVersion;
}

private static boolean isCollectBuildLogEnabled(){
return DatadogUtilities.getDatadogGlobalDescriptor() != null &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're not checking for null anymore which is the reason why you had to add https://github.com/jenkinsci/datadog-plugin/pull/85/files#diff-16902ca2fc3a4b1fa3f87d6ad966d842R43 in the tests.
I'm not totally confident in the statement "It can only throw a IllegalStateException when running tests" here. If the DatadogGlobalConfiguration extension fails to load for any reason, the singleton will be null.

Copy link
Contributor Author

@mgarabed mgarabed Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't checking for null as part of the runtime code, correct. It shouldn't be necessary to guard against that since protecting from a failed config load will just hide other problems.

DatadogUtilities do a lot of checking for null which we should actually remove too, but there is deeper logic in our tests that currently depend on the default responses which makes it a more involved change.

The general approaches of other plugins seem to have this behavior as well - see for instance this line from the referenced Azure Keyvault repo: https://github.com/jenkinsci/azure-keyvault-plugin/blob/ef3a5eb4413f7ecd47d6532735604c52fceaaef0/src/main/java/org/jenkinsci/plugins/azurekeyvaultplugin/AzureKeyVaultBuildWrapper.java#L136

Copy link
Contributor

@FlorianVeaux FlorianVeaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@mgarabed mgarabed merged commit 1be4e6e into master Jul 2, 2020
@mgarabed mgarabed deleted the mg/singleton-config branch July 2, 2020 12:39
@sarah-witt sarah-witt added the changelog/Fixed Fixed features results into a bug fix version bump label Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/Fixed Fixed features results into a bug fix version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants