Skip to content

github

Git functionality.

NewBranch

Bases: BaseModel

Information used to create a new Git branch.

Attributes:

Name Type Description
repo_path str

Local path to the git repo

branch_name str

Name of the branch to create

commit_msg str

Message that describes the git commit

files Set[str]

Files included in the commit

instruction_msg str

Message that should be displayed after the branch was pushed

Source code in dapla_team_cli/github.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class NewBranch(BaseModel):
    """Information used to create a new Git branch.

    Attributes:
        repo_path: Local path to the git repo
        branch_name: Name of the branch to create
        commit_msg: Message that describes the git commit
        files: Files included in the commit
        instruction_msg: Message that should be displayed after the branch was pushed
    """

    repo_path: str
    branch_name: str
    commit_msg: str
    files: Set[str]
    instruction_msg: str

create_branch(branch)

Create a new git branch with a set of files.

Source code in dapla_team_cli/github.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def create_branch(branch: NewBranch) -> None:
    """Create a new git branch with a set of files."""
    repo = Repo(branch.repo_path)
    current = repo.create_head(branch.branch_name)
    current.checkout()

    if repo.index.diff(None) or repo.untracked_files:
        for f in branch.files:
            repo.git.add(f)
        repo.git.commit(m=branch.commit_msg)
        repo.git.push("--set-upstream", "origin", current)
        print(branch.instruction_msg)
    else:
        console.print("No changes...", style=styles["normal"])

delete_remote_branch(branch_name, repo)

Deletes a remote branch on specified repo.

Source code in dapla_team_cli/github.py
85
86
87
88
89
90
91
def delete_remote_branch(branch_name: str, repo: Repository) -> None:
    """Deletes a remote branch on specified repo."""
    try:
        ref = repo.get_git_ref(f"heads/{branch_name}")
        ref.delete()
    except UnknownObjectException:
        print("No such branch", branch_name)

get_commit(sha, repo_name, gh_org=None)

Gets a commit given a sha.

Source code in dapla_team_cli/github.py
122
123
124
125
126
127
def get_commit(sha: str, repo_name: str, gh_org: Optional[Organization] = None) -> Commit:
    """Gets a commit given a sha."""
    if gh_org is None:
        gh_org = get_github_org()
    repo: Repository = gh_org.get_repo(repo_name)
    return repo.get_commit(sha)

get_decoded_string_from_shell(command)

Decodes subprocess output to string.

Source code in dapla_team_cli/github.py
51
52
53
54
55
def get_decoded_string_from_shell(command: str) -> str:
    """Decodes subprocess output to string."""
    result = subprocess.run(command.split(), stdout=subprocess.PIPE, check=True)  # noqa: S603
    decoded_result = result.stdout.decode("utf-8").strip()
    return decoded_result

get_github_org(org_name='statisticsnorway')

Gets auth token from the environment and returns Github object.

Source code in dapla_team_cli/github.py
58
59
60
61
62
63
64
65
66
def get_github_org(org_name: str = "statisticsnorway") -> Organization:
    """Gets auth token from the environment and returns Github object."""
    global gh_ssb
    if gh_ssb is None:
        log.debug("Getting Github org '{org_name}'")
        token = get_decoded_string_from_shell("gh auth token")
        gh = github.Github(token)
        gh_ssb = gh.get_organization(org_name)
    return gh_ssb

get_pr(pr_number, repo_name, gh_org=None)

Returns a PR given a PR number and a repository.

Source code in dapla_team_cli/github.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def get_pr(pr_number: Optional[int], repo_name: str, gh_org: Optional[Organization] = None) -> Optional[PullRequest]:
    """Returns a PR given a PR number and a repository."""
    if pr_number is None:
        console.log(RichFailure(message="PR number was not set in the state file"))
        console.log(SKIPPING)
        return None
    if gh_org is None:
        gh_org = get_github_org()
    repo: Repository = gh_org.get_repo(repo_name)
    try:  # Ensure issue exists
        return repo.get_pull(pr_number)
    except UnknownObjectException:
        console.log(RichFailure(message=f"PR with number {pr_number} was not found"))
        console.log(SKIPPING)
        return None
    except TypeError:
        console.log(RichFailure(message="PR number was not registered in the state file"))
        console.log(SKIPPING)
        return None

get_repo(repo_name, gh_org=None)

Gets a repository object given a repository name.

Source code in dapla_team_cli/github.py
94
95
96
97
98
def get_repo(repo_name: str, gh_org: Optional[Organization] = None) -> Repository:
    """Gets a repository object given a repository name."""
    if gh_org is None:
        gh_org = get_github_org()
    return gh_org.get_repo(repo_name)