Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
1 deletion.
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 |
---|---|---|
@@ -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" | ||
} | ||
} | ||
``` |
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,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"] |
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,3 @@ | ||
# go fmt | ||
|
||
Runs `gofmt`. To learn more about `gofmt`, see the [official docs](https://golang.org/cmd/gofmt/). |
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,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 |