Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions lib/github-comment.js
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
comments(first: 20, after:$cursor) {
comments(first: 100, after:$cursor) {

I just put 20 to see if the recursive querying works.

nodes {
id
body
viewerDidAuthor
}
pageInfo {
endCursor
hasNextPage
}
}
labels(first: 15) {
nodes {
name
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to use these labels for something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to recognize a bot-out label, for complete opt-out.

}
}
}`

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 })
Copy link
Member

Choose a reason for hiding this comment

The 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
})
}
10 changes: 10 additions & 0 deletions lib/github-graphql-client.js
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
Loading