Created
July 24, 2018 13:01
-
-
Save kencharos/7b6b96d9c762941bd4118658357c2812 to your computer and use it in GitHub Desktop.
Get Comments from Upsource via JSON RPC API
This file contains hidden or 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
// upsource get comments from JSON RPC API. | |
// node 8 upper | |
const request = require("request-promise") // npm i --save request-promise | |
const fs = require("fs") | |
const auth = { | |
user: "admin", | |
password: "xxxx" | |
}; | |
const baseUrl = "http://<your upsource host>/~rpc/" | |
const projectId="<your project id>" | |
const reviewId="<your review id>" | |
getComments() | |
.catch(console.log) | |
// get comments in a review, and save file. | |
async function getComments() { | |
var options = { | |
url: `${baseUrl}getReviewSummaryDiscussions`, | |
method: 'POST', | |
auth:auth, | |
json: { | |
"reviewId":{"projectId":projectId, "reviewId":reviewId}, | |
"revisions":{"selectAll":true} | |
} | |
} | |
let res = await request(options); | |
console.log(res.result.discussions[0]) | |
fs.writeFileSync('comments.json', JSON.stringify(res.result, null, 4)) | |
// debug. get single file content | |
console.log(await getSectionAndLine(res.result.discussions[0])) | |
} | |
// get file line and section from a discussion anchor range | |
async function getSectionAndLine(discussion) { | |
var options = { | |
url: `${baseUrl}getFileContent`, | |
method: 'POST', | |
auth:auth, | |
json: { | |
"projectId":projectId, | |
"revisionId":discussion.revisionId, | |
"fileName":discussion.fileName | |
} | |
} | |
let fileInfo = await request(options) | |
console.log(fileInfo) | |
let text = fileInfo.result.fileContent.text | |
let fileType = fileInfo.result.contentType.fileType | |
let range = discussion.discussionInFile.anchor.range | |
fs.writeFileSync('filecontentsample.json', JSON.stringify(fileInfo.result, null, 4)) | |
let line = text.substring(0, range.endOffset).split("\n").length; | |
let section = text.substring(range.startOffset, range.endOffset); | |
return {line,section,fileType} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment