pressbooks / composer-autoupdate-bedrock

Auto updater action 🤖
0 stars 0 forks source link

Improve composer autoupdate behaviour #73

Closed arzola closed 8 months ago

arzola commented 9 months ago

Our current plugins and themes have a GitHub action step setup after running tests and all the build process that looks like the following:

  - name: Trigger Bedrock Update
      if: (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/production')  && matrix.experimental == false
      uses: pressbooks/composer-autoupdate-bedrock@v2
      with:
        triggered-by: ${{ github.repository }}
        token: ${{ secrets.PAT_COMPOSER_UPDATE }}
        branch: ${{ github.ref }}

Our current implementation has a couple of things that should be improved.

  1. Plugins/Themes that trigger a bedrock update needs to only trigger the bedrocks that contains a reference of the plugin that is triggering the update, right now It's triggering a GENERAL update in all registered bedrocks

The following list of bedrocks is hardcoded in the composer-autoupdate-action repo

const reposToDispatchComposerUpdate = [
    'pressbooksedu-golden-bedrock',
    'pressbookspublic-bedrock',
    'pressbookspub-bedrock'
];

See #72 for a related issue.

There are different ways to solve that for example

  1. Send the bedrocks to update as a parameter in each plugin/theme (also this could be a reusable step that can be shared by different associated repos)
  - name: Trigger Bedrock Update
      if: (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/production')  && matrix.experimental == false
      uses: pressbooks/composer-autoupdate-bedrock@v2
      with:
        triggered-by: ${{ github.repository }}
        token: ${{ secrets.PAT_COMPOSER_UPDATE }}
        branch: ${{ github.ref }}
        bedrocks: ['golden','public','etc']
  1. Fetch all the bedrocks composer.json files while updating and doing a quick lookup to see if the triggering repo contains the triggering plugin/theme and update if true
ho-man-chan commented 8 months ago

Update:

ho-man-chan commented 8 months ago

Update:

ho-man-chan commented 8 months ago

Update:

Accessing parameters

echo "First parameter: $1"

You can also store parameters in more descriptive variable names

branch_starting=$1 branch_new=$2

branch_starting="dev" branch_new="chore/bot/update-autoupdate-workflow-to-main"

Define the organization from which to fetch repositories

ORGANIZATION="pressbooks"

Define the directory where repositories should be cloned

WORKDIR="./repositories"

mkdir -p "$WORKDIR"

cd "$WORKDIR"

Fetch all repository names from the organization and clone them

gh repo list "$ORGANIZATION" --limit 100 --json nameWithOwner,isArchived --jq '.[] | select(.isArchived == false) | .nameWithOwner' | while read repo; do

git clone "git@github.com:$repo.git"

done

repos=("pressbooks-book")

repos=("pressbooks" "pressbooks-book" "pressbooks-adunis")

Loop over all cloned directories

for repo in "${repos[@]}" ; do

for repo in */ ; do

echo "Processing repository: $repo"
cd "$repo"

# git branch --list $branch
# if [ $? -eq 0 ]; then
#   echo "Branch exists locally."
#   break;
# fi

if git diff-index --quiet HEAD --; then
  echo "No changes in the repository."
else
  echo "There are uncommitted changes in the repository."
  git stash;
  # break;
fi

# reset starting branch
git fetch origin
git checkout $branch_starting
git pull
git reset --hard origin/$branch_starting

current_branch=$(git rev-parse --abbrev-ref HEAD)

if [ "$current_branch" = $branch_starting ]; then

git checkout -b $branch_new

# Find and replace the target strings in all files within the repository
grep -rl 'pressbooks/composer-autoupdate-bedrock@v1' . | xargs sed -i '' 's|pressbooks/composer-autoupdate-bedrock@v1|pressbooks/composer-autoupdate-bedrock@main|g'
grep -rl 'pressbooks/composer-autoupdate-bedrock@v2' . | xargs sed -i '' 's|pressbooks/composer-autoupdate-bedrock@v2|pressbooks/composer-autoupdate-bedrock@main|g'

# Check for any changes in the repository and list them
echo "Listing modified, added, or deleted files:"
git status --porcelain | while read -r status file; do
    echo "${status} ${file}"
    git diff;
done

  # Check if there are any changes to commit
  if [ -n "$(git status --porcelain)" ]; then
      echo 'diff found'
      echo 'About to start creating branch, commit and push PR'

      read -p "Do you want to proceed? (y/n) " answer
      # Normalize the answer to lowercase
      answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')

      # Check the user's answer
      if [ "$answer" != "y" ] && [ "$answer" != "yes" ]; then
        echo "Aborting."
        skip;
      fi

      echo "Proceeding..."

      # git config user.email "you@example.com"
      # git config user.name "Your Name"

      #Commit the changes
      git add .
      git commit -m 'chore: update workflows that uses versioned pressbooks/composer-autoupdate-bedrock to use main branch to simplify releases.'

      # Push the branch to the remote
      git push origin $branch_new

      # Create a pull request using the GitHub CLI targeting the dev branch
      gh pr create --title "chore: update workflow to use main branch for composer-autoupdate-bedrock" \
                   --body "Resolves: pressbooks/composer-autoupdate-bedrock/issues/73" \
                   --base "$branch_starting" \
                   --head "$branch_new"
  else
      echo "No changes found in $repo."
  fi
fi

#clean up
git checkout $branch_starting
git branch -D $branch_new

# Go back to the parent directory
cd ..

done