Skip to content

Commit

Permalink
Merge pull request #491 from gauge-sh/add-git-info-to-upload
Browse files Browse the repository at this point in the history
Add git info to report upload
  • Loading branch information
caelean authored Dec 20, 2024
2 parents d79c427 + e970a69 commit b82ca54
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tach"
version = "0.18.0"
version = "0.19.0"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ If you use the [pre-commit framework](https://github.com/pre-commit/pre-commit),
```yaml
repos:
- repo: https://github.com/gauge-sh/tach-pre-commit
rev: v0.18.0 # change this to the latest tag!
rev: v0.19.0 # change this to the latest tag!
hooks:
- id: tach
```
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "tach"
version = "0.18.0"
version = "0.19.0"
authors = [
{ name = "Caelean Barnes", email = "[email protected]" },
{ name = "Evan Doyle", email = "[email protected]" },
Expand Down
2 changes: 1 addition & 1 deletion python/tach/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations

__version__: str = "0.18.0"
__version__: str = "0.19.0"

__all__ = ["__version__"]
19 changes: 17 additions & 2 deletions python/tach/filesystem/git_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class GitBranchInfo:
repo: str
name: str
commit: str
owner: str
user_name: str
email: str


def is_github_actions():
Expand Down Expand Up @@ -67,13 +70,25 @@ def get_current_branch_info(

try:
# TODO: support slashes or org names
repo_name = repo.remotes.origin.url.split("/")[-1].replace(".git", "")
url_parts = repo.remotes.origin.url.split("/")
repo_name = url_parts[-1].replace(".git", "")
owner_name = url_parts[0].split(":")[-1]
config_reader = repo.config_reader()
user_name = str(config_reader.get_value("user", "name", default=""))
email = str(config_reader.get_value("user", "email", default=""))
branch = _get_branch_name(repo)
commit = _get_commit(repo)
except Exception as e:
raise TachError(f"Failed to determine current branch information!\nError: {e}")

return GitBranchInfo(repo=repo_name, name=branch, commit=commit)
return GitBranchInfo(
repo=repo_name,
owner=owner_name,
name=branch,
commit=commit,
user_name=user_name,
email=email,
)


def get_changed_files(
Expand Down
35 changes: 20 additions & 15 deletions python/tach/modularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ def upload_report_to_gauge(
"""Upload a modularity report to Gauge."""
report = generate_modularity_report(project_root, project_config, force=force)
print(f"{BCOLORS.OKCYAN} > Uploading report...{BCOLORS.ENDC}")
path = build_modularity_upload_path(report.repo)
post_json_to_gauge_api(path, asdict(report))
response_data = post_json_to_gauge_api(asdict(report))
print(f"{BCOLORS.OKGREEN} > Report uploaded!{BCOLORS.ENDC}")
if response_data.get("url"):
print(
f"{BCOLORS.OKBLUE} > {GAUGE_API_BASE_URL}{response_data['url']}{BCOLORS.ENDC}"
)


GAUGE_API_KEY = os.getenv("GAUGE_API_KEY", "")
GAUGE_API_BASE_URL = os.getenv("GAUGE_API_BASE_URL", "https://app.gauge.sh")
GAUGE_UPLOAD_URL = f"{GAUGE_API_BASE_URL}/api/client/tach-upload/1.3"


def build_modularity_upload_path(repo: str) -> str:
return f"/api/client/repos/{repo}/modularity"


def post_json_to_gauge_api(path: str, data: dict[str, Any]) -> None:
def post_json_to_gauge_api(data: dict[str, Any]) -> dict[str, str]:
if not GAUGE_API_KEY:
raise TachClosedBetaError(
f"{BCOLORS.WARNING}Modularity is currently in closed beta. Visit {GAUGE_API_BASE_URL}/closed-beta to request access.{BCOLORS.ENDC}"
Expand All @@ -70,28 +70,26 @@ def post_json_to_gauge_api(path: str, data: dict[str, Any]) -> None:
}
json_data = json.dumps(data)
conn = None
full_url = f"{GAUGE_API_BASE_URL}{path}"
try:
url_parts: parse.ParseResult = parse.urlparse(full_url)
if full_url.startswith("https://"):
url_parts: parse.ParseResult = parse.urlparse(GAUGE_UPLOAD_URL)
if GAUGE_UPLOAD_URL.startswith("https://"):
conn = HTTPSConnection(url_parts.netloc, timeout=10)
else:
conn = HTTPConnection(url_parts.netloc, timeout=10)
conn.request("POST", path, body=json_data, headers=headers)
conn.request("POST", url_parts.path, body=json_data, headers=headers)
response = conn.getresponse()

response_data = response.read().decode("utf-8")
# Check for non-200 status codes
if response.status != 200:
error_message = response.read().decode("utf-8")
raise TachError(
f"API request failed with status {response.status}: {error_message}"
f"API request failed with status {response.status}: {response_data}"
)

except Exception as e:
raise TachError(f"Failed to upload modularity report: {str(e)}")
finally:
if conn is not None:
conn.close()
return json.loads(response_data)


# NOTE: these usages are all imports
Expand Down Expand Up @@ -159,13 +157,17 @@ class CheckResult:

@dataclass
class Report:
email: str
user_name: str
owner: str
repo: str
branch: str
commit: str
# [1.1] The full configuration encoded as JSON
full_configuration: str
modules: list[Module] = field(default_factory=list)
usages: list[Usage] = field(default_factory=list)
# [1.3] Check result for dependency errors
check_result: CheckResult = field(default_factory=CheckResult)
metadata: ReportMetadata = field(default_factory=ReportMetadata)
# [1.2] Deprecated
Expand Down Expand Up @@ -297,6 +299,9 @@ def generate_modularity_report(
print(f"{BCOLORS.OKCYAN} > Generating report...{BCOLORS.ENDC}")
branch_info = get_current_branch_info(project_root, allow_dirty=force)
report = Report(
user_name=branch_info.user_name,
email=branch_info.email,
owner=branch_info.owner,
repo=branch_info.repo,
branch=branch_info.name,
commit=branch_info.commit,
Expand Down

0 comments on commit b82ca54

Please sign in to comment.