Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit 4e6f35f

Browse files
jmflorezffJuan Manuel Floreznihathrael
authored
CR#31629 Teamscale CLI precommit and listing findings not working in Gerrit setup (#29)
* CR#31629 Resolve current revision to Teamscale ID before retrieving all findings Co-authored-by: Juan Manuel Florez <florez@cqse.eu> Co-authored-by: Thomas Kinnen <kinnen@cqse.eu>
1 parent 10fa7c9 commit 4e6f35f

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

.github/workflows/actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Create binary
2626
run: |
2727
pip install .
28-
python -m nuitka --assume-yes-for-downloads --standalone --onefile --linux-onefile-icon /usr/share/pixmaps/python3.xpm teamscale_precommit_client/precommit_client.py
28+
python -m nuitka --assume-yes-for-downloads --standalone --onefile --linux-onefile-icon=/usr/share/pixmaps/python3.xpm teamscale_precommit_client/precommit_client.py
2929
mv precommit_client.bin teamscale-cli
3030
- name: 'Upload Artifact'
3131
if: ${{ always() }}

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="teamscale-cli",
5-
version="7.1.0",
5+
version="7.1.1",
66
author="Thomas Kinnen - CQSE GmbH",
77
author_email="kinnen@cqse.eu",
88
description=("Client for performing precommit analysis with Teamscale."),
@@ -20,7 +20,7 @@
2020
]
2121
},
2222
install_requires=[
23-
'teamscale-client==6.7.1',
23+
'teamscale-client==7.1.1',
2424
'gitpython==2.1.15',
2525

2626
# Required for gitpython, build fails without specifying a fixed version.
@@ -29,7 +29,7 @@
2929
'gitdb2==2.0.6',
3030

3131
# Required to compile to a native binary
32-
'nuitka==0.6.14.7'
32+
'nuitka==1.1.5'
3333
],
3434
tests_require=[
3535
'teamscale-client',

teamscale_precommit_client/git_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ def get_current_branch(path_to_repository):
2626
return repo.active_branch.name
2727

2828

29+
def get_current_commit_sha(path_to_repository):
30+
"""Get the commit SHA (ID) of the current commit
31+
32+
Args:
33+
path_to_repository (str): Path to the Git repository
34+
35+
Returns:
36+
str: SHA of current commit.
37+
"""
38+
return Repo(path_to_repository).active_branch.commit.hexsha
39+
40+
2941
def get_repo_root_from_file_in_repo(path_to_file_in_repo):
3042
"""Get the repository root for the given path in the repository."""
3143
try:

teamscale_precommit_client/precommit_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from teamscale_precommit_client.client_configuration_utils import get_teamscale_client_configuration
1515
from teamscale_precommit_client.data import PreCommitUploadData
1616
from teamscale_precommit_client.git_utils import get_changed_files_and_content, get_deleted_files
17-
from teamscale_precommit_client.git_utils import get_current_branch, get_current_timestamp
17+
from teamscale_precommit_client.git_utils import get_current_branch, get_current_timestamp, get_current_commit_sha
1818
from teamscale_precommit_client.git_utils import get_repo_root_from_file_in_repo
1919

2020
# Filename of the precommit configuration. The client expects this config file at the root of the repository.
@@ -205,7 +205,8 @@ def _get_existing_findings(self):
205205
uniform_path = os.path.join(self.path_prefix, uniform_path)
206206
if self.fetch_all_findings:
207207
uniform_path = ''
208-
self.existing_findings = self.teamscale_client.get_findings(uniform_path=uniform_path, timestamp=None)
208+
self.existing_findings = self.teamscale_client.get_findings(uniform_path=uniform_path, timestamp=None,
209+
revision_id=self._get_commit_hash())
209210
self._remove_precommit_findings_from_existing_findings()
210211

211212
def _remove_precommit_findings_from_existing_findings(self):
@@ -264,6 +265,10 @@ def _get_finding_severity_message(finding):
264265
"""Formats the given finding's assessment as severity."""
265266
return 'error' if finding.assessment == 'RED' else 'warning'
266267

268+
def _get_commit_hash(self):
269+
"""Obtains the current commit SHA"""
270+
return get_current_commit_sha(self.repository_path)
271+
267272

268273
def _parse_args():
269274
"""Parses the precommit client command line arguments."""

tests/precommit_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ANALYZED_FILE_PATH = REPO_PATH + ANALYZED_FILE_NAME
3030
DELETED_FILE_NAME = 'deletedFile.ext'
3131
CURRENT_BRANCH = 'my_feature_branch'
32+
REVISION = 'mockRevision'
3233

3334

3435
class PrecommitClientTest(TestCase):
@@ -271,6 +272,10 @@ def mock_existing_findings(branch, existing_findings=None, path_prefix=DEFAULT_P
271272
Findings can be provided as list of integers each of which represents a finding instance."""
272273
existing_findings_from_current_branch = to_json(
273274
PrecommitClientTest._get_findings_as_dicts(existing_findings, path_prefix))
275+
responses.add(responses.GET,
276+
PrecommitClientTest.get_project_service_mock('repository-timestamp-by-revision', REVISION),
277+
body=to_json([{'branchName': branch, 'timestamp': 'HEAD'}]), status=200,
278+
content_type='application/json')
274279
responses.add(responses.GET, PrecommitClientTest.get_project_service_mock('findings', branch),
275280
body=existing_findings_from_current_branch, status=200,
276281
content_type="application/json", )
@@ -307,6 +312,7 @@ def _get_precommit_client(changed_files, deleted_files, path_prefix=DEFAULT_PATH
307312
precommit_client.changed_files = changed_files
308313
precommit_client.deleted_files = deleted_files
309314
PrecommitClient.PRECOMMIT_WAITING_TIME_IN_SECONDS = 0
315+
precommit_client._get_commit_hash = Mock(return_value=REVISION)
310316

311317
return precommit_client
312318

0 commit comments

Comments
 (0)