Skip to content

Commit 70e7caf

Browse files
authored
Limit TSDiffer comments to one per PR (#3700)
* Limit TSDiffer comments to one per PR
1 parent 44d7ebc commit 70e7caf

File tree

3 files changed

+116
-10
lines changed

3 files changed

+116
-10
lines changed

.circleci/api-comment.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const Octokit = require('@octokit/rest');
2+
const fs = require('fs');
3+
4+
const octokit = new Octokit({
5+
auth: `token ${process.env.GITHUB_TOKEN}`
6+
});
7+
8+
run();
9+
10+
let commentKey = '<!-- APIDifferComment -->';
11+
12+
async function run() {
13+
let pr;
14+
// If we aren't running on a PR commit, double check if this is a branch created for a fork. If so, we'll need to
15+
// comment the build link on the fork.
16+
if (!process.env.CIRCLE_PULL_REQUEST) {
17+
try {
18+
const commit = await octokit.git.getCommit({
19+
owner: 'adobe',
20+
repo: 'react-spectrum',
21+
commit_sha: process.env.CIRCLE_SHA1
22+
});
23+
24+
// Check if it is a merge commit from the github "Branch from fork action"
25+
if (commit && commit.data?.parents?.length === 2 && commit.data.message.indexOf('Merge') > -1) {
26+
// Unfortunately listPullRequestsAssociatedWithCommit doesn't return fork prs so have to use search api
27+
// to find the fork PR the original commit lives in
28+
const forkHeadCommit = commit.data.parents[1].sha;
29+
const searchRes = await octokit.search.issuesAndPullRequests({
30+
q: `${forkHeadCommit}+repo:adobe/react-spectrum+is:pr+is:open`
31+
});
32+
33+
// Look for a PR that is from a fork and has a matching head commit as the current branch
34+
const pullNumbers = searchRes.data.items.filter(i => i.pull_request !== undefined).map(j => j.number);
35+
for (let pull_number of pullNumbers) {
36+
const {data} = await octokit.pulls.get({
37+
owner: 'adobe',
38+
repo: 'react-spectrum',
39+
pull_number
40+
});
41+
if (data && data.head.repo.full_name !== 'adobe/react-spectrum' && data.head.sha === forkHeadCommit) {
42+
pr = pull_number;
43+
break;
44+
}
45+
}
46+
}
47+
} catch (error) {
48+
console.error(error);
49+
}
50+
} else {
51+
pr = process.env.CIRCLE_PULL_REQUEST.split('/').pop();
52+
}
53+
54+
if (pr != null) {
55+
let commentId = await findDifferComment(pr);
56+
let diffs = fs.readFileSync('/tmp/dist/ts-diff.txt');
57+
if (diffs.length > 0) {
58+
if (commentId != null) {
59+
// delete existing comment
60+
await octokit.issues.deleteComment({
61+
owner: 'adobe',
62+
repo: 'react-spectrum',
63+
comment_id: commentId,
64+
body: `${commentKey}## API Changes
65+
${diffs}
66+
`
67+
})
68+
}
69+
// create new comment
70+
await octokit.issues.createComment({
71+
owner: 'adobe',
72+
repo: 'react-spectrum',
73+
issue_number: pr,
74+
body: `${commentKey}## API Changes
75+
${diffs}
76+
`
77+
});
78+
} else if (commentId != null) {
79+
// delete existing comment, it no longer applies
80+
await octokit.issues.deleteComment({
81+
owner: 'adobe',
82+
repo: 'react-spectrum',
83+
comment_id: commentId
84+
});
85+
}
86+
}
87+
}
88+
89+
async function findDifferComment(pr, page = 1) {
90+
let comments = null;
91+
try {
92+
comments = await octokit.issues.listComments({
93+
owner: 'adobe',
94+
repo: 'react-spectrum',
95+
issue_number: pr,
96+
page
97+
});
98+
} catch (err) {
99+
console.log(err);
100+
return null;
101+
}
102+
103+
let commentId;
104+
for (let comment of comments.data) {
105+
if (comment.body.includes(commentKey)) {
106+
commentId = comment.id;
107+
break;
108+
}
109+
}
110+
// default results per page is 30
111+
if (commentId == null && comments.data.length === 30) {
112+
commentId = await findDifferComment(pr, page + 1);
113+
}
114+
return commentId;
115+
}

.circleci/comment.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ async function run() {
6565
}
6666

6767
if (pr != null) {
68-
let diffs = fs.readFileSync('/tmp/dist/ts-diff.txt');
6968
await octokit.issues.createComment({
7069
owner: 'adobe',
7170
repo: 'react-spectrum',
@@ -77,14 +76,5 @@ async function run() {
7776
* [View the storybook-16](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/storybook-16/index.html)
7877
* [View the documentation](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/docs/index.html)`
7978
});
80-
if (diffs.length > 0) {
81-
await octokit.issues.createComment({
82-
owner: 'adobe',
83-
repo: 'react-spectrum',
84-
issue_number: pr,
85-
body: `## API Changes
86-
${diffs}
87-
`});
88-
}
8979
}
9080
}

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ jobs:
408408
command: |
409409
if [ $GITHUB_TOKEN ]; then
410410
node .circleci/comment.js
411+
node .circleci/api-comment.js
411412
fi
412413
413414
publish-nightly:

0 commit comments

Comments
 (0)