Skip to content

Commit

Permalink
Add support for editing Gists.
Browse files Browse the repository at this point in the history
Fixes issue hub4j#466

NOTE: I could not get the deleteFile() method to work. Possibly an
issue with the underlying API itself.
  • Loading branch information
martinvanzijl committed Dec 19, 2018
1 parent fad203a commit df3c3fc
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/org/kohsuke/github/GHGist.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -159,6 +160,57 @@ public void delete() throws IOException {
new Requester(root).method("DELETE").to("/gists/" + id);
}

private String getApiRoute() {
return "/gists/" + id;
}

private void edit(String key, Object value) throws IOException {
new Requester(root)._with(key, value).method("PATCH").to(getApiRoute());
}

public void setDescription(String value) throws IOException {
edit("description",value);
}

/**
* Adds a file.
*/
public void addFile(String fileName, String content) throws IOException
{
updateFile(fileName, content);
}

// /**
// * Deletes a file.
// */
// // NOTE: I can't get this to work, even when using "curl" from the command
// // line. It may be an issue with the underlying API itself.
// public void deleteFile(String fileName) throws IOException {
// LinkedHashMap<String,Object> filesMap = new LinkedHashMap<String, Object>();
// filesMap.put(fileName, Collections.singletonMap("filename", null));
// edit("files",filesMap);
// }

/**
* Replaces the content of a file.
*/
public void updateFile(String fileName, String content) throws IOException
{
LinkedHashMap<String,Object> filesMap = new LinkedHashMap<String, Object>();
filesMap.put(fileName, Collections.singletonMap("content", content));
edit("files",filesMap);
}

/**
* Renames a file.
*/
public void renameFile(String fileName, String newFileName) throws IOException
{
LinkedHashMap<String,Object> filesMap = new LinkedHashMap<String, Object>();
filesMap.put(fileName, Collections.singletonMap("filename", newFileName));
edit("files",filesMap);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down

0 comments on commit df3c3fc

Please sign in to comment.