Skip to content

Commit 5ed3876

Browse files
committed
Try to first a comment to edit before creating
1 parent 690a55d commit 5ed3876

File tree

5 files changed

+233
-42
lines changed

5 files changed

+233
-42
lines changed

lib/github-comment.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
'use strict'
22

33
const githubClient = require('./github-client')
4+
const GQL = require('./github-graphql-client')
5+
6+
const getPRComments = `query getPRComments($owner: String!, $repo: String!, $number: Int!, $cursor: String){
7+
repository(owner: $owner, name: $repo) {
8+
pullRequest(number: $number) {
9+
comments(first: 20, after:$cursor) {
10+
nodes {
11+
id
12+
body
13+
viewerDidAuthor
14+
}
15+
pageInfo {
16+
endCursor
17+
hasNextPage
18+
}
19+
}
20+
labels(first: 15) {
21+
nodes {
22+
name
23+
}
24+
}
25+
}
26+
}
27+
}`
28+
29+
exports.getFirstBotComment = function getFirstBotComment ({ owner, repo, number }, cursor = null) {
30+
return GQL(getPRComments, { owner, repo, number, cursor }).then(data => {
31+
const { nodes, pageInfo } = data.repository.pullRequest.comments
32+
const firstBotComment = nodes.find(e => e.viewerDidAuthor)
33+
if (firstBotComment) {
34+
return firstBotComment
35+
}
36+
if (pageInfo.hasNextPage) {
37+
return exports.getFirstBotComment({ owner, repo, number }, pageInfo.endCursor)
38+
}
39+
return null
40+
})
41+
}
442

543
exports.createPrComment = function createPrComment ({ owner, repo, number, logger }, body) {
6-
githubClient.issues.createComment({
7-
owner,
8-
repo,
9-
number,
10-
body
11-
}, (err) => {
12-
if (err) {
13-
logger.error(err, 'Error while creating comment on GitHub')
44+
exports.getFirstBotComment({ owner, repo, number, logger }).then((comment) => {
45+
if (comment) {
46+
const { id, body: oldBody } = comment
47+
const newBody = `${oldBody}\n${body}`
48+
return githubClient.issues.editComment({ owner, repo, id, body: newBody })
1449
}
50+
return githubClient.issues.createComment({ owner, repo, number, body })
51+
}).catch((err) => {
52+
logger.error(err, 'Error while creating comment on GitHub')
53+
// swallow error
1554
})
1655
}

lib/github-graphql-client.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const GitHubGQL = require('@octokit/graphql').defaults({
4+
headers: {
5+
'user-agent': 'Node.js GitHub Bot v1.0-beta',
6+
authorization: 'token ' + process.env.GITHUB_TOKEN || 'invalid-placeholder-token'
7+
}
8+
})
9+
10+
module.exports = GitHubGQL

0 commit comments

Comments
 (0)