Skip to content

Commit f5da0a9

Browse files
committed
fix: robust changed file detection for PRs/MRs in GitHub, GitLab, and Bitbucket
- Use git diff with appropriate refs and environment variables to detect changed files in pull/merge requests across GitHub Actions, GitLab CI, and Bitbucket Pipelines. - Fallback to git show for single commit detection. - Ensures manifest and other file changes are correctly picked up in all major
1 parent 20110b3 commit f5da0a9

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
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.28"
9+
version = "2.1.29"
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.28'
2+
__version__ = '2.1.29'

socketsecurity/core/git_interface.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,56 @@ def __init__(self, path: str):
128128
self.commit_sha = self.commit.binsha
129129
self.commit_message = self.commit.message
130130
self.committer = self.commit.committer
131-
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
131+
# Detect changed files in PR/MR context for GitHub, GitLab, Bitbucket; fallback to git show
132+
self.show_files = []
133+
detected = False
134+
# GitHub Actions PR context
135+
github_base_ref = os.getenv('GITHUB_BASE_REF')
136+
github_head_ref = os.getenv('GITHUB_HEAD_REF')
137+
if github_base_ref and github_head_ref:
138+
try:
139+
self.repo.git.fetch('origin', github_base_ref, github_head_ref)
140+
diff_range = f"origin/{github_base_ref}...{self.commit.hexsha}"
141+
diff_files = self.repo.git.diff('--name-only', diff_range)
142+
self.show_files = diff_files.splitlines()
143+
log.debug(f"Changed files detected via git diff (GitHub): {self.show_files}")
144+
detected = True
145+
except Exception as error:
146+
log.debug(f"Failed to get changed files via git diff (GitHub): {error}")
147+
# GitLab CI Merge Request context
148+
if not detected:
149+
gitlab_target = os.getenv('CI_MERGE_REQUEST_TARGET_BRANCH_NAME')
150+
gitlab_source = os.getenv('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME')
151+
if gitlab_target and gitlab_source:
152+
try:
153+
self.repo.git.fetch('origin', gitlab_target, gitlab_source)
154+
diff_range = f"origin/{gitlab_target}...origin/{gitlab_source}"
155+
diff_files = self.repo.git.diff('--name-only', diff_range)
156+
self.show_files = diff_files.splitlines()
157+
log.debug(f"Changed files detected via git diff (GitLab): {self.show_files}")
158+
detected = True
159+
except Exception as error:
160+
log.debug(f"Failed to get changed files via git diff (GitLab): {error}")
161+
# Bitbucket Pipelines PR context
162+
if not detected:
163+
bitbucket_pr_id = os.getenv('BITBUCKET_PR_ID')
164+
bitbucket_source = os.getenv('BITBUCKET_BRANCH')
165+
bitbucket_dest = os.getenv('BITBUCKET_PR_DESTINATION_BRANCH')
166+
# BITBUCKET_BRANCH is the source branch in PR builds
167+
if bitbucket_pr_id and bitbucket_source and bitbucket_dest:
168+
try:
169+
self.repo.git.fetch('origin', bitbucket_dest, bitbucket_source)
170+
diff_range = f"origin/{bitbucket_dest}...origin/{bitbucket_source}"
171+
diff_files = self.repo.git.diff('--name-only', diff_range)
172+
self.show_files = diff_files.splitlines()
173+
log.debug(f"Changed files detected via git diff (Bitbucket): {self.show_files}")
174+
detected = True
175+
except Exception as error:
176+
log.debug(f"Failed to get changed files via git diff (Bitbucket): {error}")
177+
# Fallback to git show for single commit
178+
if not detected:
179+
self.show_files = self.repo.git.show(self.commit, name_only=True, format="%n").splitlines()
180+
log.debug(f"Changed files detected via git show: {self.show_files}")
132181
self.changed_files = []
133182
for item in self.show_files:
134183
if item != "":

0 commit comments

Comments
 (0)