Skip to content

Commit

Permalink
Project triggering the job (#388)
Browse files Browse the repository at this point in the history
* added project triggering job to produce the mapping

* made a distinction between project triggering the job and starting project

* bumped version
  • Loading branch information
shubhbapna authored Feb 14, 2023
1 parent fed3594 commit 1386d0e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ See [action.yml](action.yml)
- branch: executes the branch flow
- single_pr: executes the single pull request flow
- **starting-project** (optional. the project triggering the job by default): The project you want start building from. It's not the same as the project triggering the job (which will remain the same), but the project to take tree from. For instance
- **starting-project** (optional. the project triggering the job by default): The project you want start building from. The project to construct the tree from. It's not the same as the project triggering the job which is taken from `GITHUB_REPOSITORY` env variable or the github pull request event payload. For instance
> ```
> starting-project: 'groupX/projectX' // it will get project dependencies tree from 'groupX/projectX'
Expand Down Expand Up @@ -494,7 +494,7 @@ Let's suppose
#### mapping.dependencies
It is used to define branch mapping between E and its dependencies in case `E` is `startingProject`/`projectTriggeringTheJob`.
It is used to define branch mapping between E and its dependencies in case `E` is `projectTriggeringTheJob`.
In case the `E:7.x` branch build or PR is triggered for this `7.x` target branch:
Expand All @@ -518,7 +518,7 @@ In case the `E:anyotherbranch` branch build or PR is triggered for this `anyothe
#### mapping.dependant
It is used to define branch mapping between the rest of the projects and project A in case `E` is NOT `startingProject`/`projectTriggeringTheJob`.
It is used to define branch mapping between the rest of the projects and project A in case `E` is NOT `projectTriggeringTheJob`.
In case the `A:7.x` or any other (except `main`) branch build or PR is triggered -> `E:7.x` will be taken (since there's not `mapping.dependant` for `7.x` source)
In case the `A:main` branch build or PR is triggered -> `E:7.x` (due to `mapping.dependant.default` mapping)
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.0.15",
"version": "3.0.16",
"description": "Library to execute commands based on github projects dependencies.",
"main": "dist/index.js",
"author": "",
Expand Down
10 changes: 8 additions & 2 deletions src/service/checkout/checkout-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,19 @@ export class CheckoutService {
*/
private async getCheckoutInfo(node: Node): Promise<CheckoutInfo> {
const githubAPIService = Container.get(GithubAPIService);
const starterNode = this.config.getStarterNode();
const projectTriggeringTheJob = this.config.getProjectTriggeringTheJob();
const originalTarget = this.config.getTargetProject();
// the current node is the current target
const currentTarget = {
// map the starting project target branch to the corresponding branch defined in the mapping for the current node
// target branch is guaranteed to exist since base always exist
mappedBranch: getMappedTarget(starterNode.project, starterNode.mapping, node.project, node.mapping, originalTarget.branch!),
mappedBranch: getMappedTarget(
projectTriggeringTheJob.project,
projectTriggeringTheJob.mapping,
node.project,
node.mapping,
originalTarget.branch!
),
name: node.project.split("/")[1],
group: this.config.getGroupName() ?? node.project.split("/")[0],
};
Expand Down
29 changes: 28 additions & 1 deletion src/service/config/configuration-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ConfigurationService {
}

/**
* Get the name of the project starting the build-chain
* Get the name of the start project which produces node chain for build-chain
* @returns {string}
*/
getStarterProjectName(): string {
Expand All @@ -66,6 +66,16 @@ export class ConfigurationService {
return startProject;
}

/**
* Get the name of the project triggering build-chain
* @returns {string}
*/
getProjectTriggeringTheJobName(): string {
return process.env.GITHUB_REPOSITORY ??
this.configuration.gitEventData.base.repo.full_name ??
this.configuration.parsedInputs.startProject;
}

/**
* Check whether the given node is the starter project
* @param node
Expand All @@ -75,6 +85,15 @@ export class ConfigurationService {
return node.project === this.getStarterProjectName();
}

/**
* Check whether the given node is the triggering node
* @param node
* @returns {Boolean} true if the node is the triggering node
*/
isNodeTriggeringTheJob(node: Node): boolean {
return node.project === this.getProjectTriggeringTheJobName();
}

/**
* Finds the starter node
* @returns {Node} starter node
Expand All @@ -89,6 +108,14 @@ export class ConfigurationService {
return starterNode;
}

/**
* Finds the node triggering the job
* @returns {Node} starter node
*/
getProjectTriggeringTheJob(): Node {
return this.nodeChain.find(node => this.isNodeTriggeringTheJob(node)) ?? this.getStarterNode();
}

/**
* Gets the execution level (current, upstream or downstream) for the given node
* @param node
Expand Down
6 changes: 3 additions & 3 deletions test/unitary/service/checkout/checkout-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe.each([
jest.spyOn(ConfigurationService.prototype, "getRootFolder").mockImplementation(() => rootFolder);
jest.spyOn(ConfigurationService.prototype, "getSourceProject").mockImplementation(() => originalSource);
jest.spyOn(ConfigurationService.prototype, "getTargetProject").mockImplementation(() => originalTarget);
jest.spyOn(ConfigurationService.prototype, "getStarterNode").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "getProjectTriggeringTheJob").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "skipParallelCheckout").mockImplementation(() => skipParallelCheckout);
});

Expand Down Expand Up @@ -291,7 +291,7 @@ describe.each([
jest.spyOn(ConfigurationService.prototype, "getRootFolder").mockImplementation(() => rootFolder);
jest.spyOn(ConfigurationService.prototype, "getSourceProject").mockImplementation(() => originalSource);
jest.spyOn(ConfigurationService.prototype, "getTargetProject").mockImplementation(() => originalTarget);
jest.spyOn(ConfigurationService.prototype, "getStarterNode").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "getProjectTriggeringTheJob").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "skipParallelCheckout").mockImplementation(() => skipParallelCheckout);
});

Expand Down Expand Up @@ -507,7 +507,7 @@ describe.each([
jest.spyOn(ConfigurationService.prototype, "getRootFolder").mockImplementation(() => rootFolder);
jest.spyOn(ConfigurationService.prototype, "getSourceProject").mockImplementation(() => originalSource);
jest.spyOn(ConfigurationService.prototype, "getTargetProject").mockImplementation(() => originalTarget);
jest.spyOn(ConfigurationService.prototype, "getStarterNode").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "getProjectTriggeringTheJob").mockImplementation(() => nodeChain[1]);
jest.spyOn(ConfigurationService.prototype, "skipParallelCheckout").mockImplementation(() => skipParallelCheckout);
});

Expand Down
35 changes: 35 additions & 0 deletions test/unitary/service/config/configuration-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("methods", () => {
let config: ConfigurationService;
let currentInput: InputValues;
const startProject = "owner/project";
const projectTriggeringTheJob = "owner/job";

beforeAll(() => {
// doesn't matter whether it is a CLI or action
Expand Down Expand Up @@ -377,4 +378,38 @@ describe("methods", () => {
};
expect(config.getGroupName()).toBe(group);
});

test.each([
["github env is defined", projectTriggeringTheJob, undefined, projectTriggeringTheJob],
["github event is defined", undefined, projectTriggeringTheJob, projectTriggeringTheJob],
["neither is defined use startProject", undefined, undefined, startProject],
])("getProjectTriggeringTheJobName: %p", (_title, githubEnv, githubEvent, expected) => {
if (githubEnv) {
process.env["GITHUB_REPOSITORY"] = githubEnv;
}
jest
.spyOn(BaseConfiguration.prototype, "gitEventData", "get")
.mockImplementationOnce(() => ({ base: {repo: {full_name: githubEvent}} }) as never);
expect(config.getProjectTriggeringTheJobName()).toBe(expected);
delete process.env["GITHUB_REPOSITORY"];
});

test.each([
["projectTriggeringTheJob is found", projectTriggeringTheJob, projectTriggeringTheJob],
["projectTriggeringTheJob is not found", undefined, startProject],
])("getProjectTriggeringTheJob: %p", (_title, getProjectTriggeringTheJobName, expectedProject) => {
const chain: Node[] = [
{ ...defaultNodeValue, project: "abc" },
{ ...defaultNodeValue, project: startProject },
{ ...defaultNodeValue, project: projectTriggeringTheJob },
];
const nodeFound: Node = { ...defaultNodeValue, project: expectedProject };
jest
.spyOn(ConfigurationService.prototype, "nodeChain", "get")
.mockImplementation(() => chain);
jest
.spyOn(ConfigurationService.prototype, "getProjectTriggeringTheJobName")
.mockImplementation(() => getProjectTriggeringTheJobName!);
expect(config.getProjectTriggeringTheJob()).toStrictEqual(nodeFound);
});
});

0 comments on commit 1386d0e

Please sign in to comment.