From db2b762b8fa30f391cd1227df89922e4f2c5326f Mon Sep 17 00:00:00 2001 From: Sai Annam <107242846+otaku0304@users.noreply.github.com> Date: Sun, 6 Oct 2024 18:58:00 +0530 Subject: [PATCH] fet(pdf-remov): created auto merge flow --- .github/dependabot-auto-merge.yml | 125 +++++++++++++++++++++++++----- 1 file changed, 104 insertions(+), 21 deletions(-) diff --git a/.github/dependabot-auto-merge.yml b/.github/dependabot-auto-merge.yml index c2ab9ff..91ca166 100644 --- a/.github/dependabot-auto-merge.yml +++ b/.github/dependabot-auto-merge.yml @@ -1,5 +1,8 @@ name: Auto-merge Dependabot PRs +permissions: + pull-requests: write + on: pull_request: types: @@ -9,25 +12,105 @@ on: jobs: auto-merge: runs-on: ubuntu-latest - if: github.actor == 'dependabot[bot]' # Trigger only for Dependabot PRs - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Get Dependabot compatibility score - run: | - pr_url="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" - compatibility_score=$(curl -s "$pr_url" | jq -r '.head.repo.compatibility_score') - echo "Compatibility score is $compatibility_score" - - - name: Check compatibility score and auto-merge - run: | - if [[ $compatibility_score == "100" ]]; then - echo "Compatibility score is 100%, auto-merging..." - gh pr merge ${{ github.event.pull_request.number }} --auto --squash --delete-branch - else - echo "Compatibility score is not 100%, skipping auto-merge." - fi - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Check for existing open PRs + id: check_open_prs + run: | + pr_count=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls?state=open&base=dev" | jq '. | length') + echo "Open PR count targeting dev branch: $pr_count" + echo "pr_count=$pr_count" >> "$GITHUB_OUTPUT" + + - name: Exit if no open PRs + if: steps.check_open_prs.outputs.pr_count == '0' + run: | + echo "No open PRs targeting dev branch. Exiting workflow." + exit 0 + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check if PR is created by Dependabot and targets dev branch + id: check_dependabot + run: | + if [[ "${{ env.PR_USER }}" == "dependabot[bot]" ]] && [[ "${{ github.event.pull_request.base.ref }}" == "dev" ]]; then + echo "This PR is created by Dependabot and targets the dev branch." + echo "is_dependabot=true" >> $GITHUB_ENV + else + echo "This PR is not created by Dependabot or does not target the dev branch." + echo "is_dependabot=false" >> $GITHUB_ENV + fi + + - name: Get pull request details + id: get_pr_details + run: | + pr_url="https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" + + # Extract title from PR details + pr_title=$(curl -s "$pr_url" | jq -r '.title') + echo "Pull request title: $pr_title" + echo "PR_TITLE=$pr_title" >> $GITHUB_ENV # Save PR title to environment variable + + - name: Extract package name, package manager, previous version, and new version from PR title + id: extract_details + run: | + # Load PR title from environment + PR_TITLE="${{ env.PR_TITLE }}" + + # Extract package name (second word from title) + package_name=$(echo "$PR_TITLE" | awk '{print $2}') + + # Extract package manager if it exists, otherwise use a default (npm_and_yarn) + package_manager=$(echo "$PR_TITLE" | grep -oP '(?<=package-manager: )\S+' || echo "npm_and_yarn") + + # Extract previous and new versions from the title + previous_version=$(echo "$PR_TITLE" | grep -oP '\d+\.\d+\.\d+' | head -n 1) + new_version=$(echo "$PR_TITLE" | grep -oP '\d+\.\d+\.\d+' | tail -n 1) + + echo "Extracted parameters:" + echo "Package Name: $package_name" + echo "Package Manager: $package_manager" + echo "Previous Version: $previous_version" + echo "New Version: $new_version" + + # Set extracted values as output variables + echo "PACKAGE_NAME=$package_name" >> $GITHUB_ENV + echo "PACKAGE_MANAGER=$package_manager" >> $GITHUB_ENV + echo "PREVIOUS_VERSION=$previous_version" >> $GITHUB_ENV + echo "NEW_VERSION=$new_version" >> $GITHUB_ENV + + - name: Generate compatibility score URL and request the score + id: request_compatibility_score + run: | + # Generate the URL using extracted values + compatibility_url="https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=${{ env.PACKAGE_NAME }}&package-manager=${{ env.PACKAGE_MANAGER }}&previous-version=${{ env.PREVIOUS_VERSION }}&new-version=${{ env.NEW_VERSION }}" + + echo "Generated URL: $compatibility_url" + + # Fetch the SVG response + svg_response=$(curl -s "$compatibility_url") + echo "SVG Response: $svg_response" + + # Extract the percentage from the