|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 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 | + |
18 | 116 | module.exports = { |
19 | 117 | check_title_format: function({core, github, context}) { |
20 | 118 | const title = context.payload.pull_request.title; |
@@ -67,17 +165,30 @@ module.exports = { |
67 | 165 | } |
68 | 166 | } |
69 | 167 |
|
| 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 | + |
70 | 186 | if (found) { |
71 | 187 | console.log("PR has appropriate label(s)"); |
| 188 | + await upsert_comment(github, maybe_comment_id, body_text, false); |
72 | 189 | } 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); |
81 | 192 | core.setFailed("Missing required labels. See CONTRIBUTING.md"); |
82 | 193 | } |
83 | 194 | }, |
|
0 commit comments