diff --git a/.github/actions/flutter-deps/action.yml b/.github/actions/flutter-deps/action.yml index 1edbca1713..741157e1fb 100644 --- a/.github/actions/flutter-deps/action.yml +++ b/.github/actions/flutter-deps/action.yml @@ -9,6 +9,7 @@ runs: # NB! Keep up-to-date with the flutter version used for development flutter-version: "3.29.2" channel: "stable" + cache: true - name: Prepare build directory shell: bash diff --git a/.github/scripts/roll_sdk_packages.sh b/.github/scripts/roll_sdk_packages.sh new file mode 100755 index 0000000000..0fc299982b --- /dev/null +++ b/.github/scripts/roll_sdk_packages.sh @@ -0,0 +1,272 @@ +#!/usr/bin/env bash + +# Script to roll SDK packages in all Flutter/Dart projects +# Designed to handle git-based dependencies and work with the GitHub CI workflow + +set -e + +# Configuration +# Set to "true" to upgrade all packages, "false" to only upgrade SDK packages +UPGRADE_ALL_PACKAGES=${UPGRADE_ALL_PACKAGES:-false} +# Branch to target for PR creation +TARGET_BRANCH=${TARGET_BRANCH:-"dev"} + +# Get the current date for branch naming and commit messages +CURRENT_DATE=$(date '+%Y-%m-%d') +REPO_ROOT=$(pwd) +CHANGES_FILE="$REPO_ROOT/SDK_CHANGELOG.md" + +# SDK packages to check +SDK_PACKAGES=( + "komodo_cex_market_data" + "komodo_coin_updates" + "komodo_coins" + "komodo_defi_framework" + "komodo_defi_local_auth" + "komodo_defi_remote" + "komodo_defi_rpc_methods" + "komodo_defi_sdk" + "komodo_defi_types" + "komodo_defi_workers" + "komodo_symbol_converter" + "komodo_ui" + "komodo_wallet_build_transformer" + "komodo_wallet_cli" +) + +# Extract version information from the pubspec.lock file +get_package_info_from_lock() { + local package_name=$1 + local lock_file=$2 + + # If pubspec.lock doesn't exist, return empty + if [ ! -f "$lock_file" ]; then + echo "" + return + fi + + # Extract the entire package section + local start_line=$(grep -n "^ $package_name:" "$lock_file" | cut -d: -f1) + if [ -z "$start_line" ]; then + echo "" + return + fi + + # Find the end line (next non-indented line or EOF) + local end_line=$(tail -n +$((start_line+1)) "$lock_file" | grep -n "^[^ ]" | head -1 | cut -d: -f1) + if [ -z "$end_line" ]; then + # If no end found, use end of file + end_line=$(wc -l < "$lock_file") + else + # Adjust for the offset from tail command + end_line=$((start_line + end_line)) + fi + + # Extract the entire section + local package_section=$(sed -n "${start_line},${end_line}p" "$lock_file" | sed '$d') + + if [ -n "$package_section" ]; then + # Get package info + local version=$(echo "$package_section" | grep " version:" | head -1 | sed 's/.*version: *"\([^"]*\)".*/\1/') + local source=$(echo "$package_section" | grep " source:" | head -1 | sed 's/.*source: *\([^ ]*\).*/\1/') + + # Extract git specific info if available + local git_url="" + local git_ref="" + + if echo "$package_section" | grep -q " url:"; then + git_url=$(echo "$package_section" | grep " url:" | head -1 | sed 's/.*url: *"\([^"]*\)".*/\1/') + fi + + if echo "$package_section" | grep -q " resolved-ref:"; then + git_ref=$(echo "$package_section" | grep " resolved-ref:" | head -1 | sed 's/.*resolved-ref: *"\([^"]*\)".*/\1/') + elif echo "$package_section" | grep -q " ref:"; then + git_ref=$(echo "$package_section" | grep " ref:" | head -1 | sed 's/.*ref: *\([^ ]*\).*/\1/') + fi + + # Format the output based on what we found + if [ -n "$git_url" ] && [ -n "$git_ref" ]; then + echo "version: \"$version\", source: $source, git: $git_url, ref: $git_ref" + else + echo "version: \"$version\", source: $source" + fi + else + echo "" + fi +} + +# Initialize changes file +echo "# SDK Package Rolls" > "$CHANGES_FILE" +echo "" >> "$CHANGES_FILE" +echo "**Date:** $CURRENT_DATE" >> "$CHANGES_FILE" +echo "**Target Branch:** $TARGET_BRANCH" >> "$CHANGES_FILE" +echo "**Upgrade Mode:** $([ "$UPGRADE_ALL_PACKAGES" = "true" ] && echo "All Packages" || echo "SDK Packages Only")" >> "$CHANGES_FILE" +echo "" >> "$CHANGES_FILE" +echo "The following SDK packages were rolled to newer versions:" >> "$CHANGES_FILE" +echo "" >> "$CHANGES_FILE" + +# Find all pubspec.yaml files +echo "Finding all pubspec.yaml files..." +PUBSPEC_FILES=$(find "$REPO_ROOT" -name "pubspec.yaml" -not -path "*/build/*" -not -path "*/\.*/*" -not -path "*/ios/*" -not -path "*/android/*") + +echo "Found $(echo "$PUBSPEC_FILES" | wc -l) pubspec.yaml files" + +ROLLS_MADE=false + +for PUBSPEC in $PUBSPEC_FILES; do + PROJECT_DIR=$(dirname "$PUBSPEC") + PROJECT_NAME=$(basename "$PROJECT_DIR") + + echo "Processing $PROJECT_NAME ($PROJECT_DIR)" + + cd "$PROJECT_DIR" + + # Check if any SDK package is listed as a dependency + CONTAINS_SDK_PACKAGE=false + SDK_PACKAGES_FOUND=() + + for PACKAGE in "${SDK_PACKAGES[@]}"; do + if grep -q -E "^\s+$PACKAGE:" "$PUBSPEC"; then + CONTAINS_SDK_PACKAGE=true + SDK_PACKAGES_FOUND+=("$PACKAGE") + fi + done + + if [ "$CONTAINS_SDK_PACKAGE" = true ]; then + echo "SDK packages found in $PROJECT_NAME: ${SDK_PACKAGES_FOUND[*]}" + + # Save hash of current pubspec.lock + if [ -f "pubspec.lock" ]; then + PRE_UPDATE_HASH=$(sha256sum pubspec.lock | awk '{print $1}') + else + PRE_UPDATE_HASH="" + fi + + # Backup current pubspec.lock + if [ -f "pubspec.lock" ]; then + cp pubspec.lock pubspec.lock.bak + fi + + # Get the current git refs/versions for SDK packages before update + SDK_PACKAGE_REFS_BEFORE=() + for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do + if grep -q "$PACKAGE:" "$PUBSPEC"; then + # Get the git reference line or version line + if grep -q -A 10 "$PACKAGE:" "$PUBSPEC" | grep -q "git:"; then + REF_LINE=$(grep -A 10 "$PACKAGE:" "$PUBSPEC" | grep -m 1 "ref:") + GIT_URL=$(grep -A 10 "$PACKAGE:" "$PUBSPEC" | grep -m 1 "git:") + if [ -n "$REF_LINE" ] && [ -n "$GIT_URL" ]; then + REF_VALUE=$(echo "$REF_LINE" | sed 's/.*ref: *\([^ ]*\).*/\1/') + GIT_VALUE=$(echo "$GIT_URL" | sed 's/.*git: *\([^ ]*\).*/\1/') + SDK_PACKAGE_REFS_BEFORE+=("$PACKAGE: git: $GIT_VALUE ref: $REF_VALUE") + fi + else + # If not git-based, get version + VERSION_LINE=$(grep -A 1 "$PACKAGE:" "$PUBSPEC" | tail -1) + if [ -n "$VERSION_LINE" ]; then + VERSION=$(echo "$VERSION_LINE" | sed 's/.*: *\([^ ]*\).*/\1/') + SDK_PACKAGE_REFS_BEFORE+=("$PACKAGE: version: $VERSION") + fi + fi + fi + done + + # Perform the update - based on configuration + if [ "$UPGRADE_ALL_PACKAGES" = "true" ]; then + echo "Running flutter pub upgrade --major-versions in $PROJECT_NAME (all packages)" + flutter pub upgrade --major-versions + else + echo "Running flutter pub upgrade for SDK packages only in $PROJECT_NAME" + # Upgrade only the SDK packages + for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do + echo "Upgrading $PACKAGE" + flutter pub upgrade "$PACKAGE" + done + fi + + # Check if the pubspec.lock was modified + if [ -f "pubspec.lock" ]; then + POST_UPDATE_HASH=$(sha256sum pubspec.lock | awk '{print $1}') + + if [ "$PRE_UPDATE_HASH" != "$POST_UPDATE_HASH" ]; then + echo "Changes detected in $PROJECT_NAME pubspec.lock" + ROLLS_MADE=true + + # Get information about packages from lock file before and after + if [ -f "pubspec.lock.bak" ]; then + LOCK_BEFORE="pubspec.lock.bak" + else + LOCK_BEFORE="" + fi + LOCK_AFTER="pubspec.lock" + + # Add the project to the changes list + echo "## $PROJECT_NAME" >> "$CHANGES_FILE" + echo "" >> "$CHANGES_FILE" + + # List the SDK packages that were rolled with detailed info + for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do + echo "- Rolled \`$PACKAGE\`" >> "$CHANGES_FILE" + + # Get before and after info + if [ -n "$LOCK_BEFORE" ]; then + BEFORE_INFO=$(get_package_info_from_lock "$PACKAGE" "$LOCK_BEFORE") + else + BEFORE_INFO="" + fi + AFTER_INFO=$(get_package_info_from_lock "$PACKAGE" "$LOCK_AFTER") + + # Add detailed information if available + if [ -n "$BEFORE_INFO" ] && [ -n "$AFTER_INFO" ] && [ "$BEFORE_INFO" != "$AFTER_INFO" ]; then + echo " - From: \`$BEFORE_INFO\`" >> "$CHANGES_FILE" + echo " - To: \`$AFTER_INFO\`" >> "$CHANGES_FILE" + elif [ -n "$AFTER_INFO" ]; then + echo " - Current: \`$AFTER_INFO\`" >> "$CHANGES_FILE" + fi + done + + echo "" >> "$CHANGES_FILE" + else + echo "No changes in $PROJECT_NAME pubspec.lock" + fi + else + echo "No pubspec.lock file generated for $PROJECT_NAME" + fi + else + echo "No SDK packages found in $PROJECT_NAME, skipping..." + fi + + cd "$REPO_ROOT" +done + +# Add the SDK rolls image at the bottom of the changes file +if [ "$ROLLS_MADE" = true ]; then + echo "![SDK Package Rolls](https://raw.githubusercontent.com/KomodoPlatform/komodo-wallet/aaf19e4605c62854ba176bf1ea75d75b3cb48df9/docs/assets/sdk-rolls.png)" >> "$CHANGES_FILE" + echo "" >> "$CHANGES_FILE" + + # Clean up all .bak files to avoid committing them + echo "Cleaning up backup files..." + find "$REPO_ROOT" -name "*.bak" -type f -delete +fi + +# Set output for GitHub Actions +if [ -n "${GITHUB_OUTPUT}" ]; then + if [ "$ROLLS_MADE" = true ]; then + echo "updates_found=true" >> $GITHUB_OUTPUT + echo "Rolls found and applied!" + exit 0 + else + echo "updates_found=false" >> $GITHUB_OUTPUT + echo "No rolls needed." + exit 1 + fi +else + # When running outside of GitHub Actions + if [ "$ROLLS_MADE" = true ]; then + echo "Rolls found and applied! See $CHANGES_FILE for details." + exit 0 + else + echo "No rolls needed." + exit 1 + fi +fi diff --git a/.github/workflows/roll-sdk-packages.yml b/.github/workflows/roll-sdk-packages.yml new file mode 100644 index 0000000000..d904559fe2 --- /dev/null +++ b/.github/workflows/roll-sdk-packages.yml @@ -0,0 +1,147 @@ +name: Roll SDK Packages + +on: + schedule: + # Run once a day at midnight + - cron: "0 0 * * *" + push: + branches: + - dev + pull_request: + branches: + - dev + # Allow manual trigger + workflow_dispatch: + inputs: + upgrade_all_packages: + description: "Upgrade all packages, not just SDK packages" + required: false + default: false + type: boolean + target_branch: + description: "Target branch for PR creation" + required: false + default: "dev" + type: string + +jobs: + roll-sdk-packages: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup Flutter + uses: subosito/flutter-action@v2 + with: + # NB! Keep up-to-date with the flutter version used for development + flutter-version: "3.29.2" + channel: "stable" + cache: true + + - name: Determine configuration + id: config + run: | + # Set target branch + if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.target_branch }}" != "" ]]; then + TARGET_BRANCH="${{ github.event.inputs.target_branch }}" + elif [[ "${{ github.event_name }}" == "pull_request" ]]; then + TARGET_BRANCH="${{ github.base_ref }}" + else + TARGET_BRANCH="dev" + fi + echo "TARGET_BRANCH=$TARGET_BRANCH" >> $GITHUB_ENV + + # Set upgrade mode + if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.upgrade_all_packages }}" == "true" ]]; then + UPGRADE_ALL="true" + else + UPGRADE_ALL="false" + fi + echo "UPGRADE_ALL=$UPGRADE_ALL" >> $GITHUB_ENV + + # Branch naming based on target branch, not date + echo "PR_BRANCH_NAME=sdk-roll-${TARGET_BRANCH}" >> $GITHUB_ENV + + - name: Make roll script executable + run: | + chmod +x .github/scripts/roll_sdk_packages.sh + + - name: Run roll script + id: roll_packages + run: | + UPGRADE_ALL_PACKAGES=${{ env.UPGRADE_ALL }} TARGET_BRANCH=${{ env.TARGET_BRANCH }} .github/scripts/roll_sdk_packages.sh || echo "No rolls needed" + if [ -f "SDK_CHANGELOG.md" ]; then + echo "ROLLS_FOUND=true" >> $GITHUB_ENV + else + echo "ROLLS_FOUND=false" >> $GITHUB_ENV + fi + + - name: Setup Git identity + if: env.ROLLS_FOUND == 'true' + run: | + git config --global user.name "GitHub Action Bot" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + + - name: Create PR Branch + if: env.ROLLS_FOUND == 'true' + run: | + # Check if the branch already exists + if git ls-remote --heads origin ${{ env.PR_BRANCH_NAME }} | grep -q ${{ env.PR_BRANCH_NAME }}; then + # If it exists, delete it (force update) + git push origin --delete ${{ env.PR_BRANCH_NAME }} + fi + + # Create new branch + git checkout -b "${{ env.PR_BRANCH_NAME }}" + + # Add all changed pubspec files in a single command + git add '**/pubspec.yaml' '**/pubspec.lock' + + # Confirm which files will be committed + git status + + git commit -m "chore: roll SDK packages targeting ${{ env.TARGET_BRANCH }}" + git push --set-upstream origin "${{ env.PR_BRANCH_NAME }}" + + - name: Create Pull Request + if: env.ROLLS_FOUND == 'true' + run: | + # Check if a PR from this branch to target branch already exists + EXISTING_PR=$(gh pr list --head "${{ env.PR_BRANCH_NAME }}" --base "${{ env.TARGET_BRANCH }}" --json number --jq '.[0].number') + + if [ -n "$EXISTING_PR" ]; then + echo "Updating existing PR #$EXISTING_PR with new changes" + # Update the PR body with the latest SDK roll changes + gh pr edit "$EXISTING_PR" --body-file SDK_CHANGELOG.md + # Add a comment to notify about the update + gh pr comment "$EXISTING_PR" --body "Updated SDK roll with new changes on $(date '+%Y-%m-%d %H:%M:%S')" + else + # Create the pull request using gh CLI + gh pr create \ + --title "ci(sdk): Roll SDK packages targeting ${{ env.TARGET_BRANCH }}" \ + --body-file SDK_CHANGELOG.md \ + --base "${{ env.TARGET_BRANCH }}" \ + --head "${{ env.PR_BRANCH_NAME }}" \ + --label "dependencies" \ + --label "automated" + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: PR Details + if: env.ROLLS_FOUND == 'true' + run: | + echo "Pull request created or updated successfully with rolled SDK package dependencies" + + - name: No Updates + if: env.ROLLS_FOUND == 'false' + run: | + echo "No SDK package rolls needed"