|
1 | 1 | name: CI |
2 | 2 |
|
3 | | -on: [push, pull_request] |
| 3 | +on: [pull_request] |
| 4 | +permissions: |
| 5 | + contents: read |
4 | 6 |
|
5 | 7 | jobs: |
6 | 8 | test: |
@@ -41,17 +43,118 @@ jobs: |
41 | 43 | run: | |
42 | 44 | sudo apt-get update |
43 | 45 | sudo apt-get install imagemagick |
44 | | - - name: Check out code |
| 46 | + - name: Check out code on current branch |
45 | 47 | uses: actions/checkout@v4 |
46 | | - - name: Install Ruby & gems |
| 48 | + - name: Install Ruby & gems on current branch |
47 | 49 | uses: ruby/setup-ruby@v1 |
48 | 50 | with: |
49 | 51 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically |
50 | | - - name: Configure and initialize database |
| 52 | + - name: Configure and initialize database on current branch |
51 | 53 | run: | |
52 | 54 | cp test/config/test_tess.yml config/tess.yml |
53 | 55 | cp config/secrets.github.yml config/secrets.yml |
54 | 56 | cp config/ingestion.example.yml config/ingestion.yml |
55 | 57 | bundle exec rake db:test:prepare |
56 | | - - run: bundle exec rails test |
57 | | - name: Run tests |
| 58 | + - name: Get all changed rb files |
| 59 | + id: changed-rb-files |
| 60 | + uses: tj-actions/changed-files@24d32ffd492484c1d75e0c0b894501ddb9d30d62 # v47 |
| 61 | + with: |
| 62 | + files_ignore: 'test/**' |
| 63 | + files: '**.rb' |
| 64 | + - name: List all changed files rb files on current branch |
| 65 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 66 | + env: |
| 67 | + ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }} |
| 68 | + run: | |
| 69 | + for file in ${ALL_CHANGED_FILES}; do |
| 70 | + echo "$file was changed" |
| 71 | + done |
| 72 | + echo "ALL_CHANGED_FILES=${ALL_CHANGED_FILES}" >> $GITHUB_ENV |
| 73 | + # CURRENT branch tests |
| 74 | + - name: Run tests and extract current coverage on current branch |
| 75 | + id: current_coverage |
| 76 | + run: | |
| 77 | + bundle install |
| 78 | + bundle exec rails test |
| 79 | +
|
| 80 | + if [ -f coverage/.last_run.json ]; then |
| 81 | + CURRENT_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line') |
| 82 | + else |
| 83 | + CURRENT_COVERAGE="0.0" |
| 84 | + fi |
| 85 | + TABLE=$(echo -e "| File | Current coverage (%) |\n|------|--------------|") |
| 86 | +
|
| 87 | + echo "\n********** CODE COVERAGE **********" |
| 88 | + echo "\n*** Current coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $CURRENT_COVERAGE%" |
| 89 | + echo "*** ALL_CHANGED_FILES=${{ env.ALL_CHANGED_FILES }}" |
| 90 | +
|
| 91 | + for file in ${{ env.ALL_CHANGED_FILES }}; do |
| 92 | + COV=$(cat coverage/index.html| grep "\"$file\"" -A 1 | grep t-file__coverage | awk -F '>|%' '{print $2}') |
| 93 | + TABLE=$(echo -e "$TABLE\n| $file | $COV |") |
| 94 | + done |
| 95 | + echo "CURRENT_COV=$CURRENT_COVERAGE" >> $GITHUB_ENV |
| 96 | + echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV |
| 97 | + { |
| 98 | + echo "TABLE<<EOF" |
| 99 | + echo -e "$TABLE" |
| 100 | + echo "EOF" |
| 101 | + } >> "$GITHUB_ENV" |
| 102 | + # BASE branch tests (usually master, but can be something else) |
| 103 | + - name: Checkout base branch |
| 104 | + uses: actions/checkout@v4 |
| 105 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 106 | + with: |
| 107 | + ref: ${{ github.event.pull_request.base.ref }} |
| 108 | + path: base_branch |
| 109 | + - name: Install Ruby & gems on base |
| 110 | + uses: ruby/setup-ruby@v1 |
| 111 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 112 | + with: |
| 113 | + bundler-cache: true # runs 'bundle install' and caches installed gems automatically |
| 114 | + - name: Configure and initialize database on base |
| 115 | + working-directory: base_branch |
| 116 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 117 | + run: | |
| 118 | + cp test/config/test_tess.yml config/tess.yml |
| 119 | + cp config/secrets.github.yml config/secrets.yml |
| 120 | + cp config/ingestion.example.yml config/ingestion.yml |
| 121 | + bundle install |
| 122 | + bundle exec rake db:test:prepare |
| 123 | + - name: Run tests and extract current coverage on base |
| 124 | + id: base_coverage |
| 125 | + working-directory: base_branch |
| 126 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 127 | + env: |
| 128 | + ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }} |
| 129 | + run: | |
| 130 | + bundle install |
| 131 | + bundle exec rails test |
| 132 | +
|
| 133 | + if [ -f coverage/.last_run.json ]; then |
| 134 | + BASE_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line') |
| 135 | + else |
| 136 | + BASE_COVERAGE="0.0" |
| 137 | + fi |
| 138 | +
|
| 139 | + echo "\n********** CODE COVERAGE **********" |
| 140 | + echo "\n*** Base coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $BASE_COVERAGE%" |
| 141 | +
|
| 142 | + echo "BASE_COV=$BASE_COVERAGE" >> $GITHUB_ENV |
| 143 | + echo "BASE_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV |
| 144 | + # Compare coverages |
| 145 | + - name: Compare coverage |
| 146 | + id: compare |
| 147 | + if: steps.changed-rb-files.outputs.any_changed == 'true' |
| 148 | + run: | |
| 149 | + DIFF=$(echo "$CURRENT_COV - $BASE_COV" | bc) |
| 150 | + echo "DIFF=$DIFF" >> $GITHUB_ENV |
| 151 | +
|
| 152 | + echo "Current ($CURRENT_BRANCH)= $CURRENT_COV%" |
| 153 | + echo "Base ($BASE_BRANCH)= $BASE_COV%" |
| 154 | + if (( $(echo "$CURRENT_COV < $BASE_COV" | bc -l) )); then |
| 155 | + echo "❌ Coverage decreased by $DIFF%" |
| 156 | + echo "RESULT=0" >> $GITHUB_ENV |
| 157 | + else |
| 158 | + echo "✅ Coverage maintained or improved by $DIFF%" |
| 159 | + echo "RESULT=1" >> $GITHUB_ENV |
| 160 | + fi |
0 commit comments