Skip to content

Commit 80ed4bb

Browse files
committed
Enhance Dependabot alert workflow by updating permissions and adding auto-merge functionality for Dependabot PRs based on check statuses
1 parent 5d23c1d commit 80ed4bb

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

.github/workflows/dependabot_alert_issues.yml

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ on:
66

77
permissions:
88
issues: write
9-
contents: read
10-
pull-requests: read
9+
contents: write # was: read -> needed for merging
10+
pull-requests: write # was: read -> needed for merging
11+
checks: read
12+
statuses: read
1113

1214
jobs:
1315
create_issue_for_high_critical:
@@ -70,7 +72,6 @@ jobs:
7072
`- **Update type**: ${updateType}`,
7173
`- **Dependency type**: ${dependencyType}`,
7274
`- **PR**: #${pr.number}`,
73-
`- **PR URL**: ${alertUrl}`,
7475
];
7576
7677
if (ghsa) {
@@ -97,3 +98,71 @@ jobs:
9798
labels,
9899
assignees: ['Dopeamin'],
99100
});
101+
102+
auto_merge_dependabot:
103+
runs-on: ubuntu-latest
104+
needs: create_issue_for_high_critical
105+
if: github.event.pull_request.user.login == 'dependabot[bot]'
106+
107+
steps:
108+
- name: Check if all required checks passed
109+
id: status
110+
uses: actions/github-script@v7
111+
with:
112+
github-token: ${{ secrets.GITHUB_TOKEN }}
113+
script: |
114+
const { owner, repo } = context.repo;
115+
const pr = context.payload.pull_request;
116+
const sha = pr.head.sha;
117+
118+
// Combined status (old API)
119+
const { data: combined } = await github.rest.repos.getCombinedStatusForRef({
120+
owner,
121+
repo,
122+
ref: sha,
123+
});
124+
125+
// Checks API (GitHub Actions and other checks)
126+
const { data: checks } = await github.rest.checks.listForRef({
127+
owner,
128+
repo,
129+
ref: sha,
130+
});
131+
132+
const allStatusesSuccess =
133+
(combined.state === 'success' || combined.state === 'pending') &&
134+
combined.statuses.every(s =>
135+
['success', 'neutral', 'skipped', 'pending'].includes(s.state)
136+
);
137+
138+
const allChecksSuccess =
139+
checks.check_runs.length === 0 ||
140+
checks.check_runs.every(c =>
141+
['success', 'neutral', 'skipped'].includes(c.conclusion)
142+
);
143+
144+
if (!allStatusesSuccess || !allChecksSuccess) {
145+
core.info('Not all checks are successful yet – skipping merge.');
146+
core.setOutput('can_merge', 'false');
147+
} else {
148+
core.info('All checks look good – ready to merge.');
149+
core.setOutput('can_merge', 'true');
150+
}
151+
152+
- name: Merge Dependabot PR
153+
if: steps.status.outputs.can_merge == 'true'
154+
uses: actions/github-script@v7
155+
with:
156+
github-token: ${{ secrets.GITHUB_TOKEN }}
157+
script: |
158+
const { owner, repo } = context.repo;
159+
const pr = context.payload.pull_request;
160+
161+
await github.rest.pulls.merge({
162+
owner,
163+
repo,
164+
pull_number: pr.number,
165+
merge_method: 'squash', // or 'merge' / 'rebase'
166+
});
167+
168+
core.info(`Merged Dependabot PR #${pr.number}`);

0 commit comments

Comments
 (0)