-
Notifications
You must be signed in to change notification settings - Fork 119
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
Try to find a comment to edit before creating #228
Changes from all commits
6791951
e769633
4b1fbfc
8243996
09b360f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,61 @@ | ||
'use strict' | ||
|
||
const githubClient = require('./github-client') | ||
const GQL = require('./github-graphql-client') | ||
|
||
const getPRComments = `query getPRComments($owner: String!, $repo: String!, $number: Int!, $cursor: String){ | ||
repository(owner: $owner, name: $repo) { | ||
pullRequest(number: $number) { | ||
comments(first: 20, after:$cursor) { | ||
nodes { | ||
id | ||
body | ||
viewerDidAuthor | ||
} | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
} | ||
labels(first: 15) { | ||
nodes { | ||
name | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Planning to use these labels for something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to recognize a |
||
} | ||
} | ||
}` | ||
|
||
function graphQlIdToRestId (nodeId) { | ||
const decoded = Buffer.from(nodeId, 'base64').toString() | ||
return decoded.match(/\d+$/)[0] | ||
} | ||
|
||
exports.getFirstBotComment = function getFirstBotComment ({ owner, repo, number }, cursor = null) { | ||
return GQL(getPRComments, { owner, repo, number, cursor }).then(data => { | ||
const { nodes, pageInfo } = data.repository.pullRequest.comments | ||
const firstBotComment = nodes.find(e => e.viewerDidAuthor) | ||
if (firstBotComment) { | ||
return firstBotComment | ||
} | ||
if (pageInfo.hasNextPage) { | ||
return exports.getFirstBotComment({ owner, repo, number }, pageInfo.endCursor) | ||
} | ||
return null | ||
}) | ||
} | ||
|
||
exports.createPrComment = function createPrComment ({ owner, repo, number, logger }, body) { | ||
githubClient.issues.createComment({ | ||
owner, | ||
repo, | ||
number, | ||
body | ||
}, (err) => { | ||
if (err) { | ||
logger.error(err, 'Error while creating comment on GitHub') | ||
exports.getFirstBotComment({ owner, repo, number, logger }).then((comment) => { | ||
if (comment) { | ||
const { id: nodeId, body: oldBody } = comment | ||
const newBody = `${oldBody}\n${body}` | ||
const id = graphQlIdToRestId(nodeId) | ||
return githubClient.issues.editComment({ owner, repo, id, body: newBody }) | ||
} | ||
return githubClient.issues.createComment({ owner, repo, number, body }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure like the looks of using GraphQL and promises 👍 |
||
}).catch((err) => { | ||
logger.error(err, 'Error while creating comment on GitHub') | ||
// swallow error | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const GitHubGQL = require('@octokit/graphql').defaults({ | ||
headers: { | ||
'user-agent': 'Node.js GitHub Bot v1.0-beta', | ||
authorization: 'token ' + (process.env.GITHUB_TOKEN || 'invalid-placeholder-token') | ||
} | ||
}) | ||
|
||
module.exports = GitHubGQL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just put 20 to see if the recursive querying works.