diff --git a/.github/workflows/agent.yml b/.github/workflows/agent.yml index b01ad7b..cbe1456 100644 --- a/.github/workflows/agent.yml +++ b/.github/workflows/agent.yml @@ -28,3 +28,15 @@ jobs: args: release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Trigger sync + if: ${{ env.SYNCED == 0 }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + await github.rest.actions.createWorkflowDispatch({ + owner: 'nezhahq', + repo: 'agent', + workflow_id: 'sync.yml', + ref: 'main' + }) diff --git a/.github/workflows/sync.py b/.github/workflows/sync.py new file mode 100644 index 0000000..478bdea --- /dev/null +++ b/.github/workflows/sync.py @@ -0,0 +1,122 @@ +import os +import time +import requests +import hashlib +from github import Github + + +def get_github_latest_release(): + g = Github() + repo = g.get_repo("nezhahq/agent") + release = repo.get_latest_release() + if release: + print(f"Latest release tag is: {release.tag_name}") + print(f"Latest release info is: {release.body}") + files = [] + for asset in release.get_assets(): + url = asset.browser_download_url + name = asset.name + + response = requests.get(url) + if response.status_code == 200: + with open(name, 'wb') as f: + f.write(response.content) + print(f"Downloaded {name}") + else: + print(f"Failed to download {name}") + file_abs_path = get_abs_path(asset.name) + files.append(file_abs_path) + print('Checking file integrities') + verify_checksum(get_abs_path("checksums.txt")) + sync_to_gitee(release.tag_name, release.body, files) + else: + print("No releases found.") + + +def sync_to_gitee(tag: str, body: str, files: slice): + release_id = "" + owner = "naibahq" + repo = "agent" + release_api_uri = f"https://gitee.com/api/v5/repos/{owner}/{repo}/releases" + api_client = requests.Session() + api_client.headers.update({ + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }) + + access_token = os.environ['GITEE_TOKEN'] + release_data = { + 'access_token': access_token, + 'tag_name': tag, + 'name': tag, + 'body': body, + 'prerelease': False, + 'target_commitish': 'main' + } + release_api_response = api_client.post(release_api_uri, json=release_data) + if release_api_response.status_code == 201: + release_info = release_api_response.json() + release_id = release_info.get('id') + else: + print( + f"Request failed with status code {release_api_response.status_code}") + + print(f"Gitee release id: {release_id}") + asset_api_uri = f"{release_api_uri}/{release_id}/attach_files" + + for file_path in files: + files = { + 'file': open(file_path, 'rb') + } + + asset_api_response = requests.post( + asset_api_uri, params={'access_token': access_token}, files=files) + + if asset_api_response.status_code == 201: + asset_info = asset_api_response.json() + asset_name = asset_info.get('name') + print(f"Successfully uploaded {asset_name}!") + else: + print( + f"Request failed with status code {asset_api_response.status_code}") + + api_client.close() + print("Sync is completed!") + + +def get_abs_path(path: str): + wd = os.getcwd() + return os.path.join(wd, path) + + +def compute_sha256(file: str): + sha256_hash = hashlib.sha256() + buf_size = 65536 + with open(file, 'rb') as f: + while True: + data = f.read(buf_size) + if not data: + break + sha256_hash.update(data) + return sha256_hash.hexdigest() + + +def verify_checksum(checksum_file: str): + with open(checksum_file, 'r') as f: + lines = f.readlines() + + for line in lines: + checksum, file = line.strip().split() + abs_path = get_abs_path(file) + computed_hash = compute_sha256(abs_path) + + if checksum == computed_hash: + print(f"{file}: OK") + else: + print(f"{file}: FAIL (expected {checksum}, got {computed_hash})") + print("Will run the download process again") + get_github_latest_release() + break + + +get_github_latest_release() diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml new file mode 100644 index 0000000..a6482ed --- /dev/null +++ b/.github/workflows/sync.yml @@ -0,0 +1,16 @@ +name: Sync Release to Gitee + +on: + workflow_dispatch: + +jobs: + sync-release-to-gitee: + runs-on: ubuntu-latest + env: + GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }} + steps: + - uses: actions/checkout@v4 + - name: Sync to Gitee + run: | + pip3 install PyGitHub + python3 .github/workflows/sync.py \ No newline at end of file