@@ -7,19 +7,57 @@ const octokit = new Octokit({
7
7
run ( ) ;
8
8
9
9
async function run ( ) {
10
+ let pr ;
11
+ // 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
12
+ // comment the build link on the fork.
10
13
if ( ! process . env . CIRCLE_PULL_REQUEST ) {
11
- return ;
14
+ try {
15
+ const commit = await octokit . git . getCommit ( {
16
+ owner : 'adobe' ,
17
+ repo : 'react-spectrum' ,
18
+ commit_sha : process . env . CIRCLE_SHA1
19
+ } ) ;
20
+
21
+ // Check if it is a merge commit from the github "Branch from fork action"
22
+ if ( commit && commit . data ?. parents ?. length === 2 && commit . data . message . indexOf ( 'Merge' ) > - 1 ) {
23
+ // Unfortunately listPullRequestsAssociatedWithCommit doesn't return fork prs so have to use search api
24
+ // to find the fork PR the original commit lives in
25
+ const forkHeadCommit = commit . data . parents [ 1 ] . sha ;
26
+ const searchRes = await octokit . search . issuesAndPullRequests ( {
27
+ q : `${ forkHeadCommit } +repo:adobe/react-spectrum+is:pr+is:open`
28
+ } ) ;
29
+
30
+ // Look for a PR that is from a fork and has a matching head commit as the current branch
31
+ const pullNumbers = searchRes . data . items . filter ( i => i . pull_request !== undefined ) . map ( j => j . number ) ;
32
+ for ( let pull_number of pullNumbers ) {
33
+ const { data} = await octokit . pulls . get ( {
34
+ owner : 'adobe' ,
35
+ repo : 'react-spectrum' ,
36
+ pull_number
37
+ } ) ;
38
+ if ( data && data . head . repo . full_name !== 'adobe/react-spectrum' && data . head . sha === forkHeadCommit ) {
39
+ pr = pull_number ;
40
+ break ;
41
+ }
42
+ }
43
+ }
44
+ } catch ( error ) {
45
+ console . error ( error ) ;
46
+ }
47
+ } else {
48
+ pr = process . env . CIRCLE_PULL_REQUEST . split ( '/' ) . pop ( ) ;
12
49
}
13
50
14
- let pr = process . env . CIRCLE_PULL_REQUEST . split ( '/' ) . pop ( ) ;
15
- await octokit . issues . createComment ( {
16
- owner : 'adobe' ,
17
- repo : 'react-spectrum' ,
18
- issue_number : pr ,
19
- body : `Build successful! 🎉
51
+ if ( pr != null ) {
52
+ await octokit . issues . createComment ( {
53
+ owner : 'adobe' ,
54
+ repo : 'react-spectrum' ,
55
+ issue_number : pr ,
56
+ body : `Build successful! 🎉
20
57
21
- * [View the storybook](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /storybook/index.html)
22
- * [View the storybook-16](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /storybook-16/index.html)
23
- * [View the documentation](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /docs/index.html)`
24
- } ) ;
58
+ * [View the storybook](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /storybook/index.html)
59
+ * [View the storybook-16](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /storybook-16/index.html)
60
+ * [View the documentation](https://reactspectrum.blob.core.windows.net/reactspectrum/${ process . env . CIRCLE_SHA1 } /docs/index.html)`
61
+ } ) ;
62
+ }
25
63
}
0 commit comments