Skip to content

Commit c69e8a0

Browse files
Refactor federatedcode commmit functions (#1868)
Support committing many changes files at once and commits with data generated from other tools. Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent cbb8679 commit c69e8a0

File tree

4 files changed

+77
-28
lines changed

4 files changed

+77
-28
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ dev: virtualenv
5858
@echo "-> Configure and install development dependencies"
5959
@${ACTIVATE} pip install ${PIP_ARGS} --editable .[dev]
6060

61+
dev-mining: virtualenv
62+
@echo "-> Configure and install development dependencies"
63+
@$(MAKE) dev
64+
@${ACTIVATE} pip install ${PIP_ARGS} --editable .[mining]
65+
6166
envfile:
6267
@echo "-> Create the .env file and generate a secret key"
6368
@if test -f ${ENV_FILE}; then echo ".env file exists already"; exit 1; fi

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ dev = [
119119
android_analysis = [
120120
"android_inspector==0.0.1"
121121
]
122+
mining = [
123+
"minecode_pipelines==0.0.1b1"
124+
]
122125

123126
[project.urls]
124127
Homepage = "https://github.com/aboutcode-org/scancode.io"

scanpipe/pipelines/publish_to_federatedcode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def commit_and_push_changes(self):
8383
"""Commit and push changes to remote repository."""
8484
federatedcode.commit_and_push_changes(
8585
repo=self.repo,
86-
file_to_commit=str(self.relative_file_path),
87-
purl=self.project.purl,
86+
files_to_commit=[str(self.relative_file_path)],
87+
purls=[self.project.purl],
8888
logger=self.log,
8989
)
9090
self.log(

scanpipe/pipes/federatedcode.py

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ def check_federatedcode_eligibility(project):
127127
raise Exception("Missing version in Project PURL.")
128128

129129

130+
def check_federatedcode_configured_and_available(logger=None):
131+
"""
132+
Check if the criteria for pushing the results to FederatedCode
133+
is satisfied.
134+
135+
Criteria:
136+
- FederatedCode is configured and available.
137+
"""
138+
if not is_configured():
139+
raise Exception("FederatedCode is not configured.")
140+
141+
if not is_available():
142+
raise Exception("FederatedCode Git account is not available.")
143+
144+
if logger:
145+
logger("Federatedcode repositories are configured and available.")
146+
147+
130148
def clone_repository(repo_url, logger=None):
131149
"""Clone repository to local_path."""
132150
local_dir = tempfile.mkdtemp()
@@ -162,12 +180,6 @@ def add_scan_result(project, repo, package_scan_file, logger=None):
162180
return relative_scan_file_path
163181

164182

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-
171183
def push_changes(repo, remote_name="origin", branch_name=""):
172184
"""Push changes to remote repository."""
173185
if not branch_name:
@@ -176,29 +188,58 @@ def push_changes(repo, remote_name="origin", branch_name=""):
176188

177189

178190
def commit_and_push_changes(
179-
repo, file_to_commit, purl, remote_name="origin", logger=None
191+
repo,
192+
files_to_commit,
193+
commit_message=None,
194+
purls=None,
195+
remote_name="origin",
196+
logger=None,
180197
):
181198
"""Commit and push changes to remote repository."""
182-
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
183-
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
184-
185-
change_type = "Add" if file_to_commit in repo.untracked_files else "Update"
186-
commit_message = f"""\
187-
{change_type} scan result for {purl}
188-
189-
Tool: pkg:github/aboutcode-org/scancode.io@v{VERSION}
190-
Reference: https://{settings.ALLOWED_HOSTS[0]}/
199+
commit_changes(repo, files_to_commit, commit_message, purls)
200+
push_changes(repo, remote_name)
201+
202+
203+
def commit_changes(
204+
repo,
205+
files_to_commit,
206+
commit_message=None,
207+
purls=None,
208+
mine_type="packageURL",
209+
tool_name="pkg:github/aboutcode-org/scancode.io",
210+
tool_version=VERSION,
211+
logger=None,
212+
):
213+
"""Commit changes in files to a remote repository."""
214+
if not files_to_commit:
215+
return
216+
217+
if not commit_message:
218+
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
219+
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
220+
221+
files_added = all(
222+
[
223+
True
224+
for changed_file in files_to_commit
225+
if changed_file in repo.untracked_files
226+
]
227+
)
228+
change_type = "Add" if files_added else "Update"
229+
230+
purls = "\n".join(purls)
231+
commit_message = f"""\
232+
{change_type} {mine_type} results for:
233+
{purls}
234+
235+
Tool: {tool_name}@v{tool_version}
236+
Reference: https://{settings.ALLOWED_HOSTS[0]}
237+
238+
Signed-off-by: {author_name} <{author_email}>
239+
"""
191240

192-
Signed-off-by: {author_name} <{author_email}>
193-
"""
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-
)
241+
repo.index.add(files_to_commit)
242+
repo.index.commit(textwrap.dedent(commit_message))
202243

203244

204245
def delete_local_clone(repo):

0 commit comments

Comments
 (0)