• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cisagov / aws-profile-sync / 7108366617

12 Jul 2023 07:55PM CUT coverage: 44.595% (+0.5%) from 44.091%
7108366617

push

github

web-flow
Merge pull request #31 from cisagov/lineage/skeleton

Lineage pull request for: skeleton

20 of 58 branches covered (0.0%)

Branch coverage included in aggregate %.

79 of 164 relevant lines covered (48.17%)

2.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

37.14
/src/aws_profile_sync/handlers/ssh_git.py
1
"""A Git repository over secure shell handler."""
2

3
# Standard Python Libraries
4
import logging
6✔
5
from pathlib import Path
6✔
6
import subprocess  # nosec: Security of subprocess has been considered
6✔
7

8

9
class SSHGitHandler:
6✔
10
    """A Git repository over secure shell handler.
11

12
    This class can clone or update an existing clone of a remote repository that is
13
    served over ssh.
14
    """
15

16
    CLONE_PATH = Path("git")
6✔
17

18
    @staticmethod
6✔
19
    def can_handle(url):
4✔
20
        """Determine if this class can handle a specified URL.
21

22
        Args:
23
            url: A URL of any format.
24

25
        Returns:
26
            True if the URL can be handled.  False otherwise.
27

28
        """
29
        return url.startswith("ssh://") and url.endswith(".git")
×
30

31
    def __init__(self, work_path):
6✔
32
        """Instanciate a new SSHGitHandler.
33

34
        The class will create a directory structure it requires to store cloned
35
        repositories within the working path.
36

37
        Args:
38
            work_path: A pathlib.Path pointing to a work directory.
39

40
        """
41
        super().__init__()
×
42
        self.work_path = work_path / SSHGitHandler.CLONE_PATH
×
43
        self.work_path.mkdir(parents=True, exist_ok=True)
×
44

45
    def fetch(self, url, branch="master", filename="roles"):
6✔
46
        """Generate lines from the retrieved repository file.
47

48
        Args:
49
            url: A git-style URL pointing to a repository with profile formatted files
50
            repo_file: The file to read from the repository.
51

52
        Yields:
53
            Lines read from the specified repository file.
54

55
        Raises:
56
            subprocess.CalledProcessError: If a subprocess returns a non-zero exit code.
57

58
        """
59
        repo_name = url.split("/")[-1].split(".")[0]
×
60
        repo_path = self.work_path / repo_name
×
61
        read_file = repo_path / filename
×
62

63
        if repo_path.exists():
×
64
            logging.info(f"Pulling {url}")
×
65
            subprocess.run(["git", "pull"], check=True, cwd=repo_path)  # nosec
×
66
        else:
67
            logging.info(f"Cloning {url}")
×
68
            subprocess.run(  # nosec
×
69
                ["git", "clone", url], check=True, cwd=self.work_path
70
            )
71
        # Switch to the requested branch
72
        logging.debug(f"Switching to branch {branch}")
×
73
        subprocess.run(["git", "switch", branch], check=True, cwd=repo_path)  # nosec
×
74

75
        logging.debug(f"Reading from repo: {read_file}")
×
76
        with read_file.open() as f:
×
77
            yield from f
×
78
        return
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc