Skip to content

Commit a6f52d9

Browse files
committed
issue comment
1 parent 27597b3 commit a6f52d9

File tree

2 files changed

+120
-8
lines changed

2 files changed

+120
-8
lines changed

.github/workflows/dev_pr.js

Lines changed: 119 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,104 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
async function have_comment(github, context, pr_number, tag) {
19+
const query = `
20+
query($owner: String!, $name: String!, $number: Int!, $cursor: String) {
21+
repository(owner: $owner, name: $name) {
22+
pullRequest(number: $number) {
23+
comments (after:$cursor, first: 50) {
24+
nodes {
25+
id
26+
bodyText
27+
author {
28+
login
29+
}
30+
}
31+
pageInfo {
32+
endCursor
33+
hasNextPage
34+
}
35+
}
36+
}
37+
}
38+
}`;
39+
const tag_regexp = new RegExp(tag);
40+
41+
let cursor = null;
42+
let pr_id = null;
43+
while (true) {
44+
const result = await github.graphql(query, {
45+
owner: context.repo.owner,
46+
name: context.repo.repo,
47+
number: pr_number,
48+
cursor,
49+
});
50+
console.log(result);
51+
pr_id = result.repository.pullRequest.id;
52+
cursor = result.repository.pullRequest.comments.pageInfo;
53+
const comments = result.repository.pullRequest.comments.nodes;
54+
55+
for (const comment of comments) {
56+
if (comment.author.login === "github-actions[bot]" &&
57+
comment.bodyText.match(tag_regexp) !== null) {
58+
return {pr_id, comment_id: comment.id};
59+
}
60+
}
61+
62+
if (!result.repository.pullRequest.comments.hasNextPage ||
63+
comments.length === 0) {
64+
break;
65+
}
66+
}
67+
return {pr_id, comment_id: null};
68+
}
69+
70+
async function upsert_comment(github, {pr_id, comment_id}, body, visible) {
71+
console.log(`Upsert comment (pr_id=${pr_id}, comment_id=${comment_id}, visible=${visible})`);
72+
if (!visible) {
73+
if (comment_id === null) return;
74+
75+
const query = `
76+
mutation makeComment($comment: ID!) {
77+
minimizeComment(input: {subjectId: $comment, classifier: RESOLVED}) {
78+
clientMutationId
79+
}
80+
}`;
81+
await github.graphql(query, {
82+
comment: comment_id,
83+
body,
84+
});
85+
return;
86+
}
87+
88+
if (comment_id === null) {
89+
const query = `
90+
mutation makeComment($pr: ID!, $body: String!) {
91+
addComment(input: {subjectId: $pr, body: $body}) {
92+
clientMutationId
93+
}
94+
}`;
95+
await github.graphql(query, {
96+
pr: pr_id,
97+
body,
98+
});
99+
} else {
100+
const query = `
101+
mutation makeComment($comment: ID!, $body: String!) {
102+
unminimizeComment(input: {subjectId: $comment}) {
103+
clientMutationId
104+
}
105+
updateIssueComment(input: {id: $comment, body: $body}) {
106+
clientMutationId
107+
}
108+
}`;
109+
await github.graphql(query, {
110+
comment: comment_id,
111+
body,
112+
});
113+
}
114+
}
115+
18116
module.exports = {
19117
check_title_format: function({core, github, context}) {
20118
const title = context.payload.pull_request.title;
@@ -67,17 +165,30 @@ module.exports = {
67165
}
68166
}
69167

168+
// Look to see if we left a comment before.
169+
const comment_tag = "label_helper_comment";
170+
const maybe_comment_id = await have_comment(github, context, context.payload.pull_request.number, comment_tag);
171+
console.log("Found comment?");
172+
console.log(maybe_comment_id);
173+
const body_text = `
174+
<!-- ${comment_tag} -->
175+
Thank you for opening a pull request!
176+
177+
Please label the PR with one or more of:
178+
179+
${categories.map(c => `- ${c}`).join("\n")}
180+
181+
Also, add the 'breaking-change' label if appropriate.
182+
183+
See [CONTRIBUTING.md](https://github.com/apache/arrow-java/blob/main/CONTRIBUTING.md) for details.
184+
`;
185+
70186
if (found) {
71187
console.log("PR has appropriate label(s)");
188+
await upsert_comment(github, maybe_comment_id, body_text, false);
72189
} else {
73-
console.log("PR has is missing label(s)");
74-
console.log("Label the PR with one or more of:");
75-
for (const label of categories) {
76-
console.log(`- ${label}`);
77-
}
78-
console.log();
79-
console.log("Also, add 'breaking-change' if appropriate.");
80-
console.log("See CONTRIBUTING.md for details.");
190+
console.log(body_text);
191+
await upsert_comment(github, maybe_comment_id, body_text, true);
81192
core.setFailed("Missing required labels. See CONTRIBUTING.md");
82193
}
83194
},

.github/workflows/dev_pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ concurrency:
3535

3636
permissions:
3737
contents: read
38+
pull-requests: write
3839

3940
jobs:
4041
pr-label:

0 commit comments

Comments
 (0)