diff --git a/.github/workflows/create-release.yaml b/.github/workflows/create-release.yaml index 293ebfa..67e612f 100644 --- a/.github/workflows/create-release.yaml +++ b/.github/workflows/create-release.yaml @@ -21,6 +21,7 @@ jobs: numpy-version: [ 1.24.3 ] matplotlib-version: [ 3.7.1 ] web3-version: [ 6.2.0 ] + requests-version: [ 2.28.2 ] opt-flags: [ "--optimize --optimize-runs 200" ] @@ -49,6 +50,7 @@ jobs: python3 -m pip install web3==${{ matrix.web3-version }} python3 -m pip install numpy==${{ matrix.numpy-version }} python3 -m pip install matplotlib==${{ matrix.matplotlib-version }} + python3 -m pip install requests==${{ matrix.requests-version }} - name: Compiling contracts for PubSub/EventManager.sol run: | @@ -110,6 +112,13 @@ jobs: run: | python3 ./tests/MultiPubsGasCostEval.py + - name: Store evaluation figures to assets branch + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + python3 ./utils/GitHubCreateFile.py --path "assets/${{ github.ref_name }}-publish_gas_cost.svg" --commit-msg "Uploaded file assets/${{ github.ref_name }}-publish_gas_cost.svg" --file "${{ github.workspace }}/build/publish_gas_cost.svg" --branch assets-gas-eval + python3 ./utils/GitHubCreateFile.py --path "assets/${{ github.ref_name }}-subscribe_gas_cost.svg" --commit-msg "Uploaded file assets/${{ github.ref_name }}-subscribe_gas_cost.svg" --file "${{ github.workspace }}/build/subscribe_gas_cost.svg" --branch assets-gas-eval + - name: Generate release note run: | echo "# Release note" >> ./build/release_note.md @@ -133,14 +142,18 @@ jobs: echo "## Gas Cost Evaluations" >> ./build/release_note.md echo "### Gas Cost of Publishing Events" >> ./build/release_note.md echo "" >> ./build/release_note.md - echo '!'"[publish_gas_cost](/../../releases/download/${{ github.ref_name }}/publish_gas_cost.svg)" >> ./build/release_note.md + echo '!'"[publish_gas_cost](/../../blob/assets-gas-eval/assets/${{ github.ref_name }}-publish_gas_cost.svg)" >> ./build/release_note.md echo "" >> ./build/release_note.md echo "### Gas cost of Subscribing to Publishers" >> ./build/release_note.md echo "" >> ./build/release_note.md - echo '!'"[subscribe_gas_cost](/../../releases/download/${{ github.ref_name }}/subscribe_gas_cost.svg)" >> ./build/release_note.md + echo '!'"[subscribe_gas_cost](/../../blob/assets-gas-eval/assets/${{ github.ref_name }}-subscribe_gas_cost.svg)" >> ./build/release_note.md echo "" >> ./build/release_note.md echo "" >> ./build/release_note.md + - name: Echo release note + run: | + cat ${{ github.workspace }}/build/release_note.md + - name: Release uses: softprops/action-gh-release@v1 with: diff --git a/utils/GitHubCreateFile.py b/utils/GitHubCreateFile.py new file mode 100644 index 0000000..d4a39ce --- /dev/null +++ b/utils/GitHubCreateFile.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- +### +# Copyright (c) 2023 Haofan Zheng +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. +### + + +import argparse +import base64 +import os +import requests + +from typing import List + + +def BuildTreeObj( + path: str, + sha: str, + mode: str = '100644', + type: str = 'blob', +) -> dict: + return { + 'path': path, + 'mode': mode, + 'type': type, + 'sha': sha, + } + + +def GitBlobCreateText( + owner: str, + repo: str, + token: str, + content: str, +) -> dict: + # https://docs.github.com/en/rest/git/blobs?apiVersion=2022-11-28#create-a-blob + + url = f'https://api.github.com/repos/{owner}/{repo}/git/blobs' + req = requests.post( + url=url, + headers={ + 'Accept': 'application/vnd.github+json', + 'Authorization': f'Bearer {token}', + }, + json={ + 'content': content, + 'encoding': 'utf-8', + }, + ) + req.raise_for_status() + + return req.json() + + +def GitTreeCreate( + owner: str, + repo: str, + token: str, + baseTree: str, + trees: List[dict], +) -> dict: + # https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28 + + url = f'https://api.github.com/repos/{owner}/{repo}/git/trees' + req = requests.post( + url=url, + headers={ + 'Accept': 'application/vnd.github+json', + 'Authorization': f'Bearer {token}', + }, + json={ + 'base_tree': baseTree, + 'tree': trees, + }, + ) + req.raise_for_status() + + return req.json() + + +def ContentsCreateFile( + owner: str, + repo: str, + token: str, + path: str, + commitMsg: str, + contentB64: str, + branch: str, +) -> dict: + #https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents + + url = f'https://api.github.com/repos/{owner}/{repo}/contents/{path}' + req = requests.put( + url=url, + headers={ + 'Accept': 'application/vnd.github+json', + 'Authorization': f'Bearer {token}', + }, + json={ + 'message': commitMsg, + 'content': contentB64, + 'branch': branch, + }, + ) + req.raise_for_status() + + return req.json() + + +def main() -> None: + argParser = argparse.ArgumentParser() + argParser.add_argument( + '--path', type=str, required=True, + help='File path in repo', + ) + argParser.add_argument( + '--commit-msg', type=str, required=True, + help='Commit message', + ) + argParser.add_argument( + '--file', type=str, required=True, + help='File path to upload', + ) + argParser.add_argument( + '--branch', type=str, required=True, + help='Branch name', + ) + args = argParser.parse_args() + + # get owner and repo from environment variable + ownerAndRepo = os.environ.get('GITHUB_REPOSITORY') + if ownerAndRepo is None: + raise ValueError('GITHUB_REPOSITORY is not set') + owner, repo = ownerAndRepo.split('/', maxsplit=1) + + # get token from environment variable + token = os.environ.get('GITHUB_TOKEN') + if token is None: + raise ValueError('GITHUB_TOKEN is not set') + + # read in file + with open(args.file, 'rb') as f: + content = f.read() + + # convert to base64 + contentB64 = base64.b64encode(content).decode('utf-8') + + ContentsCreateFile( + owner=owner, + repo=repo, + token=token, + path=args.path, + commitMsg=args.commit_msg, + contentB64=contentB64, + branch=args.branch, + ) + + +if __name__ == '__main__': + main()