Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add measure for repository default branch as not master #128

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/almanack/metrics/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ def is_citable(repo: pygit2.Repository) -> bool:
return False


def default_branch_is_not_master(repo: pygit2.Repository) -> bool:
"""
Checks if the default branch of the specified
repository is "master".

Args:
repo (Repository):
A pygit2.Repository object representing the Git repository.

Returns:
bool:
True if the default branch is "master", False otherwise.
"""
# Get the symbolic reference to HEAD, which should point to the default branch
head_ref = repo.head.shorthand

# Check if the default branch is not "master"
return head_ref != "master"
d33bs marked this conversation as resolved.
Show resolved Hide resolved


def compute_repo_data(repo_path: str) -> None:
"""
Computes comprehensive data for a GitHub repository.
Expand Down Expand Up @@ -235,6 +255,7 @@ def compute_repo_data(repo_path: str) -> None:
expected_file_name="license",
),
"is-citable": is_citable(repo=repo),
"default-branch-not-master": default_branch_is_not_master(repo=repo),
"normalized_total_entropy": normalized_total_entropy,
"file_level_entropy": file_entropy,
}
Expand Down
7 changes: 7 additions & 0 deletions src/almanack/metrics/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ metrics:
description: >-
Boolean value indicating the presence of a CITATION file
or some other means of indicating how to cite the work.
- name: "default-branch-not-master"
d33bs marked this conversation as resolved.
Show resolved Hide resolved
id: "SGA-GL-0006"
result-type: "bool"
result-data-key: "default-branch-not-master"
description: >-
Boolean value indicating that the repo uses a
default branch name besides 'master'.
- name: "agg-info-entropy"
id: "SGA-VS-0001"
result-type: "float"
Expand Down
17 changes: 12 additions & 5 deletions tests/data/almanack/repo_setup/create_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,20 @@ def create_entropy_repositories(base_path: pathlib.Path) -> None:
commit_changes(repo_path, "Commit with added lines of code")


def repo_setup(repo_path: pathlib.Path, files: dict) -> pygit2.Repository:
def repo_setup(
repo_path: pathlib.Path, files: dict, branch_name: str = "main"
) -> pygit2.Repository:
"""
Set up a temporary repository with specified files.

Args:
tmp_path (Path): The temporary directory where the repo will be created.
files (dict): A dictionary where keys are filenames and values are their content.
tmp_path (Path):
The temporary directory where the repo will be created.
files (dict):
A dictionary where keys are filenames and values are their content.
branch_name (str):
A string with the name of the branch which will be used for
committing changes. Defaults to "main".

Returns:
pygit2.Repository: The initialized repository with files.
Expand All @@ -185,7 +192,7 @@ def repo_setup(repo_path: pathlib.Path, files: dict) -> pygit2.Repository:
tree = repo.index.write_tree()

repo.create_commit(
"refs/heads/main",
f"refs/heads/{branch_name}",
author,
author,
"Initial commit with setup files",
Expand All @@ -194,6 +201,6 @@ def repo_setup(repo_path: pathlib.Path, files: dict) -> pygit2.Repository:
)

# Set the head to the main branch
repo.set_head("refs/heads/main")
repo.set_head(f"refs/heads/{branch_name}")

return repo
26 changes: 26 additions & 0 deletions tests/metrics/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from almanack.metrics.data import (
METRICS_TABLE,
compute_repo_data,
default_branch_is_not_master,
file_exists_in_repo,
get_table,
is_citable,
Expand Down Expand Up @@ -225,3 +226,28 @@ def test_is_citable(tmp_path, files, expected):
repo = pygit2.Repository(str(repo_path))

assert is_citable(repo) == expected


def test_default_branch_is_not_master(tmp_path):
"""
Tests default_branch_is_not_master
"""

# create paths for example repos
# (so they aren't in the same dir and overlapping)
pathlib.Path((example1 := tmp_path / "example1")).mkdir()
pathlib.Path((example2 := tmp_path / "example2")).mkdir()

# test with a master branch
repo = repo_setup(
repo_path=example1, files={"example.txt": "example"}, branch_name="master"
)

assert not default_branch_is_not_master(repo)

# test with a main branch
repo = repo_setup(
repo_path=example2, files={"example.txt": "example"}, branch_name="main"
)

assert default_branch_is_not_master(repo)