Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE 199] Log commit hash while describing checkout/merge #202

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

# V2.6.2

## Enhancements:

- Prints out the hashes during check out/merging process

# V2.6.1

## Enhancements:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

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": "2.3.25",
"version": "2.3.26",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the changelog mentions 2.6.2 while here is 2.3.26? Is it because the disalignment we discussed between branch/tag and actual version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is!

"description": "Library to execute commands based on github projects dependencies.",
"main": "dist/build-chain-cli.js",
"author": "Enrique Mingorance Cano <[email protected]>",
Expand Down
15 changes: 13 additions & 2 deletions src/lib/flows/common/build-chain-flow-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
getForkedProject,
getRepository
} = require("../../git");
const { getRemoteSha } = require("../../service/git-service");
const { logger } = require("../../common");
const { treatUrl } = require("@kie/build-chain-configuration-reader");
const { checkUrlExist } = require("../../util/http");
Expand Down Expand Up @@ -205,9 +206,19 @@ async function checkoutProjectPullRequestFlow(
* @param {Object} dir the dir to check out
*/
async function checkoutNode(context, node, checkoutInfo, dir) {
const sourceHash = await getRemoteSha(
`${context.config.github.serverUrl}/${checkoutInfo.group}/${checkoutInfo.project}`,
checkoutInfo.branch
);

if (checkoutInfo.merge) {
const targetHash = await getRemoteSha(
`${context.config.github.serverUrl}/${node.project}`,
checkoutInfo.targetBranch
);

logger.info(
`[${node.project}] Merging ${context.config.github.serverUrl}/${node.project}:${checkoutInfo.targetBranch} into ${context.config.github.serverUrl}/${checkoutInfo.group}/${checkoutInfo.project}:${checkoutInfo.branch}`
`[${node.project}] Checking out ${context.config.github.serverUrl}/${node.project}:${checkoutInfo.targetBranch} ${targetHash} and merging ${context.config.github.serverUrl}/${checkoutInfo.group}/${checkoutInfo.project}:${checkoutInfo.branch} ${sourceHash} into '${dir}'`
Copy link
Contributor Author

@Ginxo Ginxo Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielezonca if you don't want to check the whole thing just check the log information is the one expected. Be aware getRemoteSha prefixes the hash with a @ Thanks

);
try {
await clone(
Expand Down Expand Up @@ -244,7 +255,7 @@ async function checkoutNode(context, node, checkoutInfo, dir) {
} else {
try {
logger.info(
`[${node.project}] Checking out '${context.config.github.serverUrl}/${checkoutInfo.group}/${checkoutInfo.project}:${checkoutInfo.branch}' into '${dir}'`
`[${node.project}] Checking out '${context.config.github.serverUrl}/${checkoutInfo.group}/${checkoutInfo.project}:${checkoutInfo.branch}' ${sourceHash} into '${dir}'`
);
await clone(
`${context.config.github.serverUrlWithToken}/${checkoutInfo.group}/${checkoutInfo.project}`,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ async function sha(dir, branch) {
return await git(dir, "show-ref", "-s", `refs/remotes/origin/${branch}`);
}

/**
* It retrieves the hash from a remote repository and branch
*
* @param {string} repositoryUrl the repository URL
* @param {string} branch the branch to get the hash from
*/
async function remoteSha(repositoryUrl, branch) {
return await git(".", "ls-remote", repositoryUrl, branch);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@almope do you think there is a better way to get hash from a remote repository? 🤔 thanks!

}

async function rename(dir, branch) {
return await git(dir, "branch", "--move", branch);
}
Expand Down Expand Up @@ -363,6 +373,7 @@ module.exports = {
merge,
head,
sha,
remoteSha,
rename,
rebase,
push,
Expand Down
24 changes: 24 additions & 0 deletions src/lib/service/git-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const git = require("../git");
const { logger } = require("../common");

/**
* It treats a git hash from a remote repositry.
*
* @param {string} repositoryUrl the repository URL
* @param {string} branch the branch to get the hash from
* @param {number} hashLength the length of hash to return back
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to get the full hash without knowing the length? I quickly checked git documentation and it seems there are different ways to configure git hashing so I assume this can lead to different hash size
https://git-scm.com/docs/hash-function-transition/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me check the documentation

*/
async function getRemoteSha(repositoryUrl, branch, hashLength = 7) {
try {
const remoteHashValue = await git.remoteSha(repositoryUrl, branch);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with this code so I don't know if the question makes sense but is this invoking normal git CLI command or is it a GitHub remote API call?
I remember in the past we had failures related to the number of API call so I want to double check if this is increasing the number of calls we do or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is actually a very good question. This is executing the remoteSha method from git.js file which basically at the end executes the git CLI tool from the filesystem.

logger.debug(`Hash for ${repositoryUrl}:${branch} '${remoteHashValue}'`);
return `@${remoteHashValue.substring(0, hashLength)}`;
} catch (e) {
logger.warn(
`Error getting sha for ${repositoryUrl}:${branch}. It is only for logging purposes, no worries.`
);
return undefined;
}
}

module.exports = { getRemoteSha };
33 changes: 33 additions & 0 deletions test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,36 @@ describe("version", () => {
]);
});
});

describe("remoteSha", () => {
test("ok", async () => {
// Arrange
const repositoryUrl = "https://repositoryUrl";
const branch = "branchName";
spawnMock.sequence.add(
spawnMock.simple(
0,
"9a7f82f4b090a37a855aa582d2160951853a9141 refs/heads/main"
)
);
// Act
const result = await git.remoteSha(repositoryUrl, branch);

// Assert
expect(spawnMock.calls.length).toBe(1);
const firstCall = spawnMock.calls[0];
expect(firstCall.command).toBe("git");
expect(firstCall.args).toStrictEqual([
"-c",
"user.name=GitHub",
"-c",
"[email protected]",
"ls-remote",
repositoryUrl,
branch
]);
expect(result).toBe(
"9a7f82f4b090a37a855aa582d2160951853a9141 refs/heads/main"
);
});
});
35 changes: 35 additions & 0 deletions test/lib/flows/common/build-chain-flow-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const { checkUrlExist } = require("../../../../src/lib/util/http");
jest.mock("../../../../src/lib/util/http");
jest.mock("../../../../src/lib/common");

const { getRemoteSha } = require("../../../../src/lib/service/git-service");
jest.mock("../../../../src/lib/service/git-service");

afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -434,6 +437,24 @@ describe("checkoutDefinitionTree", () => {
// Act
const result = await checkoutDefinitionTree(context, nodeChain);
// Assert
expect(getRemoteSha).toHaveBeenCalledTimes(4);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/sourceGroup/lienzo-core-forked",
"sBranch"
);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/kiegroup/lienzo-core",
"tBranch"
);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/sourceGroup/droolsjbpm-build-bootstrap-forked",
"sBranch"
);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/kiegroup/droolsjbpm-build-bootstrap",
"tBranch"
);

expect(mergeMock).toHaveBeenCalledTimes(2);
expect(mergeMock).toHaveBeenCalledWith(
"folder/kiegroup_lienzo_core",
Expand Down Expand Up @@ -543,6 +564,20 @@ describe("checkoutDefinitionTree", () => {
const result = await checkoutDefinitionTree(context, nodeChain);

// Assert
expect(getRemoteSha).toHaveBeenCalledTimes(3);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/sourceGroup/lienzo-core-forked",
"sBranch"
);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/kiegroup/lienzo-core",
"tBranch"
);
expect(getRemoteSha).toHaveBeenCalledWith(
"URL/kiegroup/droolsjbpm-build-bootstrap",
"tBranch"
);

expect(mergeMock).toHaveBeenCalledTimes(1);
expect(mergeMock).toHaveBeenCalledWith(
"folder/kiegroup_lienzo_core",
Expand Down
59 changes: 59 additions & 0 deletions test/lib/service/git-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { getRemoteSha } = require("../../../src/lib/service/git-service");
const { remoteSha } = require("../../../src/lib/git");
jest.mock("../../../src/lib/git");
jest.mock("../../../src/lib/common");

afterEach(() => {
jest.clearAllMocks();
});

describe("getRemoteSha", () => {
test("ok", async () => {
// Arrange
const repositoryUrl = "https://repositoryUrl";
const branch = "branchName";
remoteSha.mockResolvedValueOnce(
"9a7f82f4b090a37a855aa582d2160951853a9141 refs/heads/main"
);

// Act
const result = await getRemoteSha(repositoryUrl, branch);

// Assert
expect(remoteSha).toHaveBeenCalledTimes(1);
expect(remoteSha).toHaveBeenCalledWith(repositoryUrl, branch);
expect(result).toBe("@9a7f82f");
});

test("ok different length", async () => {
// Arrange
const repositoryUrl = "https://repositoryUrlX";
const branch = "branchNameX";
remoteSha.mockResolvedValueOnce(
"9a7f82f4b090a37a855aa582d2160951853a9141 refs/heads/main"
);

// Act
const result = await getRemoteSha(repositoryUrl, branch, 10);

// Assert
expect(remoteSha).toHaveBeenCalledTimes(1);
expect(remoteSha).toHaveBeenCalledWith(repositoryUrl, branch);
expect(result).toBe("@9a7f82f4b0");
});

test("not existing", async () => {
// Arrange
const repositoryUrl = "https://repositoryUrlY";
const branch = "branchNameY";
remoteSha.mockResolvedValueOnce(undefined);

// Act
const result = await getRemoteSha(repositoryUrl, branch);

// Assert
expect(remoteSha).toHaveBeenCalledTimes(1);
expect(remoteSha).toHaveBeenCalledWith(repositoryUrl, branch);
expect(result).toBe(undefined);
});
});