Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for editing Gists #484

Merged
merged 9 commits into from
Oct 11, 2019
50 changes: 50 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,55 @@ 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 description) throws IOException {
edit("description",description);
}

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

// /**
// * Deletes a file.
// */
// 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