-
Notifications
You must be signed in to change notification settings - Fork 35
107 lines (94 loc) · 3.41 KB
/
Copy pathcoderabbit-autofix.yml
File metadata and controls
107 lines (94 loc) · 3.41 KB
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
name: coderabbit-autofix
on:
pull_request_review:
types:
- submitted
permissions:
issues: write
pull-requests: read
concurrency:
group: coderabbit-autofix-${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}
cancel-in-progress: true
jobs:
trigger-autofix:
if: ${{ github.event.review.user.login == 'coderabbitai' || github.event.review.user.login == 'coderabbitai[bot]' }}
runs-on: ubuntu-latest
steps:
- name: Request CodeRabbit autofix when unresolved threads exist
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
if (!pr || pr.draft) {
core.info('Skip draft or missing pull request payload.');
return;
}
const owner = context.repo.owner;
const repo = context.repo.repo;
const marker = `<!-- coderabbit-autofix:${pr.head.sha} -->`;
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner,
repo,
issue_number: pr.number,
per_page: 100
}
);
const alreadyTriggered = comments.some((item) => {
const body = String(item.body || '');
return item.user && item.user.login === 'github-actions[bot]' && body.includes(marker);
});
if (alreadyTriggered) {
core.info('Autofix trigger already sent for this head SHA.');
return;
}
const query = `
query($owner:String!, $name:String!, $number:Int!, $cursor:String) {
repository(owner:$owner, name:$name) {
pullRequest(number:$number) {
reviewThreads(first:100, after:$cursor) {
nodes {
isResolved
isOutdated
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
`;
let unresolvedCount = 0;
let cursor = null;
let hasNextPage = true;
while (hasNextPage) {
const result = await github.graphql(query, {
owner,
name: repo,
number: pr.number,
cursor
});
const reviewThreads = result?.repository?.pullRequest?.reviewThreads;
const nodes = reviewThreads?.nodes || [];
unresolvedCount += nodes.filter((node) => !node.isResolved && !node.isOutdated).length;
hasNextPage = Boolean(reviewThreads?.pageInfo?.hasNextPage);
cursor = reviewThreads?.pageInfo?.endCursor ?? null;
}
if (unresolvedCount <= 0) {
core.info('No unresolved review threads found, skip autofix trigger.');
return;
}
const body = [
marker,
'@coderabbitai autofix'
].join('\n');
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body
});
core.info(`Autofix trigger sent. unresolvedCount=${unresolvedCount}`);