diff --git a/.github/workflows/gas_reports.yml b/.github/workflows/gas_reports.yml new file mode 100644 index 000000000..a17787de2 --- /dev/null +++ b/.github/workflows/gas_reports.yml @@ -0,0 +1,44 @@ +name: Compare Snapshot + +on: + pull_request: + paths: + - "scripts/compare_snapshot.py" + +jobs: + compare-snapshot: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" # Specify the Python version required + + - name: Set up Scarb + uses: software-mansion/setup-scarb@v1 + with: + scarb-version: "nightly-2023-08-12" + + - name: Run compare_snapshot script + id: run-script + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + run: | + result=$(python scripts/compare_snapshot.py) + result=$(echo "$result" | tail -n +3) # Strip the first two lines + result="${result//'%'/'%25'}" + result="${result//$'\n'/'%0A'}" + result="${result//$'\r'/'%0D'}" + echo "::set-output name=result::${result}" + + - name: Comment on PR + uses: thollander/actions-comment-pull-request@v2 + with: + message: "Snapshot Comparison Report:\n\n${{ + steps.run-script.outputs.result }}" # Access the result output variable + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + comment_tag: gas-report diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a6585afc..ba09c879a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to ### Added +- tooling: PR bot displays gas changes induced by PR - refactor(22/08/2023): refactor project to use scarb workspace - opcode(22/08/2023): add 0x1A-BYTE opcode - tooling: CI now generates gas snapshots artifacts. pre-push hook to compare diff --git a/scripts/compare_snapshot.py b/scripts/compare_snapshot.py index e03f955e9..7d3f1845f 100644 --- a/scripts/compare_snapshot.py +++ b/scripts/compare_snapshot.py @@ -19,25 +19,23 @@ def get_github_token_from_env(file_path=".env"): if key == "GITHUB_TOKEN": return value except FileNotFoundError: - print(f"Error: {file_path} file not found.") + return None except ValueError: print(f"Error: Invalid format in {file_path}. Expected 'KEY=VALUE' format.") return None def get_previous_snapshot(): - REPO = "enitrat/kakarot-ssj" # Replace with your GitHub username and repo name - GITHUB_TOKEN = get_github_token_from_env() + REPO = "kkrt-labs/kakarot-ssj" # Replace with your GitHub username and repo name + GITHUB_TOKEN = get_github_token_from_env() or os.environ.get("GITHUB_TOKEN") try: - REPO = "kkrt-labs/kakarot-ssj" - # Fetch the list of workflow runs cmd = f"curl -s -H 'Authorization: token {GITHUB_TOKEN}' -H 'Accept: application/vnd.github.v3+json' 'https://api.github.com/repos/{REPO}/actions/runs'" result = subprocess.check_output(cmd, shell=True) runs = json.loads(result) # Find the latest successful run - latest_successful_run = next(run for run in runs["workflow_runs"] if run["conclusion"] == "success") + latest_successful_run = next(run for run in runs["workflow_runs"] if (run["conclusion"] == "success" and run["head_branch"] == "main")) # Fetch the artifacts for that run run_id = latest_successful_run["id"] @@ -118,6 +116,8 @@ def print_colored_output(improvements, worsened, gas_changes): color = RED if gas_changes > 0 else GREEN gas_statement = "performance degradation, gas consumption +" if gas_changes > 0 else "performance improvement, gas consumption" print(color + f"Overall gas change: {gas_statement}{format(gas_changes, '.2f')} %" + ENDC) + else: + print("No changes in gas consumption.") def total_gas_used(current,previous): """Return the total gas used in the current and previous snapshot."""