Skip to content

Commit

Permalink
Merge pull request #566 from launchableinc/collect-git-branch-name
Browse files Browse the repository at this point in the history
collect branch name
  • Loading branch information
Konboi authored Jun 29, 2023
2 parents edd16d1 + 52720b2 commit d18f0f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
20 changes: 17 additions & 3 deletions launchable/commands/record/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,25 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
fg='yellow'), err=True)

sources = []
branch_name_map = {}
if detect_sources:
try:
for name, repo_dist in repos:
for repo_name, repo_dist in repos:
hash = subprocess.check_output("git rev-parse HEAD".split(), cwd=repo_dist).decode().replace("\n", "")
sources.append((repo_name, repo_dist, hash))

branch_name = None
try:
# TODO: Sometimes cannot get an actual branch name on GitHub Actions. Need to improve this method
branch_name = subprocess.check_output(
"git rev-parse --abbrev-ref HEAD".split(),
cwd=repo_dist).decode().replace(
"\n", "")
except Exception:
branch_name = ""

branch_name_map[repo_name] = branch_name

sources.append((name, repo_dist, hash))
except Exception as e:
click.echo(
click.style(
Expand Down Expand Up @@ -174,7 +187,8 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days:
try:
commitHashes = [{
'repositoryName': name,
'commitHash': commit_hash
'commitHash': commit_hash,
'branchName': branch_name_map.get(name, "")
} for name, _, commit_hash in uniq_submodules]

if not (commitHashes[0]['repositoryName'] and commitHashes[0]['commitHash']):
Expand Down
22 changes: 14 additions & 8 deletions tests/commands/record/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def test_submodule(self, mock_check_output):
mock_check_output.side_effect = [
# the first call is git rev-parse HEAD
('c50f5de0f06fe16afa4fd1dd615e4903e40b42a2').encode(),
# the second call is git submodule status --recursive
# the second call is git rev-parse --abbrev-ref HEAD
('main').encode(),
# the third call is git submodule status --recursive
(
' 491e03096e2234dab9a9533da714fb6eff5dcaa7 foo (v1.51.0-560-g491e030)\n'
' 8bccab48338219e73c3118ad71c8c98fbd32a4be bar-zot (v1.32.0-516-g8bccab4)\n'
).encode()
).encode(),
]

self.assertEqual(read_build(), None)

result = self.cli("record", "build", "--no-commit-collection", "--name", self.build_name)
self.assertEqual(result.exit_code, 0)

Expand All @@ -42,15 +43,18 @@ def test_submodule(self, mock_check_output):
"commitHashes": [
{
"repositoryName": ".",
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2"
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2",
"branchName": "main"
},
{
"repositoryName": "./foo",
"commitHash": "491e03096e2234dab9a9533da714fb6eff5dcaa7"
"commitHash": "491e03096e2234dab9a9533da714fb6eff5dcaa7",
"branchName": ""
},
{
"repositoryName": "./bar-zot",
"commitHash": "8bccab48338219e73c3118ad71c8c98fbd32a4be"
"commitHash": "8bccab48338219e73c3118ad71c8c98fbd32a4be",
"branchName": ""
},
],
"links": []
Expand Down Expand Up @@ -82,7 +86,8 @@ def test_no_submodule(self, mock_check_output):
"commitHashes": [
{
"repositoryName": ".",
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2"
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2",
"branchName": ""
},
],
"links": []
Expand Down Expand Up @@ -111,7 +116,8 @@ def test_no_git_directory(self):
"commitHashes": [
{
"repositoryName": ".",
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2"
"commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2",
"branchName": "",
},
],
"links": []
Expand Down

0 comments on commit d18f0f5

Please sign in to comment.