From 329f6232a4ae064f8b60346f22731dac8e8ae6ad Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Wed, 21 May 2025 13:57:00 +0900 Subject: [PATCH 1/3] feat(ci): auto sync with upstream --- .github/workflows/sync.yml | 41 +++++++++++++++++++++++++++++++++ update.sh | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .github/workflows/sync.yml create mode 100755 update.sh diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml new file mode 100644 index 0000000..2969264 --- /dev/null +++ b/.github/workflows/sync.yml @@ -0,0 +1,41 @@ +name: Sync scriptlets + +on: + workflow_dispatch: + schedule: + - cron: '0 20 * * 1,5' + +jobs: + sync: + name: Update scriptlets + runs-on: ubuntu-latest + timeout-minutes: 10 + if: github.repository_owner == 'ghostery' + steps: + - uses: actions/checkout@v4 + - uses: jdx/mise-action@v2 + + - name: Sync with upstream + run: ./update.sh + + - uses: tibdex/github-app-token@v + id: generate-token + with: + app_id: ${{ secrets.ADBLOCKERBOT_APP_ID }} + private_key: ${{ secrets.ADBLOCKERBOT_PRIVATE_KEY }} + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v5 + with: + token: ${{ steps.generate-token.outputs.token }} + committer: Ghostery Adblocker Bot + author: Ghostery Adblocker Bot + commit-message: "Update scriptlets" + title: "Sync scriptlets with upstream" + body: "Automated update of scriptlets." + branch: sync-scriptlets + + - name: Check outputs + run: | + echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}" + echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}" diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..9964601 --- /dev/null +++ b/update.sh @@ -0,0 +1,46 @@ +#!/bin/bash +set -xe + +function gh_repo_tags() { + # REPO_REF="owner/repo" + local REPO_REF="$1" + # https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags + curl -sL \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$REPO_REF/tags" +} + +function gh_repo_latest_tagname() { + # REPO_REF="owner/repo" + local REPO_REF="$1" + gh_repo_tags "$REPO_REF" | \ + jq -r '.[].name | select(test("^[0-9]"))' | \ + sort -Vr | \ + head -1 +} + +function get_local_tagname() { + # deno build.ts --tagName 1.64.0 > ubo.js + cat 'package.json' | sed -n -E 's/.*--tagName ([0-9\.a-zA-Z]+).*/\1/p' +} + +function update() { + local NEW_TAGNAME="$1" + cat 'package.json' | sed -E "s/(--tagName )([0-9\.a-zA-Z]+)/\1$NEW_TAGNAME/" | tee 'package.json' + deno run build +} + +function entrypoint() { + local LOCAL_REF="$(get_local_tagname)" + local UPSTREAM_REF="$(gh_repo_latest_tagname 'gorhill/ublock')" + if [[ "$UPSTREAM_REF" == 'null' ]]; then + echo "ERROR: Failed to retrieve upstream version! Run with 'set -xe' for debugging." + exit 1 + fi + if [[ "$(echo "$LOCAL_REF\n$UPSTREAM_REF" | sort -Vr | head -1)" != "$LOCAL_REF" ]]; then + update "$UPSTREAM_REF" + fi +} + +entrypoint From 6f3c2485f8dd7bb89bf73cd105318507e015829a Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Wed, 21 May 2025 17:11:31 +0900 Subject: [PATCH 2/3] refactor: inline script to actions --- .github/workflows/sync.yml | 37 +++++++++++++++++++++++++++++- update.sh | 46 -------------------------------------- 2 files changed, 36 insertions(+), 47 deletions(-) delete mode 100755 update.sh diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index 2969264..139f2d8 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -16,7 +16,42 @@ jobs: - uses: jdx/mise-action@v2 - name: Sync with upstream - run: ./update.sh + run: | + function gh_repo_tags() { + # REPO_REF="owner/repo" + local REPO_REF="$1" + # https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags + curl -sL \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/$REPO_REF/tags" + } + + function gh_repo_latest_tagname() { + # REPO_REF="owner/repo" + local REPO_REF="$1" + gh_repo_tags "$REPO_REF" | \ + jq -r '.[].name | select(test("^[0-9]"))' | \ + sort -Vr | \ + head -1 + } + + function get_local_tagname() { + # deno build.ts --tagName 1.64.0 > ubo.js + cat 'package.json' | sed -n -E 's/.*--tagName ([0-9\.a-zA-Z]+).*/\1/p' + } + + LOCAL_REF="$(get_local_tagname)" + UPSTREAM_REF="$(gh_repo_latest_tagname 'gorhill/ublock')" + if [[ "$UPSTREAM_REF" == 'null' ]]; then + echo "ERROR: Failed to retrieve upstream version! Run with 'set -xe' for debugging." + exit 1 + fi + if [[ "$(echo "$LOCAL_REF\n$UPSTREAM_REF" | sort -Vr | head -1)" != "$LOCAL_REF" ]]; then + NEW_TAGNAME="$1" + cat 'package.json' | sed -E "s/(--tagName )([0-9\.a-zA-Z]+)/\1$NEW_TAGNAME/" | tee 'package.json' + deno run build + fi - uses: tibdex/github-app-token@v id: generate-token diff --git a/update.sh b/update.sh deleted file mode 100755 index 9964601..0000000 --- a/update.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -set -xe - -function gh_repo_tags() { - # REPO_REF="owner/repo" - local REPO_REF="$1" - # https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags - curl -sL \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$REPO_REF/tags" -} - -function gh_repo_latest_tagname() { - # REPO_REF="owner/repo" - local REPO_REF="$1" - gh_repo_tags "$REPO_REF" | \ - jq -r '.[].name | select(test("^[0-9]"))' | \ - sort -Vr | \ - head -1 -} - -function get_local_tagname() { - # deno build.ts --tagName 1.64.0 > ubo.js - cat 'package.json' | sed -n -E 's/.*--tagName ([0-9\.a-zA-Z]+).*/\1/p' -} - -function update() { - local NEW_TAGNAME="$1" - cat 'package.json' | sed -E "s/(--tagName )([0-9\.a-zA-Z]+)/\1$NEW_TAGNAME/" | tee 'package.json' - deno run build -} - -function entrypoint() { - local LOCAL_REF="$(get_local_tagname)" - local UPSTREAM_REF="$(gh_repo_latest_tagname 'gorhill/ublock')" - if [[ "$UPSTREAM_REF" == 'null' ]]; then - echo "ERROR: Failed to retrieve upstream version! Run with 'set -xe' for debugging." - exit 1 - fi - if [[ "$(echo "$LOCAL_REF\n$UPSTREAM_REF" | sort -Vr | head -1)" != "$LOCAL_REF" ]]; then - update "$UPSTREAM_REF" - fi -} - -entrypoint From 01df394aac367c05b60069da580a4f5771c5286a Mon Sep 17 00:00:00 2001 From: HoJeong Go Date: Thu, 22 May 2025 02:23:40 +0900 Subject: [PATCH 3/3] refactor: simplify bash script - Use jq to fully extract the name (+ sort) - Inline function bodies - Simplify comparison between old and new versions --- .github/workflows/sync.yml | 43 +++++++++++--------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index 139f2d8..a2f7fc3 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -17,39 +17,20 @@ jobs: - name: Sync with upstream run: | - function gh_repo_tags() { - # REPO_REF="owner/repo" - local REPO_REF="$1" - # https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags - curl -sL \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/$REPO_REF/tags" - } - - function gh_repo_latest_tagname() { - # REPO_REF="owner/repo" - local REPO_REF="$1" - gh_repo_tags "$REPO_REF" | \ - jq -r '.[].name | select(test("^[0-9]"))' | \ - sort -Vr | \ - head -1 - } - - function get_local_tagname() { - # deno build.ts --tagName 1.64.0 > ubo.js - cat 'package.json' | sed -n -E 's/.*--tagName ([0-9\.a-zA-Z]+).*/\1/p' - } - - LOCAL_REF="$(get_local_tagname)" - UPSTREAM_REF="$(gh_repo_latest_tagname 'gorhill/ublock')" - if [[ "$UPSTREAM_REF" == 'null' ]]; then - echo "ERROR: Failed to retrieve upstream version! Run with 'set -xe' for debugging." + #!/usr/bin/env bash + set -euo pipefail + UPSTREAM_TAG="$(curl -sL \ + -H 'Accept: application/vnd.github+json' \ + -H 'X-GitHub-Api-Version: 2022-11-28' \ + 'https://api.github.com/repos/gorhill/ublock/tags' | \ + jq -r '[.[].name | select(test("^[0-9]"))] | sort_by(.) | last')" + if [[ "$UPSTREAM_TAG" == 'null' ]]; then + echo "ERROR: Failed to retrieve upstream version!" exit 1 fi - if [[ "$(echo "$LOCAL_REF\n$UPSTREAM_REF" | sort -Vr | head -1)" != "$LOCAL_REF" ]]; then - NEW_TAGNAME="$1" - cat 'package.json' | sed -E "s/(--tagName )([0-9\.a-zA-Z]+)/\1$NEW_TAGNAME/" | tee 'package.json' + LOCAL_TAG="$(cat 'package.json' | sed -n -E 's/.*--tagName ([0-9\.a-zA-Z]+).*/\1/p')" + if [[ "$UPSTREAM_TAG" != "$LOCAL_TAG" ]]; then + cat 'package.json' | sed -E "s/(--tagName )([0-9\.a-zA-Z]+)/\1$UPSTREAM_TAG/" | tee 'package.json' deno run build fi