forked from apple/foundationdb
-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (133 loc) · 5.5 KB
/
Copy pathcodebuild-cleanup.yml
File metadata and controls
141 lines (133 loc) · 5.5 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
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
name: CodeBuild Comment Cleanup
on:
issue_comment:
types: [created]
permissions:
issues: write
pull-requests: write
jobs:
cleanup:
if: >
github.event.issue.pull_request &&
github.event.comment.user.login == 'foundationdb-ci'
runs-on: ubuntu-latest
steps:
- name: Minimize Outdated Comments
uses: actions/github-script@v9
with:
script: |
// Example header: ### Result of foundationdb-pr-clang on Linux RHEL 9
// Check if header is at beginning of trimmed body
const commentBody = context.payload.comment.body.trimStart();
const match = commentBody.match(/^### Result of (.*) on (.*)$/m);
if (!match || commentBody.indexOf(match[0]) !== 0) {
console.log("Comment does not start with the expected header format. Skipping.");
return;
}
const header = match[0].trim();
// Get PR info to find the head SHA
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
const headSha = pullRequest.head.sha;
console.log(`Current PR head SHA: ${headSha}`);
const allOldComments = [];
const commentsToMinimize = [];
let oldCommentAtHead = null;
console.log(`Searching for previous comments with header: "${header}"`);
const query = `
query($owner: String!, $repo: String!, $prNumber: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
comments(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
databaseId
body
isMinimized
author {
login
}
}
}
}
}
}
`;
let hasNextPage = true;
let cursor = null;
while (hasNextPage) {
const result = await github.graphql(query, {
owner: context.repo.owner,
repo: context.repo.repo,
prNumber: context.issue.number,
cursor: cursor
});
const prComments = result.repository.pullRequest.comments;
for (const comment of prComments.nodes) {
if (
comment.author &&
comment.author.login === 'foundationdb-ci' &&
comment.databaseId !== context.payload.comment.id &&
comment.body.trimStart().startsWith(header)
) {
allOldComments.push(comment);
// Check for older comment matching PR head commit, in case reports came out-of-order.
// Default sort is by created ascending, newest matching head commit wins.
const m = comment.body.match(/^\* Commit ID: ([a-f0-9]+)/m);
if (m && m[1] === headSha) {
oldCommentAtHead = comment;
}
}
}
hasNextPage = prComments.pageInfo.hasNextPage;
cursor = prComments.pageInfo.endCursor;
}
const newCommentM = commentBody.match(/^\* Commit ID: ([a-f0-9]+)/m);
const newCommentSha = newCommentM ? newCommentM[1] : null;
if (newCommentSha && newCommentSha !== headSha && oldCommentAtHead) {
console.log(`New comment is for old commit (${newCommentSha}), but an older comment exists for the head commit (${headSha})`);
commentsToMinimize.push({
id: context.payload.comment.node_id,
databaseId: context.payload.comment.id,
isMinimized: false // new comment must not be minimized yet
});
for (const old of allOldComments) {
if (old.id !== oldCommentAtHead.id) {
commentsToMinimize.push(old);
}
}
} else {
commentsToMinimize.push(...allOldComments);
}
console.log(`Found ${commentsToMinimize.length} comments to evaluate.`);
for (const comment of commentsToMinimize) {
if (comment.isMinimized) {
console.log(`Comment ${comment.databaseId} is already minimized.`);
continue;
}
console.log(`Minimizing comment ${comment.databaseId}`);
const minimizeMutation = `
mutation($id: ID!, $classifier: ReportedContentClassifiers!) {
minimizeComment(input: { subjectId: $id, classifier: $classifier }) {
minimizedComment {
isMinimized
}
}
}
`;
try {
await github.graphql(minimizeMutation, {
id: comment.id,
classifier: 'OUTDATED'
});
} catch (error) {
console.error(`Failed to minimize comment ${comment.databaseId}:`, error);
}
}