1name: 'PR checks'
  2on:
  3  pull_request:
  4    types:
  5      - synchronize
  6      - labeled
  7      - opened
  8      - reopened
  9    paths:
 10      - "app/**"
 11      - "health_data_store/**"
 12      - "pr.yml"
 13
 14jobs:
 15  validate-localizations:
 16    runs-on: ubuntu-latest
 17    steps:
 18      - name: Checkout PR code
 19        uses: actions/checkout@v4
 20        with:
 21          ref: ${{ github.head_ref }}
 22          sparse-checkout: "app/lib/l10n"
 23      - name: Validate localizations
 24        # check for [^'"]'[^'"] in .arb files
 25        run: "grep \"[^'\\\"]'[^'\\\"]\" app/lib/l10n/*.arb && exit 1 || exit 0"
 26  update-goldens:
 27    if: github.event.pull_request.draft == false
 28    runs-on: ubuntu-latest
 29    permissions:
 30      contents: write
 31      pull-requests: write
 32    steps:
 33      - name: Checkout PR code
 34        uses: actions/checkout@v4
 35        with:
 36          ref: ${{ github.head_ref }}
 37      - name: Cache generated health data store
 38        id: cache-generated
 39        uses: actions/cache@v4
 40        with:
 41          path: health_data_store/lib
 42          key: builder-${{ hashFiles('health_data_store/pubspec.yaml', 'health_data_store/lib/*', 'health_data_store/lib/**/*dart') }}
 43      - name: Setup dart
 44        if: steps.cache-generated.outputs.cache-hit != 'true'
 45        uses: dart-lang/setup-dart@v1
 46        with:
 47          sdk: stable
 48      - name: Generate code
 49        if: steps.cache-generated.outputs.cache-hit != 'true'
 50        run: dart run build_runner build
 51        working-directory: health_data_store
 52      - name: Setup Flutter
 53        uses: subosito/flutter-action@v2
 54        with:
 55          channel: stable
 56          cache: true
 57      - name: Disable analytics
 58        run:
 59          flutter config --no-analytics --suppress-analytics
 60      - name: Update app dependencies
 61        run: flutter pub get
 62        working-directory: app
 63      # TODO: cache using non-generated dart files in the test dir as key
 64      - name: Generate app mock code
 65        run: flutter pub run build_runner build
 66        working-directory: app
 67      - name: Update goldens
 68        id: gold-upd
 69        run: flutter test --update-goldens --fail-fast --tags gold --run-skipped
 70        working-directory: app
 71      - name: Push golden changes
 72        # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions#example-of-failure-with-conditions
 73        if: ${{ steps.gold-upd.conclusion == 'success' }}
 74        run: |
 75          if [[ $(git status -s | grep png) ]]; then
 76              git config user.name "GitHub Action (update goldens)"
 77              git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
 78              git add app/test/
 79              git commit -m "Update goldens"
 80              git push
 81          else
 82              echo "no changes to commit"
 83          fi
 84        env:
 85          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}%
 86  test:
 87    if: contains(github.event.pull_request.labels.*.name, 'auto-test')
 88    runs-on: ubuntu-latest
 89    needs: update-goldens # this is guaranteed to fail if goldens need to be updated
 90    steps:
 91      - name: Checkout code
 92        uses: actions/checkout@v4
 93        with:
 94          # ensures there are no unexpected directories needed
 95          sparse-checkout: |
 96            app
 97            health_data_store
 98      - name: Cache generated health data store
 99        id: cache-generated
100        uses: actions/cache@v4
101        with:
102          path: health_data_store/lib
103          key: builder-${{ hashFiles('health_data_store/pubspec.yaml', 'health_data_store/lib/*', 'health_data_store/lib/**/*dart') }}
104      - name: Setup dart
105        if: steps.cache-generated.outputs.cache-hit != 'true'
106        uses: dart-lang/setup-dart@v1
107        with:
108          sdk: stable
109      - name: Generate code
110        if: steps.cache-generated.outputs.cache-hit != 'true'
111        run: dart run build_runner build
112        working-directory: health_data_store
113      - name: Setup Flutter
114        uses: subosito/flutter-action@v2
115        with:
116          channel: stable
117          cache: true
118      - name: Disable analytics
119        run:
120          flutter config --no-analytics --suppress-analytics
121      - name: Update app dependencies
122        run: flutter pub get
123        working-directory: app
124      # TODO: cache using non-generated dart files in the test dir as key
125      - name: Generate app mock code
126        run: flutter pub run build_runner build
127        working-directory: app
128      - name: Run tests
129        run: flutter test --coverage
130        working-directory: app
131  build:
132    if: contains(github.event.pull_request.labels.*.name, 'auto-build')
133    runs-on: ubuntu-latest
134    steps:
135      - name: Checkout code
136        uses: actions/checkout@v4
137        with:
138          # ensures there are no unexpected directories needed
139          sparse-checkout: |
140            app
141            health_data_store
142      - name: Cache generated health data store
143        id: cache-generated
144        uses: actions/cache@v4
145        with:
146          path: health_data_store/lib
147          key: builder-${{ hashFiles('health_data_store/pubspec.yaml', 'health_data_store/lib/*', 'health_data_store/lib/**/*dart') }}
148      - name: Setup dart
149        if: steps.cache-generated.outputs.cache-hit != 'true'
150        uses: dart-lang/setup-dart@v1
151        with:
152          sdk: stable
153      - name: Generate code
154        if: steps.cache-generated.outputs.cache-hit != 'true'
155        run: dart run build_runner build
156        working-directory: health_data_store
157      - name: Set Up Java
158        uses: actions/setup-java@v3.12.0
159        with:
160          distribution: 'oracle'
161          java-version: '17'
162      - name: Setup Flutter
163        uses: subosito/flutter-action@v2
164        with:
165          channel: stable
166          cache: true
167      - name: Disable analytics
168        run: flutter config --no-analytics --suppress-analytics
169      - name: Update app dependencies
170        run: flutter pub get
171        working-directory: app
172      - name: Build
173        run: flutter build apk --debug --flavor=github
174      - name: Upload artifacts
175        uses: actions/upload-artifact@v4
176        with:
177          name: build-results
178          path: app/build/app/outputs/flutter-apk