generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
171 lines (148 loc) · 7.27 KB
/
auto-merge-dependabot.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
name: auto-merge dependabot updates
on:
pull_request_target:
branches: [ main ]
types:
- opened
- synchronize
- reopened
- ready_for_review
permissions:
pull-requests: write
contents: write
jobs:
dependabot-merge:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.DEPENDABOT_PAT }}" # Using PAT for enhanced features, alert-lookup, and compat-lookup
alert-lookup: true # Enable security alert information
compat-lookup: true # Enable compatibility score checking
- name: Check security and compatibility
id: security_check
run: |
DEPS_JSON='${{ steps.metadata.outputs.updated-dependencies-json }}'
# Perform checks
if [ "${{ steps.metadata.outputs.alert-state }}" = "OPEN" ]; then
echo "⚠️ Security alert detected (GHSA: ${{ steps.metadata.outputs.ghsa-id }})"
echo "CVSS Score: ${{ steps.metadata.outputs.cvss }}"
echo "is_security_update=true" >> $GITHUB_OUTPUT
else
echo "is_security_update=false" >> $GITHUB_OUTPUT
fi
if [ "${{ steps.metadata.outputs.compatibility-score }}" -lt 75 ]; then
echo "⚠️ Low compatibility score: ${{ steps.metadata.outputs.compatibility-score }}"
echo "is_compatible=false" >> $GITHUB_OUTPUT
else
echo "is_compatible=true" >> $GITHUB_OUTPUT
fi
if [ "${{ steps.metadata.outputs.maintainer-changes }}" = "true" ]; then
echo "⚠️ Maintainer changes detected"
echo "has_maintainer_changes=true" >> $GITHUB_OUTPUT
else
echo "has_maintainer_changes=false" >> $GITHUB_OUTPUT
fi
- name: Checkout repository
uses: actions/checkout@v4
if: ${{ steps.metadata.outputs.package-ecosystem == 'gomod' }}
- name: Setup Go
uses: actions/setup-go@v5
if: ${{ steps.metadata.outputs.package-ecosystem == 'gomod' }}
with:
go-version: 'stable'
- name: Process Go dependencies
if: ${{ steps.metadata.outputs.package-ecosystem == 'gomod' }}
run: |
log_update_details() {
local pr_number=$1
echo "::group::Dependency Update Details for PR #$pr_number"
echo "🔄 Dependencies: ${{ steps.metadata.outputs.dependency-names }}"
echo "📦 Type: ${{ steps.metadata.outputs.dependency-type }}"
echo "📈 Version: ${{ steps.metadata.outputs.previous-version }} → ${{ steps.metadata.outputs.new-version }}"
echo "📂 Directory: ${{ steps.metadata.outputs.directory }}"
[ "${{ steps.security_check.outputs.is_security_update }}" = "true" ] && \
echo "🚨 Security update (CVSS: ${{ steps.metadata.outputs.cvss }})"
echo "::endgroup::"
}
echo "🔍 Fetching all Go-related Dependabot PRs..."
GO_PRS=$(gh pr list \
--author "dependabot[bot]" \
--json number,title,createdAt,headRefName \
--state open \
--jq 'sort_by(.createdAt) | .[] | select(.title | contains("go.mod"))')
CURRENT_PR_PROCESSED=false
echo "$GO_PRS" | while read -r pr; do
PR_NUMBER=$(echo "$pr" | jq -r .number)
HEAD_BRANCH=$(echo "$pr" | jq -r .headRefName)
log_update_details $PR_NUMBER
# Skip indirect dependencies unless they're security updates
if [ "${{ steps.metadata.outputs.dependency-type }}" = "indirect" ] && \
[ "${{ steps.security_check.outputs.is_security_update }}" != "true" ]; then
echo "⏭️ Skipping indirect dependency update"
continue
fi
# Special handling for security updates
if [ "${{ steps.security_check.outputs.is_security_update }}" = "true" ]; then
echo "🚨 Processing security update with priority"
PRIORITY_MERGE=true
fi
git fetch origin $HEAD_BRANCH
git checkout $HEAD_BRANCH
git pull origin $HEAD_BRANCH
echo "🛠️ Running go mod tidy for PR #$PR_NUMBER"
go mod tidy
if git diff --quiet; then
echo "✨ No changes required for PR #$PR_NUMBER"
else
echo "💾 Committing changes for PR #$PR_NUMBER"
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git commit -am "chore: go mod tidy for PR #$PR_NUMBER"
git push origin $HEAD_BRANCH
fi
# Auto-merge decision logic
if [ "$PR_NUMBER" = "$CURRENT_PR_NUMBER" ]; then
CURRENT_PR_PROCESSED=true
if { [ "$UPDATE_TYPE" != "version-update:semver-major" ] || \
[ "${{ steps.security_check.outputs.is_security_update }}" = "true" ]; } && \
[ "${{ steps.security_check.outputs.is_compatible }}" = "true" ] && \
[ "${{ steps.security_check.outputs.has_maintainer_changes }}" = "false" ]; then
echo "🤖 Enabling auto-merge for current PR #$PR_NUMBER"
gh pr merge --auto --merge "$PR_URL"
fi
elif [ "$CURRENT_PR_PROCESSED" = false ]; then
echo "🔄 Processing older PR #$PR_NUMBER first"
gh pr merge --auto --merge "$PR_NUMBER"
fi
done
env:
GITHUB_TOKEN: ${{ secrets.DEPENDABOT_PAT }}
PR_URL: ${{ github.event.pull_request.html_url }}
CURRENT_PR_NUMBER: ${{ github.event.pull_request.number }}
UPDATE_TYPE: ${{ steps.metadata.outputs.update-type }}
# Handle other dependencies with security awareness
- name: Enable auto-merge for pipeline dependencies
if: |
steps.security_check.outputs.is_compatible == 'true' &&
steps.security_check.outputs.has_maintainer_changes == 'false' &&
(steps.metadata.outputs.update-type != 'version-update:semver-major' || steps.security_check.outputs.is_security_update == 'true') &&
contains(steps.metadata.outputs.directory, '.github/workflows')
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.DEPENDABOT_PAT}}
- name: Enable auto-merge for other dependencies
if: |
steps.security_check.outputs.is_compatible == 'true' &&
steps.security_check.outputs.has_maintainer_changes == 'false' &&
(steps.metadata.outputs.update-type != 'version-update:semver-major' || steps.security_check.outputs.is_security_update == 'true') &&
steps.metadata.outputs.package-ecosystem != 'gomod' &&
!contains(steps.metadata.outputs.directory, '.github/workflows')
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.DEPENDABOT_PAT}}