From 5221de2e120fafceb88e86ad9f86f61f42d0fdf0 Mon Sep 17 00:00:00 2001 From: Konboi Date: Tue, 27 Jun 2023 15:52:44 +0900 Subject: [PATCH 1/4] collect branch name --- launchable/commands/record/build.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/launchable/commands/record/build.py b/launchable/commands/record/build.py index 2f7c3874c..f3d341e6f 100644 --- a/launchable/commands/record/build.py +++ b/launchable/commands/record/build.py @@ -115,12 +115,24 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days: fg='yellow'), err=True) sources = [] + branch_map = {} if detect_sources: try: for name, repo_dist in repos: hash = subprocess.check_output("git rev-parse HEAD".split(), cwd=repo_dist).decode().replace("\n", "") - sources.append((name, repo_dist, hash)) + + branch = None + try: + branch = subprocess.check_output( + "git rev-parse --abbrev-ref HEAD".split(), + cwd=repo_dist).decode().replace( + "\n", "") + except Exception: + branch = "" + + branch_map[name] = branch + except Exception as e: click.echo( click.style( @@ -174,7 +186,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_map.get(name, "") } for name, _, commit_hash in uniq_submodules] if not (commitHashes[0]['repositoryName'] and commitHashes[0]['commitHash']): From f6aa28314c52c2fae3dae2805bcc3eadb81539e4 Mon Sep 17 00:00:00 2001 From: Konboi Date: Tue, 27 Jun 2023 16:12:30 +0900 Subject: [PATCH 2/4] fix test --- tests/commands/record/test_build.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/commands/record/test_build.py b/tests/commands/record/test_build.py index 25294f872..bfebd4780 100644 --- a/tests/commands/record/test_build.py +++ b/tests/commands/record/test_build.py @@ -82,7 +82,8 @@ def test_no_submodule(self, mock_check_output): "commitHashes": [ { "repositoryName": ".", - "commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2" + "commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2", + "branchName": "" }, ], "links": [] @@ -111,7 +112,8 @@ def test_no_git_directory(self): "commitHashes": [ { "repositoryName": ".", - "commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2" + "commitHash": "c50f5de0f06fe16afa4fd1dd615e4903e40b42a2", + "branchName": "", }, ], "links": [] From d0154c3cc6b73450d03c3245f263d65ef3727da9 Mon Sep 17 00:00:00 2001 From: Konboi Date: Tue, 27 Jun 2023 16:35:42 +0900 Subject: [PATCH 3/4] fix test --- tests/commands/record/test_build.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/commands/record/test_build.py b/tests/commands/record/test_build.py index bfebd4780..8295e0b5f 100644 --- a/tests/commands/record/test_build.py +++ b/tests/commands/record/test_build.py @@ -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) @@ -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": [] From 52720b2bb7534ae7f2ce16eab8730651b67995b9 Mon Sep 17 00:00:00 2001 From: Konboi Date: Wed, 28 Jun 2023 10:37:39 +0900 Subject: [PATCH 4/4] fix variable names --- launchable/commands/record/build.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/launchable/commands/record/build.py b/launchable/commands/record/build.py index f3d341e6f..12d8ed3f2 100644 --- a/launchable/commands/record/build.py +++ b/launchable/commands/record/build.py @@ -115,23 +115,24 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days: fg='yellow'), err=True) sources = [] - branch_map = {} + 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((name, repo_dist, hash)) + sources.append((repo_name, repo_dist, hash)) - branch = None + branch_name = None try: - branch = subprocess.check_output( + # 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 = "" + branch_name = "" - branch_map[name] = branch + branch_name_map[repo_name] = branch_name except Exception as e: click.echo( @@ -187,7 +188,7 @@ def build(ctx: click.core.Context, build_name: str, source: List[str], max_days: commitHashes = [{ 'repositoryName': name, 'commitHash': commit_hash, - 'branchName': branch_map.get(name, "") + 'branchName': branch_name_map.get(name, "") } for name, _, commit_hash in uniq_submodules] if not (commitHashes[0]['repositoryName'] and commitHashes[0]['commitHash']):