-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit builds up the majority of the action: when a given command is run, the action will check if there's any diff output in the cloned repo. If there is, it will report a job summary of what changed. An older implementation of this used annotations, rather than job summaries, but these don't really work that well, as we can't comment on files which have changed outside of those referenced in the diff, so the output is mostly shown in the log output or the autogenerated job summary. Instead, we assemble a custom output, which is much nicer. For testing, we use `jet_black`, which allows us to run "black box" tests in a clean environment using RSpec which is quite pleasant to work with. To be able to do this, `diff-check` is a script that's called from the `action.yml` and the expectation is that all the logic should be in there. https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/ https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary https://github.com/orgs/community/discussions/9099 https://github.com/odlp/jet_black
- Loading branch information
1 parent
51bd774
commit 838eaa2
Showing
9 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
name: Tests | ||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
rspec: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ruby/setup-ruby@v1 | ||
with: | ||
bundler-cache: true | ||
- name: Setup Git author for test repo | ||
run: | | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "Diff Check" | ||
- run: bundle exec rspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ruby-3.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source "https://rubygems.org" | ||
|
||
group :development, :test do | ||
gem "jet_black" | ||
gem "rspec" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
diff-lcs (1.5.1) | ||
jet_black (0.7.1) | ||
rspec (3.13.0) | ||
rspec-core (~> 3.13.0) | ||
rspec-expectations (~> 3.13.0) | ||
rspec-mocks (~> 3.13.0) | ||
rspec-core (3.13.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-expectations (3.13.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-mocks (3.13.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-support (3.13.0) | ||
|
||
PLATFORMS | ||
arm64-darwin-23 | ||
ruby | ||
|
||
DEPENDENCIES | ||
jet_black | ||
rspec | ||
|
||
BUNDLED WITH | ||
2.5.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
# diff-check | ||
|
||
An Action which runs a command and fails if it changes anything. | ||
|
||
## Example | ||
|
||
For example, in: `.github/workflows/diff-check.yml`: | ||
|
||
```yaml | ||
--- | ||
name: diff-check | ||
on: [push] | ||
|
||
jobs: | ||
demo: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: nickcharlton/diff-check@main | ||
with: | ||
command: echo "hello world" >> README.md | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
name: Diff Check | ||
description: An Action which runs a command and fails if it changes anything. | ||
inputs: | ||
command: | ||
description: Command to run | ||
required: true | ||
message: | ||
description: "Message to show when failing (e.g.: explaining what to do)" | ||
default: "has changed" | ||
runs: | ||
using: "composite" | ||
steps: | ||
- run: ${{ inputs.command }} | ||
shell: bash | ||
|
||
- name: Add Action to $GITHUB_PATH | ||
run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH | ||
shell: bash | ||
env: | ||
GITHUB_ACTION_PATH: ${{ github.action_path }} | ||
|
||
- run: bin/diff-check >> $GITHUB_STEP_SUMMARY | ||
shell: bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
if ! git diff-index --quiet HEAD; then | ||
printf 'These files changed when running the command:\n\n' | ||
|
||
git diff --name-only | while read -r n ; do | ||
echo "* $n" | ||
done | ||
|
||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe "diff-check" do | ||
context "with no file change" do | ||
it "emits no output" do | ||
session = create_session | ||
|
||
expect(session.run("diff-check")).to have_no_stdout | ||
expect(session.run("diff-check")).to have_no_stderr | ||
end | ||
|
||
it "exits with a status code of 0" do | ||
session = create_session | ||
|
||
expect(session.run("diff-check")).to be_a_success | ||
end | ||
end | ||
|
||
context "with a file change" do | ||
it "emits a summary with the files changed" do | ||
session = create_session | ||
|
||
session.run("echo 'new text' >> README") | ||
|
||
expect(session.run("diff-check")).to have_stdout(<<~SUMMARY | ||
These files changed when running the command: | ||
* README | ||
SUMMARY | ||
) | ||
end | ||
|
||
it "exits with a status of 1" do | ||
session = create_session | ||
|
||
session.run("echo 'new text' >> README") | ||
|
||
expect(session.run("diff-check")).to be_a_failure | ||
end | ||
end | ||
|
||
def create_session | ||
session = JetBlack::Session.new | ||
|
||
session.run("git init") | ||
session.run("echo 'Hello world' >> README") | ||
session.run("git add .; git commit -m 'initial commit'") | ||
|
||
session | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "rspec" | ||
require "jet_black" | ||
require "jet_black/rspec" | ||
|
||
JetBlack.configure do |config| | ||
config.path_prefix = File.expand_path("../bin", __dir__) | ||
end |