diff --git a/CHANGELOG.md b/CHANGELOG.md index d31d080..2c7191f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog of the Pytest Coverage Comment +## [Pytest Coverage Comment 1.1.50](https://github.com/MishaKav/pytest-coverage-comment/tree/v1.1.50) + +**Release Date:** 2023-11-26 + +#### Changes + +- add support for updateing the comment in PR through `workflow_dispatch` event by passing manually issue number, thanks to [@alexjyong](https://github.com/alexjyong) for contribution + ## [Pytest Coverage Comment 1.1.49](https://github.com/MishaKav/pytest-coverage-comment/tree/v1.1.49) **Release Date:** 2023-11-15 diff --git a/README.md b/README.md index 57020a4..fff4812 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ You can add this action to your GitHub workflow for Ubuntu runners (e.g. runs-on | Name | Required | Default | Description | | --------------------------- | -------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `github-token` | ✓ | `${{github.token}}` | An alternative GitHub token, other than the default provided by GitHub Actions runner | +| `issue-number` | | '' | PR Number where you'd like your comments to be posted to. Required to post a comment when running the workflow on an event that isn't `push` or `pull-request` . | | `pytest-coverage-path` | | `./pytest-coverage.txt` | The location of the txt output of pytest-coverage. Used to determine coverage percentage | | `pytest-xml-coverage-path` | | '' | The location of coverage-xml from pytest-coverage (--cov-report "xml:coverage.xml) | | `coverage-path-prefix` | | '' | Prefix for path when link to files in comment | @@ -44,7 +45,7 @@ You can add this action to your GitHub workflow for Ubuntu runners (e.g. runs-on | `junitxml-title` | | '' | Title for summary for junitxml | | `create-new-comment` | | false | When false, will update the same comment, otherwise will publish new comment on each run. | | `hide-comment` | | false | Hide the whole comment (use when you need only the `output`). Useful for auto-update bagdes in readme. See the [workflow](../main/.github/workflows/live-test.yml) for example | -| `default-branch` | | `main` | This branch name is usefull when generate "coverageHtml", it points direct links to files on this branch (instead of commit).
Usually "main" or "master" | +| `default-branch` | | `main` | This branch name is useful when generate "coverageHtml", it points direct links to files on this branch (instead of commit).
Usually "main" or "master" | | `multiple-files` | | '' | You can pass array of titles and files to generate single comment with table of results.
Single line should look like `Title, ./path/to/pytest-coverage.txt, ./path/to/pytest-junit.xml`
example:
`My Title 1, ./data/pytest-coverage_3.txt, ./data/pytest_1.xml`
**Note:** In that mode the `output` for `coverage` and `color` will be for the first file only. | | `remove-link-from-badge` | | false | When true, it will remove the link from badge to readme | | `unique-id-for-comment` | | '' | When running in a matrix, pass the matrix value, so each comment will be updated its own comment `unique-id-for-comment: ${{ matrix.python-version }}` | diff --git a/action.yml b/action.yml index 845e18f..8a9abc7 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,11 @@ inputs: default: './pytest-coverage.txt' required: false + issue-number: + description: 'PR number for the PR you want the comment to post to.' + default: '' + required: false + pytest-xml-coverage-path: description: 'The location of coverage-xml from pytest-coverage' default: '' diff --git a/dist/index.js b/dist/index.js index 759bac9..9e2914e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -17820,6 +17820,42 @@ const FILE_STATUSES = Object.freeze({ RENAMED: 'renamed', }); +const createOrEditComment = async ( + octokit, + repo, + owner, + issue_number, + body, + WATERMARK, +) => { + // Now decide if we should issue a new comment or edit an old one + const { data: comments } = await octokit.issues.listComments({ + repo, + owner, + issue_number, + }); + + const comment = comments.find((c) => c.body.startsWith(WATERMARK)); + + if (comment) { + core.info('Found previous comment, updating'); + await octokit.issues.updateComment({ + repo, + owner, + comment_id: comment.id, + body, + }); + } else { + core.info('No previous comment found, creating a new one'); + await octokit.issues.createComment({ + repo, + owner, + issue_number, + body, + }); + } +}; + const main = async () => { const token = core.getInput('github-token', { required: true }); const title = core.getInput('title', { required: false }); @@ -17842,6 +17878,7 @@ const main = async () => { }); const defaultBranch = core.getInput('default-branch', { required: false }); const covFile = core.getInput('pytest-coverage-path', { required: false }); + const issueNumberInput = core.getInput('issue-number', { required: false }); const covXmlFile = core.getInput('pytest-xml-coverage-path', { required: false, }); @@ -17897,10 +17934,10 @@ const main = async () => { } if (options.reportOnlyChangedFiles) { - const changedFiles = await getChangedFiles(options); + const changedFiles = await getChangedFiles(options, issueNumberInput); options.changedFiles = changedFiles; - // when github event come different from `pull_request` or `push` + // when github event is different from `pull_request`, `workflow_dispatch` or `push` if (!changedFiles) { options.reportOnlyChangedFiles = false; } @@ -17993,7 +18030,11 @@ const main = async () => { const body = WATERMARK + finalHtml; const octokit = github.getOctokit(token); - const issue_number = payload.pull_request ? payload.pull_request.number : 0; + const issue_number = payload.pull_request + ? payload.pull_request.number + : issueNumberInput + ? issueNumberInput + : 0; if (eventName === 'push') { core.info('Create commit comment'); @@ -18017,35 +18058,40 @@ const main = async () => { body, }); } else { - // Now decide if we should issue a new comment or edit an old one - const { data: comments } = await octokit.issues.listComments({ + await createOrEditComment( + octokit, repo, owner, issue_number, - }); - - const comment = comments.find((c) => c.body.startsWith(WATERMARK)); - - if (comment) { - core.info('Found previous comment, updating'); - await octokit.issues.updateComment({ + body, + WATERMARK, + ); + } + } else if (eventName === 'workflow_dispatch') { + await core.summary.addRaw(body, true).write(); + if (!issueNumberInput) { + // prettier-ignore + core.warning(`To use this action on a \`${eventName}\`, you need to pass an pull request number.`) + } else { + if (createNewComment) { + core.info('Creating a new comment'); + await octokit.issues.createComment({ repo, owner, - comment_id: comment.id, + issue_number, body, }); } else { - core.info('No previous comment found, creating a new one'); - await octokit.issues.createComment({ + await createOrEditComment( + octokit, repo, owner, issue_number, body, - }); + WATERMARK, + ); } } - } else if (eventName === 'workflow_dispatch') { - await core.summary.addRaw(body, true).write(); } else { if (!options.hideComment) { // prettier-ignore @@ -18055,7 +18101,7 @@ const main = async () => { }; // generate object of all files that changed based on commit through Github API -const getChangedFiles = async (options) => { +const getChangedFiles = async (options, pr_number) => { try { const { context } = github; const { eventName, payload } = context; @@ -18075,9 +18121,20 @@ const getChangedFiles = async (options) => { base = payload.before; head = payload.after; break; + case 'workflow_dispatch': { + const { data } = await octokit.pulls.get({ + owner, + repo, + pull_number: pr_number, + }); + + base = data.base.ref; + head = data.head.ref; + break; + } default: // prettier-ignore - core.warning(`\`report-only-changed-files: true\` supports only on \`pull_request\` and \`push\`, \`${eventName}\` events are not supported.`) + core.warning(`\`report-only-changed-files: true\` supports only on \`pull_request\`, \`workflow_dispatch\` and \`push\`. Other \`${eventName}\` events are not supported.`) return null; } diff --git a/package-lock.json b/package-lock.json index 6d7aad3..cf3a47c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pytest-coverage-comment", - "version": "1.1.49", + "version": "1.1.50", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pytest-coverage-comment", - "version": "1.1.49", + "version": "1.1.50", "license": "MIT", "dependencies": { "@actions/core": "^1.10.1", diff --git a/package.json b/package.json index 8ceefa6..c51870b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pytest-coverage-comment", - "version": "1.1.49", + "version": "1.1.50", "description": "Comments a pull request with the pytest code coverage badge, full report and tests summary", "author": "Misha Kav", "license": "MIT", diff --git a/src/index.js b/src/index.js index e255462..b7e615d 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,42 @@ const FILE_STATUSES = Object.freeze({ RENAMED: 'renamed', }); +const createOrEditComment = async ( + octokit, + repo, + owner, + issue_number, + body, + WATERMARK, +) => { + // Now decide if we should issue a new comment or edit an old one + const { data: comments } = await octokit.issues.listComments({ + repo, + owner, + issue_number, + }); + + const comment = comments.find((c) => c.body.startsWith(WATERMARK)); + + if (comment) { + core.info('Found previous comment, updating'); + await octokit.issues.updateComment({ + repo, + owner, + comment_id: comment.id, + body, + }); + } else { + core.info('No previous comment found, creating a new one'); + await octokit.issues.createComment({ + repo, + owner, + issue_number, + body, + }); + } +}; + const main = async () => { const token = core.getInput('github-token', { required: true }); const title = core.getInput('title', { required: false }); @@ -39,6 +75,7 @@ const main = async () => { }); const defaultBranch = core.getInput('default-branch', { required: false }); const covFile = core.getInput('pytest-coverage-path', { required: false }); + const issueNumberInput = core.getInput('issue-number', { required: false }); const covXmlFile = core.getInput('pytest-xml-coverage-path', { required: false, }); @@ -94,10 +131,10 @@ const main = async () => { } if (options.reportOnlyChangedFiles) { - const changedFiles = await getChangedFiles(options); + const changedFiles = await getChangedFiles(options, issueNumberInput); options.changedFiles = changedFiles; - // when github event come different from `pull_request` or `push` + // when github event is different from `pull_request`, `workflow_dispatch` or `push` if (!changedFiles) { options.reportOnlyChangedFiles = false; } @@ -190,7 +227,11 @@ const main = async () => { const body = WATERMARK + finalHtml; const octokit = github.getOctokit(token); - const issue_number = payload.pull_request ? payload.pull_request.number : 0; + const issue_number = payload.pull_request + ? payload.pull_request.number + : issueNumberInput + ? issueNumberInput + : 0; if (eventName === 'push') { core.info('Create commit comment'); @@ -214,35 +255,40 @@ const main = async () => { body, }); } else { - // Now decide if we should issue a new comment or edit an old one - const { data: comments } = await octokit.issues.listComments({ + await createOrEditComment( + octokit, repo, owner, issue_number, - }); - - const comment = comments.find((c) => c.body.startsWith(WATERMARK)); - - if (comment) { - core.info('Found previous comment, updating'); - await octokit.issues.updateComment({ + body, + WATERMARK, + ); + } + } else if (eventName === 'workflow_dispatch') { + await core.summary.addRaw(body, true).write(); + if (!issueNumberInput) { + // prettier-ignore + core.warning(`To use this action on a \`${eventName}\`, you need to pass an pull request number.`) + } else { + if (createNewComment) { + core.info('Creating a new comment'); + await octokit.issues.createComment({ repo, owner, - comment_id: comment.id, + issue_number, body, }); } else { - core.info('No previous comment found, creating a new one'); - await octokit.issues.createComment({ + await createOrEditComment( + octokit, repo, owner, issue_number, body, - }); + WATERMARK, + ); } } - } else if (eventName === 'workflow_dispatch') { - await core.summary.addRaw(body, true).write(); } else { if (!options.hideComment) { // prettier-ignore @@ -252,7 +298,7 @@ const main = async () => { }; // generate object of all files that changed based on commit through Github API -const getChangedFiles = async (options) => { +const getChangedFiles = async (options, pr_number) => { try { const { context } = github; const { eventName, payload } = context; @@ -272,9 +318,20 @@ const getChangedFiles = async (options) => { base = payload.before; head = payload.after; break; + case 'workflow_dispatch': { + const { data } = await octokit.pulls.get({ + owner, + repo, + pull_number: pr_number, + }); + + base = data.base.ref; + head = data.head.ref; + break; + } default: // prettier-ignore - core.warning(`\`report-only-changed-files: true\` supports only on \`pull_request\` and \`push\`, \`${eventName}\` events are not supported.`) + core.warning(`\`report-only-changed-files: true\` supports only on \`pull_request\`, \`workflow_dispatch\` and \`push\`. Other \`${eventName}\` events are not supported.`) return null; }