Skip to content

Commit

Permalink
Rewrite action
Browse files Browse the repository at this point in the history
* replace docker action with GitHub Composite Action
* cache scalafmt binary
* update references
* upgrade actions/checkout to v4
* add step summary
* strip GITHUB_WORKSPACE path from output
* switch from scalafmt:v2.3.2
* enforce scala format version
* support specifying an arbitrary version of scalafmt
  (as long as it's available as `scalafmt-linux-glibc` in releases)

* take responsibility for this action
* drop GitHub Marketplace instructions (this will come back once we
  publish)
* drop references to HCL syntax (GitHub Actions Alpha is sufficiently
  obsolete that no one remembers it)

* highlight `Segmentation fault` as an error
  • Loading branch information
jsoref committed Jan 26, 2024
1 parent e7d9780 commit 9e2520b
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 65 deletions.
14 changes: 14 additions & 0 deletions .github/regenerate-good.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
act -W .github/workflows/test-regenerate-good.yml 2>/dev/null |
perl -ne '
unless ($state) {
$state = 1 if (/run-scalafmt\.sh/);
next;
}
if ($state == 1) {
$state = 2 if (/docker exec/);
next;
}
last unless s/^.*\| //;
print;
' > .github/testdata/Good.scala
6 changes: 4 additions & 2 deletions .github/testdata/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# I dont like this formatting directive in reality, but lets
# I don't like this formatting directive in reality, but it lets
# us verify that this is reading a conf file easily
align = most
align.preset = most
version = 3.7.15
runner.dialect = scala213
1 change: 0 additions & 1 deletion .github/testdata/Bad.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ sealed abstract class Formatted {
}

/** Aligned by first asterisk, default ScalaDoc style is second.
*
*/
object Formatted {
case class Success(formattedCode: String) extends Formatted
Expand Down
2 changes: 1 addition & 1 deletion .github/testdata/Good.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ messiness added to it. -mroth */
package org.scalafmt

sealed abstract class Formatted {

def toEither: Either[Throwable, String] = this match {
case Formatted.Success(s) => Right(s)
case Formatted.Failure(e) => Left(e) // too much space before comment
Expand All @@ -15,7 +16,6 @@ sealed abstract class Formatted {
}

/** Aligned by first asterisk, default ScalaDoc style is second.
*
*/
object Formatted {
case class Success(formattedCode: String) extends Formatted
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/test-all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: [workflow_dispatch]
name: Check with all testdata

jobs:
check-testdata:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checking testdata
uses: ./
with:
args: "-c .github/testdata/.scalafmt.conf --list"
12 changes: 12 additions & 0 deletions .github/workflows/test-regenerate-good.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: [workflow_dispatch]
name: Regenerate Good file (run in act)

jobs:
regenerate-good:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checking testdata
uses: ./
with:
args: "-c .github/testdata/.scalafmt.conf --stdout .github/testdata/Bad.scala"
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
on: [push]
name: Check with testdata
name: Check with good testdata

jobs:
check-testdata:
check-good-testdata:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Checking testdata
- uses: actions/checkout@v4
- name: Checking testdata (pass)
uses: ./
with:
args: "-c .github/testdata/.scalafmt.conf --exclude .github/testdata/Bad.scala --list"
10 changes: 0 additions & 10 deletions Dockerfile

This file was deleted.

23 changes: 3 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
IMAGE := openlaw/scalafmt-ci
# DEFAULT_ARGS := --list
WORKDIR := /github/workspace
TESTDATA := .github/testdata

.PHONY: test testfail testdata clean

image:
docker build -t $(IMAGE) .

test:
docker run --rm -it \
-w=$(WORKDIR) \
-v "$$(pwd)/$(TESTDATA)":$(WORKDIR) \
$(IMAGE) --list --exclude Bad.scala
act -W .github/workflows/test.yml

# expected failure, to see output
testfail:
docker run --rm -it \
-w=$(WORKDIR) \
-v "$$(pwd)/$(TESTDATA)":$(WORKDIR) \
$(IMAGE) --list
act -W .github/workflows/test-all.yml

# generate the "good" version of the sample file
$(TESTDATA)/Good.scala: $(TESTDATA)/Bad.scala $(TESTDATA)/.scalafmt.conf
docker run --rm -i \
-v "$$(pwd)/$(TESTDATA)":/conf \
mrothy/scalafmt-native:2.2.2 \
--config /conf/.scalafmt.conf \
--stdin --stdout \
< $< > $@
./.github/regenerate-good.sh

testdata: $(TESTDATA)/Good.scala

Expand Down
23 changes: 3 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Simply add a step such as the following to your your workflow yml file:

```yml
- name: Check for scalafmt conformance
uses: openlawteam/scalafmt-ci@v2
uses: garnercorp/scalafmt-ci@v3
```
Example in the full context of a workflow file, with some optional arguments:
Expand All @@ -24,28 +24,11 @@ jobs:
scalafmt-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Checking your code to see if u r naughty or nice
uses: openlawteam/scalafmt-ci@v2
uses: garnercorp/scalafmt-ci@v3
with:
args: "--exclude=third_party --list"
```
### Installing with GitHub Marketplace
https://github.com/marketplace/actions/scalafmt-action
### (Legacy) HCL Syntax
If you are still using legacy HCL format (from the GitHub Actions Alpha test),
you will need to pin to a previous version of this action.
```hcl
action "lint" {
uses="openlawteam/scalafmt-ci@v0"
# optional additional args
args="--exclude ./vendor"
}
```
42 changes: 37 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "Scalafmt Action"
description: "Format Scala code using scalafmt"
author: "Matthew Rothenberg <[email protected]>"
author: "GarnerCorp <[email protected]>"
branding:
icon: "wind"
color: "red"
Expand All @@ -9,8 +9,40 @@ inputs:
description: "Args to pass to scalafmt"
default: "--list"
required: false
version:
description: "Version of scalafmt"
default: "3.7.15"
required: false

runs:
using: "docker"
image: "Dockerfile"
args:
- ${{ inputs.args }}
using: "composite"
steps:
- shell: bash
run: |
"$action_path/set-path.sh"
env:
scalafmt_version: ${{ inputs.version }}
action_path: ${{ github.action_path }}
- uses: actions/cache/restore@v4
id: cache
with:
path: ${{ env.SCALAFMT_PATH }}
key: scalafmt-${{ inputs.version }}
- shell: bash
id: retrieve-scalafmt
if: ${{ !steps.cache.outputs.cache-hit }}
run: |
curl -sL https://github.com/scalameta/scalafmt/releases/download/v${scalafmt_version}/scalafmt-linux-glibc > "$SCALAFMT_PATH/scalafmt"
chmod +x "$SCALAFMT_PATH/scalafmt"
echo "retrieved=1" >> "$GITHUB_OUTPUT"
- shell: bash
run: |
run-scalafmt.sh
env:
args: ${{ inputs.args }}
- name: Cache scalafmt
if: ${{ always() && steps.retrieve-scalafmt.outputs.retrieved }}
uses: actions/cache/save@v4
with:
path: ${{ env.SCALAFMT_PATH }}
key: scalafmt-${{ inputs.version }}
2 changes: 0 additions & 2 deletions entrypoint.sh

This file was deleted.

22 changes: 22 additions & 0 deletions extract-errors.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env perl
my $source_file = $ENV{source};
my $errors_file = $ENV{errors};
my $new_source_file = "$source_file.new";
open my $source_in, "<", $source_file;
open my $source_out, ">", $new_source_file;
open my $errors, ">>", $errors_file;
my $found_error=0;
while (<$source_in>) {
if ($found_error) {
print $errors $_;
} elsif (/^error:|^Segmentation fault/) {
$found_error=1;
print $errors $_;
} else {
print $source_out $_;
}
}
close $source_in;
close $source_out;
close $errors;
rename $new_source_file, $source_file;
45 changes: 45 additions & 0 deletions run-scalafmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
output=$(mktemp)
warnings=$(mktemp)
errors=$(mktemp)

enforce_scala_format_version() {
if [ -e .scalafmt.conf ]; then
perl -pi -e 'next unless s/^\s*(version)=.*/$1=$ENV{scalafmt_version}/' .scalafmt.conf
fi
}

enforce_scala_format_version
scalafmt --non-interactive $args > "$output" 2> "$warnings"
result=$?
cat "$output"
cat "$warnings" >&2
[ $result -eq 0 ] && exit

report() {
file="$1"
summary="$2"

[ -s "$file" ] || return
echo "<details><summary>$summary</summary>"
echo
echo '```'
perl -pe 's/$ENV{GITHUB_WORKSPACE}//g' "$file"
echo '```'
echo '</details>'
echo
}

source="$output" errors="$errors" extract-errors.pl
source="$warnings" errors="$errors" extract-errors.pl

(
echo "# scalafmt failed ($result)"
if [ -s "$errors" ]; then
echo '# Errors'
cat "$errors"
fi
report "$warnings" "Warnings"
report "$output" "Status"
) >> "$GITHUB_STEP_SUMMARY"
exit $result
11 changes: 11 additions & 0 deletions set-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
SCALAFMT_PATH="/tmp/scalafmt/${scalafmt_version}"
mkdir -p "$SCALAFMT_PATH"
(
echo "SCALAFMT_PATH=$SCALAFMT_PATH"
echo "scalafmt_version=$scalafmt_version"
) >> "$GITHUB_ENV"
(
echo "$SCALAFMT_PATH"
echo "$action_path"
) >> "$GITHUB_PATH"

0 comments on commit 9e2520b

Please sign in to comment.