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

Support anonymous requests to public repos #5

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@ A tool for generating traces from GitHub Actions. The generated traces are compa

1. Download the `.whl` file from the [latest release](https://github.com/WATonomous/github-actions-tracing/releases/latest).
2. Install the wheel: `pip install <path_to_file.whl>`
3. Run the CLI: `gatrace`
3. Run the CLI: `gatrace --help`

Example usage:

```bash
gatrace generate-trace https://github.com/WATonomous/github-actions-tracing/actions/runs/11205960644
```

This will generate a trace file in the current directory.

To view the trace, you can use [Perfetto UI](https://ui.perfetto.dev/).

To generate traces for private repositories, you will need to provide a GitHub token. You can do this by passing the `--github-token` argument to the CLI.

```bash
gatrace generate-trace <run_url> --github-token <token>
```

Note that to view organization private repositories, the token must have the [`read:org` scope](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#types-of-personal-access-tokens).

## Development

Expand Down
Binary file added github_actions.perfetto-trace
Binary file not shown.
53 changes: 40 additions & 13 deletions src/github_actions_tracing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def run_graphql_query(query, token):


@app.command()
def get_data(url, token):
def get_data(github_url, github_token=None):
"""
Retrieve data from a GitHub Actions workflow run URL.
"""

# Extract owner, repo, run_id, and optional attempt number from the URL
match = re.match(
r"https://github\.com/(?P<owner>[^/]+)/(?P<repo>[^/]+)/actions/runs/(?P<run_id>\d+)(?:/attempts/(?P<attempt>\d+))?",
url,
github_url,
)
if not match:
raise ValueError("Invalid GitHub Actions URL format")
Expand All @@ -48,7 +52,10 @@ def get_data(url, token):
run_id = match.group("run_id")
attempt = match.group("attempt")

headers = {"Authorization": f"token {token}"}
headers = {}

if github_token:
headers["Authorization"] = f"Bearer {github_token}"

# Retrieve workflow run details
run_url = f"https://api.github.com/repos/{owner}/{repo}/actions/runs/{run_id}"
Expand Down Expand Up @@ -91,9 +98,14 @@ def to_ns(iso):


@app.command()
def create_trace_file(data=None, data_json=None):
def create_trace_file(
data=None,
data_json=None,
output_file="github_actions.perfetto-trace",
output_debug_json=None,
):
"""
This function creates a Perfetto trace file from the given data.
Create a Perfetto trace file from the given data.

References:
- https://perfetto.dev/docs/reference/synthetic-track-event
Expand Down Expand Up @@ -229,26 +241,40 @@ def create_trace_file(data=None, data_json=None):
perfetto_trace_pb2.TrackEvent.Type.TYPE_SLICE_END
)

# Write the trace to a JSON file for debugging
if output_debug_json:
with open(output_debug_json, "w") as f:
f.write(trace_to_json(trace))

print(f"Trace Debug JSON written to {output_debug_json}")

# Write the trace to a binary file
with open("trace_output_perfetto.pftrace", "wb") as f:
with open(output_file, "wb") as f:
f.write(trace.SerializeToString())

# Write the trace to a JSON file for debugging
with open("trace_output_perfetto.json", "w") as f:
f.write(trace_to_json(trace))

print(
"Trace file created successfully as 'trace_output_perfetto.pftrace and trace_output_perfetto.json'"
f"Trace file written to {output_file}. You can view it using the Perfetto UI (https://ui.perfetto.dev/)."
)


@app.command()
def generate_trace(github_url, github_token):
def generate_trace(
github_url,
github_token=None,
output_file="github_actions.perfetto-trace",
output_debug_json=None,
):
"""
Generate a Perfetto trace file from a GitHub Actions workflow run URL.
"""

try:
print(f"Fething data from {github_url}")
data = get_data(github_url, github_token)
print("Creating trace file")
create_trace_file(data)
create_trace_file(
data=data, output_file=output_file, output_debug_json=output_debug_json
)
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
raise
Expand All @@ -259,5 +285,6 @@ def generate_trace(github_url, github_token):
print(f"Unexpected error: {e}")
raise


if __name__ == "__main__":
app()