You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zh...@apache.org on 2023/03/20 14:12:43 UTC

[camel-quarkus] branch main updated: Auto synchronize dependabot branches with generated changes (#4645)

This is an automated email from the ASF dual-hosted git repository.

zhfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new db6655b1de Auto synchronize dependabot branches with generated changes (#4645)
db6655b1de is described below

commit db6655b1deb90edc84676621958150b24a304a6a
Author: James Netherton <ja...@users.noreply.github.com>
AuthorDate: Mon Mar 20 14:10:57 2023 +0000

    Auto synchronize dependabot branches with generated changes (#4645)
    
    Fixes #4532
---
 .github/workflows/ci-build.yaml                    |  59 +++++++++-
 .../workflows/synchronize-dependabot-branch.yaml   | 126 +++++++++++++++++++++
 2 files changed, 184 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/ci-build.yaml b/.github/workflows/ci-build.yaml
index 2544512d74..76ce05838e 100644
--- a/.github/workflows/ci-build.yaml
+++ b/.github/workflows/ci-build.yaml
@@ -57,9 +57,66 @@ env:
   TESTCONTAINERS_RYUK_DISABLED: true
 
 jobs:
-  initial-mvn-install:
+  pre-build-checks:
     if: github.repository == 'apache/camel-quarkus'
     runs-on: ubuntu-latest
+    outputs:
+      continue-build: ${{ steps.pre-build-checks.outputs.continue-build }}
+      run-checks: ${{ steps.init.outputs.run-checks }}
+    steps:
+      - name: Initialize
+        id: init
+        run: |
+          if [[ "${{ github.event_name }}" == "pull_request" ]] && [[ "${{ github.actor }}" == "dependabot[bot]" ]]; then
+            echo "run-checks=true" >> $GITHUB_OUTPUT
+          else
+            echo "run-checks=false" >> $GITHUB_OUTPUT
+          fi
+      - name: Set up JDK 17
+        uses: actions/setup-java@v2
+        if: steps.init.outputs.run-checks == 'true'
+        with:
+          distribution: 'temurin'
+          java-version: '17'
+      - name: Checkout
+        uses: actions/checkout@v2
+        if: steps.init.outputs.run-checks == 'true'
+        with:
+          ref: ${{ github.head_ref }}
+          fetch-depth: 0
+      - name: Pre build checks
+        id: pre-build-checks
+        run: |
+          if [[ "${{ steps.init.outputs.run-checks }}" == "true" ]]; then
+            ./mvnw cq:sync-versions -N ${CQ_MAVEN_ARGS}
+            ./mvnw clean install -pl poms/bom -am -Dcq.flatten-bom.format --fail-never ${CQ_MAVEN_ARGS}
+            ./mvnw clean install -f poms/bom ${CQ_MAVEN_ARGS}
+
+            if [[ -z "$(git status --porcelain)" ]]; then
+              echo "continue-build=true" >> $GITHUB_OUTPUT
+            else
+              mkdir ./dependabot-pr
+              echo ${{ github.head_ref }} > ./dependabot-pr/BRANCH_REF
+              echo "$GITHUB_REF" | awk -F / '{print $3}' >  ./dependabot-pr/PR_NUMBER
+              git diff -p --binary > ./dependabot-pr/changes.patch
+
+              echo "continue-build=false" >> $GITHUB_OUTPUT
+            fi
+          else
+            echo "continue-build=true" >> $GITHUB_OUTPUT
+          fi
+      - name: Upload dependabot changeset
+        uses: actions/upload-artifact@v2
+        if: steps.pre-build-checks.outputs.continue-build == 'false'
+        with:
+          name: dependabot-pr-changeset
+          path: dependabot-pr/
+          retention-days: 1
+
+  initial-mvn-install:
+    if: needs.pre-build-checks.outputs.continue-build == 'true'
+    runs-on: ubuntu-latest
+    needs: pre-build-checks
     outputs:
       matrix: ${{ steps.set-native-matrix.outputs.matrix }}
     env:
diff --git a/.github/workflows/synchronize-dependabot-branch.yaml b/.github/workflows/synchronize-dependabot-branch.yaml
new file mode 100644
index 0000000000..82e6c2845c
--- /dev/null
+++ b/.github/workflows/synchronize-dependabot-branch.yaml
@@ -0,0 +1,126 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+name: Synchronize Dependabot Branch
+
+on:
+  workflow_run:
+    workflows: ["Camel Quarkus CI"]
+    types:
+      - completed
+
+jobs:
+  update:
+    runs-on: ubuntu-latest
+    outputs:
+      branch-ref: ${{ steps.setup-dependabot-patches.outputs.branch-ref }}
+      pr-number: ${{ steps.setup-dependabot-patches.outputs.pr-number }}
+      pr-updated: ${{ steps.push-changes.outputs.updated }}
+    if: github.repository == 'apache/camel-quarkus' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' && github.actor == 'dependabot[bot]'
+    steps:
+      - name: Download dependabot pull request changes
+        id: download-pr-changes
+        uses: actions/github-script@v6
+        with:
+          result-encoding: string
+          script: |
+            const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
+               owner: context.repo.owner,
+               repo: context.repo.repo,
+               run_id: ${{github.event.workflow_run.id }},
+            });
+            
+            const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
+              return artifact.name == "dependabot-pr-changeset"
+            })[0];
+
+            if (matchArtifact !== undefined) {
+              const download = await github.rest.actions.downloadArtifact({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                artifact_id: matchArtifact.id,
+                archive_format: 'zip',
+              });
+
+              const fs = require('fs');
+              fs.writeFileSync('/home/runner/work/dependabot-pr.zip', Buffer.from(download.data));
+              return true;
+            }
+
+            return false;
+      - name: Set up dependabot patches
+        id: setup-dependabot-patches
+        if: steps.download-pr-changes.outputs.result == 'true'
+        run: |
+          if [[ -f /home/runner/work/dependabot-pr.zip ]]; then
+            mkdir -p /home/runner/work/dependabot-pr
+            unzip -o /home/runner/work/dependabot-pr.zip -d /home/runner/work/dependabot-pr
+
+            echo "branch-ref=$(cat /home/runner/work/dependabot-pr/BRANCH_REF)" >> $GITHUB_OUTPUT
+            echo "pr-number=$(cat /home/runner/work/dependabot-pr/PR_NUMBER)" >> $GITHUB_OUTPUT
+          fi
+      - name: Checkout dependabot branch
+        uses: actions/checkout@v2
+        if: steps.setup-dependabot-patches.outputs.branch-ref != ''
+        with:
+          ref: ${{ steps.setup-dependabot-patches.outputs.branch-ref }}
+          fetch-depth: 0
+      - name: Push changes to dependabot/maven branch
+        id: push-changes
+        if: steps.setup-dependabot-patches.outputs.branch-ref != ''
+        run: |
+          git config --local user.email "49699333+dependabot[bot]@users.noreply.github.com"
+          git config --local user.name "dependabot[bot]"
+
+          CHANGES_PATH=/home/runner/work/dependabot-pr/changes.patch
+          if [[ -f "${CHANGES_PATH}" ]]; then
+            COMMIT_MESSAGE=$(git log -1 --pretty=%B)
+
+            git reset --soft HEAD~1
+            git apply ${CHANGES_PATH}
+            git commit -am"${COMMIT_MESSAGE}"
+            git push --force-with-lease origin ${BRANCH_REF}
+
+            echo "updated=true" >> $GITHUB_OUTPUT
+          else
+            echo "updated=false" >> $GITHUB_OUTPUT
+          fi
+
+  rerun-pr-workflow:
+    needs: update
+    runs-on: ubuntu-latest
+    if: needs.update.outputs.pr-updated == 'true'
+    permissions:
+      actions: write
+      pull-requests: write
+    steps:
+      - name: Trigger dependabot pull request workflow
+        uses: actions/github-script@v6
+        with:
+          script: |
+            await github.rest.actions.reRunWorkflow({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              run_id: ${{ github.event.workflow_run.id }},
+            });
+
+            await github.rest.issues.createComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              issue_number: ${{ needs.update.outputs.pr-number }},
+              body: "Branch auto synchronized due to changes in generated files. New workflow run triggered:\n\nhttps://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}"
+            });