-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathHardenedImageIntentionAction.java
More file actions
77 lines (67 loc) · 2.93 KB
/
Copy pathHardenedImageIntentionAction.java
File metadata and controls
77 lines (67 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*******************************************************************************
* Copyright (c) 2025 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.intellij.image;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.util.IntentionFamilyName;
import com.intellij.codeInspection.util.IntentionName;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jboss.tools.intellij.image.build.filetype.DockerfileFileType;
import org.jboss.tools.intellij.image.build.psi.DockerfileFromInstruction;
import org.jboss.tools.intellij.image.build.psi.DockerfileImageName;
import org.jetbrains.annotations.NotNull;
/**
* Intention action that replaces a Dockerfile FROM line's image reference
* with a recommended image.
*/
public class HardenedImageIntentionAction implements IntentionAction {
private final String imageReference;
public HardenedImageIntentionAction(String imageReference) {
this.imageReference = imageReference;
}
@Override
public @IntentionName @NotNull String getText() {
return "Switch to " + imageReference + " for enhanced security";
}
@Override
public @NotNull @IntentionFamilyName String getFamilyName() {
return "RHDA";
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile psiFile) {
return DockerfileFileType.isDockerfile(psiFile);
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {
int offset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(offset);
DockerfileFromInstruction fromInstruction =
PsiTreeUtil.getParentOfType(element, DockerfileFromInstruction.class, false);
if (fromInstruction != null) {
DockerfileImageName imageName = fromInstruction.getImageName();
if (imageName != null) {
Document document = editor.getDocument();
TextRange range = imageName.getTextRange();
document.replaceString(range.getStartOffset(), range.getEndOffset(), imageReference);
}
}
}
@Override
public boolean startInWriteAction() {
return true;
}
}