From e411b2e2cb35330929a537363c7ca48522466385 Mon Sep 17 00:00:00 2001 From: Luke Walton Date: Thu, 22 Aug 2024 16:07:53 +0100 Subject: [PATCH] fix: Action fails when previous commit had a failing test (#32) --- dist/index.js | 30 ++++++++++++++++++++---------- src/main.ts | 2 +- src/scripts/prevCoverage.ts | 35 ++++++++++++++++++++++------------- src/scripts/push.ts | 4 ++-- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/dist/index.js b/dist/index.js index e4bca85..0548ea4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -113322,7 +113322,7 @@ const run = async (isLocal) => { : undefined; if (createComment) (0, comment_1.postComment)(octokit, comment, github_1.context); - await (0, push_1.push)(); + await (0, push_1.push)(exports.COVERAGE_DIR); if (analyzeStr?.error || testStr?.error || coverageStr?.error) { (0, core_1.setFailed)(`${analyzeStr?.output}\n${testStr?.output}\n${coverageStr?.output}`); } @@ -113687,6 +113687,7 @@ const artifact_1 = __nccwpck_require__(26984); const core_1 = __nccwpck_require__(72614); const core_2 = __nccwpck_require__(72614); const adm_zip_1 = __importDefault(__nccwpck_require__(98154)); +const runTests_1 = __nccwpck_require__(99054); const ARTIFACT_NAME = "coverage"; /** * Retrieve previous coverage report from the base branch @@ -113768,13 +113769,22 @@ exports.retrievePreviousCoverage = retrievePreviousCoverage; const generatePreviousCoverage = async (prev_sha, current_branch, coverage_directory) => { const artifact = new artifact_1.DefaultArtifactClient(); await (0, exec_1.exec)(`git checkout ${prev_sha}`); - await (0, exec_1.exec)(`flutter test --coverage --coverage-path ${coverage_directory}/lcov.info`); - const report = await (0, utils_1.importLcov)(coverage_directory); - const { id, size } = await artifact.uploadArtifact(ARTIFACT_NAME + "-" + prev_sha, [`${coverage_directory}/${utils_1.COV_FILE}`], ".", {}); - (0, core_2.debug)(`Artifact uploaded with id: ${id} and size: ${size}`); - await (0, exec_1.exec)(`git reset --hard`); - await (0, exec_1.exec)(`git checkout ${current_branch}`); - return report; + let report; + try { + await (0, runTests_1.getTest)(coverage_directory); + report = await (0, utils_1.importLcov)(coverage_directory); + const { id, size } = await artifact.uploadArtifact(ARTIFACT_NAME + "-" + prev_sha, [`${coverage_directory}/${utils_1.COV_FILE}`], ".", {}); + (0, core_2.debug)(`Artifact uploaded with id: ${id} and size: ${size}`); + } + catch (e) { + console.error("Failed to run tests"); + } + finally { + await (0, exec_1.exec)(`git reset --hard`); + await (0, exec_1.exec)(`git clean -d -f .`); + await (0, exec_1.exec)(`git checkout ${current_branch}`); + return report; + } }; @@ -113794,7 +113804,7 @@ const core_2 = __nccwpck_require__(72614); /** * Push changes to the branch */ -const push = async () => { +const push = async (coverageDirectory) => { (0, core_1.startGroup)("Check for changes"); let stdout = ""; try { @@ -113812,7 +113822,7 @@ const push = async () => { (0, core_1.startGroup)("Push changes"); await (0, exec_1.exec)('git config --global user.name "github-actions"'); await (0, exec_1.exec)('git config --global user.email "github-actions@github.com"'); - await (0, exec_1.exec)("git add -A"); + await (0, exec_1.exec)(`git add -A -- ':!${coverageDirectory}'`); (0, child_process_1.execSync)(`git commit -m 'chore(automated): Lint commit and format' `); await (0, exec_1.exec)("git push -f"); (0, core_2.debug)("Changes pushed onto branch"); diff --git a/src/main.ts b/src/main.ts index 6e2570a..ec69b1d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -54,7 +54,7 @@ const run = async (isLocal: boolean) => { if (createComment) postComment(octokit, comment!, context); - await push(); + await push(COVERAGE_DIR); if (analyzeStr?.error || testStr?.error || coverageStr?.error) { setFailed(`${analyzeStr?.output}\n${testStr?.output}\n${coverageStr?.output}`); diff --git a/src/scripts/prevCoverage.ts b/src/scripts/prevCoverage.ts index 6cd7c42..0fed290 100644 --- a/src/scripts/prevCoverage.ts +++ b/src/scripts/prevCoverage.ts @@ -7,6 +7,7 @@ import { DefaultArtifactClient } from "@actions/artifact"; import { endGroup, startGroup } from "@actions/core"; import { debug } from "@actions/core"; import AdmZip from "adm-zip"; +import { getTest } from "./runTests"; const ARTIFACT_NAME = "coverage"; @@ -104,20 +105,28 @@ const generatePreviousCoverage = async ( prev_sha: string, current_branch: string, coverage_directory: string -): Promise => { +): Promise => { const artifact = new DefaultArtifactClient(); await exec(`git checkout ${prev_sha}`); - await exec(`flutter test --coverage --coverage-path ${coverage_directory}/lcov.info`); - const report = await importLcov(coverage_directory); - const { id, size } = await artifact.uploadArtifact( - ARTIFACT_NAME + "-" + prev_sha, - [`${coverage_directory}/${COV_FILE}`], - ".", - {} - ); + let report: Lcov | undefined; + try { + await getTest(coverage_directory); + report = await importLcov(coverage_directory); - debug(`Artifact uploaded with id: ${id} and size: ${size}`); - await exec(`git reset --hard`); - await exec(`git checkout ${current_branch}`); - return report; + const { id, size } = await artifact.uploadArtifact( + ARTIFACT_NAME + "-" + prev_sha, + [`${coverage_directory}/${COV_FILE}`], + ".", + {} + ); + + debug(`Artifact uploaded with id: ${id} and size: ${size}`); + } catch (e) { + console.error("Failed to run tests"); + } finally { + await exec(`git reset --hard`); + await exec(`git clean -d -f .`); + await exec(`git checkout ${current_branch}`); + return report; + } }; diff --git a/src/scripts/push.ts b/src/scripts/push.ts index 303261e..6ab6932 100644 --- a/src/scripts/push.ts +++ b/src/scripts/push.ts @@ -6,7 +6,7 @@ import { debug } from "@actions/core"; /** * Push changes to the branch */ -export const push = async () => { +export const push = async (coverageDirectory: string) => { startGroup("Check for changes"); let stdout: string = ""; try { @@ -24,7 +24,7 @@ export const push = async () => { startGroup("Push changes"); await exec('git config --global user.name "github-actions"'); await exec('git config --global user.email "github-actions@github.com"'); - await exec("git add -A"); + await exec(`git add -A -- ':!${coverageDirectory}'`); execSync(`git commit -m 'chore(automated): Lint commit and format' `); await exec("git push -f"); debug("Changes pushed onto branch");