Skip to content

Commit

Permalink
fix: Action fails when previous commit had a failing test (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
thelukewalton authored Aug 22, 2024
1 parent 1a53f5d commit e411b2e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
30 changes: 20 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
};


Expand All @@ -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 {
Expand All @@ -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 "[email protected]"');
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");
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
35 changes: 22 additions & 13 deletions src/scripts/prevCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -104,20 +105,28 @@ const generatePreviousCoverage = async (
prev_sha: string,
current_branch: string,
coverage_directory: string
): Promise<Lcov> => {
): Promise<Lcov | undefined> => {
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;
}
};
4 changes: 2 additions & 2 deletions src/scripts/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 "[email protected]"');
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");
Expand Down

0 comments on commit e411b2e

Please sign in to comment.