Skip to content

Commit b9aacfe

Browse files
committed
feat: add quickfix with RH TC recommendation/remediation
Signed-off-by: Zvi Grinberg <[email protected]>
1 parent 308f681 commit b9aacfe

File tree

1 file changed

+93
-6
lines changed

1 file changed

+93
-6
lines changed

src/main/java/org/jboss/tools/intellij/componentanalysis/CAIntentionAction.java

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import com.intellij.psi.util.PsiTreeUtil;
2323
import com.intellij.util.IncorrectOperationException;
2424
import com.redhat.exhort.api.DependencyReport;
25+
import com.redhat.exhort.api.Issue;
2526
import org.jetbrains.annotations.NotNull;
2627
import org.jetbrains.annotations.Nullable;
2728

29+
import java.util.Optional;
30+
2831
public abstract class CAIntentionAction implements IntentionAction {
2932

3033
protected @SafeFieldForPreview PsiElement element;
@@ -52,6 +55,27 @@ public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws
5255
this.updateVersion(project, editor, file, getRecommendedVersion(this.report));
5356
}
5457

58+
private String getRecommendationsRepo(DependencyReport dependency) {
59+
String repo=null;
60+
if(thereAreNoIssues(dependency))
61+
{
62+
if(thereIsRecommendation(dependency))
63+
repo = dependency.getRecommendation().purl().getQualifiers().get("repository_url");
64+
}
65+
else
66+
{
67+
Optional<Issue> issue = dependency.getIssues().stream().findFirst();
68+
if(issue.isPresent())
69+
{
70+
if(thereIsTcRemediation(dependency)) {
71+
repo = issue.get().getRemediation().getTrustedContent().getRef().version();
72+
}
73+
}
74+
75+
}
76+
return repo;
77+
}
78+
5579
@Override
5680
public boolean startInWriteAction() {
5781
return true;
@@ -67,17 +91,80 @@ public boolean startInWriteAction() {
6791
protected abstract @Nullable FileModifier createCAIntentionActionInCopy(PsiElement element);
6892

6993
//TODO
70-
private static @NotNull String getRecommendedVersion(DependencyReport report) {
71-
return "123";
94+
private static @NotNull String getRecommendedVersion(DependencyReport dependency) {
95+
String version=null;
96+
if(thereAreNoIssues(dependency))
97+
{
98+
if(thereIsRecommendation(dependency))
99+
version = dependency.getRecommendation().version();
100+
}
101+
else
102+
{
103+
Optional<Issue> issue = dependency.getIssues().stream().findFirst();
104+
if(issue.isPresent())
105+
{
106+
if(thereIsTcRemediation(dependency)) {
107+
version = issue.get().getRemediation().getTrustedContent().getRef().version();
108+
}
109+
}
110+
111+
}
112+
return version;
113+
}
114+
115+
private static boolean thereIsTcRemediation(DependencyReport dependency) {
116+
Optional<Issue> issue = dependency.getIssues().stream().filter(iss -> iss.getRemediation().getTrustedContent() != null).findFirst();
117+
if(issue.isPresent()) {
118+
return issue.get().getRemediation().getTrustedContent() != null;
119+
}
120+
else
121+
{
122+
return false;
123+
}
124+
}
125+
126+
private static boolean thereIsRecommendation(DependencyReport dependency) {
127+
return dependency.getRecommendation() != null && !dependency.getRecommendation().version().trim().equals("");
128+
}
129+
130+
private static boolean thereAreNoIssues(DependencyReport dependency) {
131+
return dependency.getIssues() == null || dependency.getIssues().size() == 0;
72132
}
73133

74134
//TODO
75-
private static @NotNull String getQuickFixText(VulnerabilitySource source, DependencyReport report) {
76-
return "test";
135+
private static @NotNull String getQuickFixText(VulnerabilitySource source, DependencyReport dependency) {
136+
String text="";
137+
if(thereAreNoIssues(dependency) && thereIsRecommendation(dependency))
138+
{
139+
text = "Quick-Fix suggestion - apply redhat Recommended version";
140+
}
141+
else
142+
{
143+
if(thereIsTcRemediation(dependency))
144+
{
145+
text = "Quick-Fix suggestion - apply redhat remediation version";
146+
}
147+
}
148+
return text;
77149
}
78150

79151
//TODO
80-
static boolean isQuickFixAvailable(DependencyReport report) {
81-
return true;
152+
static boolean isQuickFixAvailable(DependencyReport dependency) {
153+
boolean result=false;
154+
if(thereAreNoIssues(dependency))
155+
{
156+
if(thereIsRecommendation(dependency))
157+
{
158+
result = true;
159+
}
160+
}
161+
else
162+
{
163+
if(thereIsTcRemediation(dependency))
164+
{
165+
result = true;
166+
}
167+
}
168+
return result;
82169
}
83170
}

0 commit comments

Comments
 (0)