|
| 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 | +} |
0 commit comments