Skip to content

Support custom Pull Request number #20

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

Open
wants to merge 6 commits into
base: main
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For more information on these inputs, see the [Workflow syntax for GitHub Action
- `github-token`: Set the `${{ secrets.GITHUB_TOKEN }}` token to have the action comment the coverage summary in the pull request. This token is provided by Actions, you do not need to create your own token. Optional. Default: ``
- `working-directory`: The working directory containing the source files referenced in the LCOV files. Optional. Default: `./`
- `title`: A prefix before the title "LCOV of commit..." Optional. Default: ``
- `pr-number`: The number of the pull request you want to process. Required when the event is not `pull_request` or `pull_request_target`

### Outputs
None.
Expand Down Expand Up @@ -50,11 +51,11 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
working-directory: apps/my-first-app
```
*Note:* Only the `pull_request` and `pull_request_target` events are supported. This action does nothing when triggered by other event types.
*Tip:* This should primarily be used with `pull_request` and `pull_request_target` events.

### Flutter Workflows

This is used in my opinionated [GitHub Actions: Flutter Workflows](https://github.com/zgosalvez/github-actions-flutter-workflows) repository along with other actions for a complete end-to-end DevOps experience.

## License
The scripts and documentation in this project are released under the [MIT License](LICENSE)
The scripts and documentation in this project are released under the [MIT License](LICENSE)
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
title-prefix:
description: 'A prefix before the title "LCOV of commit..." Optional. Default: ``'
required: false
pr-number:
descirption: 'The number of the pull request you want to process. Required when the event is not `pull_request` or `pull_request_target`'
required: false
runs:
using: 'node12'
main: 'dist/main/index.js'
Expand Down
27 changes: 20 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,27 @@ async function run() {
const gitHubToken = core.getInput('github-token').trim();
const errorMessage = `The code coverage is too low: ${totalCoverage}. Expected at least ${minimumCoverage}.`;
const isFailure = totalCoverage < minimumCoverage;
let sha;
let prNumber;

if (gitHubToken !== '') {
if (events.includes(github.context.eventName)) {
sha = github.context.payload.pull_request.head.sha;
prNumber = github.context.payload.pull_request.number;
} else {
sha = github.context.payload.after || github.context.sha;
prNumber = core.getInput('pr-number');

if (!prNumber) {
throw Error('The `pr-number` input is required when the event is not a `pull_request` or `pull_request_target`.');
}
}

if (gitHubToken !== '' && events.includes(github.context.eventName)) {
const octokit = await github.getOctokit(gitHubToken);
const summary = await summarize(coverageFile);
const details = await detail(coverageFile, octokit);
const sha = github.context.payload.pull_request.head.sha;
const details = await detail(coverageFile, octokit, prNumber);
const shaShort = sha.substr(0, 7);
let body = `### ${title ? `${title} ` : ''}[LCOV](https://github.com/marketplace/actions/report-lcov) of commit [<code>${shaShort}</code>](${github.context.payload.pull_request.number}/commits/${sha}) during [${github.context.workflow} #${github.context.runNumber}](../actions/runs/${github.context.runId})\n<pre>${summary}\n\nFiles changed coverage rate:${details}</pre>`;
let body = `### ${title ? `${title} ` : ''}[LCOV](https://github.com/marketplace/actions/report-lcov) of commit [<code>${shaShort}</code>](${prNumber}/commits/${sha}) during [${github.context.workflow} #${github.context.runNumber}](../actions/runs/${github.context.runId})\n<pre>${summary}\n\nFiles changed coverage rate:${details}</pre>`;

if (isFailure) {
body += `\n:no_entry: ${errorMessage}`;
Expand All @@ -44,7 +57,7 @@ async function run() {
await octokit.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request.number,
issue_number: prNumber,
body: body,
});
} else {
Expand Down Expand Up @@ -138,7 +151,7 @@ async function summarize(coverageFile) {
return lines.join('\n');
}

async function detail(coverageFile, octokit) {
async function detail(coverageFile, octokit, prNumber) {
let output = '';

const options = {};
Expand Down Expand Up @@ -171,7 +184,7 @@ async function detail(coverageFile, octokit) {
.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
pull_number: prNumber,
});
const listFilesResponse = await octokit.paginate(listFilesOptions);
const changedFiles = listFilesResponse.map(file => file.filename);
Expand Down