Skip to content

Commit

Permalink
Add initial implementation (#1)
Browse files Browse the repository at this point in the history
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
nickcharlton authored Feb 17, 2024
1 parent 51bd774 commit 838eaa2
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
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
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-3.3.0
6 changes: 6 additions & 0 deletions Gemfile
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
29 changes: 29 additions & 0 deletions Gemfile.lock
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
19 changes: 19 additions & 0 deletions README.md
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
```
24 changes: 24 additions & 0 deletions action.yml
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
13 changes: 13 additions & 0 deletions bin/diff-check
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
51 changes: 51 additions & 0 deletions spec/black_box/diff-check_spec.rb
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
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit 838eaa2

Please sign in to comment.