Skip to content

Commit

Permalink
add metric for repo tag count
Browse files Browse the repository at this point in the history
  • Loading branch information
d33bs committed Nov 23, 2024
1 parent 073afef commit 083ad74
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/almanack/metrics/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ def count_unique_contributors(
return len(contributors)


def count_repo_tags(repo: pygit2.Repository) -> int:
"""
Counts the number of tags in a pygit2 repository.
Args:
repo (pygit2.Repository):
The repository to analyze.
Returns:
int:
The number of tags in the repository.
"""
return sum(1 for ref in repo.references if ref.startswith("refs/tags/"))


def compute_repo_data(repo_path: str) -> None:
"""
Computes comprehensive data for a GitHub repository.
Expand Down Expand Up @@ -393,6 +408,7 @@ def compute_repo_data(repo_path: str) -> None:
"repo-unique-contributors-past-182-days": count_unique_contributors(
repo=repo, since=DATETIME_NOW - timedelta(days=182)
),
"repo-tags-count": count_repo_tags(repo=repo),
"repo-agg-info-entropy": normalized_total_entropy,
"repo-file-info-entropy": file_entropy,
}
Expand Down
5 changes: 5 additions & 0 deletions src/almanack/metrics/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ metrics:
Count of unique contributors within the last 182 days
from now (where now is a reference to table value
of almanack-table-datetime).
- name: "repo-tags-count"
id: "SGA-GL-0011"
result-type: "int"
description: >-
Count of the number of tags within the repository.
- name: "repo-agg-info-entropy"
id: "SGA-VS-0001"
result-type: "float"
Expand Down
21 changes: 16 additions & 5 deletions tests/data/almanack/repo_setup/create_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def repo_setup(
Set up a temporary repository with specified files and commit metadata.
Args:
repo_path (Path):
repo_path (pathlib.Path):
The temporary directory where the repo will be created.
files (list[dict]):
A list of dictionaries where each dictionary represents a commit.
Expand All @@ -175,13 +175,15 @@ def repo_setup(
- "author" (optional): A dictionary with "name" and "email" keys
to specify the commit author. If not provided, defaults to the
repository's default user configuration.
- "tag" (optional): A string representing the tag to associate
with the commit.
branch_name (str):
The name of the branch to use for commits. Defaults to "main".
Returns:
pygit2.Repository:
The initialized repository with the specified commits.
The initialized repository with the specified commits and tags.
"""
# Initialize the repository
repo = pygit2.init_repository(repo_path, bare=False)
Expand All @@ -198,6 +200,7 @@ def repo_setup(
commit_files = commit_data.get("files", {})
commit_date = commit_data.get("commit-date", datetime.now())
author_data = commit_data.get("author", None)
tag_name = commit_data.get("tag") # Get the optional tag name

# Create or update each file in the current commit
for filename, content in commit_files.items():
Expand Down Expand Up @@ -246,9 +249,17 @@ def repo_setup(
)

# Update the parent_commit to the latest commit for chaining
parent_commit = repo.get(
commit_id
) # Explicitly get the Commit object by its ID
parent_commit = repo.get(commit_id)

# Create a tag if specified
if tag_name:
repo.create_tag(
tag_name,
parent_commit.id,
pygit2.GIT_OBJECT_COMMIT,
author,
f"Tag {tag_name} for commit #{i + 1}",
)

# Set the HEAD to the main branch after all commits
repo.set_head(branch_ref)
Expand Down
42 changes: 42 additions & 0 deletions tests/metrics/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
METRICS_TABLE,
_get_almanack_version,
compute_repo_data,
count_repo_tags,
count_unique_contributors,
default_branch_is_not_master,
file_exists_in_repo,
Expand Down Expand Up @@ -602,3 +603,44 @@ def test_count_unique_contributors(tmp_path, files, since, expected_count):

# Assert the result matches the expected count
assert result == expected_count, f"Expected {expected_count}, got {result}"


@pytest.mark.parametrize(
"files, expected_tag_count",
[
# No tags in the repository
(
[
{"files": {"file1.txt": "Initial content"}},
{"files": {"file2.txt": "Another content"}},
],
0,
),
# One tag in the repository
(
[
{"files": {"file1.txt": "Initial content"}, "tag": "v1.0"},
{"files": {"file2.txt": "Another content"}},
],
1,
),
# Multiple tags in the repository
(
[
{"files": {"file1.txt": "Initial content"}, "tag": "v1.0"},
{"files": {"file2.txt": "More content"}, "tag": "v1.1"},
{"files": {"file3.txt": "Even more content"}, "tag": "v2.0"},
],
3,
),
],
)
def test_count_repo_tags(tmp_path, files, expected_tag_count):
"""
Test count_repo_tags
"""

repo = repo_setup(repo_path=tmp_path, files=files)

# Assert the tag count matches the expected value
assert count_repo_tags(repo) == expected_tag_count

0 comments on commit 083ad74

Please sign in to comment.