Skip to content

Commit

Permalink
Support for comment in workflow dispatch v2 (#151)
Browse files Browse the repository at this point in the history
* bump version to 1.1.50

* Hopefully this works (#150)

* Adding in support for workflow_dispatch events: Take Two (#149)

* adding the ability to create comments in non pull request related contexts

* updating documentation

* updates

* stashing changes

* stashing changes

* Update action.yml

* stashing changes

* updates

* updates

* test

* test

* updates

* updates

* more updates

* more updates

* small typo fix

* fixing

* updates

---------

Co-authored-by: Alex Yong <[email protected]>
  • Loading branch information
MishaKav and alexjyong authored Nov 26, 2023
1 parent 1aa8107 commit 0500a15
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 44 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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).<br/>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).<br/>Usually "main" or "master" |
| `multiple-files` | | '' | You can pass array of titles and files to generate single comment with table of results.<br/>Single line should look like `Title, ./path/to/pytest-coverage.txt, ./path/to/pytest-junit.xml`<br/> example:<br/> `My Title 1, ./data/pytest-coverage_3.txt, ./data/pytest_1.xml`<br/>**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 }}` |
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down
97 changes: 77 additions & 20 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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,
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
97 changes: 77 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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,
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 0500a15

Please sign in to comment.