Github: allow to change issue state
authorAleksey Pivovarov <Aleksey.Pivovarov@jetbrains.com>
Tue, 8 Apr 2014 11:46:08 +0000 (15:46 +0400)
committerAleksey Pivovarov <Aleksey.Pivovarov@jetbrains.com>
Wed, 3 Sep 2014 09:21:30 +0000 (13:21 +0400)
plugins/github/src/org/jetbrains/plugins/github/api/GithubApiUtil.java
plugins/github/src/org/jetbrains/plugins/github/api/GithubChangeIssueStateRequest.java [new file with mode: 0644]
plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepository.java
plugins/github/src/org/jetbrains/plugins/github/tasks/GithubRepositoryType.java

index a08d7a9f03faf90814858c41bb3e0617f2cacd5a..063ad3bc7bb61413cce03be6dc81941a902ed62e 100644 (file)
@@ -519,6 +519,28 @@ public class GithubApiUtil {
     }
   }
 
     }
   }
 
+  public static void setIssueState(@NotNull GithubConnection connection,
+                                   @NotNull String user,
+                                   @NotNull String repo,
+                                   @NotNull String id,
+                                   boolean open)
+    throws IOException {
+    try {
+      String path = "/repos/" + user + "/" + repo + "/issues/" + id;
+
+      GithubChangeIssueStateRequest request = new GithubChangeIssueStateRequest(open ? "open" : "closed");
+
+      JsonElement result = connection.patchRequest(path, gson.toJson(request), ACCEPT_V3_JSON);
+
+      createDataFromRaw(fromJson(result, GithubIssueRaw.class), GithubIssue.class);
+    }
+    catch (GithubConfusingException e) {
+      e.setDetails("Can't set issue state: " + user + "/" + repo + " - " + id + "@" + (open ? "open" : "closed"));
+      throw e;
+    }
+  }
+
+
   @NotNull
   public static GithubCommitDetailed getCommit(@NotNull GithubConnection connection,
                                                @NotNull String user,
   @NotNull
   public static GithubCommitDetailed getCommit(@NotNull GithubConnection connection,
                                                @NotNull String user,
diff --git a/plugins/github/src/org/jetbrains/plugins/github/api/GithubChangeIssueStateRequest.java b/plugins/github/src/org/jetbrains/plugins/github/api/GithubChangeIssueStateRequest.java
new file mode 100644 (file)
index 0000000..9e21f64
--- /dev/null
@@ -0,0 +1,12 @@
+package org.jetbrains.plugins.github.api;
+
+import org.jetbrains.annotations.NotNull;
+
+@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
+public class GithubChangeIssueStateRequest {
+  @NotNull private final String state;
+
+  public GithubChangeIssueStateRequest(@NotNull String state) {
+    this.state = state;
+  }
+}
index c77b7cf18acef2198ff1da774ee1108508f28814..39a97184fe72ca7bb44e1480d51e6b4cfc57d188 100644 (file)
@@ -5,10 +5,7 @@ import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.PasswordUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.PasswordUtil;
 import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.tasks.Comment;
-import com.intellij.tasks.Task;
-import com.intellij.tasks.TaskRepository;
-import com.intellij.tasks.TaskType;
+import com.intellij.tasks.*;
 import com.intellij.tasks.impl.BaseRepository;
 import com.intellij.tasks.impl.BaseRepositoryImpl;
 import com.intellij.util.Function;
 import com.intellij.tasks.impl.BaseRepository;
 import com.intellij.tasks.impl.BaseRepositoryImpl;
 import com.intellij.util.Function;
@@ -271,6 +268,28 @@ public class GithubRepository extends BaseRepositoryImpl {
     }
   }
 
     }
   }
 
+  @Override
+  public void setTaskState(@NotNull Task task, @NotNull TaskState state) throws Exception {
+    GithubConnection connection = getConnection();
+    try {
+      boolean isOpen;
+      switch (state) {
+        case OPEN:
+          isOpen = true;
+          break;
+        case RESOLVED:
+          isOpen = false;
+          break;
+        default:
+          throw new IllegalStateException("Unknown state: " + state);
+      }
+      GithubApiUtil.setIssueState(connection, getRepoAuthor(), getRepoName(), task.getNumber(), isOpen);
+    }
+    finally {
+      connection.close();
+    }
+  }
+
   @NotNull
   @Override
   public BaseRepository clone() {
   @NotNull
   @Override
   public BaseRepository clone() {
@@ -353,6 +372,6 @@ public class GithubRepository extends BaseRepositoryImpl {
 
   @Override
   protected int getFeatures() {
 
   @Override
   protected int getFeatures() {
-    return super.getFeatures() | BASIC_HTTP_AUTHORIZATION;
+    return super.getFeatures() | STATE_UPDATING;
   }
 }
   }
 }
index a677fad0d7818371de9b97ce61d437285fd411b7..eefab9065addce8c5fabafd5093b55141a02f1e8 100644 (file)
@@ -2,6 +2,7 @@ package org.jetbrains.plugins.github.tasks;
 
 import com.intellij.openapi.project.Project;
 import com.intellij.tasks.TaskRepository;
 
 import com.intellij.openapi.project.Project;
 import com.intellij.tasks.TaskRepository;
+import com.intellij.tasks.TaskState;
 import com.intellij.tasks.config.TaskRepositoryEditor;
 import com.intellij.tasks.impl.BaseRepositoryType;
 import com.intellij.util.Consumer;
 import com.intellij.tasks.config.TaskRepositoryEditor;
 import com.intellij.tasks.impl.BaseRepositoryType;
 import com.intellij.util.Consumer;
@@ -9,6 +10,7 @@ import icons.TasksIcons;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
+import java.util.EnumSet;
 
 /**
  * @author Dennis.Ushakov
 
 /**
  * @author Dennis.Ushakov
@@ -45,4 +47,9 @@ public class GithubRepositoryType extends BaseRepositoryType<GithubRepository> {
                                            Consumer<GithubRepository> changeListener) {
     return new GithubRepositoryEditor(project, repository, changeListener);
   }
                                            Consumer<GithubRepository> changeListener) {
     return new GithubRepositoryEditor(project, repository, changeListener);
   }
+
+  public EnumSet<TaskState> getPossibleTaskStates() {
+    return EnumSet.of(TaskState.OPEN, TaskState.RESOLVED);
+  }
+
 }
 }