Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkaliski committed Jan 6, 2019
1 parent b6f2ada commit f67196d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
@@ -1 +1,19 @@
# go-github-actions
# go-github-actions

## Actions

### gofmt

Runs `gofmt` on files in the directory. Fails if any file is not properly formatted.

```hcl
action "gofmt" {
uses = "sjkaliski/go-github-actions/fmt@v0.1.0"
needs = "previous-action"
secrets = ["GITHUB_TOKEN"]
env {
GO_WORKING_DIR = "./path/to/go/files"
}
}
```
16 changes: 16 additions & 0 deletions fmt/Dockerfile
@@ -0,0 +1,16 @@
FROM golang:1.11

LABEL "com.github.actions.name"="go fmt"
LABEL "com.github.actions.description"="Run go fmt"
LABEL "com.github.actions.icon"="terminal"
LABEL "com.github.actions.color"="blue"

LABEL "repository"="https://github.com/sjkaliski/go-github-actions"
LABEL "homepage"="https://github.com/sjkaliski/go-github-actions"
LABEL "maintainer"="Steve Kaliski <sjkaliski@gmail.com>"

RUN apt-get update
RUN apt-get install -y jq

ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
3 changes: 3 additions & 0 deletions fmt/README.md
@@ -0,0 +1,3 @@
# go fmt

Runs `gofmt`. To learn more about `gofmt`, see the [official docs](https://golang.org/cmd/gofmt/).
44 changes: 44 additions & 0 deletions fmt/entrypoint.sh
@@ -0,0 +1,44 @@
#!/bin/sh
set -e

cd "${GO_WORKING_DIR:-.}"

# Check if any files are not formatted.
set +e
test -z "$(gofmt -l -d -e $(find . -type f -iname '*.go'))"
SUCCESS=$?
set -e

# Exit if `go fmt` passes.
if [ $SUCCESS -eq 0 ]; then
exit 0
fi

# Get list of unformatted files.
set +e
FILES=$(sh -c "gofmt -l . $*" 2>&1)
echo "$FILES"
set -e

# Iterate through each unformatted file.
OUTPUT=""
for file in $FILES; do
DIFF=$(gofmt -d -e "$file")
OUTPUT="$OUTPUT
\`$file\`
\`\`\`diff
$DIFF
\`\`\`
"
done

# Post results back as comment.
COMMENT="#### \`go fmt\`
$OUTPUT
"
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null

exit $SUCCESS

0 comments on commit f67196d

Please sign in to comment.