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: Add inputs #20

Merged
merged 4 commits into from
Jul 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/on-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ permissions:
contents: write
pull-requests: write

name: On Main
name: CI - On Main

jobs:
release-please:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
pull_request_target:

name: Pull Request
name: CI - Pull Request

jobs:
set-env:
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ Follow the instructions below to integrate this action into your workflow.
<!-- x-release-please-start-version -->

```yml
name: Pull Request
on:
pull_request:

jobs:
code-quality:
runs-on: ubuntu-latest
Expand All @@ -21,6 +17,8 @@ jobs:
- uses: actions/checkout@v4
# Set up Flutter within the action
- uses: subosito/flutter-action@v2
with:
# See https://github.com/subosito/flutter-action
- uses: ZebraDevs/[email protected]
with:
# Token used for authentication.
Expand All @@ -29,9 +27,20 @@ jobs:

<!-- x-release-please-end -->

## Inputs

| Name | Description | Required | default |
| -------------- | ----------------------------------------------------------------- | -------- | ------- |
| token | Token used for pushing fixes and commenting on PRs. | true | |
| run-tests | Whether tests should be run. | false | true |
| run-analysis | Whether static analysis should be run. | false | true |
| run-coverage | Whether code coverage should be run. | false | true |
| run-behind-by | Whether action should check if HEAD branch is behind base branch. | false | true |
| create-comment | Whether the action should comment the output status. | false | true |

## Contributing

This project welcomes contributions. Pleae check out our [Contributing guide](CONTRIBUTING.md) to learn more on how to get started.
This project welcomes contributions. Please check out our [Contributing guide](CONTRIBUTING.md) to learn more on how to get started.

### License

Expand Down
30 changes: 30 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,33 @@ inputs:
token:
description: "Token used for pushing fixes and commenting on PRs"
required: true

run-tests:
description: "Run tests"
required: false
default: true
type: boolean

run-analyze:
description: "Run static analysis"
required: false
default: true
type: boolean

run-coverage:
description: "Run test coverage check"
required: false
default: true
type: boolean

run-behind-by:
description: "Run behind by check"
required: false
default: true
type: boolean

create-comment:
description: "Create a comment on PRs"
required: false
default: true
type: boolean
38 changes: 23 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30564,14 +30564,14 @@ exports.postComment = postComment;
const core_1 = __nccwpck_require__(2614);
const SIGNATURE = `<sub>Created with <a href='https://github.com/ZebraDevs/flutter-code-quality'>Flutter code quality action</a></sub>`;
const createComment = (analyze, test, coverage, behindBy) => {
const isSuccess = !analyze.error && !test.error && !coverage.error && !behindBy.error;
const isSuccess = !analyze?.error && !test?.error && !coverage?.error && !behindBy?.error;
let output = `<h2>PR Checks complete</h2>
<ul>
<li>✅ - Linting / Formatting</li>
<li>${analyze.output.replaceAll("`|\"|'|<|>", "")}</li>
<li>${test.output.replaceAll("`|\"|'|<|>", "")}</li>
${isSuccess ? "<li>✅ - Branch is not behind</li>" : ""}
<li>${coverage.output.replaceAll("`|\"|'|<|>", "")}</li>
${analyze ? `<li>${analyze?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
${test ? `<li>${test?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
${behindBy && isSuccess ? "<li>✅ - Branch is not behind</li>" : ""}
${coverage ? `<li>${coverage?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
</ul>

${SIGNATURE}
Expand Down Expand Up @@ -30685,7 +30685,7 @@ const getCoverage = (oldCoverage) => {
};
exports.getCoverage = getCoverage;
const getOldCoverage = () => {
let value;
let value = 0;
(0, core_1.startGroup)("Retrieving existing coverage value");
try {
const contents = (0, node_fs_1.readFileSync)("coverage/lcov.info", "utf8");
Expand Down Expand Up @@ -33153,18 +33153,26 @@ const push_1 = __nccwpck_require__(3662);
const run = async () => {
try {
const token = process.env.GITHUB_TOKEN || (0, core_1.getInput)("token");
const runTests = (0, core_1.getBooleanInput)("run-tests");
const runAnalyze = (0, core_1.getBooleanInput)("run-analyze");
const runCoverage = (0, core_1.getBooleanInput)("run-coverage");
const runBehindBy = (0, core_1.getBooleanInput)("run-behind-by");
const createComment = (0, core_1.getBooleanInput)("create-comment");
const octokit = (0, github_1.getOctokit)(token);
const behindByStr = await (0, behind_1.checkBranchStatus)(octokit, github_1.context);
const behindByStr = runBehindBy ? await (0, behind_1.checkBranchStatus)(octokit, github_1.context) : undefined;
await (0, setup_1.setup)();
const oldCoverage = (0, coverage_1.getOldCoverage)();
const analyzeStr = await (0, analyze_1.getAnalyze)();
const testStr = await (0, runTests_1.getTest)();
const coverageStr = (0, coverage_1.getCoverage)(oldCoverage);
const comment = (0, comment_1.createComment)(analyzeStr, testStr, coverageStr, behindByStr);
(0, comment_1.postComment)(octokit, comment, github_1.context);
const oldCoverage = runCoverage ? (0, coverage_1.getOldCoverage)() : undefined;
const analyzeStr = runAnalyze ? await (0, analyze_1.getAnalyze)() : undefined;
const testStr = runTests ? await (0, runTests_1.getTest)() : undefined;
const coverageStr = runCoverage ? (0, coverage_1.getCoverage)(oldCoverage) : undefined;
const comment = createComment
? (0, comment_1.createComment)(analyzeStr, testStr, coverageStr, behindByStr)
: undefined;
if (createComment)
(0, comment_1.postComment)(octokit, comment, github_1.context);
await (0, push_1.push)();
if (analyzeStr.error || testStr.error || coverageStr.error) {
(0, core_1.setFailed)(`${analyzeStr.output}\n${testStr.output}\n${coverageStr.output}`);
if (analyzeStr?.error || testStr?.error || coverageStr?.error) {
(0, core_1.setFailed)(`${analyzeStr?.output}\n${testStr?.output}\n${coverageStr?.output}`);
}
}
catch (err) {
Expand Down
32 changes: 21 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import { getInput, setFailed } from "@actions/core";
import { getBooleanInput, getInput, setFailed } from "@actions/core";
import { getAnalyze } from "./scripts/analyze";
import { getOctokit, context } from "@actions/github";
import { getCoverage, getOldCoverage } from "./scripts/coverage";
import { getTest } from "./scripts/runTests";
import { createComment, postComment } from "./scripts/comment";
import { createComment as getComment, postComment } from "./scripts/comment";
import { setup } from "./scripts/setup";
import { checkBranchStatus } from "./scripts/behind";
import { push } from "./scripts/push";
import { create } from "node:domain";

export type stepResponse = { output: string; error: boolean };

const run = async () => {
try {
const token = process.env.GITHUB_TOKEN || getInput("token");
const runTests = getBooleanInput("run-tests");
const runAnalyze = getBooleanInput("run-analyze");
const runCoverage = getBooleanInput("run-coverage");
const runBehindBy = getBooleanInput("run-behind-by");
const createComment = getBooleanInput("create-comment");

const octokit = getOctokit(token);
const behindByStr = await checkBranchStatus(octokit, context);
const behindByStr: stepResponse | undefined = runBehindBy ? await checkBranchStatus(octokit, context) : undefined;
await setup();
const oldCoverage: number | undefined = getOldCoverage();
const oldCoverage: number | undefined = runCoverage ? getOldCoverage() : undefined;

const analyzeStr: stepResponse | undefined = runAnalyze ? await getAnalyze() : undefined;
const testStr: stepResponse | undefined = runTests ? await getTest() : undefined;
const coverageStr: stepResponse | undefined = runCoverage ? getCoverage(oldCoverage) : undefined;

const analyzeStr: stepResponse = await getAnalyze();
const testStr: stepResponse = await getTest();
const coverageStr: stepResponse = getCoverage(oldCoverage);
const comment: string | undefined = createComment
? getComment(analyzeStr, testStr, coverageStr, behindByStr)
: undefined;

const comment = createComment(analyzeStr, testStr, coverageStr, behindByStr);
if (createComment) postComment(octokit, comment!, context);

postComment(octokit, comment, context);
await push();

if (analyzeStr.error || testStr.error || coverageStr.error) {
setFailed(`${analyzeStr.output}\n${testStr.output}\n${coverageStr.output}`);
if (analyzeStr?.error || testStr?.error || coverageStr?.error) {
setFailed(`${analyzeStr?.output}\n${testStr?.output}\n${coverageStr?.output}`);
}
} catch (err) {
setFailed(`Action failed with error ${err}`);
Expand Down
18 changes: 9 additions & 9 deletions src/scripts/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { stepResponse } from "../main";
const SIGNATURE = `<sub>Created with <a href='https://github.com/ZebraDevs/flutter-code-quality'>Flutter code quality action</a></sub>`;

export const createComment = (
analyze: stepResponse,
test: stepResponse,
coverage: stepResponse,
behindBy: stepResponse
analyze: stepResponse | undefined,
test: stepResponse | undefined,
coverage: stepResponse | undefined,
behindBy: stepResponse | undefined
): string => {
const isSuccess = !analyze.error && !test.error && !coverage.error && !behindBy.error;
const isSuccess = !analyze?.error && !test?.error && !coverage?.error && !behindBy?.error;

let output = `<h2>PR Checks complete</h2>
<ul>
<li>✅ - Linting / Formatting</li>
<li>${analyze.output.replaceAll("`|\"|'|<|>", "")}</li>
<li>${test.output.replaceAll("`|\"|'|<|>", "")}</li>
${isSuccess ? "<li>✅ - Branch is not behind</li>" : ""}
<li>${coverage.output.replaceAll("`|\"|'|<|>", "")}</li>
${analyze ? `<li>${analyze?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
${test ? `<li>${test?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
${behindBy && isSuccess ? "<li>✅ - Branch is not behind</li>" : ""}
${coverage ? `<li>${coverage?.output.replaceAll("`|\"|'|<|>", "")}</li>` : ""}
</ul>

${SIGNATURE}
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const getCoverage = (oldCoverage: number | undefined): stepResponse => {
return response;
};

export const getOldCoverage = (): number | undefined => {
let value: number | undefined;
export const getOldCoverage = (): number => {
let value: number = 0;
startGroup("Retrieving existing coverage value");
try {
const contents = readFileSync("coverage/lcov.info", "utf8");
Expand Down
3 changes: 1 addition & 2 deletions tests/src/scripts/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ test("no old coverage", () => {
test("fail", () => {
process.chdir("tests/fail_repo");
const result: stepResponse = getCoverage(undefined);
console.log(result);
expect(result.error).toBe(true);
expect(result.output).toEqual(COV_FAILURE);
process.chdir("../..");
Expand All @@ -70,7 +69,7 @@ test("oldCoverage fail", () => {
process.chdir("tests/fail_repo");

const result = getOldCoverage();
expect(result).toEqual(undefined);
expect(result).toEqual(0);
process.chdir("../..");
});
test("oldCoverage pass", () => {
Expand Down