From 217e0b0375aa5b02a6915a467edf129a7bc31e9a Mon Sep 17 00:00:00 2001 From: Tony Wang Date: Sun, 21 Dec 2025 21:46:36 -0800 Subject: [PATCH] Enhance sync-extension workflow for better asset management Refactor GitHub Actions workflow to improve release asset handling and error checking. Update the method for downloading and verifying extension files. --- .github/workflows/sync-extension.yml | 272 +++++++-------------------- 1 file changed, 67 insertions(+), 205 deletions(-) diff --git a/.github/workflows/sync-extension.yml b/.github/workflows/sync-extension.yml index d665c968..3e610b07 100644 --- a/.github/workflows/sync-extension.yml +++ b/.github/workflows/sync-extension.yml @@ -41,17 +41,24 @@ jobs: TAG="${{ github.event.client_payload.release_tag }}" else # Scheduled check - get latest release - TAG=$(curl -s https://api.github.com/repos/${{ secrets.SENTIENCE_CHROME_REPO }}/releases/latest | jq -r '.tag_name // empty') + TAG=$(curl -s -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ + "https://api.github.com/repos/${{ secrets.SENTIENCE_CHROME_REPO }}/releases/latest" | jq -r '.tag_name // empty') + + # Check if we already processed this tag (check if branch exists) + if git ls-remote --exit-code --heads origin "sync-extension-$TAG"; then + echo "Branch for $TAG already exists, skipping." + echo "skip=true" >> $GITHUB_OUTPUT + exit 0 + fi fi - if [ -z "$TAG" ] || [ "$TAG" == "null" ]; then - echo "No release found, skipping" - echo "skip=true" >> $GITHUB_OUTPUT - exit 0 + if [ -z "$TAG" ]; then + echo "Could not determine release tag." + exit 1 fi + echo "Syncing tag: $TAG" echo "tag=$TAG" >> $GITHUB_OUTPUT - echo "Release tag: $TAG" - name: Download extension files if: steps.release.outputs.skip != 'true' @@ -59,229 +66,86 @@ jobs: TAG="${{ steps.release.outputs.tag }}" REPO="${{ secrets.SENTIENCE_CHROME_REPO }}" - # Download release assets + # Setup temp directory mkdir -p extension-temp cd extension-temp - # Download individual files from release (reliable method - no zip) - echo "๐Ÿ“ Downloading individual files from release..." - echo "Available release assets:" - curl -s -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ - "https://api.github.com/repos/$REPO/releases/tags/$TAG" | \ - jq -r '.assets[] | select(.name | endswith(".js") or endswith(".wasm") or endswith(".json") or endswith(".d.ts")) | .name' || true + echo "โฌ‡๏ธ Fetching release assets for $TAG from $REPO..." - curl -L -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ + # 1. Get the URL for 'extension-files.tar.gz' specifically + # We query the release assets API and filter by name + ASSET_URL=$(curl -s -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ "https://api.github.com/repos/$REPO/releases/tags/$TAG" | \ - jq -r '.assets[] | select(.name | endswith(".js") or endswith(".wasm") or endswith(".json") or endswith(".d.ts")) | "\(.browser_download_url)|\(.name)"' | \ - while IFS='|' read -r url name; do - if [ -n "$url" ] && [ "$url" != "null" ] && [ -n "$name" ]; then - # Handle asset names that might have paths like "pkg/sentience_core.js" or "extension-package/manifest.json" - # GitHub releases might preserve directory structure in asset names - # Strip "extension-package/" prefix if present, as we'll handle it in copy step - if [[ "$name" == extension-package/* ]]; then - # Asset name is "extension-package/manifest.json" - strip prefix - filename="${name#extension-package/}" - dir=$(dirname "$filename") - if [ "$dir" != "." ]; then - mkdir -p "$dir" - fi - echo " Downloading $name -> $filename" - curl -L -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" "$url" -o "$filename" - elif [[ "$name" == pkg/* ]]; then - # Asset name is "pkg/sentience_core.js" - create pkg directory - mkdir -p pkg - filename=$(basename "$name") - echo " Downloading $name -> pkg/$filename" - curl -L -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" "$url" -o "pkg/$filename" - else - # Asset name is just "manifest.json" - put at root - dir=$(dirname "$name") - if [ "$dir" != "." ]; then - mkdir -p "$dir" - fi - echo " Downloading $name" - curl -L -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" "$url" -o "$name" - fi - fi - done - - # Verify downloaded files - echo "๐Ÿ“‹ Downloaded files structure:" - find . -type f -name "*.js" -o -name "*.wasm" -o -name "*.json" | sort - echo "" - echo "Directory structure:" - ls -laR . | head -50 - echo "" - echo "๐Ÿ” Verifying critical files:" - if [ -f "manifest.json" ]; then - echo "โœ… manifest.json found ($(wc -c < manifest.json) bytes)" - head -5 manifest.json - else - echo "โŒ manifest.json NOT FOUND" - fi - if [ -d "pkg" ]; then - echo "โœ… pkg directory found with $(ls -1 pkg | wc -l) files" - else - echo "โŒ pkg directory NOT FOUND" - fi - - - name: Copy extension files - if: steps.release.outputs.skip != 'true' - run: | - # Create extension directory structure - mkdir -p src/extension/pkg - - # Copy extension files (handle both root and extension-package/ subdirectory) - # Check root first, then extension-package/ subdirectory - if [ -f "extension-temp/manifest.json" ]; then - size=$(wc -c < extension-temp/manifest.json) - if [ "$size" -gt 0 ]; then - echo "โœ… Copying manifest.json ($size bytes)" - cp extension-temp/manifest.json src/extension/ - # Verify copy - if [ -f "src/extension/manifest.json" ] && [ "$(wc -c < src/extension/manifest.json)" -gt 0 ]; then - echo "โœ… manifest.json copied successfully" - else - echo "โŒ manifest.json copy failed or file is empty" - exit 1 - fi - else - echo "โŒ manifest.json is empty ($size bytes)" - exit 1 - fi - elif [ -f "extension-temp/extension-package/manifest.json" ]; then - size=$(wc -c < extension-temp/extension-package/manifest.json) - if [ "$size" -gt 0 ]; then - echo "โœ… Copying manifest.json from extension-package/ ($size bytes)" - cp extension-temp/extension-package/manifest.json src/extension/ - # Verify copy - if [ -f "src/extension/manifest.json" ] && [ "$(wc -c < src/extension/manifest.json)" -gt 0 ]; then - echo "โœ… manifest.json copied successfully" - else - echo "โŒ manifest.json copy failed or file is empty" - exit 1 - fi - else - echo "โŒ manifest.json is empty ($size bytes)" - exit 1 - fi - else - echo "โŒ manifest.json not found in extension-temp/" - echo "Available files:" - find extension-temp -type f | head -20 + jq -r '.assets[] | select(.name == "extension-files.tar.gz") | .browser_download_url') + + if [ -z "$ASSET_URL" ] || [ "$ASSET_URL" == "null" ]; then + echo "โŒ Critical Error: extension-files.tar.gz not found in release assets!" + echo "Debug: Listing available assets..." + curl -s -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ + "https://api.github.com/repos/$REPO/releases/tags/$TAG" | jq -r '.assets[].name' exit 1 fi + + # 2. Download the tarball + echo "๐Ÿ“ฆ Downloading tarball from $ASSET_URL..." + curl -L -H "Authorization: token ${{ secrets.SENTIENCE_CHROME_TOKEN }}" \ + -H "Accept: application/octet-stream" \ + "$ASSET_URL" -o extension.tar.gz + + # 3. Extract it + echo "๐Ÿ“‚ Extracting..." + tar -xzf extension.tar.gz + rm extension.tar.gz - if [ -f "extension-temp/content.js" ]; then - cp extension-temp/content.js src/extension/ - elif [ -f "extension-temp/extension-package/content.js" ]; then - cp extension-temp/extension-package/content.js src/extension/ - else - echo "โš ๏ธ content.js not found" - fi - - if [ -f "extension-temp/background.js" ]; then - cp extension-temp/background.js src/extension/ - elif [ -f "extension-temp/extension-package/background.js" ]; then - cp extension-temp/extension-package/background.js src/extension/ - else - echo "โš ๏ธ background.js not found" - fi + # 4. Verify extraction + echo "โœ… Extraction complete. Contents:" + ls -la - if [ -f "extension-temp/injected_api.js" ]; then - cp extension-temp/injected_api.js src/extension/ - elif [ -f "extension-temp/extension-package/injected_api.js" ]; then - cp extension-temp/extension-package/injected_api.js src/extension/ - else - echo "โš ๏ธ injected_api.js not found" + if [ ! -f "manifest.json" ]; then + echo "โŒ Error: manifest.json missing after extraction" + exit 1 fi - # Copy WASM files - try multiple locations and patterns - echo "๐Ÿ” Searching for pkg directory and WASM files..." + - name: Update extension files + if: steps.release.outputs.skip != 'true' + run: | + # Target directory in sdk-ts + TARGET_DIR="sentience-chrome" - # Check all possible locations - if [ -d "extension-temp/pkg" ]; then - echo "โœ… Found pkg directory at extension-temp/pkg" - cp -r extension-temp/pkg/* src/extension/pkg/ 2>/dev/null || true - elif [ -d "extension-temp/extension-package/pkg" ]; then - echo "โœ… Found pkg directory at extension-temp/extension-package/pkg" - cp -r extension-temp/extension-package/pkg/* src/extension/pkg/ 2>/dev/null || true - else - echo "โš ๏ธ pkg directory not found, searching for individual files..." - - # Search for files in various locations - find extension-temp -name "sentience_core.js" -type f | while read file; do - echo " Found: $file" - cp "$file" src/extension/pkg/ 2>/dev/null || true - done - - find extension-temp -name "sentience_core_bg.wasm" -type f | while read file; do - echo " Found: $file" - cp "$file" src/extension/pkg/ 2>/dev/null || true - done - - find extension-temp -name "*.d.ts" -type f | while read file; do - echo " Found: $file" - cp "$file" src/extension/pkg/ 2>/dev/null || true - done - fi + # Ensure target directory exists and is clean + rm -rf "$TARGET_DIR" + mkdir -p "$TARGET_DIR" - # Verify copied files - echo "๐Ÿ“‹ Copied files:" - echo "Extension root:" - ls -la src/extension/ || echo "โš ๏ธ Extension directory empty" - echo "" - echo "WASM files (pkg directory):" - if [ -d "src/extension/pkg" ]; then - ls -la src/extension/pkg/ || echo "โš ๏ธ pkg directory empty" - else - echo "โŒ ERROR: pkg directory not created!" - exit 1 - fi + # Copy files from temp directory + cp -r extension-temp/* "$TARGET_DIR/" - # Verify required files exist - if [ ! -f "src/extension/pkg/sentience_core.js" ]; then - echo "โŒ ERROR: sentience_core.js not found!" - exit 1 - fi - if [ ! -f "src/extension/pkg/sentience_core_bg.wasm" ]; then - echo "โŒ ERROR: sentience_core_bg.wasm not found!" + # Verify copy + if [ ! -f "$TARGET_DIR/manifest.json" ]; then + echo "โŒ Failed to copy manifest.json to $TARGET_DIR" exit 1 fi - echo "โœ… All required WASM files verified" - # Clean up temporary directory - cd .. + # Cleanup rm -rf extension-temp - echo "๐Ÿงน Cleaned up extension-temp directory" + + echo "โœ… Extension files updated in $TARGET_DIR" + ls -la "$TARGET_DIR" - name: Check for changes if: steps.release.outputs.skip != 'true' id: changes run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - - # Show what files exist before adding - echo "๐Ÿ“‹ Files in src/extension before git add:" - find src/extension -type f | sort || echo "No files found" - - # Add all files including binary files - # Use -f to force add in case files are in .gitignore - git add -f src/extension/ || true + git add sentience-chrome/ - # Show what was staged - echo "๐Ÿ“‹ Staged files:" - git diff --staged --name-only || echo "No staged files" - - # Check if there are actual changes + # Check if anything actually changed if git diff --staged --quiet; then + echo "No changes detected." echo "changed=false" >> $GITHUB_OUTPUT - echo "No changes detected" else + echo "Changes detected." echo "changed=true" >> $GITHUB_OUTPUT - echo "Changes detected" - # Show file sizes to verify binary files are included + + # Show staged files to verify binary files are included echo "๐Ÿ“Š Staged file sizes:" git diff --staged --name-only | while read file; do if [ -f "$file" ]; then @@ -295,9 +159,8 @@ jobs: if: steps.release.outputs.skip != 'true' && steps.changes.outputs.changed == 'true' uses: peter-evans/create-pull-request@v5 with: - # Use PR_TOKEN if available (for repos with org restrictions), otherwise use GITHUB_TOKEN - # To use PAT: create secret named PR_TOKEN with a Personal Access Token that has 'repo' scope - token: ${{ secrets.PR_TOKEN }} + # Use PR_TOKEN if available, otherwise GITHUB_TOKEN + token: ${{ secrets.PR_TOKEN || secrets.GITHUB_TOKEN }} commit-message: "chore: sync extension files from sentience-chrome ${{ steps.release.outputs.tag }}" title: "Sync Extension: ${{ steps.release.outputs.tag }}" body: | @@ -313,4 +176,3 @@ jobs: labels: | automated extension-sync -