Skip to content

Commit

Permalink
refactor: Create issue
Browse files Browse the repository at this point in the history
Create issue through cli instead of httpd

Signed-off-by: stelios maurommatakis <[email protected]>
  • Loading branch information
Stelios123 committed Nov 25, 2024
1 parent 8624b51 commit e8df735
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import network.radicle.jetbrains.radiclejetbrainsplugin.models.RadIssue;
import network.radicle.jetbrains.radiclejetbrainsplugin.models.RadPatch;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleCliService;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectApi;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -40,7 +39,7 @@ public RadIssue getIssue() {
var json = output.getStdout();
RadIssue issue = null;
try {
issue = RadicleProjectApi.MAPPER.readValue(json, new TypeReference<>() { });
issue = RadicleCliService.MAPPER.readValue(json, new TypeReference<>() { });
var firstDiscussion = issue.discussion.get(0);
issue.author = firstDiscussion.author;
issue.project = repo.getProject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad;

import com.intellij.execution.process.ProcessOutput;
import git4idea.repo.GitRepository;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectService;

import java.util.List;

public class RadIssueCreate extends RadAction {
private final String title;
private final String description;
private final List<String> assignees;
private final List<String> labels;

public RadIssueCreate(GitRepository repo, String title, String description, List<String> assignees, List<String> labels) {
super(repo);
this.title = title;
this.description = description;
this.assignees = assignees;
this.labels = labels;
}

@Override
public ProcessOutput run() {
var rad = project.getService(RadicleProjectService.class);
return rad.createIssue(repo, title, description, assignees, labels);
}

@Override
public String getActionName() {
return null;
}

public boolean shouldUnlockIdentity() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.commands;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.project.Project;
import java.util.List;

public class LinuxScriptCommand extends RadicleScriptCommand {

public LinuxScriptCommand(String workDir, String exePath, String radHome, List<String> args) {
super(workDir, exePath, radHome, args);
public LinuxScriptCommand(String workDir, String exePath, String radHome, List<String> args, Project project) {
super(workDir, exePath, radHome, args, project);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.commands;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import network.radicle.jetbrains.radiclejetbrainsplugin.config.RadicleProjectSettingsHandler;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleCliService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -24,17 +28,19 @@ public abstract class RadicleScriptCommand {
public String exePath;
public String shebang;
public List<String> args;
public RadicleCliService cliService;

public RadicleScriptCommand(String workDir, String shebang, String exePath, String radHome, List<String> args) {
public RadicleScriptCommand(String workDir, String shebang, String exePath, String radHome, List<String> args, Project project) {
this.workDir = workDir;
this.shebang = shebang;
this.exePath = exePath;
this.radHome = radHome;
this.args = args;
this.cliService = project.getService(RadicleCliService.class);
}

public RadicleScriptCommand(String workDir, String exePath, String radHome, List<String> args) {
this(workDir, "#!/bin/bash", exePath, radHome, args);
public RadicleScriptCommand(String workDir, String exePath, String radHome, List<String> args, Project project) {
this(workDir, "#!/bin/bash", exePath, radHome, args, project);
}

public File createTempExecutableScript() {
Expand All @@ -52,7 +58,8 @@ public File createTempExecutableScript() {

public String getCommand() {
var command = String.join(" ", args);
return shebang + "\n" + getExportRadHomeCommand() + exePath + " " + command;
return shebang + "\n" + getExportRadPassphrase() +
getExportRadHomeCommand() + exePath + " " + command;
}

public File getTempPath() {
Expand All @@ -73,6 +80,19 @@ private String getExportRadHomeCommand() {
return "export RAD_HOME=" + radHome + "\n";
}

private String getExportRadPassphrase() {
var currentIdentity = cliService.getCurrentIdentity();
if (currentIdentity == null) {
return "";
}
var projectSettings = new RadicleProjectSettingsHandler(cliService.getProject());
var password = projectSettings.getPassword(currentIdentity.nodeId);
if (Strings.isNullOrEmpty(password)) {
return "";
}
return "export RAD_PASSPHRASE=" + password + "\n";
}

private String getRandomScriptName() {
var randomStr = UUID.randomUUID().toString();
var firstPart = randomStr.split("-")[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.commands;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectService;

import java.util.List;

public class RadicleScriptCommandFactory {
public static RadicleScriptCommand create(String workDir, String exePath, String radHome, List<String> args,
RadicleProjectService service) {
RadicleProjectService service, Project project) {
if (SystemInfo.isWindows) {
return new WindowsScriptCommand(service, workDir, exePath, radHome, args);
return new WindowsScriptCommand(service, workDir, exePath, radHome, args, project);
} else {
return new LinuxScriptCommand(workDir, exePath, radHome, args);
return new LinuxScriptCommand(workDir, exePath, radHome, args, project);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Strings;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.wsl.WSLDistribution;
import com.intellij.openapi.project.Project;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectService;

import java.io.File;
Expand All @@ -11,8 +12,9 @@
public class WindowsScriptCommand extends RadicleScriptCommand {
private final WSLDistribution wslDistribution;

public WindowsScriptCommand(RadicleProjectService service, String workDir, String exePath, String radHome, List<String> args) {
super(workDir, exePath, radHome, args);
public WindowsScriptCommand(RadicleProjectService service, String workDir, String exePath, String radHome,
List<String> args, Project project) {
super(workDir, exePath, radHome, args, project);
this.wslDistribution = new WSLDistribution(service.getWslDistro());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

public class CreateIssuePanel {
private final RadicleProjectApi api;

Check warning on line 60 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/issues/CreateIssuePanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Field can be local

Field can be converted to a local variable
private final RadicleCliService cli;
private final Project project;
private AssigneesSelect assigneeSelect;
private LabelSelect labelSelect;
Expand All @@ -68,6 +67,7 @@ public class CreateIssuePanel {
private DragAndDropField descriptionField;
protected IssueTabController issueTabController;
private ComboBox<GitRepository> projectSelect;
private final RadicleCliService cliService;

public AssigneesSelect getAssigneeSelect() {
return assigneeSelect;
Expand All @@ -80,7 +80,7 @@ public LabelSelect getLabelSelect() {
public CreateIssuePanel(IssueTabController issueTabController, Project project) {
this.issueTabController = issueTabController;
this.api = project.getService(RadicleProjectApi.class);
this.cli = project.getService(RadicleCliService.class);
this.cliService = project.getService(RadicleCliService.class);
this.project = project;
}

Expand Down Expand Up @@ -140,7 +140,7 @@ private JComponent infoComponent() {
borderPanel.add(projectSelect, BorderLayout.NORTH);
borderPanel.add(titleField, BorderLayout.CENTER);

descriptionField = new DragAndDropField(project, 0);
descriptionField = new DragAndDropField(project, 0, false);
descriptionField.setBackground(UIUtil.getListBackground());
descriptionField.setFont(JBFont.label());
descriptionField.getComponent().setBorder(JBUI.Borders.empty(8));
Expand Down Expand Up @@ -178,8 +178,8 @@ public void actionPerformed(ActionEvent e) {
}
newIssueButton.setEnabled(false);
ApplicationManager.getApplication().executeOnPooledThread(() -> {
var radProject = cli.getRadRepo(repo);
var isSuccess = api.createIssue(issueTitle, issueDescription, assignees, labels, repo, radProject.id, descriptionField.getEmbedList());
var output = cliService.createIssue(repo, issueTitle, issueDescription, assignees, labels);
var isSuccess = RadAction.isSuccess(output);
ApplicationManager.getApplication().invokeLater(() -> {
if (isSuccess) {
issueTabController.createPanel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import git4idea.repo.GitRepository;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleProjectApi;
import network.radicle.jetbrains.radiclejetbrainsplugin.services.RadicleCliService;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -107,7 +107,7 @@ public List<RadDiscussion> deserialize(JsonParser jsonParser, DeserializationCon
for (Iterator<String> it = comments.fieldNames(); it.hasNext();) {
var key = it.next();
var comment = comments.get(key);
var discussion = RadicleProjectApi.MAPPER.readValue(comment.toString(), new TypeReference<RadDiscussion>() { });
var discussion = RadicleCliService.MAPPER.readValue(comment.toString(), new TypeReference<RadDiscussion>() { });
discussion.id = key;
discussions.add(discussion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.intellij.execution.process.ProcessOutput;
import com.intellij.openapi.project.Project;
import git4idea.commands.Git;
import git4idea.repo.GitRemote;
Expand All @@ -12,6 +13,7 @@
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadAction;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadCobList;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadCobShow;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadIssueCreate;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadSelf;
import network.radicle.jetbrains.radiclejetbrainsplugin.config.RadicleProjectSettingsHandler;
import network.radicle.jetbrains.radiclejetbrainsplugin.models.RadAuthor;
Expand Down Expand Up @@ -44,6 +46,11 @@ public RadicleCliService(Project project) {
radRepoIds = new HashMap<>();
}

public ProcessOutput createIssue(GitRepository repo, String title, String description, List<String> assignees, List<String> labels) {
var issueCreate = new RadIssueCreate(repo, title, description, assignees, labels);
return issueCreate.perform();
}

public RadIssue getIssue(GitRepository repo, String projectId, String objectId) {
var cobShow = new RadCobShow(repo, projectId, objectId, RadCobList.Type.ISSUE);
return cobShow.getIssue();
Expand All @@ -65,6 +72,7 @@ public List<RadIssue> getIssues(GitRepository repo, String projectId) {
}
issues.add(issue);
}
issues.sort(Comparator.comparing(issue -> ((RadIssue) issue).discussion.get(0).timestamp).reversed());
return issues;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ private ProcessOutput addRemoveLabels(GitRepository root, String id, List<String
return executeCommandFromFile(root, params);
}

public ProcessOutput createIssue(GitRepository root, String title, String description, List<String> assignees, List<String> labels) {
var labelsStr = labels.stream().map(label -> "--label " + ExecUtil.escapeUnixShellArgument(label)).collect(Collectors.joining(" "));
var assigneesStr = assignees.stream().map(assignee -> "--assign " + ExecUtil.escapeUnixShellArgument(assignee)).collect(Collectors.joining(" "));
List<String> params = new ArrayList<>(Arrays.asList("issue", "open", "--title",
ExecUtil.escapeUnixShellArgument(title), "--description", ExecUtil.escapeUnixShellArgument(description)));
if (!Strings.isNullOrEmpty(labelsStr)) {
params.add(labelsStr);
}
if (!Strings.isNullOrEmpty(assigneesStr)) {
params.add(assigneesStr);
}
return executeCommandFromFile(root, params);
}

public ProcessOutput changeIssueState(GitRepository root, String issueId, String state) {
return executeCommand(root.getRoot().getPath(), List.of("issue", "state", issueId, state), root);
}
Expand Down Expand Up @@ -422,7 +436,7 @@ public ProcessOutput executeCommandFromFile(GitRepository repo, List<String> par
final var radPath = projectSettings.getPath();
final var radHome = projectSettings.getRadHome();
var workDir = repo.getRoot().getPath();
var scriptCommand = RadicleScriptCommandFactory.create(workDir, radPath, radHome, params, this);
var scriptCommand = RadicleScriptCommandFactory.create(workDir, radPath, radHome, params, this, project);
var output = runCommand(scriptCommand.getCommandLine(), repo, workDir, null);
scriptCommand.deleteTempFile();
return output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public DragAndDropField(Project project, boolean allowDragAndDrop) {
this.addDocumentListener(new MyListener(this));
}

public DragAndDropField(Project project, int border) {
this(project, true);
public DragAndDropField(Project project, int border, boolean allowDragAndDrop) {
this(project, allowDragAndDrop);
this.border = border;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ public void testFilterByProject() throws ExecutionException, InterruptedExceptio

var issueModel = listPanel.getModel();
assertThat(issueModel.getSize()).isEqualTo(2);

var issue = issueModel.get(0);
assertThat(issue.author.id).isEqualTo(issues.get(0).author.id);
}

@Test
Expand All @@ -172,12 +169,6 @@ public void testFilterByAssignees() {

var issueModel = listPanel.getModel();
assertThat(issueModel.getSize()).isEqualTo(2);

var issue = issueModel.get(0);
assertThat(issue.id).isEqualTo(issues.get(0).id);

issue = issueModel.get(1);
assertThat(issue.id).isEqualTo(issues.get(1).id);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,11 @@ public void createNewIssueTest() throws InterruptedException {
var createIssueButton = UIUtil.findComponentOfType(buttonsPanel, JButton.class);
createIssueButton.doClick();
executeUiTasks();
var res = response.poll(10, TimeUnit.SECONDS);

var embeds = (ArrayList<HashMap<String, String>>) res.get("embeds");
assertThat(embeds.get(0).get("oid")).isEqualTo(dummyEmbed.getOid());
assertThat(embeds.get(0).get("name")).isEqualTo(dummyEmbed.getName());
assertThat(embeds.get(0).get("content")).isEqualTo(dummyEmbed.getContent());

assertThat(res.get("title")).isEqualTo(ISSUE_NAME);
assertThat(res.get("description")).isEqualTo(issueDescription);
assertThat(res.get("labels")).usingRecursiveAssertion().isEqualTo(List.of(label2));
assertThat(res.get("assignees")).usingRecursiveAssertion().isEqualTo(List.of(getTestProjects().get(0).delegates.get(0).id));
var issueCommand = radStub.commandsStr.poll();
assertThat(issueCommand).contains(ExecUtil.escapeUnixShellArgument(ISSUE_NAME));
assertThat(issueCommand).contains(ExecUtil.escapeUnixShellArgument(issueDescription));
assertThat(issueCommand).contains(ExecUtil.escapeUnixShellArgument(label2));
assertThat(issueCommand).contains(ExecUtil.escapeUnixShellArgument(getTestProjects().get(0).delegates.get(0).id));
}

@Test
Expand Down

0 comments on commit e8df735

Please sign in to comment.