Skip to content

Commit 434c673

Browse files
Update federatedcode pipes (#1831)
* Separate commiting and pushing functionality in federatedcode pipes Signed-off-by: Jono Yang <[email protected]> * Add write_data_as_yaml function to federatedcode pipes Signed-off-by: Jono Yang <[email protected]> * Update CHANGELOG.rst Signed-off-by: Jono Yang <[email protected]> --------- Signed-off-by: Jono Yang <[email protected]> Co-authored-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 473c6fe commit 434c673

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ v35.4.0 (unreleased)
2929
* Add support when the "components" entry is missing.
3030
https://github.com/aboutcode-org/scancode.io/issues/1727
3131

32+
- Split the functionality of
33+
``scanpipe.pipes.federatedcode.commit_and_push_changes`` into
34+
``scanpipe.pipes.federatedcode.commit_changes`` and
35+
``scanpipe.pipes.federatedcode.push_changes``. Add
36+
``scanpipe.pipes.federatedcode.write_data_as_yaml``.
37+
38+
3239
v35.3.0 (2025-08-20)
3340
--------------------
3441

scanpipe/pipes/federatedcode.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from django.conf import settings
3232

3333
import requests
34+
import saneyaml
3435
from git import Repo
3536
from packageurl import PackageURL
3637

@@ -161,6 +162,19 @@ def add_scan_result(project, repo, package_scan_file, logger=None):
161162
return relative_scan_file_path
162163

163164

165+
def commit_changes(repo, files_to_commit, commit_message):
166+
"""Commit changes to remote repository."""
167+
repo.index.add(files_to_commit)
168+
repo.index.commit(textwrap.dedent(commit_message))
169+
170+
171+
def push_changes(repo, remote_name="origin", branch_name=""):
172+
"""Push changes to remote repository."""
173+
if not branch_name:
174+
branch_name = repo.active_branch.name
175+
repo.git.push(remote_name, branch_name, "--no-verify")
176+
177+
164178
def commit_and_push_changes(
165179
repo, file_to_commit, purl, remote_name="origin", logger=None
166180
):
@@ -177,14 +191,27 @@ def commit_and_push_changes(
177191
178192
Signed-off-by: {author_name} <{author_email}>
179193
"""
180-
181-
default_branch = repo.active_branch.name
182-
183-
repo.index.add([file_to_commit])
184-
repo.index.commit(textwrap.dedent(commit_message))
185-
repo.git.push(remote_name, default_branch, "--no-verify")
194+
files_to_commit = [file_to_commit]
195+
commit_changes(
196+
repo=repo, files_to_commit=files_to_commit, commit_message=commit_message
197+
)
198+
push_changes(
199+
repo=repo,
200+
remote_name=remote_name,
201+
)
186202

187203

188204
def delete_local_clone(repo):
189205
"""Remove local clone."""
190206
shutil.rmtree(repo.working_dir)
207+
208+
209+
def write_data_as_yaml(base_path, file_path, data):
210+
"""
211+
Write the ``data`` as YAML to the ``file_path`` in the ``base_path`` root directory.
212+
Create directories in the path as needed.
213+
"""
214+
write_to = Path(base_path) / Path(file_path)
215+
write_to.parent.mkdir(parents=True, exist_ok=True)
216+
with open(write_to, encoding="utf-8", mode="w") as f:
217+
f.write(saneyaml.dump(data))

scanpipe/tests/pipes/test_federatedcode.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from django.test import TestCase
2929

3030
import git
31+
import saneyaml
3132

3233
from scanpipe import models
3334
from scanpipe.pipes import federatedcode
@@ -77,3 +78,28 @@ def test_scanpipe_pipes_federatedcode_delete_local_clone(self):
7778
federatedcode.delete_local_clone(repo)
7879

7980
self.assertEqual(False, Path(local_dir).exists())
81+
82+
def test_scanpipe_pipes_federatedcode_write_data_as_yaml(self):
83+
# create local repo
84+
local_dir = tempfile.mkdtemp()
85+
repo = git.Repo.init(local_dir)
86+
87+
# write data
88+
data = ["123", "abc", 3]
89+
federatedcode.write_data_as_yaml(
90+
base_path=repo.working_dir,
91+
file_path="test.yml",
92+
data=data,
93+
)
94+
95+
# Check if file was written
96+
test_file_path = Path(repo.working_dir) / "test.yml"
97+
self.assertEqual(True, test_file_path.exists())
98+
with open(test_file_path) as f:
99+
contents = f.read()
100+
yml = saneyaml.load(contents)
101+
expected_results = ["123", "abc", "3"]
102+
self.assertEqual(expected_results, yml)
103+
104+
# clean up
105+
shutil.rmtree(repo.working_dir)

0 commit comments

Comments
 (0)