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

Rename the id parameter of analysisModel tool to analysisModelId #1135

Merged
merged 3 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ recordIssues tool: checkStyle(pattern: 'checkstyle-result.xml')
#### Pipeline step with a generic symbol

```groovy
recordIssues tool: analysisParser(pattern: 'checkstyle-result.xml', id: 'checkstyle')
recordIssues tool: analysisParser(pattern: 'checkstyle-result.xml', analysisModelId: 'checkstyle')
```

### Creating support for a custom tool
Expand Down
4 changes: 3 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<analysis-model-tests.version>${analysis-model-api.version}</analysis-model-tests.version>

<forensics-api-plugin.version>1.7.0</forensics-api-plugin.version>
<plugin-util-api.version>2.5.1</plugin-util-api.version>
<plugin-util-api.version>2.6.0</plugin-util-api.version>

<data-tables-api.version>1.11.3-4</data-tables-api.version>
<echarts-api.version>5.2.2-1</echarts-api.version>
Expand Down Expand Up @@ -566,9 +566,11 @@
<package>io.jenkins.plugins.analysis.core.scm</package>
<package>io.jenkins.plugins.analysis.core.model</package>
<package>io.jenkins.plugins.analysis.core.util</package>
<package>io.jenkins.plugins.analysis.warnings</package>
</packages>
<excludes combine.children="append">
<exclude>.*Thresholds</exclude>
<exclude>.*TaskScanner.*</exclude>
</excludes>
<entryPointClassPackage>io.jenkins.plugins.analysis.core.assertions</entryPointClassPackage>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.hm.hafner.analysis.ParsingCanceledException;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.util.VisibleForTesting;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundSetter;
Expand Down Expand Up @@ -40,6 +41,13 @@ public abstract class Tool extends AbstractDescribableImpl<Tool> implements Seri
private String id = StringUtils.EMPTY;
private String name = StringUtils.EMPTY;

private JenkinsFacade jenkins = new JenkinsFacade();

@VisibleForTesting
public void setJenkinsFacade(final JenkinsFacade jenkinsFacade) {
this.jenkins = jenkinsFacade;
}

/**
* Overrides the default ID of the results. The ID is used as URL of the results and as identifier in UI elements.
* If no ID is given, then the default ID is used, see corresponding {@link ToolDescriptor}.
Expand Down Expand Up @@ -122,7 +130,7 @@ public StaticAnalysisLabelProvider getLabelProvider() {

@Override
public ToolDescriptor getDescriptor() {
return (ToolDescriptor) super.getDescriptor();
return (ToolDescriptor) jenkins.getDescriptorOrDie(getClass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,30 @@ public class RegisteredParser extends ReportScanningTool {

private static final ParserRegistry REGISTRY = new ParserRegistry();

private final String id;
private final String analysisModelId;

/**
* Creates a new instance of {@link RegisteredParser}.
*
* @param id
* the unique ID of the tool
* @param analysisModelId
* the unique ID of the tool in the analysis-model module
*/
@DataBoundConstructor
public RegisteredParser(final String id) {
public RegisteredParser(final String analysisModelId) {
super();

this.id = id;
if (!REGISTRY.contains(id)) {
throw new NoSuchElementException("No such parser found with the specified ID: " + id);
this.analysisModelId = analysisModelId;
if (!REGISTRY.contains(analysisModelId)) {
throw new NoSuchElementException("No such parser found with the specified ID: " + analysisModelId);
}
}

public String getAnalysisModelId() {
return analysisModelId;
}

private ParserDescriptor getParserDescriptor() {
return REGISTRY.get(id);
return REGISTRY.get(analysisModelId);
}

@Override
Expand All @@ -67,7 +71,7 @@ public IssueParser createParser() {
public StaticAnalysisLabelProvider getLabelProvider() {
ParserDescriptor descriptor = getParserDescriptor();

return new StaticAnalysisLabelProvider(descriptor.getId(), descriptor.getName(), descriptor::getDescription);
return new StaticAnalysisLabelProvider(descriptor.getId(), getName(), descriptor::getDescription);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.jenkins.plugins.analysis.warnings;

import java.util.NoSuchElementException;

import org.junit.jupiter.api.Test;

import edu.hm.hafner.analysis.parser.checkstyle.CheckStyleParser;

import io.jenkins.plugins.util.JenkinsFacade;

import static io.jenkins.plugins.analysis.core.assertions.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Tests the class {@link RegisteredParser}.
*
* @author Ullrich Hafner
*/
class RegisteredParserTest {
private static final String CHECKSTYLE_ID = "checkstyle";
private static final String CHECK_STYLE_NAME = "CheckStyle";
private static final String CHECKSTYLE_PATTERN = "**/checkstyle-result.xml";

@Test
void shouldThrowExceptionIfThereIsNoParserAvailable() {
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() -> new RegisteredParser("-unknown-"));
}

@Test
void shouldAllowChangingId() {
RegisteredParser parser = new RegisteredParser(CHECKSTYLE_ID);

JenkinsFacade jenkins = mock(JenkinsFacade.class);
when(jenkins.getDescriptorOrDie(RegisteredParser.class)).thenReturn(new RegisteredParser.Descriptor());
parser.setJenkinsFacade(jenkins);

assertThat(parser.createParser()).isInstanceOf(CheckStyleParser.class);
assertThat(parser)
.hasAnalysisModelId(CHECKSTYLE_ID)
.hasId(CHECKSTYLE_ID)
.hasActualId(CHECKSTYLE_ID)
.hasActualName(CHECK_STYLE_NAME)
.hasActualPattern(CHECKSTYLE_PATTERN);

assertThat(parser.getLabelProvider()).hasId(CHECKSTYLE_ID);
assertThat(parser.getLabelProvider()).hasName(CHECK_STYLE_NAME);

String customId = "customId";
parser.setId(customId);
String customName = "Custom Name";
parser.setName(customName);
String customPattern = "Custom Pattern";
parser.setPattern(customPattern);

assertThat(parser)
.hasAnalysisModelId(CHECKSTYLE_ID)
.hasId(customId)
.hasActualId(customId)
.hasActualName(customName)
.hasActualPattern(customPattern);

assertThat(parser.getLabelProvider()).hasId(CHECKSTYLE_ID); // get decorations for checkstyle
assertThat(parser.getLabelProvider()).hasName(customName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void shouldFindAllCodeCheckerIssues() {
"recordIssues tool:analysisParser("
+ "pattern:'**/%s', "
+ "reportEncoding:'UTF-8', "
+ "id:'code-checker')", logFile)));
+ "analysisModelId:'code-checker')", logFile)));

AnalysisResult result = scheduleSuccessfulBuild(job);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void shouldParseCheckstyleUsingTheParserRegistry() {

job.setDefinition(new CpsFlowDefinition("node {\n"
+ " stage ('Integration Test') {\n"
+ " recordIssues tool: analysisParser(id: 'checkstyle', pattern: '**/" + "checkstyle1" + "*')\n"
+ " recordIssues tool: analysisParser(analysisModelId: 'checkstyle', pattern: '**/" + "checkstyle1" + "*')\n"
+ " }\n"
+ "}", true));

Expand Down