Skip to content

Commit

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

Signed-off-by: stelios maurommatakis <[email protected]>
  • Loading branch information
Stelios123 committed Dec 6, 2024
1 parent d426dc5 commit a2493d9
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad;

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

public class RadPatchCreate extends RadAction {
private final String title;
private final String description;
private final String branch;

public RadPatchCreate(GitRepository repo, String title, String description, String branch) {
super(repo);
this.title = title;
this.description = description;
this.branch = branch;
}

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

@Override
public String getActionName() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ProcessOutput run() {

@Override
public String getActionName() {
return "Label";
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,45 +311,20 @@ private void createPatch() {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
newPatchButton.setEnabled(false);
loadingPanel.setVisible(true);
var result = radicleProjectService.pushChanges(mySelectedRepo, mySelectedBranch, remote);
if (result == null) {
RadAction.showErrorNotification(project, RadicleBundle.message("gitPushError"), "");
var cli = project.getService(RadicleCliService.class);
var patchId = cli.createPatch(mySelectedRepo, titleField.getText(), descriptionField.getText(),
mySelectedBranch.getName(), labelSelect.storeLabels);
if (patchId == null) {
loadingPanel.setVisible(false);
newPatchButton.setEnabled(true);
return;
}
var isSuccess = (boolean) result.get("success");
if (!isSuccess) {
var errorMsg = (String) result.get("message");
RadAction.showErrorNotification(project, RadicleBundle.message("gitPushError"), errorMsg);
loadingPanel.setVisible(false);
newPatchButton.setEnabled(true);
return;
}
var branchRevision = radicleProjectService.getBranchRevision(project, mySelectedRepo, mySelectedBranch.getName());
if (Strings.isNullOrEmpty(branchRevision)) {
RadAction.showErrorNotification(project,
RadicleBundle.message("resolveRevNumberError"),
RadicleBundle.message("resolveRevNumberErrorDesc"));
loadingPanel.setVisible(false);
newPatchButton.setEnabled(true);
return;
}
var patchId = api.createPatch(titleField.getText(), descriptionField.getText(), labelSelect.storeLabels, projectInfo.head,
branchRevision, mySelectedRepo, projectInfo.id);
if (patchId != null) {
//Fetch the patch that created from the remote
radicleProjectService.fetchPeerChanges(mySelectedRepo);
//Set the created patch as ref for the branch
var success = radicleProjectService.setUpstream(mySelectedBranch.getName(), "rad/patches/" + patchId, mySelectedRepo);
if (RadAction.isSuccess(success)) {
mySelectedRepo.update();
}
var success = radicleProjectService.setUpstream(mySelectedBranch.getName(), "rad/patches/" + patchId, mySelectedRepo);
if (RadAction.isSuccess(success)) {
mySelectedRepo.update();
}
ApplicationManager.getApplication().invokeLater(() -> {
if (patchId != null) {
patchTabController.createPanel();
}
patchTabController.createPanel();
newPatchButton.setEnabled(true);
loadingPanel.setVisible(false);
}, ModalityState.any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
import network.radicle.jetbrains.radiclejetbrainsplugin.UpdateBackgroundTask;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadAction;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadCheckout;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadPatchLabel;
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.toolwindow.LabeledListPanelHandle;
import network.radicle.jetbrains.radiclejetbrainsplugin.toolwindow.PopupBuilder;
Expand Down Expand Up @@ -71,6 +71,7 @@ public class PatchProposalPanel {
protected PatchTabController controller;
private SingleHeightTabs tabs;
private final RadicleProjectApi api;
private final RadicleCliService cliService;
private final LabelSelect labelSelect;
private final StateSelect stateSelect;
private final PatchComponentFactory patchComponentFactory;
Expand All @@ -82,6 +83,7 @@ public PatchProposalPanel(PatchTabController controller, SingleValueModel<RadPat
this.controller = controller;
this.patch = patch.getValue();

Check warning on line 84 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/patches/PatchProposalPanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'getValue()' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental
this.api = this.patch.project.getService(RadicleProjectApi.class);
this.cliService = this.patch.project.getService(RadicleCliService.class);
this.patchModel = patch;
this.labelSelect = new LabelSelect();
this.stateSelect = new StateSelect();
Expand Down Expand Up @@ -435,9 +437,7 @@ public boolean storeValues(List<Label> labels) {

var deletedLabels = new ArrayList<>(patch.labels);
deletedLabels.removeAll(labelList);

var radPatchLabel = new RadPatchLabel(patch.repo, patch.id, addedLabels, deletedLabels);
var output = radPatchLabel.perform();
var output = cliService.createPatchLabels(patch.repo, patch.id, addedLabels, deletedLabels);
var isSuccess = RadAction.isSuccess(output);
if (isSuccess) {
patchModel.setValue(patch);

Check warning on line 443 in src/main/java/network/radicle/jetbrains/radiclejetbrainsplugin/patches/PatchProposalPanel.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'setValue(T)' is declared in unstable package 'com.intellij.collaboration.ui' marked with @ApiStatus.Experimental
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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.RadPatchCreate;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadPatchLabel;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadPatchReview;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadComment;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadIssueCreate;
Expand Down Expand Up @@ -55,6 +57,35 @@ public ProcessOutput createIssue(GitRepository repo, String title, String descri
return issueCreate.perform();
}

public String createPatch(GitRepository repo, String title, String description, String branch, List<String> labels) {
var radPatch = new RadPatchCreate(repo, title, description, branch);
var output = radPatch.perform();
if (!RadAction.isSuccess(output)) {
return null;
}
var lines = output.getStderrLines();
if (lines.isEmpty()) {
return "";
}
var firstLine = lines.get(0);
var parts = firstLine.split(" ");
String patchId = "";
if (parts.length > 2) {
patchId = parts[2];
}

if (!labels.isEmpty()) {
createPatchLabels(repo, patchId, labels, List.of());
}
return patchId;
}

public ProcessOutput createPatchLabels(GitRepository repo, String patchId,
List<String> addedLabels, List<String> deletedLabels) {
var radPatchLabel = new RadPatchLabel(repo, patchId, addedLabels, deletedLabels);
return radPatchLabel.perform();
}

public RadIssue getIssue(GitRepository repo, String projectId, String objectId) {
var cobShow = new RadCobShow(repo, projectId, objectId, RadCobList.Type.ISSUE);
return cobShow.getIssue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ public ProcessOutput cobList(GitRepository repo, String projectId, RadCobList.Ty
return executeCommand(repo.getRoot().getPath(), List.of("cob", "list", "--repo", projectId, "--type", type.value), repo);
}

public ProcessOutput createPatch(GitRepository repo, String title, String description, String branch) {
branch = branch + ":refs/patches";
var params = List.of("push", "rad", "-o", "patch.message=" + ExecUtil.escapeUnixShellArgument(title),
"-o", "patch.message=" + ExecUtil.escapeUnixShellArgument(description), branch);
return executeCommandFromFile("git", repo, params);
}

public ProcessOutput patchCache(GitRepository repo) {
var params = List.of("patch", "cache");
return executeCommandFromFile(repo, params);
}

public ProcessOutput clone(String urn, String directory, String radPath, String radHome) {
if (!Strings.isNullOrEmpty(radPath) && !Strings.isNullOrEmpty(radHome)) {
return executeCommand(radPath, radHome, directory, List.of("clone", urn, "--no-confirm"), null);
Expand Down Expand Up @@ -448,17 +460,22 @@ public ProcessOutput executeCommand(String exePath, String radHome, String workD
return executeCommand(exePath, radHome, workDir, args, repo, null);
}

public ProcessOutput executeCommandFromFile(GitRepository repo, List<String> params) {
public ProcessOutput executeCommandFromFile(String exePatch, GitRepository repo, List<String> params) {
final var projectSettings = projectSettingsHandler.loadSettings();
final var radPath = projectSettings.getPath();
final var radHome = projectSettings.getRadHome();
var workDir = repo.getRoot().getPath();
var scriptCommand = RadicleScriptCommandFactory.create(workDir, radPath, radHome, params, this, project);
var scriptCommand = RadicleScriptCommandFactory.create(workDir, exePatch, radHome, params, this, project);
var output = runCommand(scriptCommand.getCommandLine(), repo, workDir, null);
scriptCommand.deleteTempFile();
return output;
}

public ProcessOutput executeCommandFromFile(GitRepository repo, List<String> params) {
final var projectSettings = projectSettingsHandler.loadSettings();
final var radPath = projectSettings.getPath();
return executeCommandFromFile(radPath, repo, params);
}

public ProcessOutput runCommand(GeneralCommandLine cmdLine, GitRepository repo, String workDir, String stdin) {
ProcessOutput result;
if (cmdLine == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ public RadStub(String commitHash, Project project) {
this.firstCommitHash = commitHash;
}

@Override
public ProcessOutput executeCommandFromFile(String exePatch, GitRepository repo, List<String> params) {
var command = exePatch + " " + String.join(" ", params);
commandsStr.add(command);
var pr = new ProcessOutput(0);
var stdout = "stdout";
pr.appendStdout(stdout);
if (exePatch.equals("git")) {
pr.appendStderr("t t 123123 r");
}
return pr;
}

@Override
public ProcessOutput executeCommandFromFile(GitRepository repo, List<String> params) {
var command = String.join(" ", params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,14 @@ public GitRemote findRadRemote(GitRepository repo, String myUrl) {
var createPatchButton = UIUtil.findComponentOfType(buttonsPanel, JButton.class);
createPatchButton.doClick();
executeUiTasks();
var res = response.poll(5, TimeUnit.SECONDS);
assertThat(res.get("target")).isEqualTo(currentRevision);
assertThat(res.get("description")).isEqualTo(PATCH_DESC);
assertThat(res.get("title")).isEqualTo(PATCH_NAME);
var labels = (ArrayList) res.get("labels");
assertThat(labels.get(0)).isEqualTo(label1);
assertThat(labels.get(1)).isEqualTo(label2);
var res = radStub.commandsStr.poll();
assertThat(res).contains(PATCH_DESC);
assertThat(res).contains(PATCH_NAME);
var cache = radStub.commandsStr.poll(10, TimeUnit.SECONDS);
assertThat(cache).contains("patch cache");
var labels = radStub.commandsStr.poll(10, TimeUnit.SECONDS);
assertThat(labels).contains(label1);
assertThat(labels).contains(label2);
}

@Test
Expand Down

0 comments on commit a2493d9

Please sign in to comment.