Skip to content

Commit 59f0977

Browse files
dblockCopilot
andcommitted
Skip expensive test setup for documentation-only changes
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7563ee8 commit 59f0977

7 files changed

Lines changed: 178 additions & 2 deletions

File tree

‎.github/scripts/test-changes.sh‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
case "$EVENT_NAME" in
5+
pull_request)
6+
comparison="${BASE_SHA}...${HEAD_SHA}"
7+
;;
8+
push)
9+
if [[ "$BASE_SHA" == 0000000000000000000000000000000000000000 ]] ||
10+
! git cat-file -e "${BASE_SHA}^{commit}" 2>/dev/null; then
11+
echo '::notice::No previous push revision available; running the full test matrix.'
12+
echo 'run_tests=true' >> "$GITHUB_OUTPUT"
13+
exit 0
14+
fi
15+
comparison="${BASE_SHA}..${HEAD_SHA}"
16+
;;
17+
*)
18+
echo "::error::Unsupported event: $EVENT_NAME"
19+
exit 1
20+
;;
21+
esac
22+
23+
changed_files="$(mktemp)"
24+
trap 'rm -f "$changed_files"' EXIT
25+
git diff --name-only --no-renames -z "$comparison" > "$changed_files"
26+
27+
run_tests=false
28+
while IFS= read -r -d '' path; do
29+
case "$path" in
30+
*.md|LICENSE|LICENSE.txt|images/*) ;;
31+
*) run_tests=true ;;
32+
esac
33+
done < "$changed_files"
34+
35+
echo "run_tests=$run_tests" >> "$GITHUB_OUTPUT"

‎.github/workflows/test-changes.yml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: Test changes
3+
on:
4+
workflow_call:
5+
outputs:
6+
run_tests:
7+
value: ${{ jobs.changes.outputs.run_tests }}
8+
permissions:
9+
contents: read
10+
jobs:
11+
changes:
12+
runs-on: ubuntu-24.04
13+
timeout-minutes: 5
14+
outputs:
15+
run_tests: ${{ steps.changes.outputs.run_tests }}
16+
steps:
17+
- uses: actions/checkout@v7
18+
with:
19+
fetch-depth: 0
20+
- id: changes
21+
env:
22+
EVENT_NAME: ${{ github.event_name }}
23+
BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before }}
24+
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
25+
run: bash .github/scripts/test-changes.sh

‎.github/workflows/test-mongodb.yml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ concurrency:
77
group: mongodb-${{ github.ref }}
88
cancel-in-progress: true
99
jobs:
10+
changes:
11+
uses: ./.github/workflows/test-changes.yml
1012
test:
13+
needs: changes
1114
runs-on: ubuntu-24.04
1215
timeout-minutes: 20
1316
env:
@@ -34,7 +37,7 @@ jobs:
3437
name: test (ruby=${{ matrix.entry.ruby }}, mongoid=${{ matrix.entry.mongoid }}, mongodb=${{ matrix.entry.mongodb }}, grape=${{ matrix.entry.grape || 'auto' }}, json=${{ matrix.entry.json }})
3538
services:
3639
mongodb:
37-
image: mongo:${{ matrix.entry.mongodb }}
40+
image: ${{ needs.changes.outputs.run_tests == 'true' && format('mongo:{0}', matrix.entry.mongodb) || '' }}
3841
ports:
3942
- 27017:27017
4043
options: >-
@@ -44,13 +47,20 @@ jobs:
4447
--health-retries 10
4548
steps:
4649
- uses: actions/checkout@v7
50+
if: needs.changes.outputs.run_tests == 'true'
4751
- uses: ruby/setup-ruby@v1
52+
if: needs.changes.outputs.run_tests == 'true'
4853
with:
4954
ruby-version: ${{ matrix.entry.ruby }}
5055
bundler-cache: true
5156
cache-version: mongoid-${{ matrix.entry.mongoid }}-grape-${{ matrix.entry.grape || 'auto' }}-json-${{ matrix.entry.json }}
5257
- uses: browser-actions/setup-firefox@v1.7.2
58+
if: needs.changes.outputs.run_tests == 'true'
5359
with:
5460
firefox-version: latest
5561
- name: Test
62+
if: needs.changes.outputs.run_tests == 'true'
5663
run: xvfb-run --auto-servernum bundle exec rake spec
64+
- name: Documentation only
65+
if: needs.changes.outputs.run_tests == 'false'
66+
run: echo 'Documentation-only change; database, dependencies, browser, and tests skipped.'

‎.github/workflows/test-postgresql.yml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ concurrency:
77
group: postgresql-${{ github.ref }}
88
cancel-in-progress: true
99
jobs:
10+
changes:
11+
uses: ./.github/workflows/test-changes.yml
1012
test:
13+
needs: changes
1114
runs-on: ubuntu-24.04
1215
timeout-minutes: 20
1316
env:
@@ -35,7 +38,7 @@ jobs:
3538
name: test (ruby=${{ matrix.entry.ruby }}, postgresql=${{ matrix.entry.postgresql }}, active_record=${{ matrix.entry.active_record }}, grape=${{ matrix.entry.grape || 'auto' }}, json=${{ matrix.entry.json }})
3639
services:
3740
postgresql:
38-
image: postgres:${{ matrix.entry.postgresql }}
41+
image: ${{ needs.changes.outputs.run_tests == 'true' && format('postgres:{0}', matrix.entry.postgresql) || '' }}
3942
env:
4043
POSTGRES_DB: slack_ruby_bot_server_test
4144
POSTGRES_USER: test
@@ -49,13 +52,20 @@ jobs:
4952
--health-retries 10
5053
steps:
5154
- uses: actions/checkout@v7
55+
if: needs.changes.outputs.run_tests == 'true'
5256
- uses: ruby/setup-ruby@v1
57+
if: needs.changes.outputs.run_tests == 'true'
5358
with:
5459
ruby-version: ${{ matrix.entry.ruby }}
5560
bundler-cache: true
5661
cache-version: activerecord-${{ matrix.entry.active_record }}-grape-${{ matrix.entry.grape || 'auto' }}-json-${{ matrix.entry.json }}
5762
- uses: browser-actions/setup-firefox@v1.7.2
63+
if: needs.changes.outputs.run_tests == 'true'
5864
with:
5965
firefox-version: latest
6066
- name: Test
67+
if: needs.changes.outputs.run_tests == 'true'
6168
run: xvfb-run --auto-servernum bundle exec rake spec
69+
- name: Documentation only
70+
if: needs.changes.outputs.run_tests == 'false'
71+
run: echo 'Documentation-only change; database, dependencies, browser, and tests skipped.'

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 2.3.0 (Next)
44

55
* [#186](https://github.com/slack-ruby/slack-ruby-bot-server/pull/186): Document persisted custom Team attributes for both adapters and Rails concern loading/reloading - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
6+
* Skip expensive database test setup for documentation-only changes while preserving every matrix check and Danger - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
67
* [#183](https://github.com/slack-ruby/slack-ruby-bot-server/pull/183): Add a `config.server_pages` block to configure page middleware options centrally without changing the installed library's defaults - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
78
* [#184](https://github.com/slack-ruby/slack-ruby-bot-server/pull/184): Add a configurable API application and separately mountable team read/registration endpoints, preserving the default API while supporting custom routes and registration-only applications - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).
89
* [#185](https://github.com/slack-ruby/slack-ruby-bot-server/pull/185): Skip inactive installations in status checks and deactivate revoked credentials through service callbacks, returning an explicit failed ping instead of failing the whole status response - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/apps/copilot-swe-agent).

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ On Linux, install [Firefox](https://www.mozilla.org/firefox/) and `Xvfb`, then r
4747
xvfb-run --auto-servernum bundle exec rake
4848
```
4949

50+
Documentation-only changes (`*.md`, `LICENSE`, `LICENSE.txt`, and `images/`) do not start database containers, install dependencies or Firefox, or run the database test suites. The workflows still publish every existing matrix check name so required checks can complete; Danger and RuboCop remain unchanged. Code, assets in `public/`, dependency files, and CI configuration still run the full matrices. New branches or unavailable previous push revisions conservatively run all tests.
51+
5052
## Write Tests
5153

5254
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.

‎spec/ci/test_changes_spec.rb‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
require 'spec_helper'
2+
require 'open3'
3+
require 'tmpdir'
4+
require 'fileutils'
5+
6+
describe 'CI test change classification' do
7+
around do |example|
8+
Dir.mktmpdir('test-changes') do |directory|
9+
@repository = directory
10+
example.run
11+
end
12+
end
13+
14+
before do
15+
git('init', '--quiet')
16+
git('config', 'user.name', 'CI')
17+
git('config', 'user.email', 'ci@example.invalid')
18+
@base = commit('README.md')
19+
end
20+
21+
def git(*args)
22+
output, error, status = Open3.capture3('git', *args, chdir: @repository)
23+
raise error unless status.success?
24+
25+
output.strip
26+
end
27+
28+
def commit(path)
29+
FileUtils.mkdir_p(File.dirname(File.join(@repository, path)))
30+
File.write(File.join(@repository, path), "content for #{path}\n")
31+
git('add', '--all')
32+
git('commit', '--quiet', '-m', 'Test change')
33+
git('rev-parse', 'HEAD')
34+
end
35+
36+
def classify(base: @base, head: git('rev-parse', 'HEAD'), event: 'pull_request')
37+
output = File.join(@repository, 'output')
38+
script = File.expand_path('../../.github/scripts/test-changes.sh', __dir__)
39+
_, error, status = Open3.capture3(
40+
{ 'EVENT_NAME' => event, 'BASE_SHA' => base, 'HEAD_SHA' => head, 'GITHUB_OUTPUT' => output },
41+
'bash', script, chdir: @repository
42+
)
43+
raise error unless status.success?
44+
45+
File.read(output).strip
46+
end
47+
48+
it 'skips Markdown and documentation images' do
49+
commit('docs/guide.md')
50+
commit('images/example.png')
51+
expect(classify).to eq 'run_tests=false'
52+
end
53+
54+
it 'runs tests when documentation is mixed with runtime changes' do
55+
commit('UPGRADING.md')
56+
commit('public/styles.css')
57+
expect(classify).to eq 'run_tests=true'
58+
end
59+
60+
it 'does not ignore a code file renamed to Markdown' do
61+
@base = commit('lib/example.rb')
62+
File.rename(File.join(@repository, 'lib/example.rb'), File.join(@repository, 'example.md'))
63+
git('add', '--all')
64+
git('commit', '--quiet', '-m', 'Rename')
65+
expect(classify).to eq 'run_tests=true'
66+
end
67+
68+
it 'compares pull requests from their merge base, not unrelated base-branch changes' do
69+
head = commit('CHANGELOG.md')
70+
git('checkout', '--quiet', '--detach', @base)
71+
base = commit('lib/base-only.rb')
72+
expect(classify(base: base, head: head)).to eq 'run_tests=false'
73+
end
74+
75+
it 'includes removed code when a push rewrites history' do
76+
before = commit('lib/removed.rb')
77+
git('checkout', '--quiet', '--detach', @base)
78+
head = commit('CHANGELOG.md')
79+
expect(classify(base: before, head: head, event: 'push')).to eq 'run_tests=true'
80+
end
81+
82+
it 'runs the full matrix for a new branch' do
83+
expect(classify(base: '0' * 40, event: 'push')).to eq 'run_tests=true'
84+
end
85+
86+
it 'runs the full matrix when a previous pushed revision is unavailable' do
87+
expect(classify(base: 'f' * 40, event: 'push')).to eq 'run_tests=true'
88+
end
89+
90+
it 'fails instead of silently skipping tests when a pull request comparison is invalid' do
91+
expect { classify(base: 'f' * 40) }.to raise_error(RuntimeError)
92+
end
93+
end

0 commit comments

Comments
 (0)