From 793ce5b6ef8f87337e135f18faff12f23dbd22bb Mon Sep 17 00:00:00 2001 From: Tom Lebreux Date: Fri, 13 Dec 2024 10:32:43 -0500 Subject: [PATCH 1/4] Add sync deps workflow --- .github/workflows/scripts/sync-deps.sh | 43 ++++++++++++++ .github/workflows/sync-deps.yaml | 78 ++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100755 .github/workflows/scripts/sync-deps.sh create mode 100644 .github/workflows/sync-deps.yaml diff --git a/.github/workflows/scripts/sync-deps.sh b/.github/workflows/scripts/sync-deps.sh new file mode 100755 index 000000000..c77089227 --- /dev/null +++ b/.github/workflows/scripts/sync-deps.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +set -e + +RANCHER_REPO_DIR=$1 + +DEPS_TO_SYNC=" + github.com/rancher/dynamiclistener + github.com/rancher/lasso + github.com/rancher/wrangler + github.com/rancher/wrangler/v2 + github.com/rancher/wrangler/v3 +" + +rancher_deps=$(cd "$RANCHER_REPO_DIR" && go mod graph) +webhook_deps=$(go mod graph) + +rancher_ref=$(cd "$RANCHER_REPO_DIR" && git rev-parse HEAD) +rancher_ref_version=$(go mod download -json "github.com/rancher/rancher/pkg/apis@$rancher_ref" | jq -r '.Version') +echo "Syncing github.com/rancher/rancher/pkg/apis" +go mod edit "-require=github.com/rancher/rancher/pkg/apis@$rancher_ref_version" + +for dep in $DEPS_TO_SYNC; do + if ! rancher_version=$(echo "$rancher_deps" | grep "^$dep@\w*\S"); then + continue + fi + + if ! webhook_version=$(echo "$webhook_deps" | grep "^$dep@\w*\S"); then + continue + fi + + rancher_version=$(echo "$rancher_version" | head -n 1 | cut -d' ' -f1 | cut -d@ -f2) + webhook_version=$(echo "$webhook_version" | head -n 1 | cut -d' ' -f1 | cut -d@ -f2) + if [ "$rancher_version" = "$webhook_version" ]; then + continue + fi + + echo "Version mismatch for $dep (rancher=$rancher_version, webhook=$webhook_version) detected" + go mod edit -require=$dep@$rancher_version +done + +echo "Running go mod tidy" +go mod tidy diff --git a/.github/workflows/sync-deps.yaml b/.github/workflows/sync-deps.yaml new file mode 100644 index 000000000..c1c09cc8d --- /dev/null +++ b/.github/workflows/sync-deps.yaml @@ -0,0 +1,78 @@ +name: Sync dependencies + +on: + workflow_dispatch: + inputs: + rancher_ref: + description: "Version of rancher/rancher to compare" + required: true + default: "main" + rancher_repository: + description: "Repository for rancher/rancher" + required: true + default: "rancher/rancher" + +env: + RANCHER_REF: "${{ github.event.inputs.rancher_ref }}" + WEBHOOK_REF: "${{ github.ref_name }}" + +permissions: + contents: write + pull-requests: write + +jobs: + sync: + name: Sync dependencies + runs-on: ubuntu-latest + steps: + - name : Checkout webhook repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + ref: "${{ env.WEBHOOK_REF }}" + path: webhook + + - name : Checkout rancher repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + repository: "${{ github.event.inputs.rancher_repository }}" + ref: "${{ env.RANCHER_REF }}" + path: rancher + + - name: Install dependencies + run: sudo snap install yq --channel=v4/stable + + - name: Configure the committer + run: | + cd webhook + git config --global user.name "Webhook Sync Bot" + git config --global user.email "webhooksyncbot@users.noreply.github.com" + + - name: Run sync-deps script + run: | + cd webhook + BRANCH="sync-deps-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" + echo "BRANCH=${BRANCH}" >> $GITHUB_ENV + git checkout -b "$BRANCH" + ./.github/workflows/scripts/sync-deps.sh ../rancher + git add go.mod go.sum + git commit -m "Sync dependencies" + git push origin "$BRANCH" + + - name: Create PR + run: | + cd webhook + body=$(cat < Date: Fri, 13 Dec 2024 12:11:56 -0500 Subject: [PATCH 2/4] List changes in PR description --- .github/workflows/scripts/sync-deps.sh | 25 ++++++++++++++++++++----- .github/workflows/sync-deps.yaml | 15 +++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/.github/workflows/scripts/sync-deps.sh b/.github/workflows/scripts/sync-deps.sh index c77089227..2b244e7c7 100755 --- a/.github/workflows/scripts/sync-deps.sh +++ b/.github/workflows/scripts/sync-deps.sh @@ -3,7 +3,10 @@ set -e RANCHER_REPO_DIR=$1 +# File to write the changes to +CHANGES_FILE=${2:-/dev/null} +PKG_APIS="github.com/rancher/rancher/pkg/apis" DEPS_TO_SYNC=" github.com/rancher/dynamiclistener github.com/rancher/lasso @@ -12,13 +15,26 @@ DEPS_TO_SYNC=" github.com/rancher/wrangler/v3 " +update_dep() { + module=$1 + old_version=$2 + new_version=$3 + + echo "Version mismatch for $module (rancher=$new_version, webhook=$old_version) detected" + go mod edit -require="$module@$new_version" + echo "Updated $module ($old_version => $new_version)" >> "$CHANGES_FILE" +} + rancher_deps=$(cd "$RANCHER_REPO_DIR" && go mod graph) webhook_deps=$(go mod graph) rancher_ref=$(cd "$RANCHER_REPO_DIR" && git rev-parse HEAD) -rancher_ref_version=$(go mod download -json "github.com/rancher/rancher/pkg/apis@$rancher_ref" | jq -r '.Version') -echo "Syncing github.com/rancher/rancher/pkg/apis" -go mod edit "-require=github.com/rancher/rancher/pkg/apis@$rancher_ref_version" +rancher_pkg_apis_version=$(go mod download -json "github.com/rancher/rancher/pkg/apis@$rancher_ref" | jq -r '.Version') +webhook_pkg_apis_version=$(echo "$webhook_deps" | grep "^$PKG_APIS@\w*\S" | head -n 1 | cut -d' ' -f1 | cut -d@ -f2) + +if [ "$rancher_pkg_apis_version" != "$webhook_pkg_apis_version" ]; then + update_dep "$PKG_APIS" "$webhook_pkg_apis_version" "$rancher_pkg_apis_version" +fi for dep in $DEPS_TO_SYNC; do if ! rancher_version=$(echo "$rancher_deps" | grep "^$dep@\w*\S"); then @@ -35,8 +51,7 @@ for dep in $DEPS_TO_SYNC; do continue fi - echo "Version mismatch for $dep (rancher=$rancher_version, webhook=$webhook_version) detected" - go mod edit -require=$dep@$rancher_version + update_dep "$dep" "$webhook_version" "$rancher_version" done echo "Running go mod tidy" diff --git a/.github/workflows/sync-deps.yaml b/.github/workflows/sync-deps.yaml index c1c09cc8d..6d9b1ca90 100644 --- a/.github/workflows/sync-deps.yaml +++ b/.github/workflows/sync-deps.yaml @@ -53,17 +53,24 @@ jobs: BRANCH="sync-deps-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" echo "BRANCH=${BRANCH}" >> $GITHUB_ENV git checkout -b "$BRANCH" - ./.github/workflows/scripts/sync-deps.sh ../rancher - git add go.mod go.sum - git commit -m "Sync dependencies" - git push origin "$BRANCH" + ./.github/workflows/scripts/sync-deps.sh ../rancher "changes.txt" + if [ -f changes.txt ]; then + git add go.mod go.sum + git commit -m "Sync dependencies" + git push origin "$BRANCH" + fi - name: Create PR + # Only create the PR if changes were detected + if: ${{ hashFiles('webhook/changes.txt') != '' }} run: | cd webhook + changes=$(cat changes.txt) body=$(cat < Date: Fri, 13 Dec 2024 12:28:23 -0500 Subject: [PATCH 3/4] Add usage and error handling for pkg/apis version extraction --- .github/workflows/scripts/sync-deps.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scripts/sync-deps.sh b/.github/workflows/scripts/sync-deps.sh index 2b244e7c7..f3623a7b0 100755 --- a/.github/workflows/scripts/sync-deps.sh +++ b/.github/workflows/scripts/sync-deps.sh @@ -1,4 +1,6 @@ #!/bin/sh +# +# set -e @@ -15,6 +17,15 @@ DEPS_TO_SYNC=" github.com/rancher/wrangler/v3 " +if [ -z "$RANCHER_REPO_DIR" ]; then + usage + exit 1 +fi + +usage() { + echo "$0 []" +} + update_dep() { module=$1 old_version=$2 @@ -29,7 +40,10 @@ rancher_deps=$(cd "$RANCHER_REPO_DIR" && go mod graph) webhook_deps=$(go mod graph) rancher_ref=$(cd "$RANCHER_REPO_DIR" && git rev-parse HEAD) -rancher_pkg_apis_version=$(go mod download -json "github.com/rancher/rancher/pkg/apis@$rancher_ref" | jq -r '.Version') +if ! rancher_pkg_apis_version=$(go mod download -json "github.com/rancher/rancher/pkg/apis@$rancher_ref" | jq -r '.Version'); then + echo "Unable to get version of $PKG_APIS" + exit 1 +fi webhook_pkg_apis_version=$(echo "$webhook_deps" | grep "^$PKG_APIS@\w*\S" | head -n 1 | cut -d' ' -f1 | cut -d@ -f2) if [ "$rancher_pkg_apis_version" != "$webhook_pkg_apis_version" ]; then From 66d2ab6cc5b0e8b556d9fb57cd351e693272cdde Mon Sep 17 00:00:00 2001 From: Tom Lebreux Date: Fri, 13 Dec 2024 12:32:09 -0500 Subject: [PATCH 4/4] Update format of changes for PR description --- .github/workflows/scripts/sync-deps.sh | 2 +- .github/workflows/sync-deps.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/scripts/sync-deps.sh b/.github/workflows/scripts/sync-deps.sh index f3623a7b0..d6857ed95 100755 --- a/.github/workflows/scripts/sync-deps.sh +++ b/.github/workflows/scripts/sync-deps.sh @@ -33,7 +33,7 @@ update_dep() { echo "Version mismatch for $module (rancher=$new_version, webhook=$old_version) detected" go mod edit -require="$module@$new_version" - echo "Updated $module ($old_version => $new_version)" >> "$CHANGES_FILE" + printf '**%s**\n`%s` => `%s`' "$module" "$old_version" "$new_version" >> "$CHANGES_FILE" } rancher_deps=$(cd "$RANCHER_REPO_DIR" && go mod graph) diff --git a/.github/workflows/sync-deps.yaml b/.github/workflows/sync-deps.yaml index 6d9b1ca90..908314aa0 100644 --- a/.github/workflows/sync-deps.yaml +++ b/.github/workflows/sync-deps.yaml @@ -53,8 +53,8 @@ jobs: BRANCH="sync-deps-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" echo "BRANCH=${BRANCH}" >> $GITHUB_ENV git checkout -b "$BRANCH" - ./.github/workflows/scripts/sync-deps.sh ../rancher "changes.txt" - if [ -f changes.txt ]; then + ./.github/workflows/scripts/sync-deps.sh ../rancher "changes.md" + if [ -f changes.md ]; then git add go.mod go.sum git commit -m "Sync dependencies" git push origin "$BRANCH" @@ -62,10 +62,10 @@ jobs: - name: Create PR # Only create the PR if changes were detected - if: ${{ hashFiles('webhook/changes.txt') != '' }} + if: ${{ hashFiles('webhook/changes.md') != '' }} run: | cd webhook - changes=$(cat changes.txt) + changes=$(cat changes.md) body=$(cat <