-
Notifications
You must be signed in to change notification settings - Fork 24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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", | ||
"description": "Library to execute commands based on github projects dependencies.", | ||
"main": "dist/build-chain-cli.js", | ||
"author": "Enrique Mingorance Cano <[email protected]>", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
@@ -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}'` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
try { | ||
await clone( | ||
|
@@ -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}`, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
@@ -363,6 +373,7 @@ module.exports = { | |
merge, | ||
head, | ||
sha, | ||
remoteSha, | ||
rename, | ||
rebase, | ||
push, | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is actually a very good question. This is executing the |
||
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 }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is!