Skip to content
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

feat(): update comment #49

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ branch. When this is set a diff of the coverage percentages is shown.
If set to true, only changed files will be included in the report. Total percentage will still include all files.

##### `delete-old-comments` (**Default: false**)
If set to true, old comments will be deleted before a new comment is posted
If set to true, old comments will be deleted before a new comment is posted.

##### `update-comment` (**Default: false**)
If set to true, the most recently updated comment will be updated with the new report.

##### `title` (**Optional**)
If included, will be added as a title for the comment produced.

##### `comment_prepend` (**Optional**)
If included, will be added at the beginning of the produced comment.

##### `comment_append` (**Optional**)
If included, will be added at the end of the produced comment if comment is not too long.

## Example usage

```yml
Expand Down
12 changes: 11 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ inputs:
description: Set to true to delete old Coverage Report comments
required: false
default: false
update-comment:
description: Set to true to update the last Coverage Report comment
required: false
default: false
working-directory:
description: Set working directory if project is not in root folder
description: Set working directory if project is not in root folder
required: false
default: "./"
title:
description: Title to add to the comment
required: false
comment_prepend:
description: Content to add at the beginning of the comment
required: false
comment_append:
description: Content to add at the end of the comment, may not appear if comment is too long
required: false
runs:
using: node12
main: dist/main.js
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@
"format": "prettier --write src/*.js src/**/*.js"
},
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/github": "^1.1.0",
"@actions/core": "^1.9.1",
"@actions/github": "^5.0.3",
"encoding": "^0.1.13",
"lcov-parse": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.8.6",
"@babel/node": "^7.8.4",
"@babel/preset-env": "^7.12.11",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"babel-jest": "^25.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"babel-jest": "^29.0.1",
"core-js": "3",
"jest": "^25.1.0",
"prettier": "^1.19.1",
"jest": "^29.0.1",
"prettier": "^2.7.1",
"regenerator-runtime": "^0.13.3",
"rollup": "^1.32.0",
"rollup-plugin-node-externals": "^2.1.3"
"rollup": "^2.78.1",
"rollup-plugin-node-externals": "^4.1.1"
},
"babel": {
"presets": [
Expand Down
8 changes: 6 additions & 2 deletions src/delete_old_comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as core from "@actions/core"

const REQUESTED_COMMENTS_PER_PAGE = 20

export async function deleteOldComments(github, options, context) {
export async function deleteOldComments(github, options, context, keepLast) {
const existingComments = await getExistingComments(github, options, context)
const commentToUpdate = keepLast ? existingComments.shift() : null
for (const comment of existingComments) {
core.debug(`Deleting comment: ${comment.id}`)
try {
Expand All @@ -16,9 +17,10 @@ export async function deleteOldComments(github, options, context) {
console.error(error)
}
}
return commentToUpdate
}

async function getExistingComments(github, options, context) {
export async function getExistingComments(github, options, context) {
let page = 0
let results = []
let response
Expand All @@ -29,6 +31,8 @@ async function getExistingComments(github, options, context) {
repo: context.repo.repo,
per_page: REQUESTED_COMMENTS_PER_PAGE,
page: page,
sort: "updated",
direction: "desc",
})
results = results.concat(response.data)
page++
Expand Down
55 changes: 41 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
import { promises as fs } from "fs"
import core from "@actions/core"
import { GitHub, context } from "@actions/github"
import { context, getOctokit } from "@actions/github"
import { promises as fs } from "fs"
import path from "path"

import { parse } from "./lcov"
import { diff } from "./comment"
import { deleteOldComments, getExistingComments } from "./delete_old_comments"
import { getChangedFiles } from "./get_changes"
import { deleteOldComments } from "./delete_old_comments"
import { parse } from "./lcov"
import { normalisePath } from "./util"

const MAX_COMMENT_CHARS = 65536

async function main() {
const token = core.getInput("github-token")
const githubClient = new GitHub(token)
const workingDir = core.getInput('working-directory') || './';
const lcovFile = path.join(workingDir, core.getInput("lcov-file") || "./coverage/lcov.info")
const githubClient = getOctokit(token).rest
const workingDir = core.getInput("working-directory") || "./"
const lcovFile = path.join(
workingDir,
core.getInput("lcov-file") || "./coverage/lcov.info",
)
const baseFile = core.getInput("lcov-base")
const shouldFilterChangedFiles =
core.getInput("filter-changed-files").toLowerCase() === "true"
const shouldDeleteOldComments =
core.getInput("delete-old-comments").toLowerCase() === "true"
const shouldUpdateLastComment =
core.getInput("update-comment").toLowerCase() === "true"
const title = core.getInput("title")
const prepend = core.getInput("comment_prepend") || ""
const append = core.getInput("comment_append") || ""

const raw = await fs.readFile(lcovFile, "utf-8").catch(err => null)
const raw = await fs.readFile(lcovFile, "utf-8").catch((err) => null)
if (!raw) {
console.log(`No coverage report found at '${lcovFile}', exiting...`)
return
}

const baseRaw =
baseFile && (await fs.readFile(baseFile, "utf-8").catch(err => null))
baseFile && (await fs.readFile(baseFile, "utf-8").catch((err) => null))
if (baseFile && !baseRaw) {
console.log(`No coverage report found at '${baseFile}', ignoring...`)
}
Expand Down Expand Up @@ -61,13 +68,33 @@ async function main() {

const lcov = await parse(raw)
const baselcov = baseRaw && (await parse(baseRaw))
const body = diff(lcov, baselcov, options).substring(0, MAX_COMMENT_CHARS)

const reportMaxChars = MAX_COMMENT_CHARS - prepend.length - append.length - 4
const body = `${prepend}\n\n${diff(lcov, baselcov, options).substring(
0,
reportMaxChars,
)}\n\n${append}`
let commentToUpdate
if (shouldDeleteOldComments) {
await deleteOldComments(githubClient, options, context)
commentToUpdate = await deleteOldComments(
githubClient,
options,
context,
shouldUpdateLastComment,
)
} else if (shouldUpdateLastComment) {
commentToUpdate = (
await getExistingComments(githubClient, options, context)
).shift()
}

if (context.eventName === "pull_request") {
if (context.eventName === "pull_request" && commentToUpdate) {
await githubClient.issues.updateComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: commentToUpdate.id,
body: body,
})
} else if (context.eventName === "pull_request") {
await githubClient.issues.createComment({
repo: context.repo.repo,
owner: context.repo.owner,
Expand All @@ -84,7 +111,7 @@ async function main() {
}
}

main().catch(function(err) {
main().catch(function (err) {
console.log(err)
core.setFailed(err.message)
})
Loading