Skip to content

Commit 360e981

Browse files
Refactor federatedcode commmit functions
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 360e981

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
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/pipes/federatedcode.py

Lines changed: 81 additions & 17 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()
@@ -179,26 +197,72 @@ def commit_and_push_changes(
179197
repo, file_to_commit, purl, remote_name="origin", logger=None
180198
):
181199
"""Commit and push changes to remote repository."""
182-
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
183-
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
200+
commit_changes(repo, file_to_commit, purl)
201+
push_changes(repo, remote_name)
202+
203+
204+
def commit_changes(
205+
repo,
206+
file_to_commit,
207+
purl,
208+
commit_message=None,
209+
tool_name="pkg:github/aboutcode-org/scancode.io",
210+
tool_version=VERSION,
211+
logger=None,
212+
):
213+
"""Commit changes to remote repository."""
214+
215+
if not commit_message:
216+
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
217+
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
184218

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}
219+
change_type = "Add" if file_to_commit in repo.untracked_files else "Update"
220+
commit_message = f"""\
221+
{change_type} scan result for {purl}
188222
189-
Tool: pkg:github/aboutcode-org/scancode.io@v{VERSION}
190-
Reference: https://{settings.ALLOWED_HOSTS[0]}/
223+
Tool: {tool_name}@v{tool_version}
224+
Reference: https://{settings.ALLOWED_HOSTS[0]}/
191225
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-
)
226+
Signed-off-by: {author_name} <{author_email}>
227+
"""
228+
229+
repo.index.add([file_to_commit])
230+
repo.index.commit(textwrap.dedent(commit_message))
231+
232+
233+
def commit_many_changes(
234+
repo,
235+
files_to_commit,
236+
purls,
237+
mine_type="packageURL",
238+
commit_message=None,
239+
tool_name="pkg:github/aboutcode-org/scancode.io",
240+
tool_version=VERSION,
241+
logger=None,
242+
):
243+
"""Commit changes in many files to a remote repository."""
244+
245+
if not commit_message:
246+
author_name = settings.FEDERATEDCODE_GIT_SERVICE_NAME
247+
author_email = settings.FEDERATEDCODE_GIT_SERVICE_EMAIL
248+
249+
purls = "\n".join(purls)
250+
commit_message = f"""\
251+
Update {mine_type} results for:
252+
{purls}
253+
254+
Tool: {tool_name}@v{tool_version}
255+
Signed-off-by: {author_name} <{author_email}>
256+
"""
257+
258+
repo.index.add(files_to_commit)
259+
repo.index.commit(textwrap.dedent(commit_message))
260+
261+
262+
def push_changes(repo, remote_name="origin"):
263+
"""Push changes to remote repositiry."""
264+
default_branch = repo.active_branch.name
265+
repo.git.push(remote_name, default_branch, "--no-verify")
202266

203267

204268
def delete_local_clone(repo):

0 commit comments

Comments
 (0)