|
| 1 | +const fs = require('fs') |
| 2 | + |
| 3 | +const updateCodeCoverageComment = module.exports = async ({ context, github }) => { |
| 4 | + const comments = await github.rest.issues.listComments({ |
| 5 | + owner: context.repo.owner, |
| 6 | + repo: context.repo.repo, |
| 7 | + issue_number: context.issue.number, |
| 8 | + per_page: 100 |
| 9 | + }) |
| 10 | + |
| 11 | + const coverageComment = comments.data.find((comment) => { |
| 12 | + return comment.body.startsWith('<!-- coverage -->') |
| 13 | + }) || {} |
| 14 | + |
| 15 | + const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1) |
| 16 | + const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop() |
| 17 | + |
| 18 | + const commentBody = [ |
| 19 | + '<!-- coverage -->', |
| 20 | + `### Code Coverage Report ${process.env.REVISION}`, |
| 21 | + '```', |
| 22 | + `Total: ${coverageTextSummary}`, |
| 23 | + '```', |
| 24 | + '<details>', |
| 25 | + '<summary>Full coverage report</summary>', |
| 26 | + '', |
| 27 | + '```', |
| 28 | + ...coverageText, |
| 29 | + '```', |
| 30 | + '</details>', |
| 31 | + ].join('\n') |
| 32 | + |
| 33 | + const upsertCommentOptions = { |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + issue_number: context.issue.number, |
| 37 | + comment_id: coverageComment.id, |
| 38 | + body: commentBody |
| 39 | + } |
| 40 | + |
| 41 | + if (coverageComment.id) { |
| 42 | + await github.rest.issues.updateComment(upsertCommentOptions) |
| 43 | + } else { |
| 44 | + await github.rest.issues.createComment(upsertCommentOptions) |
| 45 | + } |
| 46 | +} |
0 commit comments