Skip to content

Commit 54f9a80

Browse files
committed
Add fetch of previous commit
1 parent eca3b11 commit 54f9a80

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.1.30"
9+
version = "2.1.31"
1010
requires-python = ">= 3.10"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.1.30'
2+
__version__ = '2.1.31'

socketsecurity/core/git_interface.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,49 @@ def __init__(self, path: str):
135135
github_base_ref = os.getenv('GITHUB_BASE_REF')
136136
github_head_ref = os.getenv('GITHUB_HEAD_REF')
137137
github_event_name = os.getenv('GITHUB_EVENT_NAME')
138+
github_before_sha = os.getenv('GITHUB_EVENT_BEFORE') # previous commit for push
139+
github_sha = os.getenv('GITHUB_SHA') # current commit
138140
if github_event_name == 'pull_request' and github_base_ref and github_head_ref:
139141
try:
140-
self.repo.git.fetch('origin', github_base_ref, github_head_ref)
142+
# Fetch both branches individually
143+
self.repo.git.fetch('origin', github_base_ref)
144+
self.repo.git.fetch('origin', github_head_ref)
145+
# Try remote diff first
141146
diff_range = f"origin/{github_base_ref}...origin/{github_head_ref}"
142-
diff_files = self.repo.git.diff('--name-only', diff_range)
147+
try:
148+
diff_files = self.repo.git.diff('--name-only', diff_range)
149+
self.show_files = diff_files.splitlines()
150+
log.debug(f"Changed files detected via git diff (GitHub PR remote): {self.show_files}")
151+
detected = True
152+
except Exception as remote_error:
153+
log.debug(f"Remote diff failed: {remote_error}")
154+
# Try local branch diff
155+
local_diff_range = f"{github_base_ref}...{github_head_ref}"
156+
try:
157+
diff_files = self.repo.git.diff('--name-only', local_diff_range)
158+
self.show_files = diff_files.splitlines()
159+
log.debug(f"Changed files detected via git diff (GitHub PR local): {self.show_files}")
160+
detected = True
161+
except Exception as local_error:
162+
log.debug(f"Local diff failed: {local_error}")
163+
except Exception as error:
164+
log.debug(f"Failed to fetch branches or diff for GitHub PR: {error}")
165+
# Commits to default branch (push events)
166+
elif github_event_name == 'push' and github_before_sha and github_sha:
167+
try:
168+
diff_files = self.repo.git.diff('--name-only', f'{github_before_sha}..{github_sha}')
143169
self.show_files = diff_files.splitlines()
144-
log.debug(f"Changed files detected via git diff (GitHub PR): {self.show_files}")
170+
log.debug(f"Changed files detected via git diff (GitHub push): {self.show_files}")
145171
detected = True
146172
except Exception as error:
147-
log.debug(f"Failed to get changed files via git diff (GitHub PR): {error}")
148-
# Commits to default branch (push events)
173+
log.debug(f"Failed to get changed files via git diff (GitHub push): {error}")
149174
elif github_event_name == 'push':
150175
try:
151176
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
152-
log.debug(f"Changed files detected via git show (GitHub push): {self.show_files}")
177+
log.debug(f"Changed files detected via git show (GitHub push fallback): {self.show_files}")
153178
detected = True
154179
except Exception as error:
155-
log.debug(f"Failed to get changed files via git show (GitHub push): {error}")
180+
log.debug(f"Failed to get changed files via git show (GitHub push fallback): {error}")
156181
# GitLab CI Merge Request context
157182
if not detected:
158183
gitlab_target = os.getenv('CI_MERGE_REQUEST_TARGET_BRANCH_NAME')

0 commit comments

Comments
 (0)