1+ const isTestFile = ( file ) => / \. ( t e s t | s p e c ) \. [ j t ] s x ? $ / . test ( file ) ;
2+
3+ const deriveTestFiles = ( files ) => {
4+ return files . map ( ( file ) => {
5+ if ( isTestFile ( file ) ) return file ;
6+
7+ const withoutExt = file . replace ( / \. [ j t ] s x ? $ / , '' ) ;
8+ const parts = withoutExt . split ( '/' ) ;
9+ const baseName = parts [ parts . length - 1 ] ;
10+ const dir = parts . slice ( 0 , - 1 ) . join ( '/' ) ;
11+
12+ return `${ dir } /__tests__/${ baseName } .test.ts` ;
13+ } ) ;
14+ } ;
15+
16+ module . exports = async ( { github, context, core } ) => {
17+ const owner = context . repo . owner ;
18+ const repo = context . repo . repo ;
19+ const pr = context . payload . pull_request ;
20+ const prNumber = pr . number ;
21+ const prState = pr . state ;
22+
23+ const backendFiles = [ ] ;
24+ const mobileFiles = [ ] ;
25+ const webFiles = [ ] ;
26+
27+ try {
28+ if ( prState === 'closed' ) {
29+ console . log ( `PR state is: ${ prState } ` ) ;
30+ return {
31+ backendChanged : false ,
32+ mobileChanged : false ,
33+ webChanged : false
34+ } ;
35+ }
36+
37+ const changedFiles = await github . paginate (
38+ github . rest . pulls . listFiles ,
39+ {
40+ owner,
41+ repo,
42+ pull_number : prNumber
43+ }
44+ ) ;
45+
46+ changedFiles . forEach ( ( file ) => {
47+ const fileName = file . filename ;
48+
49+ if ( fileName . startsWith ( 'apps/backend/' ) ) {
50+ backendFiles . push ( fileName ) ;
51+ } else if ( fileName . startsWith ( 'apps/mobile/' ) ) {
52+ mobileFiles . push ( fileName ) ;
53+ } else if ( fileName . startsWith ( 'apps/web/' ) ) {
54+ webFiles . push ( fileName ) ;
55+ }
56+ } ) ;
57+
58+ const strippedBackend = backendFiles . map ( f => f . replace ( 'apps/backend/' , '' ) ) ;
59+ const strippedMobile = mobileFiles . map ( f => f . replace ( 'apps/mobile/' , '' ) ) ;
60+
61+ console . log ( { backendFiles, mobileFiles, webFiles } ) ;
62+
63+ core . setOutput ( 'backendFiles' , strippedBackend . join ( ' ' ) ) ;
64+ core . setOutput ( 'mobileFiles' , strippedMobile . join ( ' ' ) ) ;
65+ core . setOutput ( 'webFiles' , webFiles . map ( f => f . replace ( 'apps/web/' , '' ) ) . join ( ' ' ) ) ;
66+ core . setOutput ( 'backendTestFiles' , deriveTestFiles ( strippedBackend ) . join ( ' ' ) ) ;
67+ core . setOutput ( 'mobileTestFiles' , deriveTestFiles ( strippedMobile ) . join ( ' ' ) ) ;
68+ core . setOutput ( 'backendChanged' , backendFiles . length > 0 ) ;
69+ core . setOutput ( 'mobileChanged' , mobileFiles . length > 0 ) ;
70+ core . setOutput ( 'webChanged' , webFiles . length > 0 ) ;
71+
72+ } catch ( error ) {
73+ console . error ( error ) ;
74+
75+ return {
76+ backendChanged : false ,
77+ mobileChanged : false ,
78+ webChanged : false
79+ } ;
80+ }
81+ } ;
0 commit comments