Skip to content

Commit ebf8b48

Browse files
committed
feat: download alerts in background and save github token in PasswordStore
1 parent bf59d08 commit ebf8b48

File tree

6 files changed

+86
-48
lines changed

6 files changed

+86
-48
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "org.ck"
7-
version = "0.1.0"
7+
version = "0.2.0"
88

99
repositories {
1010
mavenCentral()

src/main/java/org/ck/githubsecurityscanalerts/action/DownloadAction.java

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.ck.githubsecurityscanalerts.action;
22

3+
import com.intellij.ide.passwordSafe.PasswordSafe;
4+
import com.intellij.notification.NotificationGroupManager;
5+
import com.intellij.notification.NotificationType;
36
import com.intellij.openapi.actionSystem.ActionUpdateThread;
47
import com.intellij.openapi.actionSystem.AnActionEvent;
8+
import com.intellij.openapi.application.ApplicationManager;
59
import com.intellij.openapi.diagnostic.Logger;
610
import com.intellij.openapi.project.DumbAwareAction;
711
import com.intellij.openapi.wm.ToolWindow;
@@ -13,6 +17,7 @@
1317
import org.ck.githubsecurityscanalerts.store.alerts.AlertsStore;
1418
import org.ck.githubsecurityscanalerts.store.settings.ProjectSettings;
1519
import org.ck.githubsecurityscanalerts.store.settings.ProjectSettingsStore;
20+
import org.ck.githubsecurityscanalerts.store.settings.StoreUtil;
1621
import org.ck.githubsecurityscanalerts.ui.tool.AlertsPanel;
1722
import org.jetbrains.annotations.NotNull;
1823

@@ -31,40 +36,57 @@ public void update(AnActionEvent e) {
3136
.setEnabled(
3237
!state.getGithubRepo().isEmpty()
3338
&& !state.getGithubOwner().isEmpty()
34-
&& !state.getGithubToken().isEmpty());
39+
&& PasswordSafe.getInstance()
40+
.getPassword(StoreUtil.getTokenAttributes(e.getProject()))
41+
!= null);
3542
}
3643

3744
@Override
3845
public void actionPerformed(AnActionEvent e) {
39-
final ProjectSettings state = e.getProject().getService(ProjectSettingsStore.class).getState();
40-
final Alerts savedAlerts = e.getProject().getService(AlertsStore.class).getState();
41-
42-
Set<Alert> alerts =
43-
new GithubClient(state.getGithubToken())
44-
.fetchAlerts(state.getGithubOwner(), state.getGithubRepo());
45-
46-
LOGGER.warn("fetched %d alerts".formatted(alerts.size()));
47-
48-
savedAlerts.setAlerts(
49-
alerts.stream()
50-
.map(
51-
alert ->
52-
new org.ck.githubsecurityscanalerts.store.alerts.Alert(
53-
alert.rule().id(),
54-
getRuleName(alert),
55-
alert.most_recent_instance().location().path(),
56-
alert.most_recent_instance().location().start_line(),
57-
alert.most_recent_instance().location().start_column()))
58-
.toList());
59-
60-
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(e.getProject());
61-
ToolWindow gitHubSecurityAlerts =
62-
toolWindowManager.getToolWindow("GitHub Security Scan Alerts");
63-
for (Content content : gitHubSecurityAlerts.getContentManager().getContents()) {
64-
if (content.getComponent() instanceof AlertsPanel alertsPanel) {
65-
alertsPanel.init();
66-
}
67-
}
46+
ApplicationManager.getApplication()
47+
.executeOnPooledThread(
48+
() -> {
49+
final ProjectSettings state =
50+
e.getProject().getService(ProjectSettingsStore.class).getState();
51+
final Alerts savedAlerts = e.getProject().getService(AlertsStore.class).getState();
52+
53+
Set<Alert> alerts =
54+
new GithubClient(
55+
PasswordSafe.getInstance()
56+
.getPassword(StoreUtil.getTokenAttributes(e.getProject())))
57+
.fetchAlerts(state.getGithubOwner(), state.getGithubRepo());
58+
59+
LOGGER.warn("fetched %d alerts".formatted(alerts.size()));
60+
61+
savedAlerts.setAlerts(
62+
alerts.stream()
63+
.map(
64+
alert ->
65+
new org.ck.githubsecurityscanalerts.store.alerts.Alert(
66+
alert.rule().id(),
67+
getRuleName(alert),
68+
alert.most_recent_instance().location().path(),
69+
alert.most_recent_instance().location().start_line(),
70+
alert.most_recent_instance().location().start_column()))
71+
.toList());
72+
73+
NotificationGroupManager.getInstance()
74+
.getNotificationGroup("GitHub Security Scan Alerts")
75+
.createNotification(
76+
"Downloaded %d alerts".formatted(savedAlerts.getAlerts().size()),
77+
NotificationType.INFORMATION)
78+
.setImportant(true)
79+
.notify(e.getProject());
80+
81+
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(e.getProject());
82+
ToolWindow gitHubSecurityAlerts =
83+
toolWindowManager.getToolWindow("GitHub Security Scan Alerts");
84+
for (Content content : gitHubSecurityAlerts.getContentManager().getContents()) {
85+
if (content.getComponent() instanceof AlertsPanel alertsPanel) {
86+
alertsPanel.init();
87+
}
88+
}
89+
});
6890
}
6991

7092
@Override

src/main/java/org/ck/githubsecurityscanalerts/store/settings/ProjectSettings.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22

33
public final class ProjectSettings {
44

5-
private String githubToken = "";
65
private String githubOwner = "";
76
private String githubRepo = "";
87

9-
public String getGithubToken() {
10-
return githubToken;
11-
}
12-
13-
public void setGithubToken(final String githubToken) {
14-
this.githubToken = githubToken;
15-
}
16-
178
public String getGithubOwner() {
189
return githubOwner;
1910
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.ck.githubsecurityscanalerts.store.settings;
2+
3+
import com.intellij.credentialStore.CredentialAttributes;
4+
import com.intellij.openapi.project.Project;
5+
6+
public class StoreUtil {
7+
public static CredentialAttributes getTokenAttributes(Project project) {
8+
return new CredentialAttributes("GitHub Code Scanning Alerts - Token - " + project.getName());
9+
}
10+
}

src/main/java/org/ck/githubsecurityscanalerts/ui/settings/SettingsPanel.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.ck.githubsecurityscanalerts.ui.settings;
22

3+
import com.intellij.credentialStore.Credentials;
4+
import com.intellij.ide.passwordSafe.PasswordSafe;
35
import com.intellij.notification.NotificationGroupManager;
46
import com.intellij.notification.NotificationType;
57
import com.intellij.openapi.project.Project;
@@ -9,6 +11,7 @@
911
import org.ck.githubsecurityscanalerts.github.GithubClient;
1012
import org.ck.githubsecurityscanalerts.store.settings.ProjectSettings;
1113
import org.ck.githubsecurityscanalerts.store.settings.ProjectSettingsStore;
14+
import org.ck.githubsecurityscanalerts.store.settings.StoreUtil;
1215

1316
import javax.swing.*;
1417
import java.awt.*;
@@ -18,6 +21,8 @@
1821
import static java.awt.GridBagConstraints.*;
1922

2023
public class SettingsPanel {
24+
25+
private final Project project;
2126
private final JPanel root;
2227

2328
private JBTextField githubToken;
@@ -29,6 +34,7 @@ public class SettingsPanel {
2934
private JButton testButton;
3035

3136
public SettingsPanel(Project project) {
37+
this.project = project;
3238
root = new JPanel(new BorderLayout());
3339

3440
JPanel grid = new JPanel(new GridBagLayout());
@@ -64,19 +70,22 @@ public SettingsPanel(Project project) {
6470
@Override
6571
public void actionPerformed(ActionEvent e) {
6672
final Optional<Long> alertCount =
67-
new GithubClient(getGithubToken()).getAlertCount(getGithubOwner(), getGithubRepo());
73+
new GithubClient(
74+
PasswordSafe.getInstance()
75+
.getPassword(StoreUtil.getTokenAttributes(project)))
76+
.getAlertCount(getGithubOwner(), getGithubRepo());
6877

6978
if (alertCount.isPresent()) {
7079
NotificationGroupManager.getInstance()
71-
.getNotificationGroup("GitHub Security Scan Alerts: Connection Test")
80+
.getNotificationGroup("GitHub Security Scan Alerts")
7281
.createNotification(
7382
"Connection OK: %d alerts found".formatted(alertCount.get()),
7483
NotificationType.INFORMATION)
7584
.setImportant(true)
7685
.notify(project);
7786
} else {
7887
NotificationGroupManager.getInstance()
79-
.getNotificationGroup("GitHub Security Scan Alerts: Connection Test")
88+
.getNotificationGroup("GitHub Security Scan Alerts")
8089
.createNotification("Connection failed", NotificationType.WARNING)
8190
.setImportant(true)
8291
.notify(project);
@@ -95,19 +104,26 @@ public JPanel getRootPane() {
95104
}
96105

97106
public void load(ProjectSettings projectSettings) {
98-
githubToken.setText(projectSettings.getGithubToken());
107+
if (PasswordSafe.getInstance().getPassword(StoreUtil.getTokenAttributes(project)) != null) {
108+
githubToken.getEmptyText().setText("< Saved Github token >");
109+
}
110+
99111
githubOwner.setText(projectSettings.getGithubOwner());
100112
githubRepo.setText(projectSettings.getGithubRepo());
101113
}
102114

103115
public void save(ProjectSettings projectSettings) {
104-
projectSettings.setGithubToken(getGithubToken());
116+
if (!getGithubToken().isEmpty()) {
117+
Credentials credentials = new Credentials(null, getGithubToken());
118+
PasswordSafe.getInstance().set(StoreUtil.getTokenAttributes(project), credentials);
119+
}
120+
105121
projectSettings.setGithubOwner(getGithubOwner());
106122
projectSettings.setGithubRepo(getGithubRepo());
107123
}
108124

109125
public boolean isModified(ProjectSettings projectSettings) {
110-
return !projectSettings.getGithubToken().equals(getGithubToken())
126+
return !getGithubToken().isEmpty()
111127
|| !projectSettings.getGithubOwner().equals(getGithubOwner())
112128
|| !projectSettings.getGithubRepo().equals(getGithubRepo());
113129
}

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
<projectService serviceImplementation="org.ck.githubsecurityscanalerts.store.settings.ProjectSettingsStore"/>
3333
<projectService serviceImplementation="org.ck.githubsecurityscanalerts.store.alerts.AlertsStore"/>
3434

35-
<notificationGroup id="GitHub Security Scan Alerts: Connection Test"
36-
displayType="BALLOON"/>
35+
<notificationGroup id="GitHub Security Scan Alerts" displayType="BALLOON"/>
3736
</extensions>
3837

3938
<actions>

0 commit comments

Comments
 (0)