From 86504796fbb3454adaf97b58d6a07ea260497b19 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 16 Jul 2024 13:18:15 +0100 Subject: [PATCH 1/4] feat: Add inputs to enable / disable parts of the action --- README.md | 19 ++++++++++++++----- action.yml | 30 ++++++++++++++++++++++++++++++ src/main.ts | 30 ++++++++++++++++++++---------- src/scripts/comment.ts | 18 +++++++++--------- src/scripts/coverage.ts | 4 ++-- 5 files changed, 75 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3a53bc3..dcd0005 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,6 @@ Follow the instructions below to integrate this action into your workflow. ```yml -name: Pull Request -on: - pull_request: - jobs: code-quality: runs-on: ubuntu-latest @@ -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/flutter-code-quality@v1.0.3 with: # Token used for authentication. @@ -29,9 +27,20 @@ jobs: +## 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 diff --git a/action.yml b/action.yml index 0fb5dc9..c03f450 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/main.ts b/src/main.ts index b4b3216..5632c16 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,32 +3,42 @@ 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 = getInput("run-tests"); + const runAnalyze = getInput("run-analyze"); + const runCoverage = getInput("run-coverage"); + const runBehindBy = getInput("run-behind-by"); + const createComment = getInput("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}`); diff --git a/src/scripts/comment.ts b/src/scripts/comment.ts index f80b358..ffbb465 100644 --- a/src/scripts/comment.ts +++ b/src/scripts/comment.ts @@ -6,20 +6,20 @@ import { stepResponse } from "../main"; const SIGNATURE = `Created with Flutter code quality action`; 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 = `

PR Checks complete

${SIGNATURE} diff --git a/src/scripts/coverage.ts b/src/scripts/coverage.ts index e3a7a4a..d5e8baf 100644 --- a/src/scripts/coverage.ts +++ b/src/scripts/coverage.ts @@ -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"); From fed65641b676aa11a706710d9425bed5cce695a4 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 16 Jul 2024 13:23:27 +0100 Subject: [PATCH 2/4] test: Fix coverage test --- tests/src/scripts/coverage.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/src/scripts/coverage.test.ts b/tests/src/scripts/coverage.test.ts index bf5ff55..adba009 100644 --- a/tests/src/scripts/coverage.test.ts +++ b/tests/src/scripts/coverage.test.ts @@ -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("../.."); @@ -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", () => { From 513700e1efe7d303880f1bd858241f333dc15cc2 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 16 Jul 2024 13:36:54 +0100 Subject: [PATCH 3/4] fix import --- .github/workflows/on-main.yml | 2 +- .github/workflows/pull-request.yml | 2 +- dist/index.js | 38 ++++++++++++++++++------------ src/main.ts | 12 +++++----- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.github/workflows/on-main.yml b/.github/workflows/on-main.yml index 7159a03..73de421 100644 --- a/.github/workflows/on-main.yml +++ b/.github/workflows/on-main.yml @@ -7,7 +7,7 @@ permissions: contents: write pull-requests: write -name: On Main +name: CI - On Main jobs: release-please: diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 1260d4e..b3f1175 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,7 +1,7 @@ on: pull_request_target: -name: Pull Request +name: CI - Pull Request jobs: set-env: diff --git a/dist/index.js b/dist/index.js index a09c544..744ad5a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30564,14 +30564,14 @@ exports.postComment = postComment; const core_1 = __nccwpck_require__(2614); const SIGNATURE = `Created with Flutter code quality action`; 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 = `

PR Checks complete

  • ✅ - Linting / Formatting
  • -
  • ${analyze.output.replaceAll("`|\"|'|<|>", "")}
  • -
  • ${test.output.replaceAll("`|\"|'|<|>", "")}
  • - ${isSuccess ? "
  • ✅ - Branch is not behind
  • " : ""} -
  • ${coverage.output.replaceAll("`|\"|'|<|>", "")}
  • + ${analyze ? `
  • ${analyze?.output.replaceAll("`|\"|'|<|>", "")}
  • ` : ""} + ${test ? `
  • ${test?.output.replaceAll("`|\"|'|<|>", "")}
  • ` : ""} + ${behindBy && isSuccess ? "
  • ✅ - Branch is not behind
  • " : ""} + ${coverage ? `
  • ${coverage?.output.replaceAll("`|\"|'|<|>", "")}
  • ` : ""}
${SIGNATURE} @@ -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"); @@ -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.getInput)("run-tests"); + const runAnalyze = (0, core_1.getInput)("run-analyze"); + const runCoverage = (0, core_1.getInput)("run-coverage"); + const runBehindBy = (0, core_1.getInput)("run-behind-by"); + const createComment = (0, core_1.getInput)("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) { diff --git a/src/main.ts b/src/main.ts index 5632c16..95c1590 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -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"; @@ -14,11 +14,11 @@ export type stepResponse = { output: string; error: boolean }; const run = async () => { try { const token = process.env.GITHUB_TOKEN || getInput("token"); - const runTests = getInput("run-tests"); - const runAnalyze = getInput("run-analyze"); - const runCoverage = getInput("run-coverage"); - const runBehindBy = getInput("run-behind-by"); - const createComment = getInput("create-comment"); + 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: stepResponse | undefined = runBehindBy ? await checkBranchStatus(octokit, context) : undefined; From 4997ebbb64b35ab1cb2e2cfc5ee5e66f35aa2def Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 16 Jul 2024 14:20:49 +0000 Subject: [PATCH 4/4] ci(automated commit): Build bundled file --- dist/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index 744ad5a..522f0e5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -33153,11 +33153,11 @@ 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.getInput)("run-tests"); - const runAnalyze = (0, core_1.getInput)("run-analyze"); - const runCoverage = (0, core_1.getInput)("run-coverage"); - const runBehindBy = (0, core_1.getInput)("run-behind-by"); - const createComment = (0, core_1.getInput)("create-comment"); + 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 = runBehindBy ? await (0, behind_1.checkBranchStatus)(octokit, github_1.context) : undefined; await (0, setup_1.setup)();