-
Notifications
You must be signed in to change notification settings - Fork 288
Tools trend chart should use names and not IDs in legend #3236
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3934315
Trend chart should use names and not IDs in legend
akash-manna-sky 85a7956
updated
akash-manna-sky 75d3b6f
modified
akash-manna-sky de8d042
Change exception handling in ToolNameRegistry to catch RuntimeException
akash-manna-sky 539dba8
modified
akash-manna-sky 66ab408
Enhance ToolNameRegistry to ensure HTML escaping of names and improve…
akash-manna-sky ccb8b9a
Refactor ToolNameRegistry to remove JenkinsFacade dependency and simp…
akash-manna-sky 28c4bd1
Merge branch 'main' into JENKINS-67301
akash-manna-sky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
plugin/src/main/java/io/jenkins/plugins/analysis/core/model/ToolNameRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package io.jenkins.plugins.analysis.core.model; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.text.StringEscapeUtils; | ||
|
|
||
| import hudson.model.Run; | ||
|
|
||
| /** | ||
| * Registry that maps tool IDs to their human-readable names. This is used to display tool names instead of IDs in | ||
| * trend charts and other visualizations. | ||
| * | ||
| * @author Akash Manna | ||
| */ | ||
| public class ToolNameRegistry { | ||
| private final Map<String, String> idToNameMap; | ||
|
|
||
| /** | ||
| * Creates an empty registry. | ||
| */ | ||
| public ToolNameRegistry() { | ||
| this(new HashMap<>()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a registry with the given ID-to-name mapping. | ||
| * | ||
| * @param idToNameMap | ||
| * the mapping of tool IDs to names | ||
| */ | ||
| public ToolNameRegistry(final Map<String, String> idToNameMap) { | ||
| this.idToNameMap = new HashMap<>(idToNameMap); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a registry from the {@link ResultAction}s of a build. Each action provides a tool ID and name, which | ||
| * are stored in the registry for later lookup. Names are HTML-escaped at creation time. | ||
| * | ||
| * @param build | ||
| * the build that contains the result actions | ||
| * | ||
| * @return a registry containing all tool IDs and HTML-escaped names from the build | ||
| */ | ||
| public static ToolNameRegistry fromBuild(final Run<?, ?> build) { | ||
| Map<String, String> mapping = new HashMap<>(); | ||
| LabelProviderFactory factory = new LabelProviderFactory(); | ||
| for (ResultAction action : build.getActions(ResultAction.class)) { | ||
| String id = action.getId(); | ||
| String name = action.getName(); | ||
| if (StringUtils.isBlank(name)) { | ||
| name = factory.create(id).getName(); | ||
| } | ||
| mapping.put(id, StringEscapeUtils.escapeHtml4(name)); | ||
| } | ||
| return new ToolNameRegistry(mapping); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the human-readable name for a tool ID. If the ID is not registered, attempts to look up the name from | ||
| * the {@link LabelProviderFactory}. If that also fails, returns the ID itself. Names returned are already | ||
| * HTML-escaped. | ||
| * | ||
| * @param id | ||
| * the tool ID | ||
| * | ||
| * @return the HTML-escaped human-readable name, or the escaped ID if no name is found | ||
| */ | ||
| public String getName(final String id) { | ||
| if (idToNameMap.containsKey(id)) { | ||
| return idToNameMap.get(id); | ||
| } | ||
| var labelProvider = new LabelProviderFactory().create(id); | ||
| return StringEscapeUtils.escapeHtml4(labelProvider.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * Registers a tool ID with its corresponding name. The name will be HTML-escaped before storing. | ||
| * | ||
| * @param id | ||
| * the tool ID | ||
| * @param name | ||
| * the human-readable name | ||
| */ | ||
| public void register(final String id, final String name) { | ||
| idToNameMap.put(id, StringEscapeUtils.escapeHtml4(name)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the registry contains a mapping for the given tool ID. | ||
| * | ||
| * @param id | ||
| * the tool ID | ||
| * | ||
| * @return {@code true} if the registry contains a mapping for the ID, {@code false} otherwise | ||
| */ | ||
| public boolean contains(final String id) { | ||
| return idToNameMap.containsKey(id); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the number of registered tool IDs. | ||
| * | ||
| * @return the number of registered IDs | ||
| */ | ||
| public int size() { | ||
| return idToNameMap.size(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the ID-to-name mapping as an immutable map. The names in the returned map are already HTML-escaped. | ||
| * | ||
| * @return an immutable map from tool IDs to HTML-escaped names | ||
| */ | ||
| public Map<String, String> asMap() { | ||
| return Collections.unmodifiableMap(idToNameMap); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
plugin/src/test/java/io/jenkins/plugins/analysis/core/model/ToolNameRegistryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package io.jenkins.plugins.analysis.core.model; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import hudson.model.Run; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| /** | ||
| * Tests the class {@link ToolNameRegistry}. | ||
| * | ||
| * @author Akash Manna | ||
| */ | ||
| class ToolNameRegistryTest { | ||
| @Test | ||
| void shouldCreateEmptyRegistry() { | ||
| ToolNameRegistry registry = new ToolNameRegistry(); | ||
|
|
||
| assertThat(registry.size()).isEqualTo(0); | ||
| assertThat(registry.contains("checkstyle")).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldRegisterAndRetrieveNames() { | ||
| ToolNameRegistry registry = new ToolNameRegistry(); | ||
|
|
||
| registry.register("checkstyle", "CheckStyle Warnings"); | ||
| registry.register("spotbugs", "SpotBugs"); | ||
|
|
||
| assertThat(registry.size()).isEqualTo(2); | ||
| assertThat(registry.contains("checkstyle")).isTrue(); | ||
| assertThat(registry.contains("spotbugs")).isTrue(); | ||
| assertThat(registry.getName("checkstyle")).isEqualTo("CheckStyle Warnings"); | ||
| assertThat(registry.getName("spotbugs")).isEqualTo("SpotBugs"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldEscapeHtmlInNames() { | ||
| ToolNameRegistry registry = new ToolNameRegistry(); | ||
|
|
||
| registry.register("custom", "<script>alert('xss')</script>"); | ||
|
|
||
| assertThat(registry.getName("custom")).isEqualTo("<script>alert('xss')</script>"); | ||
| assertThat(registry.asMap().get("custom")).isEqualTo("<script>alert('xss')</script>"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldReturnEscapedIdForUnknownId() { | ||
| ToolNameRegistry registry = new ToolNameRegistry(); | ||
|
|
||
| registry.register("unknown", "unknown"); | ||
| assertThat(registry.getName("unknown")).isEqualTo("unknown"); | ||
|
|
||
| registry.register("<script>", "<script>"); | ||
| assertThat(registry.getName("<script>")).isEqualTo("<script>"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldCreateRegistryFromBuild() { | ||
| Run<?, ?> build = mock(Run.class); | ||
|
|
||
| ResultAction checkstyleAction = mock(ResultAction.class); | ||
| when(checkstyleAction.getId()).thenReturn("checkstyle"); | ||
| when(checkstyleAction.getName()).thenReturn("CheckStyle"); | ||
|
|
||
| ResultAction spotbugsAction = mock(ResultAction.class); | ||
| when(spotbugsAction.getId()).thenReturn("spotbugs"); | ||
| when(spotbugsAction.getName()).thenReturn("SpotBugs"); | ||
|
|
||
| when(build.getActions(ResultAction.class)).thenReturn(java.util.List.of(checkstyleAction, spotbugsAction)); | ||
|
|
||
| ToolNameRegistry registry = ToolNameRegistry.fromBuild(build); | ||
|
|
||
| assertThat(registry.size()).isEqualTo(2); | ||
| assertThat(registry.getName("checkstyle")).isEqualTo("CheckStyle"); | ||
| assertThat(registry.getName("spotbugs")).isEqualTo("SpotBugs"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldFallbackToIdForRegisteredIds() { | ||
| ToolNameRegistry registry = new ToolNameRegistry(); | ||
|
|
||
| registry.register("checkstyle", "CheckStyle"); | ||
| registry.register("unknownToolId", "Unknown Tool"); | ||
|
|
||
| assertThat(registry.getName("checkstyle")).isEqualTo("CheckStyle"); | ||
| assertThat(registry.getName("unknownToolId")).isEqualTo("Unknown Tool"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.