Skip to content

Commit

Permalink
implement fail at end (#438)
Browse files Browse the repository at this point in the history
* implement fail at end

* bump version

* updated readme
  • Loading branch information
shubhbapna authored May 30, 2023
1 parent b1bf2ec commit cccbb74
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ Options:
to replace with the ReplacementEx
--skipProjectCheckout <projects...> A list of projects to skip checkout.
--skipCheckout skip checkout for all projects. Overrides skipProjectCheckout (default: false)
-fae, --fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue (default: false)
-h, --help display help for command
```

Expand Down Expand Up @@ -649,6 +650,7 @@ Options:
to replace with the ReplacementEx
--skipProjectCheckout <projects...> A list of projects to skip checkout.
--skipCheckout skip checkout for all projects. Overrides skipProjectCheckout (default: false)
-fae, --fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue (default: false)
-h, --help display help for command
```

Expand Down Expand Up @@ -685,6 +687,7 @@ Options:
to replace with the ReplacementEx
--skipProjectCheckout <projects...> A list of projects to skip checkout.
--skipCheckout skip checkout for all projects. Overrides skipProjectCheckout (default: false)
-fae, --fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue (default: false)
-h, --help display help for command
```

Expand Down Expand Up @@ -725,6 +728,7 @@ Options:
to replace with the ReplacementEx
--skipProjectCheckout <projects...> A list of projects to skip checkout.
--skipCheckout skip checkout for all projects. Overrides skipProjectCheckout (default: false)
-fae, --fail-at-end Only fail the build afterwards; allow all non-impacted builds to continue (default: false)
-h, --help display help for command
```
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/build-chain-action",
"version": "3.1.0",
"version": "3.1.1",
"description": "Library to execute commands based on github projects dependencies.",
"main": "dist/index.js",
"author": "",
Expand Down
1 change: 1 addition & 0 deletions src/domain/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface InputValues extends OptionValues {
url?: string;
branch?: string;
group?: string;
failAtEnd?: boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class BuildSubCommandFactory {
.option("-t, --customCommandTreatment <exp...>", "Each exp must be of the form <RegEx||ReplacementEx>. Regex defines the regular expression for what you want to replace with the ReplacementEx")
.option("--skipProjectCheckout <projects...>", "A list of projects to skip checkout.")
.option("--skipCheckout", "skip checkout for all projects. Overrides skipProjectCheckout", false)
.option("-fae, --fail-at-end", "Only fail the build afterwards; allow all non-impacted builds to continue", false)
.action((options) => {
const parsedInputs = Container.get(InputService);
if (options.debug) options.loggerLevel = LoggerLevel.DEBUG;
Expand Down
13 changes: 13 additions & 0 deletions src/service/command/execute-command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class ExecuteCommandService {
return result;
}

private nodeExecutionFailed(result: ExecuteNodeResult[]): boolean {
return !!result.find(res => res.executeCommandResults.find(r => r.result === ExecutionResult.NOT_OK));
}

private async executeNodeChainSequential(chain: NodeExecution[], printResults?: (node: ExecuteNodeResult[]) => void) {
const result: ExecuteNodeResult[][] = [];
for (const node of chain) {
Expand All @@ -93,6 +97,11 @@ export class ExecuteCommandService {
}

this.logger.endGroup();

if (!this._configurationService.failAtEnd() && this.nodeExecutionFailed(currentNodeResult)) {
this.logger.info(`${node.node.project} failed. Won't execute remaining projects`);
return result;
}
}
return result;
}
Expand Down Expand Up @@ -158,6 +167,10 @@ export class ExecuteCommandService {
printResults(currentResults[project]);
}
this.logger.endGroup();
if (!this._configurationService.failAtEnd() && this.nodeExecutionFailed(currentResults[project])) {
this.logger.info(`${project} failed. Won't execute remaining projects`);
return result;
}
}
}
return result;
Expand Down
4 changes: 4 additions & 0 deletions src/service/config/configuration-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,8 @@ export class ConfigurationService {
isToolsCommand(): boolean {
return this.configuration.parsedInputs.CLICommand === CLIActionType.TOOLS;
}

failAtEnd(): boolean {
return this.configuration.parsedInputs.failAtEnd ?? false;
}
}
34 changes: 34 additions & 0 deletions test/unitary/service/command/execute-command-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,38 @@ describe("executeNodeChain", () => {
// last call to execSpy must be for the last node
expect(execSpy.mock.calls[3][0]).toStrictEqual(nodeChain[3]);
});

test.each([
["sequential: fail fast", false, false, 1],
["sequential: fail at end", false, true, 2],
["parallel: fail fast", true, true, 2],
["parallel: fail at end", true, true, 2],
])("%p", async (_title, isParallel, failAtEnd, numExecCalls) => {
jest.spyOn(ConfigurationService.prototype, "isParallelExecutionEnabled").mockReturnValueOnce(isParallel);
jest.spyOn(ConfigurationService.prototype, "failAtEnd").mockReturnValueOnce(failAtEnd);
const execSpy = jest.spyOn(executeCommandService, "executeNodeCommands");
execSpy.mockResolvedValueOnce([{executeCommandResults: [{result: ExecutionResult.NOT_OK}]} as unknown as ExecuteNodeResult]);
execSpy.mockResolvedValueOnce([{executeCommandResults: [{result: ExecutionResult.OK}]} as unknown as ExecuteNodeResult]);

const nodeChain: NodeExecution[] = [
{
node: {
...defaultNodeValue,
project: "project1",
depth: 0
}
},
{
node: {
...defaultNodeValue,
project: "project2",
depth: 1
}
}
];

await executeCommandService.executeNodeChain(nodeChain, undefined);
expect(execSpy).toHaveBeenCalledTimes(numExecCalls);

});
});

0 comments on commit cccbb74

Please sign in to comment.