Skip to content

Commit 2bc6f25

Browse files
author
Trong Nhan Mai
committed
feat: allow defining a git service from defaults.ini
1 parent ba3fcb0 commit 2bc6f25

File tree

5 files changed

+110
-7
lines changed

5 files changed

+110
-7
lines changed

docs/source/pages/supported_technologies/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ such as GitHub Actions workflows.
2424
* Docker
2525

2626

27+
.. _supported_git_services:
28+
2729
------------
2830
Git Services
2931
------------

docs/source/pages/using.rst

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,53 @@ Analyzing a locally cloned repository
314314

315315
If you have a local repository that you want to analyze, Macaron also supports running the analysis against a local repository.
316316

317-
Assume that the dir tree at the local repository has the following components:
317+
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
318+
Analyzing a repository whose git service is not supported by Macaron
319+
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
320+
321+
If the repository remote URL is from an unknown git service (see :ref:`Git Services <supported_git_services>` for a list of supported git services in Macaron), Macaron won't recognize it when analyzing the repository.
322+
323+
You would need to tell Macaron about that git service through the ``defaults.ini`` config.
324+
For example, let's say you want to analyze the Bitbucket repository at ``https://bitbucket.org/snakeyaml/snakeyaml``. First, you need to create a ``defaults.ini`` file in the current workspace with the following content:
325+
326+
.. code-block:: ini
327+
328+
[git_service.local_repo]
329+
hostname = bitbucket.org
330+
331+
In which ``hostname`` contains the domain of the git service URL. In this example it's ``bitbucket.org``.
332+
333+
.. note::
334+
335+
This ``defaults.ini`` section must only be used for analyzing a locally cloned repository. If the domain name has already been supported in other services, it doesn't need to be defined again here.
336+
337+
Assume that the dir tree at the current workspace has the following structure:
338+
339+
.. code-block:: shell
340+
341+
boo
342+
├── foo
343+
│ └── snakeyaml
344+
345+
We can run Macaron against the local repository at ``snakeyaml`` by using this command:
346+
347+
.. code-block:: shell
348+
349+
./run_macaron.sh --local-repos-path ./boo/foo --defaults-path ./defaults.ini analyze -rp snakeyaml <rest_of_args>
350+
351+
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``-b``, ``-d`` or ``--skip-deps`` similar to two previous examples).
352+
353+
The ``-lr`` flag tells Macaron to look into ``path/to/boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
354+
355+
.. note:: If ``-lr`` is not provided, Macaron will looks inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``-rp``.
356+
357+
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
358+
Analyzing a local repository with supported git service
359+
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
360+
361+
If the local repository you want to analyze has remote origin hosted on a supported git service, you can run the analysis directly without having to prepare ``defaults.ini`` as above.
362+
363+
Assume that the dir tree at the current workspace has the following structure:
318364

319365
.. code-block:: shell
320366
@@ -326,13 +372,13 @@ We can run Macaron against the local repository at ``target`` by using this comm
326372

327373
.. code-block:: shell
328374
329-
./run_macaron.sh -lr path/to/boo/foo analyze -rp target <rest_of_args>
375+
./run_macaron.sh --local-repos-path ./boo/foo analyze -rp target <rest_of_args>
330376
331-
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``-b``, ``-d`` or ``--skip-deps`` similar to two previous examples)
377+
With ``rest_of_args`` being the arguments to the ``analyze`` command (e.g. ``-b``, ``-d`` or ``--skip-deps`` similar to two previous examples).
332378

333-
The ``-lr`` flag configure Macaron to looks into ``path/to/boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
379+
The ``-lr`` flag tells Macaron to look into ``path/to/boo/foo`` for local repositories. For more information, please see :ref:`Command Line Usage <cli-usage>`.
334380

335-
.. note:: If ``-lr`` is not provided, Macaron will looks inside ``<working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``-rp``.
381+
.. note:: If ``-lr`` is not provided, Macaron will looks inside ``<current_working_directory>/output/git_repos/local_repos/`` whenever you provide a local path to ``-rp``.
336382

337383
-------------------------
338384
Running the policy engine

src/macaron/config/defaults.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ hostname = gitlab.com
9090
# [git_service.gitlab.self_hosted]
9191
# hostname = example.org
9292

93+
# This section defines a git service that Macaron doesn't recognize yet.
94+
# It must only be used for analyzing a locally cloned repository.
95+
# If the host name is already supported in other services, it doesn't need to be defined again here.
96+
# [git_service.local_repo]
97+
# hostname = example.org
98+
9399
# This is the spec for trusted Maven build tools.
94100
[builder.maven]
95101
entry_conf = settings.xml
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""The git_service package contains the supported git services for Macaron."""
@@ -7,7 +7,14 @@
77
from .bitbucket import BitBucket
88
from .github import GitHub
99
from .gitlab import PubliclyHostedGitLab, SelfHostedGitLab
10+
from .local_repo_git_service import LocalRepoGitService
1011

1112
# The list of supported git services. The order of the list determines the order
1213
# in which each git service is checked against the target repository.
13-
GIT_SERVICES: list[BaseGitService] = [GitHub(), PubliclyHostedGitLab(), SelfHostedGitLab(), BitBucket()]
14+
GIT_SERVICES: list[BaseGitService] = [
15+
GitHub(),
16+
PubliclyHostedGitLab(),
17+
SelfHostedGitLab(),
18+
BitBucket(),
19+
LocalRepoGitService(),
20+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module contains the spec for the local repo git service."""
5+
6+
import logging
7+
8+
from pydriller.git import Git
9+
10+
from macaron.errors import ConfigurationError, RepoCheckOutError
11+
from macaron.slsa_analyzer import git_url
12+
from macaron.slsa_analyzer.git_service.base_git_service import BaseGitService
13+
14+
logger: logging.Logger = logging.getLogger(__name__)
15+
16+
17+
class LocalRepoGitService(BaseGitService):
18+
"""This class contains the spec of the local repo git service."""
19+
20+
def __init__(self) -> None:
21+
"""Initialize instance."""
22+
super().__init__("generic")
23+
24+
def load_defaults(self) -> None:
25+
"""Load the values for this git service from the ini configuration."""
26+
try:
27+
self.hostname = self.load_hostname(section_name="git_service.local_repo")
28+
except ConfigurationError as error:
29+
raise error
30+
31+
def clone_repo(self, _clone_dir: str, _url: str) -> None:
32+
"""Cloning from a local repo git service is not supported."""
33+
raise NotImplementedError
34+
35+
def check_out_repo(self, git_obj: Git, branch: str, digest: str, offline_mode: bool) -> Git:
36+
"""Checkout the branch and commit specified by the user of a repository."""
37+
if not git_url.check_out_repo_target(git_obj, branch, digest, offline_mode):
38+
raise RepoCheckOutError(
39+
f"Failed to check out branch {branch} and commit {digest} for repo {git_obj.project_name}."
40+
)
41+
42+
return git_obj

0 commit comments

Comments
 (0)