Skip to content

Instantly share code, notes, and snippets.

@tompretty
Last active October 28, 2021 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompretty/7a6e24b39132207d8481155b70c55ed4 to your computer and use it in GitHub Desktop.
Save tompretty/7a6e24b39132207d8481155b70c55ed4 to your computer and use it in GitHub Desktop.
Get the ts error history
import subprocess
from dataclasses import dataclass, asdict
import json
import csv
from typing import List
# ---- Dataclasses ---- #
@dataclass
class Commit:
sha: str
timestamp: str
@staticmethod
def from_log_line(log_line: str):
return Commit(*log_line.split(" "))
@dataclass
class ErrorLog:
sha: str
timestamp: str
error_count: int
@staticmethod
def from_commit(commit: Commit):
return ErrorLog(
commit.sha, commit.timestamp, get_error_count_for_commit(commit.sha)
)
# ---- Main ---- #
def get_ts_error_history() -> None:
commits = [get_latest_commit(), *get_all_merge_commits()]
error_logs = get_all_error_logs(commits)
save_error_logs_to_csv(error_logs)
# ---- Helpers ---- #
def get_latest_commit() -> Commit:
process = subprocess.run(
"""
cd ~/code/support-frontend;
git log \
--pretty='format:%h %at' \
-1
""",
shell=True,
capture_output=True,
)
output = process.stdout.decode("utf-8")
return Commit.from_log_line(output)
def get_all_merge_commits() -> List[Commit]:
process = subprocess.run(
"""
cd ~/code/support-frontend;
git log \
--merges \
--first-parent main \
--pretty='format:%h %at' \
b291896..HEAD
""",
shell=True,
capture_output=True,
)
output = process.stdout.decode("utf-8").split("\n")
return [Commit.from_log_line(line) for line in output]
def get_all_error_logs(commits: List[Commit]) -> List[ErrorLog]:
return [ErrorLog.from_commit(c) for c in commits]
def save_error_logs_to_csv(error_logs: List[ErrorLog]) -> None:
with open("error_log.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["sha", "timestamp", "error_count"])
writer.writeheader()
writer.writerows(asdict(el) for el in error_logs)
def get_error_count_for_commit(sha: str) -> int:
process = subprocess.run(
f"""
cd ~/code/support-frontend;
git show {sha}:./support-frontend/typescript-errors.json
""",
shell=True,
capture_output=True,
)
output = process.stdout.decode("utf-8")
error_json = json.loads(output)
return sum(error_json["errorCounts"]["byCode"].values())
# ---- Run ---- #
if __name__ == "__main__":
get_ts_error_history()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment