From fc04ab4e40977bafa1b821c77e38554d287f69bc Mon Sep 17 00:00:00 2001 From: welpo Date: Tue, 24 Sep 2024 16:18:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20misc(deps):=20automate=20KaTeX/m?= =?UTF-8?q?ermaid=20upgrades?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/upgrade-deps.yml | 74 ++++++++++++++++++++++++++++++ scripts/upgrade-deps | 25 ++++++---- 2 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/upgrade-deps.yml diff --git a/.github/workflows/upgrade-deps.yml b/.github/workflows/upgrade-deps.yml new file mode 100644 index 0000000..06d74ed --- /dev/null +++ b/.github/workflows/upgrade-deps.yml @@ -0,0 +1,74 @@ +name: Dependency upgrade + +on: + workflow_dispatch: + inputs: + dependency: + description: 'Dependency to upgrade' + required: true + type: choice + options: + - all + - mermaid + - katex + schedule: + - cron: '32 4 * * *' + +jobs: + upgrade-dependency: + name: Upgrade dependency + runs-on: ubuntu-22.04 + permissions: + contents: write + pull-requests: write + strategy: + matrix: + dependency: ${{ github.event_name == 'schedule' && fromJson('["mermaid", "katex"]') || fromJson(format('["{0}"]', github.event.inputs.dependency)) }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up environment + run: | + sudo apt-get update + sudo apt-get install -y jq npm curl git + npm install uglify-js -g + uglifyjs --version + + - name: Configure GPG key + run: | + echo -n ${{ secrets.GPG_PRIVATE_KEY }} | base64 --decode | gpg --import + + - name: Configure Git + run: | + git config --global user.signingkey 33EACFE956484C3940BFEEDCE4EC28F8DFB57474 + git config --global commit.gpgsign true + git config --global user.name "welpo" + git config --global user.email "welpo@users.noreply.github.com" + + - name: Create and switch to new branch + run: | + git checkout -b deps/upgrade-${{ matrix.dependency }} + + - name: Run upgrade script + shell: bash + run: | + if [[ "${{ matrix.dependency }}" == "all" ]]; then + bash scripts/upgrade-deps --all + else + bash scripts/upgrade-deps --${{ matrix.dependency }} + fi + + - name: Push changes and create PR + shell: bash + run: | + if git diff --quiet HEAD origin/main; then + echo "No changes to push for ${{ matrix.dependency }}" + exit 0 + fi + git push -u origin deps/upgrade-${{ matrix.dependency }} + gh pr create --fill --base main --head deps/upgrade-${{ matrix.dependency }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/scripts/upgrade-deps b/scripts/upgrade-deps index 01f4b53..d95a9e4 100755 --- a/scripts/upgrade-deps +++ b/scripts/upgrade-deps @@ -361,17 +361,22 @@ main() { echo "Updating local repository…" git fetch origin - local git_status - git_status=$(git status -uno) - if echo "$git_status" | grep -q "Your branch is behind"; then - exit_with_message "Your local branch is behind the remote. Pull the latest changes before running this script." - elif echo "$git_status" | grep -q "Your branch is ahead"; then - echo "Your local branch is ahead of the remote. Checking if local changes can be pushed…" - if ! git push --dry-run &> /dev/null; then - exit_with_message "Unable to push local changes. Resolve any conflicts before running this script." + current_branch=$(git rev-parse --abbrev-ref HEAD) + echo "Current branch: $current_branch" + # Check if the branch exists on the remote + if git ls-remote --exit-code --heads origin "$current_branch" >/dev/null 2>&1; then + # Branch exists on remote, compare with local. + local_commit=$(git rev-parse HEAD) + remote_commit=$(git rev-parse origin/"$current_branch") + if [ "$local_commit" = "$remote_commit" ]; then + echo "Branch is up to date with origin/$current_branch" + elif git merge-base --is-ancestor "$remote_commit" "$local_commit"; then + echo "Local branch is ahead of origin/$current_branch" + else + exit_with_message "Your local branch is behind origin/$current_branch. Pull the latest changes before running this script." fi - elif ! echo "$git_status" | grep -q "Your branch is up to date"; then - exit_with_message "Unable to determine if branch is up to date. Check your git status manually." + else + echo "Branch $current_branch does not exist on remote. Assuming it's a new branch." fi echo "Local repository is ready."