Skip to content

Commit 86b8576

Browse files
Copilotzkoppert
andcommitted
Exclude contributors not found in org chart from InnerSource analysis
Co-authored-by: zkoppert <6935431+zkoppert@users.noreply.github.com>
1 parent 8df4fdb commit 86b8576

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

measure_innersource.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ def main(): # pragma: no cover
234234
logger.info("Analyzing all contributors in the repository...")
235235
for contributor in repo_data.contributors():
236236
all_contributors.append(contributor.login)
237+
238+
# Check if contributor is not found in org chart
239+
if contributor.login not in org_data:
240+
logger.warning(
241+
"Contributor '%s' not found in org chart. "
242+
"Excluding from InnerSource analysis.",
243+
contributor.login
244+
)
245+
continue
246+
237247
if (
238248
contributor.login not in team_members_that_own_the_repo
239249
and "[bot]" not in contributor.login

test_measure_innersource.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,84 @@ def test_main_missing_user_in_org_chart(tmp_path, monkeypatch, caplog):
154154

155155
# Should NOT contain the log message about
156156
# "Original commit author: X, with manager: Y"
157-
assert not any(isinstance(msg, str) and "Original commit author:"
158-
in msg and "with manager:" in msg for msg in
157+
assert not any(isinstance(msg, str) and "Original commit author:"
158+
in msg and "with manager:" in msg for msg in
159159
info_calls)
160+
161+
162+
def test_contributors_missing_from_org_chart_excluded(tmp_path, monkeypatch):
163+
"""Test that contributors missing from org chart are excluded from
164+
InnerSource analysis."""
165+
# Switch working directory to tmp_path
166+
monkeypatch.chdir(tmp_path)
167+
168+
# Create org-data.json with some users
169+
org_data = {
170+
"original_author": {"manager": "manager1"},
171+
"manager1": {"manager": "director1"},
172+
"user1": {"manager": "manager1"}
173+
}
174+
175+
org_file = tmp_path / "org-data.json"
176+
org_file.write_text(json.dumps(org_data), encoding="utf-8")
177+
178+
# Mock GitHub repository and commit data
179+
mock_original_author = MagicMock()
180+
mock_original_author.login = "original_author"
181+
182+
mock_commit = MagicMock()
183+
mock_commit.author = mock_original_author
184+
185+
# Mock contributors - include one that's not in org_data
186+
mock_contributor1 = MagicMock()
187+
mock_contributor1.login = "unknown_contributor" # Not in org_data
188+
189+
mock_repo = MagicMock()
190+
mock_repo.full_name = "test/repo"
191+
mock_repo.commits.return_value = [mock_commit]
192+
mock_repo.contributors.return_value = [mock_contributor1]
193+
# Mock empty pull requests and issues to avoid infinite loops
194+
mock_repo.pull_requests.return_value = iter([])
195+
mock_repo.issues.return_value = iter([])
196+
197+
mock_github = MagicMock()
198+
mock_github.repository.return_value = mock_repo
199+
200+
# Mock environment variables
201+
mock_env_vars = MagicMock()
202+
mock_env_vars.github_token = "fake_token"
203+
mock_env_vars.github_enterprise_hostname = None
204+
mock_env_vars.github_org = "test"
205+
mock_env_vars.github_repo = "repo"
206+
mock_env_vars.gh_app_id = None
207+
mock_env_vars.gh_app_installation_id = None
208+
mock_env_vars.gh_app_private_key_bytes = None
209+
mock_env_vars.gh_app_enterprise_only = False
210+
mock_env_vars.report_title = "Test Report"
211+
mock_env_vars.output_file = "test_output.md"
212+
mock_env_vars.chunk_size = 100
213+
214+
# Apply mocks
215+
with patch('measure_innersource.get_env_vars',
216+
return_value=mock_env_vars), \
217+
patch('measure_innersource.auth_to_github',
218+
return_value=mock_github), \
219+
patch('measure_innersource.setup_logging') as mock_setup_logging, \
220+
patch('measure_innersource.write_to_markdown'), \
221+
patch('measure_innersource.evaluate_markdown_file_size'):
222+
223+
# Configure logging to capture our test
224+
mock_logger = MagicMock()
225+
mock_setup_logging.return_value = mock_logger
226+
227+
with patch('measure_innersource.get_logger',
228+
return_value=mock_logger):
229+
# Call main function
230+
mi.main()
231+
232+
# Verify that warning was logged about missing contributor
233+
mock_logger.warning.assert_any_call(
234+
"Contributor '%s' not found in org chart. "
235+
"Excluding from InnerSource analysis.",
236+
"unknown_contributor"
237+
)

0 commit comments

Comments
 (0)