Skip to content

Commit

Permalink
add file change detector
Browse files Browse the repository at this point in the history
  • Loading branch information
AAriam committed Oct 20, 2023
1 parent c076164 commit 4a22ea3
Showing 1 changed file with 106 additions and 10 deletions.
116 changes: 106 additions & 10 deletions src/repodynamics/actions/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ class Commit(NamedTuple):
action: PrimaryCommitAction | SecondaryCommitAction | None = None


class RepoFileType(Enum):
SUPERMETA = "SuperMeta Content"
WORKFLOW = "Workflows"
META = "Meta Content"
DYNAMIC = "Dynamic Content"
PACKAGE = "Package Files"
TEST = "Test-Suite Files"
WEBSITE = "Website Files"
README = "README Files"
OTHER = "Other Files"


class _FileStatus(NamedTuple):
title: str
emoji: str


class FileChangeType(Enum):
REMOVED = _FileStatus("Removed", "🔴")
MODIFIED = _FileStatus("Modified", "🟣")
BROKEN = _FileStatus("Broken", "🟠")
CREATED = _FileStatus("Created", "🟢")
UNMERGED = _FileStatus("Unmerged", "⚪️")
UNKNOWN = _FileStatus("Unknown", "⚫")


class ChangelogManager:
def __init__(
self,
Expand Down Expand Up @@ -233,6 +259,7 @@ def __init__(
self.metadata_ci: dict = {}
self.last_ver: PEP440SemVer | None = None
self.dist_ver: int = 0
self.changed_files: dict[RepoFileType, list[str]] = {}

self.summary_oneliners: list[str] = []
self.summary_sections: list[str | html.ElementCollection | html.Element] = []
Expand Down Expand Up @@ -1137,20 +1164,89 @@ def commit_website_announcement(
return commit_hash, commit_link

def get_changed_files(self) -> list[str]:
filepaths = []
name = "File Change Detector"
self.logger.h1(name)
change_type_map = {
"added": FileChangeType.CREATED,
"deleted": FileChangeType.REMOVED,
"modified": FileChangeType.MODIFIED,
"unmerged": FileChangeType.UNMERGED,
"unknown": FileChangeType.UNKNOWN,
"broken": FileChangeType.BROKEN,
"copied_to": FileChangeType.CREATED,
"renamed_from": FileChangeType.REMOVED,
"renamed_to": FileChangeType.CREATED,
"copied_modified_to": FileChangeType.CREATED,
"renamed_modified_from": FileChangeType.REMOVED,
"renamed_modified_to": FileChangeType.CREATED,
}
summary_detail = {file_type: [] for file_type in RepoFileType}
change_group = {file_type: [] for file_type in RepoFileType}
changes = self.git.changed_files(ref_start=self.hash_before, ref_end=self.hash_after)
self.logger.success("Detected changed files", json.dumps(changes, indent=3))
input_path = self.meta.output_paths.input_paths
fixed_paths = [outfile.rel_path for outfile in self.meta.output_paths.fixed_files]
for change_type, changed_paths in changes.items():
if change_type in ["unknown", "broken"]:
self.logger.warning(
f"Found {change_type} files",
f"Running 'git diff' revealed {change_type} changes at: {changed_paths}. "
"These files will be ignored."
)
continue
# if change_type in ["unknown", "broken"]:
# self.logger.warning(
# f"Found {change_type} files",
# f"Running 'git diff' revealed {change_type} changes at: {changed_paths}. "
# "These files will be ignored."
# )
# continue
if change_type.startswith("copied") and change_type.endswith("from"):
continue
filepaths.extend(changed_paths)
return filepaths
for path in changed_paths:
if path.endswith("/README.md") or path == ".github/_README.md":
typ = RepoFileType.README
elif path.startswith(f'{input_path["dir"]["source"]}/'):
typ = RepoFileType.PACKAGE
elif path in fixed_paths:
typ = RepoFileType.DYNAMIC
elif path.startswith(f'{input_path["dir"]["website"]}/'):
typ = RepoFileType.WEBSITE
elif path.startswith(f'{input_path["dir"]["tests"]}/'):
typ = RepoFileType.TEST
elif path.startswith(".github/workflows/"):
typ = RepoFileType.WORKFLOW
elif (
path.startswith(".github/DISCUSSION_TEMPLATE/")
or path.startswith(".github/ISSUE_TEMPLATE/")
or path.startswith(".github/PULL_REQUEST_TEMPLATE/")
or path.startswith(".github/workflow_requirements")
):
typ = RepoFileType.DYNAMIC
elif path.startswith(f'{input_path["dir"]["meta"]}/'):
typ = RepoFileType.META
elif path == ".path.json":
typ = RepoFileType.SUPERMETA
else:
typ = RepoFileType.OTHER
summary_detail[typ].append(f"{change_type_map[change_type].value.emoji}{path}")
change_group[typ].append(path)

self.changed_files = change_group
summary_details = []
changed_groups_str = ""
for file_type, summaries in summary_detail.items():
if summaries:
summary_details.append(html.h(3, file_type.value.title))
summary_details.append(html.ul(summaries))
changed_groups_str += f", {file_type.value.title}"
if changed_groups_str:
oneliner = f"Found changes in following groups: {changed_groups_str[2:]}."
else:
oneliner = "No changes were found."
legend = [f"{status.value.emoji}{status.value.title}" for status in FileChangeType]
color_legend = html.details(content=html.ul(legend), summary="Color Legend")
summary_details.insert(0, html.ul([oneliner, color_legend]))
self.add_summary(
name=name,
status="pass" if changed_groups_str else "skip",
oneliner=oneliner,
details=html.ElementCollection(summary_details)
)
return

def get_commits(self):
primary_action = {}
Expand Down

0 comments on commit 4a22ea3

Please sign in to comment.