Skip to content

Commit b367e15

Browse files
feat: add Devin PR conflict resolver workflow (#26624)
* feat: add Devin PR conflict resolver workflow Adds a GitHub Actions workflow that automatically detects PRs with merge conflicts and spins up Devin sessions to resolve them. How it works: 1. Triggers on push to main branch (when main updates could cause conflicts) 2. Also supports manual trigger via workflow_dispatch 3. Lists all open PRs and checks their mergeable status 4. For PRs with conflicts (mergeable=false, mergeable_state=dirty): - Checks if a Devin session was already created (avoids duplicates) - Creates a new Devin session with instructions to resolve conflicts - Posts a comment on the PR with the Devin session link The workflow follows the same pattern as cubic-devin-review.yml for consistency. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: skip draft PRs in conflict detection workflow Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: use GitHub Search API to filter draft PRs at API level Instead of filtering draft PRs in the loop, use the GitHub Search API with 'draft:false' filter which is more efficient as it filters at the API level rather than fetching all PRs and filtering locally. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style: remove explanatory comments from workflow Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style: remove all unnecessary explanatory comments Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor: use GraphQL for batched PR fetching and labels for tracking Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat: add support for resolving conflicts on fork PRs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: check response.ok before parsing Devin API response Address Cubic AI review feedback: Add response.ok check before parsing the response body to explicitly handle HTTP error status codes from the Devin API. This distinguishes API failures (authentication errors, server errors) from successful responses that might be missing expected fields. Co-Authored-By: unknown <> * fix: improve Devin API error logging with PR number Co-Authored-By: unknown <> * feat: add pr_number input for manual fork PR conflict resolution Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat: add maximum 15 PRs safety net limit Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent bd36184 commit b367e15

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
name: Devin PR Conflict Resolver
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
inputs:
9+
pr_number:
10+
description: 'PR number to resolve conflicts for (optional, bypasses maintainer access check for fork PRs)'
11+
required: false
12+
type: string
13+
14+
permissions:
15+
contents: read
16+
pull-requests: write
17+
18+
jobs:
19+
check-conflicts:
20+
name: Check Open PRs for Conflicts
21+
runs-on: blacksmith-2vcpu-ubuntu-2404
22+
steps:
23+
- name: Get open PRs and check for conflicts
24+
id: check-prs
25+
uses: actions/github-script@v7
26+
env:
27+
INPUT_PR_NUMBER: ${{ inputs.pr_number }}
28+
with:
29+
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
script: |
31+
const { owner, repo } = context.repo;
32+
const manualPrNumber = process.env.INPUT_PR_NUMBER ? parseInt(process.env.INPUT_PR_NUMBER, 10) : null;
33+
34+
const query = `
35+
query($owner: String!, $repo: String!, $cursor: String) {
36+
repository(owner: $owner, name: $repo) {
37+
pullRequests(states: OPEN, first: 100, after: $cursor) {
38+
pageInfo {
39+
hasNextPage
40+
endCursor
41+
}
42+
nodes {
43+
number
44+
title
45+
mergeable
46+
isDraft
47+
headRefName
48+
baseRefName
49+
url
50+
headRepository {
51+
owner {
52+
login
53+
}
54+
name
55+
}
56+
maintainerCanModify
57+
labels(first: 10) {
58+
nodes {
59+
name
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
`;
67+
68+
const allPRs = [];
69+
let cursor = null;
70+
71+
do {
72+
const result = await github.graphql(query, { owner, repo, cursor });
73+
const { nodes, pageInfo } = result.repository.pullRequests;
74+
allPRs.push(...nodes);
75+
cursor = pageInfo.hasNextPage ? pageInfo.endCursor : null;
76+
} while (cursor);
77+
78+
console.log(`Found ${allPRs.length} open PRs via GraphQL`);
79+
80+
if (manualPrNumber) {
81+
console.log(`Manual PR number provided: ${manualPrNumber}`);
82+
}
83+
84+
const conflictingPRs = [];
85+
86+
for (const pr of allPRs) {
87+
const isTargetPR = manualPrNumber && pr.number === manualPrNumber;
88+
89+
if (!isTargetPR && pr.isDraft) {
90+
console.log(`PR #${pr.number} is a draft, skipping`);
91+
continue;
92+
}
93+
94+
if (!isTargetPR) {
95+
const hasDevinLabel = pr.labels.nodes.some(label => label.name === 'devin-conflict-resolution');
96+
if (hasDevinLabel) {
97+
console.log(`PR #${pr.number} already has devin-conflict-resolution label, skipping`);
98+
continue;
99+
}
100+
}
101+
102+
if (pr.mergeable === 'CONFLICTING' || (isTargetPR && pr.mergeable !== 'MERGEABLE')) {
103+
const isFork = pr.headRepository?.owner?.login !== owner;
104+
const headRepoOwner = pr.headRepository?.owner?.login || owner;
105+
const headRepoName = pr.headRepository?.name || repo;
106+
107+
if (!isTargetPR && isFork && !pr.maintainerCanModify) {
108+
console.log(`PR #${pr.number} is from a fork without maintainer push access, skipping`);
109+
continue;
110+
}
111+
112+
if (isTargetPR) {
113+
console.log(`PR #${pr.number} manually targeted for conflict resolution${isFork ? ' (from fork)' : ''}`);
114+
} else {
115+
console.log(`PR #${pr.number} has conflicts${isFork ? ' (from fork)' : ''}`);
116+
}
117+
118+
conflictingPRs.push({
119+
number: pr.number,
120+
title: pr.title,
121+
head_ref: pr.headRefName,
122+
base_ref: pr.baseRefName,
123+
html_url: pr.url,
124+
is_fork: isFork,
125+
head_repo_owner: headRepoOwner,
126+
head_repo_name: headRepoName,
127+
is_manual: isTargetPR
128+
});
129+
130+
if (isTargetPR) break;
131+
} else if (pr.mergeable === 'UNKNOWN') {
132+
console.log(`PR #${pr.number} mergeable status is still being computed`);
133+
} else {
134+
console.log(`PR #${pr.number} has no conflicts (mergeable: ${pr.mergeable})`);
135+
}
136+
}
137+
138+
if (manualPrNumber && conflictingPRs.length === 0) {
139+
console.log(`Warning: PR #${manualPrNumber} not found or has no conflicts`);
140+
}
141+
142+
const MAX_PRS = 15;
143+
if (conflictingPRs.length > MAX_PRS) {
144+
console.log(`Warning: Found ${conflictingPRs.length} PRs with conflicts, limiting to ${MAX_PRS} for safety`);
145+
conflictingPRs.length = MAX_PRS;
146+
}
147+
148+
console.log(`Found ${conflictingPRs.length} PRs with conflicts that need Devin sessions`);
149+
150+
const fs = require('fs');
151+
fs.writeFileSync('/tmp/conflicting-prs.json', JSON.stringify(conflictingPRs));
152+
153+
core.setOutput('has-conflicts', conflictingPRs.length > 0 ? 'true' : 'false');
154+
core.setOutput('conflict-count', conflictingPRs.length.toString());
155+
156+
- name: Create Devin sessions for conflicting PRs
157+
if: steps.check-prs.outputs.has-conflicts == 'true'
158+
env:
159+
DEVIN_API_KEY: ${{ secrets.DEVIN_API_KEY }}
160+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
161+
uses: actions/github-script@v7
162+
with:
163+
github-token: ${{ secrets.GITHUB_TOKEN }}
164+
script: |
165+
const fs = require('fs');
166+
const conflictingPRs = JSON.parse(fs.readFileSync('/tmp/conflicting-prs.json', 'utf8'));
167+
const { owner, repo } = context.repo;
168+
169+
for (const pr of conflictingPRs) {
170+
console.log(`Creating Devin session for PR #${pr.number}: ${pr.title}${pr.is_fork ? ' (fork)' : ''}`);
171+
172+
const forkInstructions = pr.is_fork ? `
173+
IMPORTANT: This PR is from a fork. The contributor has enabled "Allow edits from maintainers".
174+
- Clone the FORK repository: ${pr.head_repo_owner}/${pr.head_repo_name}
175+
- The branch to work on is: ${pr.head_ref}
176+
- Add the upstream remote: git remote add upstream https://github.com/${owner}/${repo}.git
177+
- Fetch upstream and merge: git fetch upstream && git merge upstream/${pr.base_ref}` : `
178+
- Clone the repository: ${owner}/${repo}
179+
- Check out the PR branch: ${pr.head_ref}
180+
- Merge the base branch: git merge origin/${pr.base_ref}`;
181+
182+
const prompt = `You are resolving merge conflicts on PR #${pr.number} in repository ${owner}/${repo}.
183+
184+
PR Title: ${pr.title}
185+
PR URL: ${pr.html_url}
186+
Head Branch: ${pr.head_ref}
187+
Base Branch: ${pr.base_ref}
188+
${pr.is_fork ? `Fork Repository: ${pr.head_repo_owner}/${pr.head_repo_name}` : ''}
189+
190+
Your tasks:
191+
${forkInstructions}
192+
193+
Then:
194+
1. Resolve all merge conflicts carefully:
195+
- Review the conflicting changes from both branches
196+
- Make intelligent decisions about how to combine the changes
197+
- Preserve the intent of both the PR changes and the base branch updates
198+
- If unsure about a conflict, prefer keeping both changes where possible
199+
2. Test that the code still works after resolving conflicts (run lint/type checks).
200+
3. Commit the merge resolution with a clear commit message.
201+
4. Push the resolved changes to the PR branch.
202+
203+
Rules and Guidelines:
204+
1. Be careful when resolving conflicts - understand the context of both changes.
205+
2. Follow the existing code style and conventions in the repository.
206+
3. Run lint and type checks before pushing to ensure the code is valid.
207+
4. If a conflict seems too complex or risky to resolve automatically, explain the situation in a PR comment instead.
208+
5. Never ask for user confirmation. Never wait for user messages.`;
209+
210+
try {
211+
const response = await fetch('https://api.devin.ai/v1/sessions', {
212+
method: 'POST',
213+
headers: {
214+
'Authorization': `Bearer ${process.env.DEVIN_API_KEY}`,
215+
'Content-Type': 'application/json'
216+
},
217+
body: JSON.stringify({
218+
prompt: prompt,
219+
title: `Resolve Conflicts: PR #${pr.number}`,
220+
tags: ['conflict-resolution', `pr-${pr.number}`]
221+
})
222+
});
223+
224+
if (!response.ok) {
225+
console.error(`Devin API error for PR #${pr.number}: ${response.status} ${response.statusText}`);
226+
continue;
227+
}
228+
229+
const data = await response.json();
230+
const sessionUrl = data.url || data.session_url;
231+
232+
if (sessionUrl) {
233+
console.log(`Devin session created for PR #${pr.number}: ${sessionUrl}`);
234+
235+
await github.rest.issues.addLabels({
236+
owner,
237+
repo,
238+
issue_number: pr.number,
239+
labels: ['devin-conflict-resolution']
240+
});
241+
242+
await github.rest.issues.createComment({
243+
owner,
244+
repo,
245+
issue_number: pr.number,
246+
body: `### Devin AI is resolving merge conflicts
247+
248+
This PR has merge conflicts with the \`${pr.base_ref}\` branch. A Devin session has been created to automatically resolve them.
249+
250+
[View Devin Session](${sessionUrl})
251+
252+
Devin will:
253+
1. Merge the latest \`${pr.base_ref}\` into this branch
254+
2. Resolve any conflicts intelligently
255+
3. Run lint/type checks to ensure validity
256+
4. Push the resolved changes
257+
258+
If you prefer to resolve conflicts manually, you can close the Devin session and handle it yourself.`
259+
});
260+
} else {
261+
console.log(`Failed to get session URL for PR #${pr.number}:`, data);
262+
}
263+
} catch (error) {
264+
console.error(`Error creating Devin session for PR #${pr.number}:`, error);
265+
}
266+
267+
await new Promise(resolve => setTimeout(resolve, 1000));
268+
}

0 commit comments

Comments
 (0)