Last active
November 1, 2016 09:35
-
-
Save claudius108/c331229950b2bbbc4c0adc600131cac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script uses Oxygen operation called JSOperation, see https://www.oxygenxml.com/doc/versions/18.0/ug-editorEclipse/topics/dg-default-author-operations.html. | |
For generating the personal access token to be used in this script, please select "public_repo | Access public repositories". | |
This script has to be put in the file ${framework}/commons.js. | |
In order to add the button for publishing to github, add the following code to any CSS file that is associated within the framework with the edited XML file: | |
rdf|RDF:before { | |
content: oxy_button(action, oxy_action(name, "Publish to github", operation, "ro.sync.ecss.extensions.commons.operations.JSOperation", arg-script, "function doOperation(){publishToGihub();}")) | |
} | |
*/ | |
function publishLanguages() { | |
var gitToken = "<your-github-token>"; | |
var userName = "<your-github-username>"; | |
var repoName = "<your-repo-name>"; | |
var filePath = "<file-path>"; | |
var message = "<commit-message>"; | |
var newFileContent = org.apache.commons.io.IOUtils.toByteArray(authorAccess.getEditorAccess().createContentReader(), "UTF-8"); | |
var fileUrl = new java.net.URL("https://api.github.com/repos/" + userName + "/" + repoName + "/contents/" + filePath) | |
var commitFileRequest = fileUrl.openConnection(); | |
var fileShaResponse = JSON.parse(getHttpResponse(commitFileRequest.getInputStream())); | |
var fileSha = fileShaResponse.sha; | |
var commitFileParameters = | |
{ | |
"path": filePath, | |
"message": message, | |
"content": new String(java.util.Base64.getEncoder().encodeToString(newFileContent)), | |
"sha": fileSha | |
} | |
var commitFileRequest = new org.apache.commons.httpclient.methods.PutMethod(fileUrl); | |
commitFileRequest.setRequestHeader("Authorization", "token " + gitToken); | |
commitFileRequest.setRequestEntity(new org.apache.commons.httpclient.methods.StringRequestEntity(JSON.stringify(commitFileParameters), "application/json", "UTF-8")); | |
(new org.apache.commons.httpclient.HttpClient()).executeMethod(commitFileRequest); | |
var response = JSON.parse(getHttpResponse(commitFileRequest.getResponseBodyAsStream())); | |
commitFileRequest.releaseConnection(); | |
if (response.message) { | |
authorAccess.getWorkspaceAccess().showErrorMessage(response.message); | |
} else { | |
authorAccess.getWorkspaceAccess().showInformationMessage("The file was published!"); | |
} | |
} | |
function getHttpResponse(inputStream) { | |
var inputStream = new java.io.InputStreamReader(inputStream); | |
var bufferedReader = new java.io.BufferedReader(inputStream); | |
var inputLine = bufferedReader.readLine(); | |
bufferedReader.close(); | |
return String(inputLine); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment