-
Notifications
You must be signed in to change notification settings - Fork 0
117 lines (95 loc) · 4.71 KB
/
dependabot-auto-merge.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
name: Auto-merge Dependabot PRs
permissions:
contents: write
pull-requests: write
on:
pull_request:
types:
- opened
- synchronize
jobs:
auto-merge:
runs-on: ubuntu-latest
steps:
- 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 $3}')
# 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 <title> tag in the SVG response
compatibility_score=$(echo "$svg_response" | grep -oP '(?<=<title>compatibility: )\d+(?=%</title>)')
# If the compatibility score is not found, set a default or handle error
if [[ -z "$compatibility_score" ]]; then
echo "Unable to extract compatibility score. Defaulting to 0."
compatibility_score=0
fi
echo "Extracted Compatibility Score: $compatibility_score"
# Save the score to environment
echo "COMPATIBILITY_SCORE=$compatibility_score" >> $GITHUB_ENV
- 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 }}